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 1fHKhT-0007dE-LN for pgsql-general@arkaria.postgresql.org; Sat, 12 May 2018 02:55:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fHKhO-0000Fq-BR for pgsql-general@arkaria.postgresql.org; Sat, 12 May 2018 02:55:54 +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_SHA384:256) (Exim 4.89) (envelope-from ) id 1fHKhO-0000FP-3P for pgsql-general@lists.postgresql.org; Sat, 12 May 2018 02:55:54 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fHKhF-0003Qg-Vv for pgsql-general@postgresql.org; Sat, 12 May 2018 02:55:53 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id A431044586; Sat, 12 May 2018 04:55:44 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id KMv3UmaQN90S; Sat, 12 May 2018 04:55:43 +0200 (CEST) Message-ID: <1526093742.2636.7.camel@cybertec.at> Subject: Re: Best conception of a table From: Laurenz Albe To: hmidi slim , pgsql-general@postgresql.org Date: Sat, 12 May 2018 04:55:42 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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