Solved

Slow search when using wildcards on indexed table

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

21 Experts available now in Live!

Get 1:1 Help Now