From: Mladen Gogala <mladen.gogala@vmsinfo.com>
To: Ãéùñãïò Âáëêáíáò <lebiathan@gmail.com>
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: "SELECT .. WHERE NOT IN" query running for hours
Date: Thu, 06 Jan 2011 23:25:45 -0500
Message-ID: <4D2695C9.10204@vmsinfo.com> (raw)
In-Reply-To: <AANLkTi=ir8MdWX3ecbhfx3AFp1Uv7DWkLG68S26VLkRM@mail.gmail.com>
References: <AANLkTi=ir8MdWX3ecbhfx3AFp1Uv7DWkLG68S26VLkRM@mail.gmail.com>
On 1/6/2011 9:36 PM, Ãéùñãïò Âáëêáíáò wrote:
>
> 1) Why is it taking *so* long for the first query (with the "NOT IN" )
> to do even the simple select?
Because NOT IN has to execute the correlated subquery for every row and
then check whether the requested value is in the result set, usually by
doing sequential comparison. The NOT EXIST plan is also bad because
there is no index but at least it can use very fast and efficient hash
algorithm. Indexing the "hwdocid" column on the "Document" table or,
ideally, making it a primary key, should provide an additional boost to
your query. If you already do have an index, you may consider using
enable_seqscan=false for this session, so that the "hwdocid" index will
be used. It's a common wisdom that in the most cases NOT EXISTS will
beat NOT IN. That is so all over the database world. I've seen that in
Oracle applications, MS SQL applications and, of course MySQL
applications. Optimizing queries is far from trivial.
Ìëáäåí Ãïãáëá
--
Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
www.vmsinfo.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: mladen.gogala@vmsinfo.com, lebiathan@gmail.com
Subject: Re: "SELECT .. WHERE NOT IN" query running for hours
In-Reply-To: <4D2695C9.10204@vmsinfo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox