Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal LIKE search in SQL

I have a parts database that I am going to be constantly querying for a quoting system. The parts database has 1,400,000+ records in it. The users are just going to start typing part numbers, which they expect the system to be able to find after only a few characters, so I need to be able to do a wildcard search, something like:

SELECT NeededFields FROM Parts WHERE PartNumber LIKE 'ML%'

Is there any kind of optimization that I can perform to try to wring the most performance out of this type of query? I have the PartNumber field indexed, but I'm not sure if that is the best that I can get. I'd be willing to consider alternate indexing structures built into the database separate from SQL indexes too. The primary key is a Guid, but I need this for replication and because of specific data structures that I use.

like image 755
Michael Bray Avatar asked Jan 04 '09 21:01

Michael Bray


People also ask

How can you improve the performance of a like query?

The only other way (other than using fulltext indexing) you could improve performance is to use "LIKE ABC%" - don't add the wildcard on both ends of your search term - in that case, an index could work. If your requirements are such that you have to have wildcards on both ends of your search term, you're out of luck...

How do you do multiple like conditions in SQL?

Not only LIKE, but you can also use multiple NOT LIKE conditions in SQL. You will get records of matching/non-matching characters with the LIKE – this you can achieve by percentile (%) wildcard character. Below use cases, help you know how to use single and multiple like conditions.

Can we use multiple like in SQL?

No, MSSQL doesn't allow such queries.


1 Answers

I'd guess that your primary key (the GUID) probably has a clustered index. You may want to consider making the primary key NOT be clustered. Instead, you could cluster the index you created for the PartNumber. (there can only be one clustered index per table)

You should also consider adding a TOP predicate to the query, so that only the top 100 (or so) rows are returned. I'm thinking... if the user first types an M, there could be a couple hundred thousand matches, which would be slow to load. By limiting the number of rows, you should get better performance.

like image 116
George Mastros Avatar answered Sep 18 '22 18:09

George Mastros