I am trying to optimize the T-SQL stored procedure that I have. It is for pulling records based on VIN (17-character alphanumeric string); usually people only know a few numbers - for example. the first digit can be "1", "2" or "J"; the second is “H” and the third may be “M” or “G”; etc.
This leads to a rather confusing query, whose suggestion is WHEREsimilar to
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 is huge (millions of records), so the queries that I run that are of this type WHEREcan take hours to run if more than two digits are found in them, even if I only request the best 3000 records . I feel there must be a way to make this substring character work faster. The watch is completely unacceptable; I would like these requests to be completed in just a few minutes.
I do not have any edit permissions in the database, unfortunately, therefore I cannot add indexes or anything like that; all I can do is change my stored procedure (although I can try to ask the database administrators to change the table).
source
share