July 5, 2005 at 9:11 am
Hi
Maybe someone here can help me with a question I have:
Does the "sequesnce" in which you do your WHERE clause impact on the performance in a query?
e.g.
Will there be any performance differences between:
SELECT this, that
FROM tbl
WHERE field1=1 ANDfield2='that'
and
SELECT this, that
FROM tbl
WHERE field2='that' AND field1=1
if so, can you please explain...
Thanx in advance
July 5, 2005 at 9:16 am
No. Sql server picks up the best sequence to apply the where condition. You can't really have much control on that part (unless you use a case statement).
July 5, 2005 at 7:37 pm
hm ... i read something about this somwhere. It says if your first condition match with your first column in your index, it will fasten the speed to build the query plan (if i am not mistaken).
for example, you have an index consists of col2, col1, col3. In your WHERE clause, if you put "WHERE col2 = xxx and col3 = yyy and col1 = zzz", it will run faster. Second condition onwards can be ignore. However, if we follow the sequence, it may give us a little bit of performance gain too.
if you put "WHERE col1 = zzz AND col3 = yyy AND col2 = xxx", query optimizer is intelligent enough to determine the best plan that you shold take.
Please correct me if I am wrong, Remi.
Thanks.
Leo
July 5, 2005 at 8:45 pm
hm ... i read something about this somwhere. It says if your first condition match with your first column in your index, it will fasten the speed to build the query plan (if i am not mistaken).
I would love to see that link...
The one thing about compound indexes is that if your where condition uses only the 2nd or third column, then the index becomes useless because an index scan is required instead of a seek (because the data is sorted by col1, col2, col3). This is why it can be usefull to have an index on last_name, first_name and another index on first_name only.
About the order in the query, the optimizer will just ignoring it and figure the fastest (correct) way to filter out the data.
July 5, 2005 at 9:48 pm
here is the link:
http://www.sqlservercentral.com/columnists/lPeysakhovich/indexcreationguidelines.asp
There is a line of statement as below:
May be you are right. If we specified all indexed columns (for an index), regardless their sequence order, there should be no difference in performance.
Thanks, dude.
Leo
July 5, 2005 at 9:52 pm
That's exactly what I said... only in proper english . In the case of such an index, if you use only the right column (which is not really sorted), the server would have to do and index scan + bookmark lookup. It's most likely faster to just do a clustered index scan in such cases.
July 5, 2005 at 10:14 pm
i've learned something today.
thanks a lot.
Leo
July 5, 2005 at 10:14 pm
i've learned something today.
thank a lot.
Leo
July 6, 2005 at 12:09 am
thanx for all the replies, and yeah: I've also learned something today =)
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply