Solved

Slow search when using wildcards on indexed table

Posted on 2001-08-06
4
281 Views
Last Modified: 2008-02-26
I have an application that makes queries on a moderately sized table of company names with upwards of 200,000 records.
My users will want to make searches on the field tblCompanies.CompanyName. My problem is that the time it is taking to get a response from SQL Server with a recordset is just too long.
The field is indexed - an ordinary index with default values for fill factor etc.
When a user makes a query " select * from tblCompanies where tblCompanies.CompanyName = @Name", the search executes VERY fast.
When a user makes a query " select * from tblCompanies where tblCompanies.CompanyName like @Name + '%' ", the search executes satisfactorily.
However, when a user makes a query " select * from tblCompanies where tblCompanies.CompanyName
like '%' + @Name + '%' ", the search executes very slowly. It can take up to 5 minutes to return a resultset. I have had to set query timeout to 0 so that the server can complete the search in its own times.

I know that SQL query ignores indexes when the '%' wildcard is used. But are there other ways of at least reducing the times taken to get results when the search is made?
I am using MSSQL 6.5 and have the queries in stored procedures being called from VB5 with relevant parameter values.
0
Comment
Question by:tawandat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
nigelrivett earned 100 total points
ID: 6355260
I am afraid this will have to perform a scan so not a lot you can do.
Try with a non-clustered index so that the scan can use the leaf of the index which is a copy of the field and reduce page reads (look at the query plan - you may need to give a hint).
Check the fragmentation - try rebuilding the index every day leaving no free space.
0
 
LVL 2

Expert Comment

by:ZhongYu
ID: 6355489

You should use clustered index on CompanyName if you can. I think migelrivett had a typo.

Also rewrite the code as:

SELECT @Name = '%' + @Name + '%'
SELECT ... where tblCompanies.CompanyName like @Name



0
 
LVL 1

Expert Comment

by:wdalton
ID: 6357256
Actually NigelRivett is right. A non-clustered index on CompanyName will provide with the least amount of pages to be scanned, but you will always end up with a scan, and that's something to avoid.
In SQL 7 and 2000 I would suggest to put a full text index on that field, and use that in the query. Otherwise, Let the customer choose if he wants the most flexibility(%name%) but the slowest response, or a limited search (name%) and a fast response. So Always search for @name% and if the user needs the full search capabilities, he'll have to enter the first % himself.

0
 
LVL 2

Expert Comment

by:ZhongYu
ID: 6358757
NigelRivett was right. Sorry.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question