Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Optimization - Faster data retrival

Author  Topic 

raj.prabhu001
Starting Member

16 Posts

Posted - 2011-11-23 : 02:29:49
I want to select name from table where name like raj

1) Select name From table
Where name like 'raj%'

2) Select name From table
Where name like '%raj'

1)Out of the two queries which one would retrieve data faster and why ?
2)and how to make the slower query faster?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-23 : 03:05:58
i think 1 will definitely retrieve data faster as there's more likely that it would go for index seek if one present in column. In second case, at max it will go for index scan as it starts with wildcard

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-11-23 : 10:35:39
Depends.

How many rows are in the table?
How many rows does 'raj%' versus '%raj' return?
Is there an index on Name?
Does the table have a primary key?
If the table has a primary key is it clustered or is there another index that is clustered?

One way to speed up preceeding wildcard searches ('%something') is to created a persisted computed column with the text reversed, in this case REVERSE(Name). Then you can index that and use LIKE 'gnihtemos%' which gives you the possibility of an index seek.
Go to Top of Page
   

- Advertisement -