I have table which has schema as follows
TABLE EMP
EMPID - Primary KEY
DEPTID - NON Clustered Index
ISActive - NON Clustered Index
It has other non index columns as well
This table is highly transactional and will have lots of data. Currently I have 824392 rows in this table.
Now I am executing query which takes lot of time sometime and sometime it works like a charm.
PFB the query :
UPDATE EMP SET ISActive= 0 WHERE DEPTID in (11,22)
Questions :
Will only nonclustered index on
ISActive
get updated?Will all the nonclustered indexes and clustered index get updated ?
If I remove some of the rows as they are quite old will that help with performance ?
Why this query take some minutes to execute and sometimes it will take less then second ?
What is the best possible solution for this ?
Any pointers are highly appreciated
Thanks in advance
Regards,
Phani
4 Answers 4
1&2 - Every update query must update the data for the table, wherever it may appear - in the clustered index (always), in any index that has the column in its definition, and in any index which just includes this column.
3 - Removing records may or may not help, it depends on 4:
4 - Probably, because sometimes it will not use the index, but will instead choose to scan the entire table. If it's scanning the entire table, then reducing the size of the table (via (3) above) will help. It will scan the entire table if, so far as the optimizer is concerned, that will be more efficient than using the index, then accessing the clustered index to complete the operation. It may choose to do this because it's actually true that the index will not help, or if statistics are out of date, or just sometimes it gets this wrong. Examine execution plans to determine if this has happened.
4a - If there's a lot of activity on the table, then other operation may have locks that prevent your query from processing as fast as it could if it was the only activity occurring in the database. Waiting for locks can take seconds, minutes, or hours - it depends on how timely the other operations are
5 - Solution to what?
824392 isn't that many rows, but it may make for a big table - it depends on how wide those rows are, really.
-
Thanks Damien for the valuable information. i did not the explanantion for the point 1 and 2 . Could you please dig in to this more and explain it .Jaddu– Jaddu2012年02月02日 02:59:23 +00:00Commented Feb 2, 2012 at 2:59
-
@Jaddu - there's no concept of an
update
just affecting a particular index - it affects the entire table (clustered index + all non-clustered indexes) and maintains their consistency. So the answer to 1 is no, and to 2 is yes.Damien_The_Unbeliever– Damien_The_Unbeliever2012年02月02日 05:17:05 +00:00Commented Feb 2, 2012 at 5:17 -
That should be: "it affects the entire table (clustered index + all non-clustered indexes that include an updated column)", right?ypercubeᵀᴹ– ypercubeᵀᴹ2012年09月28日 18:45:36 +00:00Commented Sep 28, 2012 at 18:45
-
1@ypercube - that would be correct (if the column is in the index, or is an included column in the index)Damien_The_Unbeliever– Damien_The_Unbeliever2012年09月28日 19:07:48 +00:00Commented Sep 28, 2012 at 19:07
EMPID is PK Key which is customer index. And your query is using cluster index. Mean.. there should not be problem with index. However, you may need to check index fragmentation. If index fragmentation % is high, more than 30%, you should reubilt your index otherwise, need to reorganize.
According to what you are saying that someitmes take less than second, sometimes take minues. mean..because of others query process that make your query to wait.
Just curious, if your IsActive is one of your non-clustered index, and it appears to me as it is boolean, why not drop the non-clustered index for IsActive before the update and recreate the index after update.
Partition on Indexes ... may help in increasing the Performance... So avoiding a whole table scan.. (A local may Help.. )