Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TNJHO-0003oO-1t for pgsql-admin@postgresql.org; Sun, 14 Oct 2012 08:10:34 +0000 Received: from smtp-vbr8.xs4all.nl ([194.109.24.28]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TNJHM-0000qm-Bp for pgsql-admin@postgresql.org; Sun, 14 Oct 2012 08:10:33 +0000 Received: from [10.0.0.211] (vindev.xs4all.nl [80.101.96.12]) (authenticated bits=0) by smtp-vbr8.xs4all.nl (8.13.8/8.13.8) with ESMTP id q9E8AU6t054874 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 14 Oct 2012 10:10:31 +0200 (CEST) (envelope-from vinny@xs4all.nl) Message-ID: <507A7375.7000903@xs4all.nl> Date: Sun, 14 Oct 2012 10:10:29 +0200 From: Vincent User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: Re: Query with limit takes hours References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------060900040202040701040101" X-Virus-Scanned: by XS4ALL Virus Scanner X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201210/42 X-Sequence-Number: 40158 This is a multi-part message in MIME format. --------------060900040202040701040101 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 10/14/2012 08:56 AM, henk de wit wrote: > On PG 9.1 and 9.2 I'm running the following query: > > SELECT > * > FROM > stream_store > JOIN > ( > SELECT > UNNEST(stream_store_ids) AS id > FROM > stream_store_version_index > WHERE > stream_id = 607106 AND > version = 11 > ) AS records USING (id) > ORDER BY > id DESC > > This takes several (10 to 20) milliseconds at most. > > When I add a LIMIT 1 to the end of the query, the query time goes to > several hours(!). > > The full version String of PG 9.1 is "PostgreSQL 9.1.5 on > x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) > 4.4.5, 64-bit". The 9.1 machine is a socket 771 dual quad core at > 3.16Ghz with 64GB memory and 10 Intel x25M SSDs in a RAID5 setup on 2 > ARECA 1680 RAID controllers. The "stream_store" table has 122 million > rows and is partitioned. The array that's being unnested for the join > has 27 entries. > > Any idea? I'd look at the difference in the output of EXPLAIN for both queries, that's bound to tell you something... --------------060900040202040701040101 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 10/14/2012 08:56 AM, henk de wit wrote:
On PG 9.1 and 9.2 I'm running the following query:

SELECT 
    *
FROM 
    stream_store 
JOIN
    (
        SELECT 
            UNNEST(stream_store_ids) AS id
        FROM 
            stream_store_version_index 
        WHERE 
            stream_id = 607106 AND
            version = 11
    ) AS records USING (id)
ORDER BY 
    id DESC

This takes several (10 to 20) milliseconds at most.

When I add a LIMIT 1 to the end of the query, the query time goes to several hours(!).

The full version String of PG 9.1 is "PostgreSQL 9.1.5 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) 4.4.5, 64-bit". The 9.1 machine is a socket 771 dual quad core at 3.16Ghz with 64GB memory and 10 Intel x25M SSDs in a RAID5 setup on 2 ARECA 1680 RAID controllers. The "stream_store" table has 122 million rows and is partitioned. The array that's being unnested for the join has 27 entries.

Any idea?

I'd look at the difference in the output of EXPLAIN for both queries, that's bound to tell you something...
--------------060900040202040701040101--