Link to home
Start Free TrialLog in
Avatar of tawandat
tawandat

asked on

Slow search when using wildcards on indexed table

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.
ASKER CERTIFIED SOLUTION
Avatar of nigelrivett
nigelrivett

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ZhongYu
ZhongYu


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



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.

NigelRivett was right. Sorry.