473,799 Members | 3,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoid index scan with LIKE and a variable

Hi,
Here's my problem: I want to write a stored procedure that returns all
records from a table that have a certain column starting with given
text. I however find that using LIKE and a variable always causes an
index scan... which is causing performance issues. My table has about
3.5M records.

Below is a test. In query analyser if I look at the execution plan for
the following it will come up as in index scan. However, if i just
hard-code the text it all works fine (index seek).

How can I do this with reasonable speed???
Thanks Greg
DECLARE @find varchar(50)
SET @find = 'start'

SELECT TOP 100
*
FROM Test
WHERE
Col1 LIKE @find + '%'
--Col1 LIKE 'start%'

Jul 23 '05 #1
1 5273
gregbacchus (gr**********@g mail.com) writes:
Here's my problem: I want to write a stored procedure that returns all
records from a table that have a certain column starting with given
text. I however find that using LIKE and a variable always causes an
index scan... which is causing performance issues. My table has about
3.5M records.

Below is a test. In query analyser if I look at the execution plan for
the following it will come up as in index scan. However, if i just
hard-code the text it all works fine (index seek).

How can I do this with reasonable speed???
Thanks Greg
DECLARE @find varchar(50)
SET @find = 'start'

SELECT TOP 100
*
FROM Test
WHERE
Col1 LIKE @find + '%'
--Col1 LIKE 'start%'


You don't say whether the index on Test.Col1 is clustered or not, and
whether this is the index that is scanned. I would expect that the index
on Test.Col1 is non-clustered, and the scan you see is a clustered index
scan.

When you have the literal, SQL Server knows about the query than you
have the variable. For the variable, SQL Server can only make a standard
assumption. Had the variable instead been a parameter to a stored procedure,
SQL Server would have looked at that value.

The best way out may be to simply use an index hint. You could also use
sp_executesql and pass the variable as a parameter.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3979
by: Robert Brown | last post by:
If I use _reverse_ wildcard search will it always result in a table scan? Is it possible to get the DB (Oracle or SQL server) to use indexes when doing reverse wildcard match? let's say I have: table email_address (id int, email varchar) with the following entries
0
563
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the parent query, the subquery is inlined for each one. This means multiple redundant executions of the subquery. I recall there was a way to defeat this optimization involving introducing an extra subquery layer somewhere. But I'm failing to be able...
3
5703
by: Carlos Moreno | last post by:
I can't find a reasonable explanation for this. I have a table game, with primary key gameid (an int). If I use a where involving gameid and <, or >, or <=, or >=, then I get a sequential scan. If I use =, then of course I get an Index scan. More surprising to me is the fact that using BETWEEN, I get an Index scan!!
17
6203
by: Dima Tkach | last post by:
Hi, everybody! I just ran into a weird problem on 7.3.4. Here is a simple testcase: rapidb=# create table nametab (name text); CREATE TABLE rapidb=# create index name_idx on nametab(name); CREATE INDEX rapidb=# set enable_seqscan=false;
13
5679
by: LUIS FAJARDO | last post by:
I have the following sintax: Select * From Inventory Where PartId = Coalesce(v_PartId, PartId) this type of query is used within an store procedure that provide the v_PartId parameter, the data type for PartId on the database is CHAR(25) same for the parameter. The problem that I have is that this query uses a FULL SCAN to the table instead of using the PartId index
1
6743
by: Gorilla | last post by:
I bound my package with EXPLAIN(YES), and it's got the following static SQL in it: EXEC SQL SELECT CARDF, RECLENGTH INTO :CARDF,:RECLENGTH FROM SYSIBM.SYSTABLES WHERE NAME = :TBNAME AND CREATOR = :TBCREATOR The explain shows that it does a *full table scan* on SYSIBM.SYSTABLES!
2
1636
by: Hervé Piedvache | last post by:
Hi, I have may be a stupid question, but I'm a little surprised with some explains I have, using date fields ... I would like to understand exactly when index are used ... I'm using PostgresQL 7.4.1 I have a table with 351 000 records. I have about 300 to 600 new records by day
2
1664
by: Thomas F.O'Connell | last post by:
I'm interested to know a little bit more about the postgres implementation of indexes. I'm specifically wondering what it means in the output of EXPLAIN when a filter is applied. I'm trying to decide whether it makes sense to use indexes on expressions rather than relying on a left-anchored LIKE for date filtering. Here's what I've got:
10
2430
by: Dia | last post by:
At the company I work job applicants are required to do a little test. The human resource manager recently had a candidate who claimed one of the questions was ambiguous. Dependent upon the version of DB2 one or the other answer of a multiple choice question could be right, the candidate said. The HRM asked me to look into the matter, but since I am no expert in the workings of DB2 I submit the question to you hoping any of you can
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.