Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gunXg-0003mc-Uy for pgsql-general@arkaria.postgresql.org; Sat, 16 Feb 2019 00:09:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gunXf-000533-8w for pgsql-general@arkaria.postgresql.org; Sat, 16 Feb 2019 00:09:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gunXd-00052n-LK for pgsql-general@lists.postgresql.org; Sat, 16 Feb 2019 00:09:15 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gunXU-0001sG-Bs for pgsql-general@lists.postgresql.org; Sat, 16 Feb 2019 00:09:12 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 5820221E8D; Fri, 15 Feb 2019 19:09:02 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 15 Feb 2019 19:09:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=C mbwtw0NnPEuPgTM1CJ3Itt39VebhAYVHX/mPLeYMfk=; b=J2pPQVxUCEoRoZ7m8 hzrWpsvJgc60GwEMzfDFP8ynNLec7oDud4ZcfHQwYTQxlZBkvB0ecgaXt9uJZfbi VLqc+UiDWajhiGsDNAgADoFK57qoyOgCgPGn8CykgYhKTucvJKFja+zA5p6caEdP BJ2DJpAe1PAsO2fapXgOVd2W6LslQns0/e0E82eep9J2ZJFL/pp3jeY4Fl6WvUYf 99a+Cixo7/+rTXpfpk+zND8cXf989+sZN/x5bFZDKyP9ts6usE2sx8AivkYfo6J7 mg0pSRezfJT0jaVxaZDxEevf1yseM28nM6THYDEYlBYgBqRx8FJAhNFiXrpbUvlu im+eA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=Cmbwtw0NnPEuPgTM1CJ3Itt39VebhAYVHX/mPLeYM fk=; b=HyXCmkhn2gG2GF5mr7Nyp8t7GHGQ7B3N8t61qkCMmQWKwS5s1AKzeF/vV 8lELqmrubMGAvffigiJhe6MmjCCo2o4dHvvjzYWDFyyybRJZjC9hPPs/hPCt8YvL NgskB8RULPMzk+z+5san/2Ej9vBE3VlvBwSYnZ5h0zYLXeQklIVMONYsseD/oNMP trbN7yHb9v5YhqK4oJeNecpG4L8ZqX9DobVonEAhIXtoE9mieGTqCnovJNPDuQPV LurKttNL3lSYu1rBzIntQ0XBJdRtYebopFjD11Ik7WeqrDHS8zVXGQRTlz8TgulT 5VZph0jJbfb9rIDoar4CsF8QzQ6UA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledruddtkedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvd duvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id D9BA31026E; Fri, 15 Feb 2019 19:09:00 -0500 (EST) Subject: Re: Problems pushing down WHERE-clause to underlying view To: =?UTF-8?Q?Nicklas_Av=c3=a9n?= , pgsql-general@lists.postgresql.org References: <4d8a05b6-39de-9a06-7e86-85754fd94716@jordogskog.no> <9cd9e5c1-1909-6f36-1473-f3d2a773762e@aklaver.com> <9770a1ec-c7be-7527-66d7-0c95b8277508@jordogskog.no> <473540ab-5933-04e8-ce55-074f4faaab82@aklaver.com> <232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no> From: Adrian Klaver Message-ID: <9f19bc9e-eec2-04db-67c1-0d449fd5f80c@aklaver.com> Date: Fri, 15 Feb 2019 16:08:59 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: <232283a6-4422-1efd-ec80-3dda8b2f5496@jordogskog.no> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/15/19 12:43 PM, Nicklas Avén wrote: > > > I have not had chance to fully go through all of below. Some > questions/suggestions: > > > > 1) Thanks for the formatted queries. If I could make a suggestion, > when aliasing could you include AS. It would make finding what l.* > refers to easier for those of us with old eyes:) > > > Yes, of course, sorry :-) > > > > 2) t4e_contractor_id is in the shiny_adm.contractor_access table? > > If not where? > > Yes, sorry again, it is there > > > > > > 3) What is the schema for shiny_adm.contractor_access? > > In particular what indexes are on it? > > > shiny_adm.contractor_access looks like this: > > > CREATE TABLE shiny_adm.contractor_access > ( >   machine_key text, >   t4e_contractor_id text, >   active integer DEFAULT 1, >   id serial NOT NULL, >   CONSTRAINT contractor_access_pkey PRIMARY KEY (id), >   CONSTRAINT contractor_unique UNIQUE (machine_key, t4e_contractor_id), >   CONSTRAINT co_check_t4e_co_email CHECK > (utils.verify_email(t4e_contractor_id)) > ) > > > CREATE INDEX idx_contractor >   ON shiny_adm.contractor_access >   USING btree >   (t4e_contractor_id COLLATE pg_catalog."default"); > > CREATE INDEX idx_contractor_mk >   ON shiny_adm.contractor_access >   USING btree >   (machine_key COLLATE pg_catalog."default"); > > > I tried to format the below a little better with AS and some more > consistent indents. > > > I also, in the first query, changed the where clause to filter on > machine_key in table contractor _access. Just to illustrate the problem > better. > > Both queries filter on the same table which is joined the same way. But > in the second example the where clause is not pushed to the subquery l > > > Thanks a lot for looking into it > > > Nicklas > > > Next query, the slow one that calculates the whole dataset: > > > > EXPLAIN ANALYZE > SELECT >     l.machine_key, >     o.object_name, >     o.sub_object_name, >     o.object_user_id, >     o.sub_object_user_id, >     o.start_date AS object_start_date, >     s.species_group_name, >     p.product_group_name, >     l.m3_sub AS volume_m3sub, >     l.number_of_logs, >     mi.basemachine_manufacturer, >     mi.basemachine_model > FROM  shiny_adm.contractor_access ci join >     ( >         SELECT >             hl.contractor_id, >             hl.machine_key, >             hl.operator_key, >             hl.object_key, >             hl.sub_object_key, >             date(hl.harvest_date) AS harvest_date, >             hl.species_group_key, >             hl.product_key, >             sum(hl.m3_sub) AS m3_sub, >             count(*) AS number_of_logs >         FROM >             version_union_tables_r02.harvester_logs AS hl >         GROUP BY >             hl.machine_key, hl.contractor_id, hl.operator_key, > hl.object_key, hl.sub_object_key, (date(hl.harvest_date)), > hl.species_group_key, hl.product_key >     )  AS l on l.machine_key=ci.machine_key >     LEFT JOIN version_union_tables_r02.machine_info  AS mi ON > l.machine_key::text = mi.machine_key::text >     LEFT JOIN version_union_tables_r02.objects AS o ON > l.machine_key::text = o.machine_key::text AND l.object_key = > o.object_key AND l.sub_object_key = o.sub_object_key >     LEFT JOIN version_union_tables_r02.products  AS p ON > l.machine_key::text = p.machine_key::text AND l.product_key = p.product_key >     LEFT JOIN version_union_tables_r02.species  AS s ON > l.machine_key::text = s.machine_key::text AND l.species_group_key = > s.species_group_key > WHERE t4e_contractor_id = 'nicklas.aven@jordogskog.no'; To make it apples to apples try changing above to be more like first query: ... AS l LEFT JOIN version_union_tables_r02.machine_info AS mi ON l.machine_key::text = mi.machine_key::text LEFT JOIN version_union_tables_r02.objects AS o ON l.machine_key::text = o.machine_key::text AND l.object_key = o.object_key AND l.sub_object_key = o.sub_object_key LEFT JOIN version_union_tables_r02.products AS p ON l.machine_key::text = p.machine_key::text AND l.product_key = p.product_key LEFT JOIN version_union_tables_r02.species AS s ON l.machine_key::text = s.machine_key::text AND l.species_group_key = s.species_group_key JOIN shiny_adm.contractor_access AS ci ON l.machine_key=ci.machine_key WHERE t4e_contractor_id = 'nicklas.aven@jordogskog.no' ; > > results in this query plan: -- Adrian Klaver adrian.klaver@aklaver.com