I have a table which contains a few columns, one of which is an image datatype, this is used to store BLOB data, which could be anything from a jpg to a large PDF.
I have a requirement to update one of the other columns in the table, however the query is taking a ludicrously long time to complete – that's if it does complete at all.
The table is the following format:
ID (int), HexData (image), FileName(nvar), Filetype(nvar), folder(nvar), user(int)
The query used is simply –
update [database].[dbo].[myTable]
set user = 5
where id = 2
This is a test table on a test database, not the live system. There are only 5 rows, no indexes (including clustered), and no other tables in the database.
Best Answer
The table in question is a heap. This storage structure does not release pages made empty by delete operations, unless the delete occurs while an exclusive table lock is taken (and not always then).
From
DELETE
(Transact-SQL) in the documentation:The upshot is that your table likely contains a large number of empty pages allocated for rows the heap held in the past. You can check this using:
Without any indexes, the only way for the SQL Server to process the update query is to employ a full table scan. This will touch every data page allocated to the table, including the empty ones. If there are many pages that must be brought into memory from permanent storage, this can take a long time.
The solution is to rebuild the heap table using
ALTER TABLE <table_name> REBUILD
, or create a clustered index.The image data type has been deprecated in favour of varbinary(max) for a very long time now. If you have any freedom to change this, you probably should. Even so, it is also possible that the existence of this LOB column is causing the slowness through a quite separate mechanism I describe in detail here on SQLblog.com:
From that article: