Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

How does SQL engine process LIKE expressions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arun - 12 Jul 2008 01:07 GMT
Any pointers on how the sql engine process LIKE expressions, especially on
cases like '%searchkey%'.

Thanks
Andrew J. Kelly - 12 Jul 2008 03:04 GMT
Sure that is easy. It will scan the entire index or table to find the
matching values.  You cannot do a seek when the wildcard is at the
beginning.

Signature

Andrew J. Kelly    SQL MVP
Solid Quality Mentors

> Any pointers on how the sql engine process LIKE expressions, especially on
> cases like '%searchkey%'.
>
> Thanks
Tibor Karaszi - 12 Jul 2008 08:49 GMT
...and on some cases, an index containing this column can be scanned and then "dive down" to the
data pages where matches are found (as opposed to all data is in the index or table scan). This is
where the "string indexes" (aren't really indexes, only statistics over the words in the string
column) can be useful to the optimizer - to try to determine selectivity. To check if such "string
index" exist see the output from DBCC SHOW_STATISTICS.

Signature

Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi

> Sure that is easy. It will scan the entire index or table to find the matching values.  You cannot
> do a seek when the wildcard is at the beginning.
[quoted text clipped - 3 lines]
>>
>> Thanks
Gert-Jan Strik - 12 Jul 2008 10:58 GMT
To elaborate on Tibor's response: if you are running SQL Server 2005 (or
later), then in many situations the database engine will (automatically)
keep String Summary information. Then, if you submit a query like

  SELECT *
  FROM my_table
  WHERE string_column LIKE '%searchkey%'

(assuming a nonclustered, noncovering index on string_column, and a
clustered index on some other column)
it will estimate the number of qualifying rows based on these String
Summary statistics. With that (accurate) estimate it will determine
whether to scan a nonclustered index on string_column and look up the
remaining columns through the clustered index, or whether to simply scan
the table / clustered index.

If you are on SQL Server 2000 or earlier, or if your column + data does
not support String Statistics, then the optimizer will assume the number
of qualifying rows to be a fixed percentage of all rows, which typically
leads to the scanning of a covering index (such as the clustered index).

Signature

Gert-Jan
SQL Server MVP

> ...and on some cases, an index containing this column can be scanned and then "dive down" to the
> data pages where matches are found (as opposed to all data is in the index or table scan). This is
[quoted text clipped - 18 lines]
> >>
> >> Thanks
TheSQLGuru - 12 Jul 2008 21:43 GMT
I will add that if you routinely need to do such searches you may want to
consider using Full Text Indexing.

Signature

Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net

> Any pointers on how the sql engine process LIKE expressions, especially on
> cases like '%searchkey%'.
>
> Thanks
Arun - 14 Jul 2008 21:08 GMT
Thanks for the replies.

If I'm performing a prefix match such as LIKE 'FOO%' does it translate this
internally into something like a range predicate: columnName >= “FOO” AND
columnName < “FOOA” and if that is the case then what will be the range for
the expression LIKE '%OO%'?
Andrew J. Kelly - 14 Jul 2008 22:20 GMT
Internally the engine will look at the stats for that index and decide how
to approach it but it may end up acting like a range for the case when the
wildcard is on the end. For the wildcard at the beginning it is exactly as
everyone has told you. It can not do a range unless you consider the range
the entire index or table.

Signature

Andrew J. Kelly    SQL MVP
Solid Quality Mentors

> Thanks for the replies.
>
[quoted text clipped - 4 lines]
> for
> the expression LIKE '%OO%'?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.