0

I simply would like to know if there are benefits going one way or another for the following scenario:

I have a SQL query with many "WHERE" items, like so:

Select
*
from
a_table
WHERE
(W like X) AND
(Y like Z) AND
(A not like B) AND
(C not like D)

The thing is, I'm wondering for the "Not-like" portion if it would be faster/more efficient as an 'exclusionary subquery', by which I mean I do a select FOR those things instead of cutting those out, and then at the bottom of my query throw in something to the tune of:

AND someId NOT IN
(select distinct someId
FROM a_table
WHERE
A like B AND
C like D)

Another note, this is of course an example query. The real one has 5+ "where's" and 5+ "where not's". in case that makes any difference.

Is there a huge difference in efficiency/time/processing between these two methods? I was always taught that the "like" function was bad and I should feel bad for using it, and by nature I would like to avoid it if I can.

Thank you for your time.

4
  • why don't you use EXPLAIN or similar to check on your database?
    – leeor
    Sep 23, 2015 at 20:12
  • 2
    Most RDBMSs allow you to examine the execution plan of the query to examine the cost involved. Find out how to do that with your RDBMS and compare both queries.
    – sstan
    Sep 23, 2015 at 20:12
  • @sstan is it potentially different per-query? I was hoping for not only an answer to this but something to use going forward should a similar situation arise, so I want to make sure any answer I decide upon is ok for more than just this instance. Sep 23, 2015 at 20:27
  • 1
    Depending on the data (i.e. multiple rows per someId) the NOT IN might return a different result set. And n most cases a single pass over a table will be more efficient than two (outer SELECT and subquery)
    – dnoeth
    Sep 23, 2015 at 20:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.