From: Moritz Onken <onken@houseofdesign.de>
Cc: pgsql-performance@postgresql.org
Subject: Re: Planner should use index on a LIKE 'foo%' query
Date: Mon, 30 Jun 2008 18:42:07 +0200
Message-ID: <35332CE1-8962-40D2-99DA-16BA80087979@houseofdesign.de> (raw)
In-Reply-To: <Pine.LNX.4.64.0806301713590.4085@aragorn.flymine.org>
References: <287E4428-BECC-46D6-BC47-EA94EA0121F0@houseofdesign.de>
<20080628191931.GB13013@uio.no>
<3D5EF684-9585-4057-BB6E-C81509635A37@houseofdesign.de>
<20080630145910.GA16955@uio.no>
<0E158087-4E44-4398-96E0-BFEEAAE099A7@houseofdesign.de>
<Pine.LNX.4.64.0806301713590.4085@aragorn.flymine.org>
>
> The thing here is that you are effectively causing Postgres to run a
> sub-select for each row of the "result" table, each time generating
> either an empty list or a list with one or more identical URLs. This
> is effectively forcing a nested loop. In a way, you have two
> constraints where you only need one.
>
> You can safely take out the constraint in the subquery, so it is
> like this:
>
> SELECT COUNT(*) FROM result WHERE url IN (SELECT shorturl FROM item);
>
> This will generate equivalent results, because those rows that
> didn't match the constraint wouldn't have affected the IN anyway.
> However, it will alter the performance, because the subquery will
> contain more results, but it will only be run once, rather than
> multiple times. This is effectively forcing a hash join (kind of).
>
> Whereas if you rewrite the query as I demonstrated earlier, then you
> allow Postgres to make its own choice about which join algorithm
> will work best.
>
> Matthew
Thank you! I learned a lot today :-)
I thought the subquery will be run on every row thus I tried to make
it as fast as possible by using a where clause. I didn't try your
first query on the hole table so it could be faster than mine approach.
greetings,
moritz
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-performance@postgresql.org
Cc: onken@houseofdesign.de
Subject: Re: Planner should use index on a LIKE 'foo%' query
In-Reply-To: <35332CE1-8962-40D2-99DA-16BA80087979@houseofdesign.de>
References: <287E4428-BECC-46D6-BC47-EA94EA0121F0@houseofdesign.de> <20080628191931.GB13013@uio.no> <3D5EF684-9585-4057-BB6E-C81509635A37@houseofdesign.de> <20080630145910.GA16955@uio.no> <0E158087-4E44-4398-96E0-BFEEAAE099A7@houseofdesign.de> <Pine.LNX.4.64.0806301713590.4085@aragorn.flymine.org> <35332CE1-8962-40D2-99DA-16BA80087979@houseofdesign.de>
* 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