From: Laurenz Albe <laurenz.albe@cybertec.at>
To: hmidi slim <hmidi.slim2@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Best conception of a table
Date: Sat, 12 May 2018 04:55:42 +0200
Message-ID: <1526093742.2636.7.camel@cybertec.at> (raw)
In-Reply-To: <CAMsqVxvTLNrhiNrgLm0XKv9VU6m22ox8umkzX4OCRgK-5LVCbg@mail.gmail.com>
References: <CAMsqVxvTLNrhiNrgLm0XKv9VU6m22ox8umkzX4OCRgK-5LVCbg@mail.gmail.com>
hmidi slim wrote:
> To clarify the purpose of the table 'product_price_period': If I have a product
> and I choose period like [2018-05-01, 2018-05-31] and in the days_checked = [0,2,3].
> The values of days_checked are the indexes of the week's day.
> In this case 0 => sunday, 2 => tuesday, 3 => wednesday.
> So the product is not product for every sunday and tuesday and wednesday in the given period.
> The problem with this design is when I make a select to fetch all the product
> available for a given period, I have to generate all the dates of a given period
> and then eliminate the dates corresponding to days_checked and after that return the products.
>
> E.g:
> If I want to fetch all the products in a period of [2018-05-01, 2018-05-08]
>
> And considering that I have a list of products :
> 1) product_id_1 [2018-04-01, 2018-05-05] [0,2]
> 2) product_id_2 [2018-05-01, 2018-05-01] [2]
> 3) product_id_3 [2018-04-01, 2018-05-17] []
>
> The result wil be product_id_3.
This should be possible without using a temporary table.
First filter out the products whose daterange does not contain your interval,
then remove all products where generate_series(DATE '2018-05-01', DATE '2018-05-08')
contains one of the forbidden week days.
Maybe you should choose a simpler data model, like storing all
allowed days for a product in an array (you can use a GIN index to
speed up the <@ operator).
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, hmidi.slim2@gmail.com
Subject: Re: Best conception of a table
In-Reply-To: <1526093742.2636.7.camel@cybertec.at>
References: <CAMsqVxvTLNrhiNrgLm0XKv9VU6m22ox8umkzX4OCRgK-5LVCbg@mail.gmail.com> <1526093742.2636.7.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