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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 speed issue in like operators ?

Author  Topic 

shantanu88d
Starting Member

35 Posts

Posted - 2011-06-18 : 11:29:51
I was asked which one of these queries would run faster...
select * from emp where emp_name like 'John%'

and
select * from emp where emp_name like '%John'

I answered that second would run slower.
Then I was asked what to do to make it run faster ???

Any ideas folks ?

There are no failures...only experiences!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-18 : 11:36:36
The first search term can use an index on the emp_name table, since it does not begin with a wildcard character.

The 2nd term cannot be optimized that way, unless you modify the table and the term as below:

ALTER TABLE emp ADD rev_emp_name AS REVERSE(emp_name)
CREATE INDEX ix_rev_emp_name ON emp(rev_emp_name)
SELECT * FROM emp_name WHERE rev_emp_name LIKE REVERSE('%John')
Go to Top of Page
   

- Advertisement -