T
T
tukreb2019-10-23 08:35:49
PostgreSQL
tukreb, 2019-10-23 08:35:49

How expensive is the NOT IN/NOT EXISTS SQL operation?

I'm wondering how expensive SQL queries with NOT IN or NOT EXISTS will be?
Let's say there are 2 tables
items and using_items
In the items table there are 12 million records, in using_items 11 million. Of course, the number is hypothetical and unlikely.

SELECT t.id, t.name 
FROM items t
WHERE (t.type = 'some_item_type') AND t.id
      NOT IN (
          SELECT w.id_item
          FROM using_items w
         )
ORDER BY t.name ASC;

or
SELECT t.id, t.name 
FROM items t
WHERE (t.type = 'some_item_type') AND 
      NOT EXISTS (
          SELECT w.id_item
          FROM using_items w
          WHERE t.id = w.id_item
         )
ORDER BY t.name ASC;

How bad will everything be in each of the requests? If it is very bad, what are the possible solutions?
Or will you have to split the using_items table into tables by the type attribute (using_items_type1, using_items_type2)?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2019-10-23
@tukreb

SQL JOIN
VQ5XP.png
Second from the left.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question