From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Ben Snaidero <bensnaidero@geotab.com>
To: pgsql-general@postgresql.org
Subject: Re: strange slow query performance
Date: Thu, 17 Jan 2019 21:04:52 +0100
Message-ID: <c5c42a8563796db5cc126dbff2377e8e7532f525.camel@cybertec.at> (raw)
In-Reply-To: <CAEPE5bOdhQm41PEjvTeQK2mrUA5xeOnZ-Oe1Kw=AShvfEbAZpw@mail.gmail.com>
References: <CAEPE5bOdhQm41PEjvTeQK2mrUA5xeOnZ-Oe1Kw=AShvfEbAZpw@mail.gmail.com>
Ben Snaidero wrote:
> The following query runs as expected.
>
> explain analyze SELECT MainTable.gid AS MainTable_gid,MainTable.datetime AS MainTable_datetime,MainTable.objectid AS MainTable_objectid,MainTable.type AS MainTable_type FROM MainTable
> WHERE objectid = ANY(ARRAY(SELECT NE.objectid AS entityId FROM ( nodeobject NE INNER JOIN ( Node N1NE INNER JOIN Node N2NE ON ((N1NE.iLeft >= N2NE.iLeft) AND (N1NE.iLeft <= N2NE.iRight))) ON NE.inodeid = N1NE.iId ) WHERE N2NE.iId = ANY(ARRAY[14438,10814])))
> AND objectid=3161;
> QUERY PLAN
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Index Scan using ix_MainTable_objectid_datetime on MainTable (cost=3254.91..3264.39 rows=1 width=32) (actual time=33.094..33.094 rows=0 loops=1)
> Index Cond: ((objectid = ANY ($3)) AND (objectid = 3161))
> InitPlan 1 (returns $3)
> -> Nested Loop (cost=1.10..3254.75 rows=16296 width=4) (actual time=0.403..26.147 rows=19042 loops=1)
> [...]
> Planning time: 5.693 ms
> Execution time: 33.383 ms
> (15 rows)
>
> But when we add a second condition to the where clause it causes the query performance to drop dramatically. Values in "any(array" do not make a difference. In this example they are the same but even for different values performance is still the poor. Any ideas as to why this is happening?
>
> explain analyze SELECT MainTable.gid AS MainTable_gid,MainTable.datetime AS MainTable_datetime,MainTable.objectid AS MainTable_objectid,MainTable.type AS MainTable_type FROM MainTable
> WHERE objectid = ANY(ARRAY(SELECT NE.objectid AS entityId FROM ( nodeobject NE INNER JOIN ( Node N1NE INNER JOIN Node N2NE ON ((N1NE.iLeft >= N2NE.iLeft) AND (N1NE.iLeft <= N2NE.iRight))) ON NE.inodeid = N1NE.iId ) WHERE N2NE.iId = ANY(ARRAY[14438,10814])))
> AND objectid = ANY(ARRAY(SELECT NE.objectid AS entityId FROM ( nodeobject NE INNER JOIN ( Node N1NE INNER JOIN Node N2NE ON ((N1NE.iLeft >= N2NE.iLeft) AND (N1NE.iLeft <= N2NE.iRight))) ON NE.inodeid = N1NE.iId ) WHERE N2NE.iId = ANY(ARRAY[14438,10814]))) AND objectid=3161;
>
> QUERY PLAN
>
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Index Scan using ix_MainTable_objectid_datetime on MainTable (cost=6509.66..6534.02 rows=1 width=32) (actual time=16442.004..16442.004 rows=0 loops=1)
> Index Cond: ((objectid = ANY ($3)) AND (objectid = ANY ($7)) AND (objectid = 3161))
> InitPlan 1 (returns $3)
> -> Nested Loop (cost=1.10..3254.75 rows=16296 width=4) (actual time=0.438..28.484 rows=19042 loops=1)
> [...]
> InitPlan 2 (returns $7)
> -> Nested Loop (cost=1.10..3254.75 rows=16296 width=4) (actual time=0.056..11.786 rows=19042 loops=1)
> [...]
> Planning time: 4.860 ms
> Execution time: 16442.462 ms
> (26 rows)
Not sure what is going on, but why don't you put the condition "WHERE ne.objectid=3161"
into the subselects? Then there should be at most one result row, and I can imagine
that things will become faster.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.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 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-general@postgresql.org
Cc: laurenz.albe@cybertec.at, bensnaidero@geotab.com
Subject: Re: strange slow query performance
In-Reply-To: <c5c42a8563796db5cc126dbff2377e8e7532f525.camel@cybertec.at>
References: <CAEPE5bOdhQm41PEjvTeQK2mrUA5xeOnZ-Oe1Kw=AShvfEbAZpw@mail.gmail.com> <c5c42a8563796db5cc126dbff2377e8e7532f525.camel@cybertec.at>
* 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