V
V
Vladimir Sergeevich2016-11-28 22:59:01
PostgreSQL
Vladimir Sergeevich, 2016-11-28 22:59:01

Why can the request just hang?

Hi all!
Can you please tell me why this request is stuck? Something I don't get at all:

SELECT COUNT(*) FROM gen_lenta WHERE lenta_id NOT IN (SELECT id FROM lenta_bundle)

It hangs because of NOT IN;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2016-11-28
@ollisso

SELECT COUNT(*)
FROM gen_lenta
left join lenta_bundle on lenta_bundle.id=lenta_id
where lenta_bundle.id is not null
Try this way. Usually works much faster than your approach.
It is also worth checking that you have indexes for all the required fields.

Конструкцию NOT IN лучше применять, если подзапрос возвращает менее десятка-двух записей. Эта конструкция при большом объёме данных сжирает кучу ресурсов, в том числе и оперативной памяти.
Используйте подход Oleg или конструкцию NOT EXISTS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question