Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Update Query does not use Index

I have a update query that runs slow (see first query below). I have an index created on the table PhoneStatus and column PhoneID that is named IX_PhoneStatus_PhoneID. The Table PhoneStatus contains 20 million records. When I run the following query, the index is not used and a Clustered Index Scan is used and in-turn the update runs slow.

UPDATE PhoneStatus    
SET RecordEndDate = GETDATE()    
WHERE PhoneID = 126  

If I execute the following query, which includes the new FROM, I still have the same problem with the index not used.

UPDATE PhoneStatus    
SET RecordEndDate = GETDATE()   
FROM Cust_Profile.PhoneStatus     
WHERE PhoneID = 126 

But if I add the HINT to force the use of the index on the FROM it works correctly, and an Index Seek is used.

UPDATE PhoneStatus     
SET RecordEndDate = GETDATE()   
FROM Cust_Profile.PhoneStatus WITH(INDEX(IX_PhoneStatus_PhoneID))   
WHERE PhoneID = 126    

Does anyone know why the first query would not use the Index?

Update

In the table of 20 million records, each phoneID could show up 10 times at the most

BarDev

like image 586
Mike Barlow - BarDev Avatar asked Aug 19 '10 18:08

Mike Barlow - BarDev


People also ask

Why is my query not using index?

Answer: Oracle SQL not using an index is a common complaint, and it's often because the optimizer thinks that a full-scan is cheaper than index access.

Does update use index?

UPDATE can use index in WHERE clause (if the column in the clause has index) INSERT can use index when uses SELECT (but in this case, index is from another table)

Why index is not being used in SQL Server?

Analysis: SQL Server might ignore the index if the range is too wide. For example, these two queries will probably hit the index on the LastUpdated column in a 300 million rows table because the range is very narrow.

Can we update index in SQL Server?

Using SQL Server Management StudioRight-click the index that you want to modify and then click Properties. In the Index Properties dialog box, make the desired changes. For example, you can add or remove a column from the index key, or change the setting of an index option.


4 Answers

How many distinct PhoneIDs are in the 20M table? If the condition where PhoneID=126 is not selective enough, you may be hitting the index tipping point. If this query and access condition is very frequent, PhoneID is a good candidate for a clustered index leftmost key.

like image 73
Remus Rusanu Avatar answered Nov 11 '22 04:11

Remus Rusanu


Pablo is correct, SQL Server will use an index only if it thinks this will run the query more efficiently. But with 20 million rows it should have known to use the index. I would imagine that you simply need to update statistics on the database.

Form more information, see http://msdn.microsoft.com/en-us/library/aa260645(SQL.80).aspx.

like image 28
Russ Avatar answered Nov 11 '22 03:11

Russ


Take a look at Is an index seek always better or faster than an index scan? Sometimes a seek and a scan will be the exact same.

An index might be disregarded because your stats could be stale or the selectivity of the index is so low that SQL Server thinks a scan will be better

Turn on stats and see if there are any differences between the query with and without a seek

SET STATISTICS io ON
UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
WHERE PhoneID = 126


UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
FROM Cust_Profile.PhoneStatus WITH(INDEX(IX_PhoneStatus_PhoneID))
WHERE PhoneID = 126

Now look at the reads that came back

like image 2
SQLMenace Avatar answered Nov 11 '22 03:11

SQLMenace


SQLServer (or any other SQL Server product for that matter) if not forced to use any index at all. It will use it, if it thinks will help running the query more efficiently.

So, in your case, SQLServer is thinking that it doesn't need using IX_PhoneStatus_PhoneID and by using its clustered index might get better results. It might be wrong though, that's what index hints are for: letting the Server know it would do a better job by using other index.

If your table was recently created and populated, it might be the case that statistics are somewhat outdated. So you might want to force a statistic update.

like image 1
Pablo Santa Cruz Avatar answered Nov 11 '22 04:11

Pablo Santa Cruz