Re: Best conception of a table

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: hmidi slim <hmidi(dot)slim2(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Best conception of a table
Date: 2018-05-12 02:55:42
Message-ID: 1526093742.2636.7.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2018-05-12 11:04:06 Re: Domain based on TIMEZONE WITH TIME ZONE
Previous Message hmidi slim 2018-05-11 22:54:40 Best conception of a table