Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fOgUZ-0007qR-WA for pgsql-general@arkaria.postgresql.org; Fri, 01 Jun 2018 09:37:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fOgUY-0004Xk-1X for pgsql-general@arkaria.postgresql.org; Fri, 01 Jun 2018 09:37:02 +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.89) (envelope-from ) id 1fOgUX-0004Xb-Gy for pgsql-general@lists.postgresql.org; Fri, 01 Jun 2018 09:37:01 +0000 Received: from mail-it0-x241.google.com ([2607:f8b0:4001:c0b::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fOgUS-0007X9-5d for pgsql-general@lists.postgresql.org; Fri, 01 Jun 2018 09:36:59 +0000 Received: by mail-it0-x241.google.com with SMTP id c3-v6so1087020itj.4 for ; Fri, 01 Jun 2018 02:36:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=fyvKBSBPXu4Zgm70ThoGTy+Sj8wKI8p32hbyXGz/a1A=; b=Sv2v12103EL+CGf3KAgC/RSI4vnNfRHVW17E0j+ETVNASYCDi9tO7CzO4LbgZmLDre OiX1jWMv1ersr4XfkiUG/UO34V1Vcks7VZIL90/kpxPP1pIwS9snMHXq5GQpQEjWP2jE KKc9Djv2ZHUdn5R/M7htNU5oMInxJGPv17PX54aKnNy8JNcgXLWBEPzNLkDGgqEHBWDf mo8WsQ3hAumlstoMlVKWc0M8O8EP2uRO7PImJh9D6dk9h1qVgsuuwYl9JRLdSxkSOlIC HviJwQr1YaIBAVscGJLLJ2DsLCsgr+HruEAJBpY3OIA+GQVCAFt70BCy7C+dvvpTy/hC tCaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=fyvKBSBPXu4Zgm70ThoGTy+Sj8wKI8p32hbyXGz/a1A=; b=my+ur2uvxmSb6e0Z36or5pKQ9W5ieIgfpaLwDc+TsFLWLvgnt+xXFdhosgjPJWyQIq Gjr6OHeZ7XA+glGI6cfjhL1ET5hm84e4/CojhThCJ/vrw+bwWuXn3c+IJHxKWczHaVfq 9uKqX08p9HS8TjlvCxldNv0j2Qf/zw85+lEgFTKTd8uRAE6l/828X4NxnTFbF979W80H 7p6GLerR4aioNFFPnL3BrwSdpUf/e92llr5H3+vPtVML1dZW+y3IjfDUf1zOYycSQ4KR RnxVG90CBU2Fh3Pwkp2EzaIO3Hipd5AaFpNG4fYJDeB6QNLx6IMmdXFb+xqlUDGsfT/p 0dPg== X-Gm-Message-State: ALKqPwfJqh36PSvyrrkwuJVOjYVMMXm8WIPQZ0Zmvc1PcgCqdd+zPSj0 MNUikFFwJo5fbxle/dVUaYNKTTSRnv8CO+YR/YmVUV45 X-Google-Smtp-Source: ADUXVKLNxtJQ5haRKl9WsaFTCVxbi/lO2hgGos53Zsxvi0kSTFHzfnKngtgOGcf86e0TjOEeQCqxoy2fH/l3eX43+Mc= X-Received: by 2002:a24:3c42:: with SMTP id m63-v6mr3441900ita.138.1527845815066; Fri, 01 Jun 2018 02:36:55 -0700 (PDT) MIME-Version: 1.0 Sender: nicolas.seinlet@gmail.com Received: by 2002:a4f:6d88:0:0:0:0:0 with HTTP; Fri, 1 Jun 2018 02:36:54 -0700 (PDT) In-Reply-To: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com> References: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com> From: Nicolas Seinlet Date: Fri, 1 Jun 2018 11:36:54 +0200 X-Google-Sender-Auth: cHV-bLorKd6sY7WRF4cRUZPEY4A Message-ID: Subject: Re: Sort is generating rows To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000143178056d91530c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000143178056d91530c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, thanks for the answer. The query is based on a view, so here are the view, the query as well as the query plan. I've already taken into account remarks like date ranges. SELECT min(l.id) AS id, l.product_id, t.uom_id AS product_uom, sum(l.product_uom_qty / u.factor * u2.factor) AS product_uom_qty, sum(l.qty_delivered / u.factor * u2.factor) AS qty_delivered, sum(l.qty_invoiced / u.factor * u2.factor) AS qty_invoiced, sum(l.qty_to_invoice / u.factor * u2.factor) AS qty_to_invoice, sum(l.price_total / COALESCE(cr.rate, 1.0)) AS price_total, sum(l.price_subtotal / COALESCE(cr.rate, 1.0)) AS price_subtotal, sum(l.price_reduce * l.qty_to_invoice / COALESCE(cr.rate, 1.0)) AS amount_to_invoice, sum(l.price_reduce * l.qty_invoiced / COALESCE(cr.rate, 1.0)) AS amount_invoiced, count(*) AS nbr, s.name, s.date_order AS date, s.confirmation_date, s.state, s.partner_id, s.user_id, s.company_id, date_part('epoch'::text, avg(date_trunc('day'::text, s.date_order) - date_trunc('day'::text, s.create_date))) / (24 * 60 * 60)::numeric(16,2)::double precision AS delay, t.categ_id, s.pricelist_id, s.analytic_account_id, s.team_id, p.product_tmpl_id, partner.country_id, partner.commercial_partner_id, sum(p.weight * l.product_uom_qty / u.factor * u2.factor) AS weight, sum(p.volume * l.product_uom_qty::double precision / u.factor::double precision * u2.factor::double precision) AS volume FROM sale_order_line l JOIN sale_order s ON l.order_id =3D s.id JOIN res_partner partner ON s.partner_id =3D partner.id LEFT JOIN product_product p ON l.product_id =3D p.id LEFT JOIN product_template t ON p.product_tmpl_id =3D t.id LEFT JOIN uom_uom u ON u.id =3D l.product_uom LEFT JOIN uom_uom u2 ON u2.id =3D t.uom_id JOIN product_pricelist pp ON s.pricelist_id =3D pp.id LEFT JOIN LATERAL ( SELECT res_currency_rate.rate FROM res_currency_rate WHERE res_currency_rate.currency_id =3D pp.currency_id AND (res_currency_rate.company_id =3D s.company_id OR res_currency_rate.company_id IS NULL) AND daterange(res_currency_rate.name, COALESCE(res_currency_rate.date_end, now()::date)) @> COALESCE(s.date_order::timestamp with time zone, now())::date LIMIT 1) cr ON true GROUP BY l.product_id, l.order_id, t.uom_id, t.categ_id, s.name, s.date_order, s.confirmation_date, s.partner_id, s.user_id, s.state, s.company_id, s.pricelist_id, s.analytic_account_id, s.team_id, p.product_tmpl_id, partner.country_id, partner.commercial_partner_id; explain analyse select team_id,partner_id,sum(price_total) from sale_report group by team_id,partner_id; QUERY PLAN ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------- GroupAggregate (cost=3D1344575.91..1344986.97 rows=3D3654 width=3D40) (ac= tual time=3D8934.915..8944.487 rows=3D43 loops=3D1) Group Key: sale_report.team_id, sale_report.partner_id -> Sort (cost=3D1344575.91..1344667.26 rows=3D36539 width=3D40) (actua= l time=3D8934.686..8937.833 rows=3D32732 loops=3D1) Sort Key: sale_report.team_id, sale_report.partner_id Sort Method: quicksort Memory: 3323kB -> Subquery Scan on sale_report (cost=3D1339157.70..1341806.77 rows=3D36539 width=3D40) (actual time=3D8870.269..8923.114 rows=3D32732 loo= ps=3D1) -> GroupAggregate (cost=3D1339157.70..1341441.38 rows=3D36= 539 width=3D395) (actual time=3D8870.268..8920.155 rows=3D32732 loops=3D1) Group Key: l.product_id, l.order_id, t.uom_id, t.categ_id, s.name, s.date_order, s.confirmation_date, s.partner_id, s.user_id, s.state, s.company_id, s.pricelist_id, s.analytic_account_id, s.team_id, p.product_tmpl_id, partner.country_id, partner.commercial_partner_id -> Sort (cost=3D1339157.70..1339249.04 rows=3D36539 width=3D92) (actual time=3D8870.247..8875.191 rows=3D32732 loops=3D1) Sort Key: l.product_id, l.order_id, t.uom_id, t.categ_id, s.name, s.date_order, s.confirmation_date, s.partner_id, s.user_id, s.state, s.company_id, s.pricelist_id, s.analytic_account_id, s.team_id, p.product_tmpl_id, partner.country_id, partner.commercial_partner_id Sort Method: quicksort Memory: 5371kB -> Nested Loop Left Join (cost=3D695.71..1336388.56 rows=3D36539 width=3D92) (actual time=3D13.468..= 8797.655 rows=3D32732 loops=3D1) -> Hash Left Join (cost=3D695.43..3338.1= 9 rows=3D36539 width=3D88) (actual time=3D13.323..65.600 rows=3D32732 loops= =3D1) Hash Cond: (l.product_id =3D p.id) -> Hash Join (cost=3D656.36..2796.= 71 rows=3D36539 width=3D76) (actual time=3D13.236..49.047 rows=3D32732 loops= =3D1) Hash Cond: (l.order_id =3D s.i= d) -> Seq Scan on sale_order_line l (cost=3D0.00..1673.39 rows=3D36539 width=3D17) (actual time=3D0.019..7.338 rows=3D32732 loops=3D1) -> Hash (cost=3D550.72..550.= 72 rows=3D8451 width=3D67) (actual time=3D13.184..13.184 rows=3D8382 loops=3D1= ) Buckets: 16384 Batches: 1 Memory Usage: 947kB -> Hash Join (cost=3D37.69..550.72 rows=3D8451 width=3D67) (actual time=3D0.164..10.135 rows=3D8382 loops=3D1) Hash Cond: (s.pricelist_id =3D pp.id) -> Hash Join (cost=3D13.97..420.42 rows=3D8451 width=3D63) (actual time=3D0.151..7.064 r= ows=3D8382 loops=3D1) Hash Cond: (s.partner_id =3D partner.id) -> Seq Scan on sale_order s (cost=3D0.00..301.51 rows=3D8451 width=3D55) (actual time=3D0.005..1.807 rows=3D8382 loops=3D1) -> Hash (cost=3D13.43..13.43 rows=3D43 width=3D12) (actual time=3D0.136..0.136 rows= =3D43 loops=3D1) Buckets: 1024 Batches: 1 Memory Usage: 10kB -> Seq Scan on res_partner partner (cost=3D0.00..13.43 rows=3D43 width=3D12) (actual time=3D0.013..0.112 rows=3D43 loops=3D1) -> Hash (cost=3D16.10..16.10 rows=3D610 width=3D8) (actual time=3D0.007..0.007 rows= =3D1 loops=3D1) Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on product_pricelist pp (cost=3D0.00..16.10 rows=3D610 width=3D8) (actual time=3D0.005..0.005 rows=3D1 loops=3D1) -> Hash (cost=3D32.95..32.95 rows=3D490 width=3D16) (actual time=3D0.076..0.076 rows=3D43 loops=3D1) Buckets: 1024 Batches: 1 Memory Usage: 11kB -> Hash Left Join (cost=3D11.88..32.95 rows=3D490 width=3D16) (actual time=3D0.051..0.068 row= s=3D43 loops=3D1) Hash Cond: (p.product_tmpl_id =3D t.id) -> Seq Scan on product_product p (cost=3D0.00..14.90 rows=3D490 width=3D8) (actual time=3D0.007..0.010 rows=3D43 loops=3D1) -> Hash (cost=3D11.39..11.39 rows=3D39 width=3D12) (actual time=3D0.039..0.039 rows= =3D39 loops=3D1) Buckets: 1024 Batches: 1 Memory Usage: 10kB -> Seq Scan on product_template t (cost=3D0.00..11.39 rows=3D39 width=3D12) (actual time=3D0.006..0.030 rows=3D39 loops=3D1) -> Limit (cost=3D0.28..36.46 rows=3D1 width=3D8) (actual time=3D0.266..0.266 rows=3D1 loops=3D32732) -> Index Scan using res_currency_rate_currency_id_index on res_currency_rate (cost=3D0.28..36.= 46 rows=3D1 width=3D8) (actual time=3D0.266..0.266 rows=3D1 loops=3D32732) Index Cond: (currency_id =3D pp.currency_id) Filter: (((company_id =3D s.company_id) OR (company_id IS NULL)) AND (daterange(name, COALESCE(date_end, (now())::date)) @> (COALESCE((s.date_order)::timestamp with time zone, now()))::date)) Rows Removed by Filter: 502 Planning time: 5.731 ms Execution time: 8944.950 ms (45 rows) Have a nice day, Nicolas. 2018-06-01 0:32 GMT+02:00 Adrian Klaver : > On 05/31/2018 02:09 AM, Nicolas Seinlet wrote: > >> Hi, >> >> I have a query with a strange query plan. >> >> This query is roughly searching for sales, and convert them with a >> currency rate. As currency rate changes from time to time, table contain= s >> the currency, the company, the rate, the start date of availability of t= his >> rate and the end date of availability. >> > > My guess is to get a complete answer you are going to need to provide: > > 1) The complete query. > > 2) The complete EXPLAIN ANALYZE. > > More comments inline below. > > >> The join is done using : >> left join currency_rate cr on (cr.currency_id =3D pp.currency_id an= d >> cr.company_id =3D s.company_id and >> cr.date_start <=3D coalesce(s.date_order, now()) and >> (cr.date_end is null or cr.date_end > coalesce(s.date_order, >> now()))) >> >> The tricky part is the date range on the currency rate, which is not an >> equality. >> > > >> My question is then , is there a better way to join a table to another >> using a date range, knowing that there's no overlap between date ranges? >> > > Use date ranges?: > > https://www.postgresql.org/docs/10/static/functions-range.html > > > > Should we generate a virtual table with rates for all dates, and joining >> using an equality? >> >> For now, the more currency rates, the slowest the query. There's not tha= t >> much currency rates (1k in this case), as you can only have one rate per >> day per currency. >> >> Have a nice day, >> >> Nicolas. >> > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > --000000000000143178056d91530c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

thanks for the answer. The query is= based on a view, so here are the view, the query as well as the query plan= .
I've already taken into account remarks like date ranges.

=C2=A0SELECT min(l.id<= /a>) AS id,
=C2=A0= =C2=A0 sum(l.qty_to_invoice / u.factor * u2.factor) AS qty_to_invoice,
=C2=A0 =C2=A0 sum(l.price_total / COALESCE(cr.rate, 1.0)) AS price_t= otal,
=C2=A0 =C2=A0 sum(l.price_subtotal / COALESCE(cr.rate, 1.0)= ) AS price_subtotal,
=C2=A0 =C2=A0 sum(l.price_reduce * l.qty_to_= invoice / COALESCE(cr.rate, 1.0)) AS amount_to_invoice,
=C2=A0 = =C2=A0 sum(l.price_reduce * l.qty_invoiced / COALESCE(cr.rate, 1.0)) AS amo= unt_invoiced,
=C2=A0 =C2=A0 count(*) AS nbr,
=C2=A0 =C2=A0 s.date_or= der AS date,
=C2=A0 =C2=A0 s.confirmation_date,
=C2=A0 = =C2=A0 s.state,
=C2=A0 =C2=A0 s.partner_id,
=C2=A0 =C2= =A0 s.user_id,
=C2=A0 =C2=A0 s.company_id,
=C2=A0 =C2= =A0 date_part('epoch'::text, avg(date_trunc('day'::text, s.= date_order) - date_trunc('day'::text, s.create_date))) / (24 * 60 *= 60)::numeric(16,2)::double precision AS delay,
=C2=A0 =C2=A0 t.c= ateg_id,
=C2=A0 =C2=A0 s.pricelist_id,
=C2=A0 =C2=A0 s.= analytic_account_id,
=C2=A0 =C2=A0 s.team_id,
=C2=A0 = =C2=A0 p.product_tmpl_id,
=C2=A0 =C2=A0 partner.country_id,
=
=C2=A0 =C2=A0 partner.commercial_partner_id,
=C2=A0 =C2=A0 s= um(p.weight * l.product_uom_qty / u.factor * u2.factor) AS weight,
=C2=A0 =C2=A0 sum(p.volume * l.product_uom_qty::double precision / u.fact= or::double precision * u2.factor::double precision) AS volume
=C2= =A0 =C2=A0FROM sale_order_line l
=C2=A0 =C2=A0 =C2=A0JOIN sale_or= der s ON l.order_id =3D s.id
=C2=A0 = =C2=A0 =C2=A0JOIN res_partner partner ON s.partner_id =3D partner.id
=C2=A0 =C2=A0 =C2=A0LEFT JOIN product= _product p ON l.product_id =3D p.id
= =C2=A0 =C2=A0 =C2=A0LEFT JOIN product_template t ON p.product_tmpl_id =3D <= a href=3D"http://t.id">t.id
=C2=A0 =C2=A0 =C2=A0LEFT JOIN uom= _uom u ON u.id =3D l.product_uom
=C2= =A0 =C2=A0 =C2=A0LEFT JOIN uom_uom u2 ON u2.id= =3D t.uom_id
=C2=A0 =C2=A0 =C2=A0JOIN product_pricelist pp ON s.= pricelist_id =3D pp.id
=C2=A0 =C2=A0= =C2=A0LEFT JOIN LATERAL ( SELECT res_currency_rate.rate
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0FROM res_currency_rate
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 WHERE res_currency_rate.currency_id =3D pp.curr= ency_id AND (res_currency_rate.company_id =3D s.company_id OR res_currency_= rate.company_id IS NULL) AND daterange(res_currency_rate.name, COALESCE(res_currency_rate.date_end, now(= )::date)) @> COALESCE(s.date_order::timestamp with time zone, now())::da= te
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0LIMIT 1) cr ON true
=C2=A0 GROUP BY l.product_id, l.order_id, t.uom_id, t.categ_id, s.name, s.date_order, s.confirmation_date, s.partner= _id, s.user_id, s.state, s.company_id, s.pricelist_id, s.analytic_account_i= d, s.team_id, p.product_tmpl_id, partner.country_id, partner.commercial_par= tner_id;

explain analyse select team_id,part= ner_id,sum(price_total) from sale_report group by team_id,partner_id;

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 QUERY PLAN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0
------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ----------------
=C2=A0GroupAggregate=C2=A0 (cost=3D1344575.91..1= 344986.97 rows=3D3654 width=3D40) (actual time=3D8934.915..8944.487 rows=3D= 43 loops=3D1)
=C2=A0 =C2=A0Group Key: sale_report.team_id, sale_r= eport.partner_id
=C2=A0 =C2=A0->=C2=A0 Sort=C2=A0 (cost=3D1344= 575.91..1344667.26 rows=3D36539 width=3D40) (actual time=3D8934.686..8937.8= 33 rows=3D32732 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Sort= Key: sale_report.team_id, sale_report.partner_id
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0Sort Method: quicksort=C2=A0 Memory: 3323kB
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Subquery Scan on sale_report= =C2=A0 (cost=3D1339157.70..1341806.77 rows=3D36539 width=3D40) (actual time= =3D8870.269..8923.114 rows=3D32732 loops=3D1)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 GroupAggregate=C2=A0 (cos= t=3D1339157.70..1341441.38 rows=3D36539 width=3D395) (actual time=3D8870.26= 8..8920.155 rows=3D32732 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Group Key: l.product_id, l.= order_id, t.uom_id, t.categ_id, s.name, s.dat= e_order, s.confirmation_date, s.partner_id, s.user_id, s.state, s.company_i= d, s.pricelist_id, s.analytic_account_id, s.team_id, p.product_tmpl_id, par= tner.country_id, partner.commercial_partner_id
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Sort= =C2=A0 (cost=3D1339157.70..1339249.04 rows=3D36539 width=3D92) (actual time= =3D8870.247..8875.191 rows=3D32732 loops=3D1)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Sort Key: l.product_id, l.order_id, t.uom_id, t.categ_id, s.name, s.date_order, s.confirmation_date, s.partner_id,= s.user_id, s.state, s.company_id, s.pricelist_id, s.analytic_account_id, s= .team_id, p.product_tmpl_id, partner.country_id, partner.commercial_partner= _id
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Sort Method: quicksort=C2=A0 Memory: = 5371kB
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Nested Loop Left Join= =C2=A0 (cost=3D695.71..1336388.56 rows=3D36539 width=3D92) (actual time=3D1= 3.468..8797.655 rows=3D32732 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0->=C2=A0 Hash Left Join=C2=A0 (cost=3D695.43..3338.1= 9 rows=3D36539 width=3D88) (actual time=3D13.323..65.600 rows=3D32732 loops= =3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Hash Cond: (l.product_id =3D p.id)
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->= =C2=A0 Hash Join=C2=A0 (cost=3D656.36..2796.71 rows=3D36539 width=3D76) (ac= tual time=3D13.236..49.047 rows=3D32732 loops=3D1)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ha= sh Cond: (l.order_id =3D s.id)
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0->=C2=A0 Seq Scan on sale_order_line l=C2=A0 (cost=3D0.00..1673.39= rows=3D36539 width=3D17) (actual time=3D0.019..7.338 rows=3D32732 loops=3D= 1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Hash=C2=A0 (cost=3D550.72..550.72 rows= =3D8451 width=3D67) (actual time=3D13.184..13.184 rows=3D8382 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Buckets: 16384=C2=A0 Batches: 1=C2= =A0 Memory Usage: 947kB
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2= =A0 Hash Join=C2=A0 (cost=3D37.69..550.72 rows=3D8451 width=3D67) (actual t= ime=3D0.164..10.135 rows=3D8382 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Hash Cond: (s.pricelist_id =3D pp.id)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0->=C2=A0 Hash Join=C2=A0 (cost=3D13.97..420.42 rows=3D8= 451 width=3D63) (actual time=3D0.151..7.064 rows=3D8382 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0Hash Cond: (s.partner_id =3D partner.id)
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Seq Scan on res_partner partner=C2= =A0 (cost=3D0.00..13.43 rows=3D43 width=3D12) (actual time=3D0.013..0.112 r= ows=3D43 loops=3D1)
=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0->=C2=A0 Seq Scan on product_product p=C2=A0 (cost= =3D0.00..14.90 rows=3D490 width=3D8) (actual time=3D0.007..0.010 rows=3D43 = loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Hash=C2= =A0 (cost=3D11.39..11.39 rows=3D39 width=3D12) (actual time=3D0.039..0.039 = rows=3D39 loops=3D1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0Buckets: 1024=C2=A0 Batches: 1=C2=A0 Memory Usage: 10kB
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Seq = Scan on product_template t=C2=A0 (cost=3D0.00..11.39 rows=3D39 width=3D12) = (actual time=3D0.006..0.030 rows=3D39 loops=3D1)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Limit=C2=A0 (cost=3D0.28..36.46 = rows=3D1 width=3D8) (actual time=3D0.266..0.266 rows=3D1 loops=3D32732)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-&= gt;=C2=A0 Index Scan using res_currency_rate_currency_id_index on res_curre= ncy_rate=C2=A0 (cost=3D0.28..36.46 rows=3D1 width=3D8) (actual time=3D0.266= ..0.266 rows=3D1 loops=3D32732)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Index Cond: (currenc= y_id =3D pp.currency_id)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Filter: (((company_id =3D s= .company_id) OR (company_id IS NULL)) AND (daterange(name, COALESCE(date_en= d, (now())::date)) @> (COALESCE((s.date_order)::timestamp with time zone= , now()))::date))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Rows Removed by Filter: 502
<= div>=C2=A0Planning time: 5.731 ms
=C2=A0Execution time: 8944.950 = ms
(45 rows)


Have a= nice day,

Nicolas.

2018-06-01 0:32 GMT+02:00 Adrian Kla= ver <adrian.klaver@aklaver.com>:
On 05/31/2018 02:09 AM, Nicolas Seinlet wro= te:
Hi,

I have a query with a strange query plan.

This query is roughly searching for sales, and convert them with a currency= rate. As currency rate changes from time to time, table contains the curre= ncy, the company, the rate, the start date of availability of this rate and= the end date of availability.

My guess is to get a complete answer you are going to need to provide:

1) The complete query.

2) The complete EXPLAIN ANALYZE.

More comments inline below.


The join is done using :
=C2=A0=C2=A0 =C2=A0 left join currency_rate cr on (cr.currency_id =3D pp.cu= rrency_id and
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 cr.company_id =3D s.company_id and=
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 cr.date_start <=3D coalesce(s.d= ate_order, now()) and
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(cr.date_end is null or cr.date_end= > coalesce(s.date_order, now())))

The tricky part is the date range on the currency rate, which is not an equ= ality.


My question is then , is there a better way to join a table to another usin= g a date range, knowing that there's no overlap between date ranges?

Use date ranges?:

https://www.postgresql.org/docs/1= 0/static/functions-range.html



Should we generate a virtual table with rates for all dates, and joining us= ing an equality?

For now, the more currency rates, the slowest the query. There's not th= at much currency rates (1k in this case), as you can only have one rate per= day per currency.

Have a nice day,

Nicolas.


--
Adrian Klaver
adrian.klave= r@aklaver.com

--000000000000143178056d91530c--