Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character-matching queries in SQL

Tags:

sql-server

I'm attempting to optimize a T-SQL stored procedure I have. It's for pulling records based on a VIN (a 17-character alphanumeric string); usually people only know a few of the digits—e.g. the first digit could be '1', '2', or 'J'; the second is 'H' but the third could be 'M' or 'G'; and so on.

This leads to a pretty convoluted query whose WHERE clause is something like

WHERE SUBSTRING(VIN,1,1) IN ('J','1','2')
AND SUBSTRING(VIN,2,1) IN ('H')
AND SUBSTRING(VIN,3,1) IN ('M','G')
AND SUBSTRING(VIN,4,1) IN ('E')
AND ... -- and so on for however many digits we need to search on

The table I'm querying on is huge (millions of records) so the queries I'm running that have this kind of WHERE clause can take hours to run if there are more than a couple digits being searched on, even if I'm only requesting the top 3000 records. I feel like there has to be a way to get this substring character matching to run faster. Hours are completely unacceptable; I'd like to have these kinds of queries run in just a few minutes.

I don't have any editing privileges on the database, sadly, so I can't add indexes or anything like that; all I can do is change my stored procedure (although I can try to beg the DBAs to modify the table).

like image 671
Andrew Avatar asked Feb 01 '26 20:02

Andrew


2 Answers

You can use

WHERE VIN LIKE '[J12]H[MG]E%'

At least that should hopefully lead to 3 index seeks on the ranges JH%, 1H%, and 2H% rather than a full scan.

Edit Although testing locally I found that it does not do multiple index seeks as I had hoped it converts the above to a single seek on the larger range VIN >= '1' and VIN < 'K' with a residual predicate to evaluate the LIKE

I'm not sure whether it will do this for larger tables or not but otherwise it may well be worth trying to encourage this plan with

WHERE (VIN LIKE 'JH%' OR  VIN LIKE '1H%' OR  VIN LIKE '2H%') 
        AND VIN LIKE '[J12]H[MG]E%'
like image 96
Martin Smith Avatar answered Feb 04 '26 10:02

Martin Smith


You could use the LIKE keyword

SELECT
  *
FROM Table
WHERE VIN LIKE '[J12]H[MG]E%'

This would even allow you to work with instance where they know the second character is not 'A' by using [^A] in the statement, such as:

WHERE VIN LIKE '[J12][^A][MG]E%'

Reference http://msdn.microsoft.com/en-us/library/ms179859.aspx

like image 20
John Hartsock Avatar answered Feb 04 '26 08:02

John Hartsock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!