Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dikSJ-0002Im-Qg for pgsql-performance@arkaria.postgresql.org; Fri, 18 Aug 2017 16:49:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dikSI-0007dJ-Ni for pgsql-performance@arkaria.postgresql.org; Fri, 18 Aug 2017 16:49:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dikQX-0004Zo-C7 for pgsql-performance@postgresql.org; Fri, 18 Aug 2017 16:47:17 +0000 Received: from mail-ua0-x241.google.com ([2607:f8b0:400c:c08::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dikQP-0002p5-H1 for pgsql-performance@postgresql.org; Fri, 18 Aug 2017 16:47:16 +0000 Received: by mail-ua0-x241.google.com with SMTP id q13so4681907uaf.3 for ; Fri, 18 Aug 2017 09:47:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=RAF7E/qDyjpq0mczxQ8QYx9pEKhNPx634hA3Ao2Bp58=; b=omud8vj4Pulf00iIS4T/Q7ruY+CljK4QYvwPBgllmnEdlu9L3gEroXtdTD6LYP8VfM eDTqBiixU20inkFdhQV8yFJopxmr7M9aIAk8vLFDoPpfZCznUBwSZbZU1IqwWVsTD5Ro 7iX2ZV14DvDKiEa/HnjEhw9Yda4wj8Unriigt2tG6Egv8uZCPhnliPpJHAbltyldgrih ex7hx2fmPZigJsUKU8cqz+BCtPo0DPiHPox2+zVwNBYQedsHIzOfJKGUyeVgp2FJvwdD +CQ8yy73F2Z54HIz2sRbkNShnwxlO6f+P2lTDe7hOuHBoD/QLMpujR2VJ3hztl4fOZDj zdnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=RAF7E/qDyjpq0mczxQ8QYx9pEKhNPx634hA3Ao2Bp58=; b=THQnEhGQc6AOhUxBQGMG7FtK5Oat0VRxeR7AIqGfPD574O13u6ga+QeXyZfXfbJlQq 7+hxmLrdfZYPSkYPa1OT7QGRF+pbsdP3l+Aq2iqO7n915c+LQeHC5oQN4MRgt3ShPr9P /iR98c572mLkyw5FaU+l8W/XD0I/tblCucGIHOZMN+SMx48PG86nVjasa4jiqtdF0TX3 YKOTyeT+g/cFZJkr7xCvVWGN82pEohXj5E5H0yTorlA/YPkXOSvY6Phtx2xtLYuP1/l+ QWuHT0m3H2PHM5PYSY7ECi2Rj90pf09SpLlfFBP9HrfWXUjcYR7NQpyev62c4WyiYm3L M4QQ== X-Gm-Message-State: AHYfb5j1LUAgr1iAKv80/mJ2/Leke9UagKNyLJO9Fjoltc5yeeK96zT9 YcNuvRlfZ7UA+gNYytUyH3Is788E7xbb X-Received: by 10.176.21.175 with SMTP id i44mr6601930uae.54.1503074827892; Fri, 18 Aug 2017 09:47:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.137.85 with HTTP; Fri, 18 Aug 2017 09:46:47 -0700 (PDT) From: kimaidou Date: Fri, 18 Aug 2017 18:46:47 +0200 Message-ID: Subject: Query plan for views and WHERE clauses, Luke is not using the index To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary="001a1145343c2ffb5d055709e102" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org --001a1145343c2ffb5d055709e102 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all, I have come across a unexpected behavior. You can see full detail on an issue on the QGEP project in Github : https://github.com/QGEP/QGEP/issues/308#issuecomment-323122514 Basically, we have this view with some LEFT JOIN : http://paste.debian.net/982003/ We have indexes on some fields ( foreign keys, and a GIST index for the PostGIS geometry field) If I use the raw SQL defining the view, and add a WHERE clause like: WHERE "progression_geometry" && st_makeenvelope(1728327.03249295568093657,8240789.26074041239917278,1728608= .10987572139129043,8240958.16933418624103069,3949) the query plan is "as expected", as it is using the spatial index (and others too). This query gets 100 lines from a "main" table containing 20000 lines (and child tables having more). It is pretty fast and "low cost" See the query plan: https://explain.depesz.com/s/6Qgb When we call the WHERE on the view: EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS) SELECT * FROM "qgep"."vw_qgep_reach" WHERE "progression_geometry" && st_makeenvelope(1728327.03249295568093657,8240789.26074041239917278,1728608= .10987572139129043,8240958.16933418624103069,3949) The query plan is "wrong", as PostgreSQL seems to consider it should do a seq scan on the tables, and only afterwards filter with the WHERE: https://explain.depesz.com/s/wXV The query takes about 1 second instead of less than 100ms. Do you have any hint on this kind of issue ? Thanks in advance Regards, Micha=C3=ABl --001a1145343c2ffb5d055709e102 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi all,

I = have come across a unexpected behavior.
You can see full detail on= an issue on the QGEP project in Github :
Basically, we= have this view with some LEFT JOIN :
http://paste.debian.net/982003/

We have indexes= on some fields ( foreign keys, and a GIST index for the PostGIS geometry f= ield)
If I use the raw SQL defining the view, and add a WHERE clau= se like:

WHERE "progression_geometry" && st_makeen= velope(1728327.03249295568093657,8240789.26074041239917278,1728608.10987572= 139129043,8240958.16933418624103069,3949)

the query plan is &q= uot;as expected", as it is using the spatial index (and others too). T= his query gets 100 lines from a "main" table containing 20000 lin= es (and child tables having more). It is pretty fast and "low cost&quo= t;
See the query plan:

<= /div>When we call the WHERE on the view:

EXPLAIN (ANALYZE, COSTS, VE= RBOSE, BUFFERS)
SELECT *
FROM "qgep"."vw_qgep_reach&qu= ot;
WHERE "progression_geometry" && st_makeenvelope(1= 728327.03249295568093657,8240789.26074041239917278,1728608.1098757213912904= 3,8240958.16933418624103069,3949)


The query plan is "wrong", as PostgreSQL seems to consider it s= hould do a seq scan on the tables, and only afterwards filter with the WHER= E:
The query=
 takes about 1 second instead of less than 100ms.

=
Do you have any hint on this kind of issue ?
=
Thanks in advance

<= /span>
Regards,
<=
span class=3D"gmail-pl-k">Micha=C3=ABl
--001a1145343c2ffb5d055709e102--