I am working on some legacy SQL code, and am looking for some help trying to fix an odd query I have. It looks like this:
SELECT *
FROM TA LEFT OUTER JOIN TB ON TA.a1 = TB.b3
WHERE TA.a1 LIKE 'usersearch'
OR TB.b1 + ':' + TB.b2 LIKE 'usersearch'
usersearch is a user supplied regex that is unknown at the time of the query's creation.
The usersearch variable is the same in both LIKE sections. This is an insane bit of code, and takes forever to run, but I'm having a hard time figuring out how I can optimize it.
The terrible part is that it constructs a string to perform a regex on for every single line. However, I'm not really sure how to avoid this.
If anyone has any ideas I'd love to hear them!