Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Slow search when using wildcards on indexed table

Posted on 2001-08-06
4
Medium Priority
?
284 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 300 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

604 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