I have a situation where I have to dynamically create my SQL strings, and I try to use paramators and sp_executesql where possible so that I can reuse query plans. With a lot of online reading and personal experience, I found that “NOT IN” and “INNER / LEFT JOIN” are slow executors and expensive when the main (leftmost) table is large (1.5M rows with 50 columns). I also read that you should avoid using any functions as this slows down the queries, so I wonder what is worse?
I have used this workaround in the past, although I'm not sure if this is the best thing to do to avoid using “NOT IN” with a list of items when, for example, I go to a list of 3 character strings with, for example, a pipe separator ( only between items):
LEN(@param1) = LEN(REPLACE(@param1, [col], ''))
instead:
[col] NOT IN('ABD', 'RDF', 'TRM', 'HYP', 'UOE')
... imagine that the list of strings has from 1 to 80 possible long values, and this method does not provide itself with the possibility of smoothing.
In this example, I can use "=" for NOT IN, and I would use the traditional list technique for my IN, or! = if it’s faster, although I doubt it. Is it faster than using NOT IN?
As a possible third alternative, that if I knew all the other possibilities (IN capabilities, which could potentially be more than 80-95x), instead transmit them; this will be done in the business layer of the application to take the load off SQL Server. It's not a good opportunity to reuse a query plan, but if it saves a second or two on a big nasty query, why the hell not.
I am also good at creating SQL CLR. So how would string manipulation above be a better CLR function?
Thoughts?
Thanks in advance for any help / advice, etc.