pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Nicolas Seinlet <nicolas@seinlet.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Sort is generating rows
Date: Thu, 31 May 2018 15:32:51 -0700
Message-ID: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com> (raw)
In-Reply-To: <CAMEUSaRx0UZ4Z0JGuaxbz3UtfMG9_q+v6pV0t41XzGOuVY307Q@mail.gmail.com>
References: <CAMEUSaRx0UZ4Z0JGuaxbz3UtfMG9_q+v6pV0t41XzGOuVY307Q@mail.gmail.com>

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 
> contains the currency, 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 :
>      left join currency_rate cr on (cr.currency_id = pp.currency_id and
>            cr.company_id = s.company_id and
>            cr.date_start <= 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 
> that 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




view thread (9+ messages)  latest in thread

Message-ID: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com>
Permalink:  ../64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com/
Also on:    postgresql.org/message-id/64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com

 · 

reply

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: adrian.klaver@aklaver.com, nicolas@seinlet.com
  Subject: Re: Sort is generating rows
  In-Reply-To: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com>
  References: <CAMEUSaRx0UZ4Z0JGuaxbz3UtfMG9_q+v6pV0t41XzGOuVY307Q@mail.gmail.com> <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com>

* 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