January 17, 2010 at 4:24 pm
I have an image column(FileBlob) in a large table with about 50,000 records. Now I have a query which needs to retrieve only those rows that have an image i.e. FileBlob is not NULL.
The problem is the execution plan shows that the image column is being retrieved to do the filtering and so, that is causing heavy reads on my system.
Any solutions?
Create table dbo.AppTable
(ID int,
FileBlob image,
ModifiedDate datetime,
CONSTRAINT [PK_AppTable]
PRIMARY KEY CLUSTERED ([ID])
)
on Primary;
Indexes:
Has clustered index on ID
Query Used:
select ModifiedDate
from AppTable
where id = @rid
and fileblob is not null
BitBucket- Thanks for responding. I tried to upload what I can.
January 17, 2010 at 4:51 pm
badkow
It would help those who want to help you to have some further information. Can you post the table definition, perhaps some sample data (does not have to be real or in any way compromise your organizations data or security) as well as your Select statement.
In other words try to follow the link in my signature block
January 17, 2010 at 6:20 pm
I think the problem is in that it is trying to retrieve the entire image when its check the not null constraint
January 17, 2010 at 7:24 pm
Is there any way to run a checksum on blobs?
If so just add a calculated column as checksum. Figure out what checksum of null is then compare to that value.
Plan B would be to add a tinyint flag Like ImageIsSet and check against that. Of cours with that option you need to change all the programs accessing that table...
January 17, 2010 at 7:38 pm
That was the first thought that came to my mind. I could add a persisted computed column that does a textvalid(imagecolumn) and stores it in a separate column of data type bit but the problem is SQL Server 2000 does not have persisted computed columns and either way, I don't think making changes is an option for me.
The only way out seems to be to have an indexed view that stores this information but we generally keep away from indexed views and I was wondering if someone on here had a good solution. 🙂
January 17, 2010 at 9:21 pm
WHERE ImageBlob > 0x00
--Jeff Moden
Change is inevitable... Change for the better is not.
January 17, 2010 at 9:25 pm
sounds very interesting!!! let me login to my business machine and see if that reduces the execution plan.
🙁
Aww man.. it says "the text, ntext, and image data types cannot be compared or sorted except when using the IS NULL or LIKE operator
January 17, 2010 at 9:43 pm
Sorry about that... that's why I only store file names for images.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 17, 2010 at 9:49 pm
I know but I am having to work with what is there. When SQL Server parses the query, it is retrieving all the images that satisfy the first condition i.e. id = @rid and retrieving the FILEBLOBs of all those rows so it can filter using the NOT NULL operator;
The performance as a result is very slow. Its a very interesting problem though.
January 18, 2010 at 3:28 am
Yes you have persisted columns in sql 2000, they just don't have that name. All you have to do is put the column in an index and it'll persisted there. Which is exactly what you need here.
check this out :
SELECT DATALENGTH(NULL)
SELECT DATALENGTH(CAST(NULL AS IMAGE))
SELECT DATALENGTH(CAST(0X00 AS IMAGE))
The first 2 return null, #3 returns 1. You could put a where condition on the DATALENGTH persisted column < 2 and that should fetch all nulls. You would need to test this on a single pixel image tho to confirm you considered everything.
January 18, 2010 at 7:10 am
Persisted computed columns are different from what you are talking about.
You are talking about Non-Clustered indexes (correct me if I am wrong.) They are only pointers to clustered index. If you look at the execution plan that is an attachment to the opening post, you will see that FileBlob (the image column) is being retrieved to check for the IS NOT NULL condition in the where clause and this is degrading the performance.
A persisted computed column will only be useful if it stores information about which row contains an image (like a bit column, containing TEXTVALID(Image)).
Maybe I misunderstood you, can you tell in detail your solution.
January 18, 2010 at 8:23 am
Please add a calculated as checksum or textisvalid(blob image).
add that column in the index of your current query. When the index is created the value will be calculated AND SAVED in the index.
Run the query with the current where condition is see how many pages are accessed.
Then run the version with the new indexed column. If everything is done right, you'll get a dramatic difference in page reads.
January 18, 2010 at 9:45 am
Now, there's an idea. Thanks, Remi.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 18, 2010 at 11:26 am
Jeff Moden (1/18/2010)
Now, there's an idea. Thanks, Remi.
Yup, haven't used that one in a while, but that's a perfect exemple of how it could be used.
Of course now with 2008, that discussion is a better known fact (wasn't quite a widespread knowledge back with 2000).
January 18, 2010 at 2:07 pm
Thanks a lot! That works perfectly. You are the man. :hehe:
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply