Re: improve 'where not exists' query..
От
Richard Huxton
Тема
Re: improve 'where not exists' query..
Дата
Msg-id
4360999B.1000101@archonet.com
Список
Дерево обсуждения
improve 'where not exists' query.. Noel Whelan <noel.whelan@gmail.com>
Re: improve 'where not exists' query.. Bruno Wolff III <bruno@wolff.to>
Re: improve 'where not exists' query.. Noel Whelan <noel.whelan@gmail.com>
Re: improve 'where not exists' query.. Bruno Wolff III <bruno@wolff.to>
Noel Whelan wrote: > I executed the following: > > EXPLAIN ANALYZE SELECT cwit.cempid > FROM "cwItems" cwit > WHERE (NOT (EXISTS (SELECT con.cempid > FROM contacts con > WHERE (con.cempid = cwit.cempid)))); > > It comes back with: > > Seq Scan on "cwItems" cwit (cost=0.00..8929702.11 rows=5132 width=8) (actual > time=132218.29..148623.27 rows=31 loops=1) > Filter: (NOT (subplan)) > SubPlan > -> Seq Scan on contacts con (cost=0.00..870.77 rows=1 width=8) (actual time= > 11.82..11.82 rows=1 loops=12528) > Filter: (cempid = $0) > Total runtime: 148623.54 msec > > I'm not certain it's an issue with the query itself going wrong. I consider > I'm basically telling it: for each cempid in 'cwItems', check whether or not > it exists as a cempid in 'contacts', which could be inherently inefficient > in itself. Well, as you say it's never going to be possible without checking all items. However, you've got two seq-scans there and I'd expect a seq-scan and looping over an index (or a hash-based plan perhaps). Now, looking at the values it's only seeing 31 rows in cwItems and 1 row (!) in contacts, so that would explain the scans. However, in that case I'd expect it to be much faster than it is. Hmm - I'd take the following steps: 1. VACUUM FULL VERBOSE ANALYSE
В списке pgsql-general по дате отправления