Solved

Slow search when using wildcards on indexed table

Posted on 2001-08-06
4
275 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now