简体   繁体   中英

different execution time for same query where different values in where clause

Dears, I run the same query and it takes different execution times as the following: the query is :

select * from table1 where userID = 2

it scans the non cluster index idx1 which contains the userID as one of its keys, so I get the results in 5 seconds. BUT when I run it again with userID = 5 it scans the non cluster index idx2 which not contains userID as one of its keys, and get the results after 2 HOURS, I think it maybe read the whole table to find the userID = 5

what is the cause of this problem?? maybe the value of userID = 5 is not in the idx1 leaf?? I think the leaf level in NC index stores a range of values and the query run well for userID =4 and for userID = 6 so userID = 5 should be in the leaf level,

please advice

Try to use index hint

For the hot fix

select * from table1 with(index(idx1) where userID = 5

Ref: http://blog.sqlauthority.com/2009/02/08/sql-server-introduction-to-force-index-query-hints-index-hint-part2/

For the long term solution

You might need to update statistic of the table or rebuild the index. You can follow this linke: Script for rebuilding and reindexing the fragmented index?

First update statistics of the indexes of that tables. If it does not work and you're using ASE15 or +, you should update statistics of the column userID. The ASE15 optimizer is more sensitive to statistics than ASE12.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM