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 1fHUj9-0002cD-1H for pgsql-general@arkaria.postgresql.org; Sat, 12 May 2018 13:38:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fHUj3-0002GH-Qh for pgsql-general@arkaria.postgresql.org; Sat, 12 May 2018 13:38:17 +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 1fHUj1-0002G7-SS for pgsql-general@lists.postgresql.org; Sat, 12 May 2018 13:38:17 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fHUis-0001ut-Eb for pgsql-general@postgresql.org; Sat, 12 May 2018 13:38:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id D0FDB24AD0; Sat, 12 May 2018 09:38:03 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 12 May 2018 09:38:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=Sh/zh5QmGkaAgRO9XBsiRY4S9TBno sDPlkYw3vkv7OE=; b=GmEOoF/LDZldYiTGUR7X27Sg+Z3a02XwfGf+MR970t8WN O00M4WNMOX15zdyqrm6rwYytzc7BoiCC8o541z99vckZVUh7Gcm4RIEtiD7GQ+9y q7pyLiRflnh9IpE7x7UWAI3amk3LqNYdNgHAL7JTn+xPi5P7JTh10oo5gElBXE0/ i2qqSRBDlXaQpqn7g9zIMsUKCUVbrRjjQ/mz2lmEmjkTBqrwYrl5jNnLuEWMNy9a uIGUSt4aA9rBrhqcVNsp9P11NDYyetxorLg1Bx6ytpBsXFt4ZyeqFzKhXGq3f8Ew Spfn27el+jCA6q9xSeTwAgenfjxdJARfQUtc+qVTw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=Sh/zh5 QmGkaAgRO9XBsiRY4S9TBnosDPlkYw3vkv7OE=; b=Fgrl2lyzYAvYX0pYTuChWW t2JLksr2nuYBa8i2AKR+1gojZDaapR1NfguSFLw1m1I/KYxk8tHU6zbQ9z134b+w CTacfZKOAEJUK+6DLjq1Lvy6Awoh1ganj/UOJREt9A5s7LfdAXDSj338BFfTIe8p ju/euLUCiA/29M+zyolW2kH9fk0vgv/85YGfi4gQ/xOa5wkKdQRZ4UMFPsUNMsz7 eisC0NG4zgq5R5EvNJoXL93Tj95tPha1iPdZzD2JPHvuCqmonMi0hMcEWA6pEYni omXzLxjk5zk2TLP52QlqSQkIJhcINjrHbcq0YQVQNGYsCQEMz9Srj7so5VTJ9n5w == X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id 16C9CE4350; Sat, 12 May 2018 09:38:03 -0400 (EDT) Subject: Re: Best conception of a table To: hmidi slim , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <8ddee3bf-3f7d-6999-4650-79980b1e19df@aklaver.com> Date: Sat, 12 May 2018 06:38:02 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/11/2018 03:54 PM, hmidi slim wrote: > Hi, > I want to get some different opinions and tips about two different > conception. > I want to create a table named proudct_price_period > createtableproduct_price_period { > id integer, > product_id integer, > occupation_type_id integer, > price_mode_id integer, > price_perioddaterange, > days_checked integer[], > CONSTRAINTproduct_price_period_id PRIMARYKEY(id) NOTNULL, > CONSTRAINTproduct_price_occupation_type_id FOREIGN KEY(occupation_type_id) > REFERENCESoccupation_type(id) > CONSTRAINTproduct_price_price_mode_id FOREIGN KEY(price_mode_id) > REFERENCESprice_mode(id) > CONSTRAINTproduct_price_product_id FOREIGN KEY(product_id) > REFERENCESproduct(id) > } > > > This table has relations with other tables such as 'product', > 'price_mode' and 'occupation_type' which have these schema: > > createtableprice_mode { > id integerPRIMARYKEYNOTNULL, > namecharactervarying(255) > } > > createtableoccupation_type { > id integerPRIMARYKEYNOTNULL, > namecharactervarying(255) > } > > createtableproduct { > id integerPRIMARYKEYNOTNULL, > namecharactervarying(255), > addresscharactervarying(255), > statusboolean > } > > 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. The result does not seem to correspond with your problem description. The description would seem to indicate you are looking for any product available at any time during the period. That would include product_id_1. The result you show says you are only looking for a product that is available for all days during the period. Can you clarify this? > > This solution need a  huge effort and I tried to solve that with CTE. > > *Solution2: > > * > In the second  solution I keep the same tables but I added another table > called 'product_price_period': > createtableproduct_price_sub_period { > id integerPRIMARYKEYNOTNULL, > product_price_period_id integer, > sub_period daterange, > CONSTRAINTproduct_price_sub_period FOREIGN KEY(product_price_period_id) > REFERENCESproduct_price_period(id) > } > > It's a temporary table and filled using a trigger. The trigger insert or > update the table if any row was added or updated in the table > product_price_period. > I want to know if it's a good practice to use temporary tables (when > should temporary tables will be used) or I use CTE and keep the first > solution (despite the long query that I should to write in order to > select the data)? > Every solution, tip or advice will be welcome. > Thanks. > ** > -- Adrian Klaver adrian.klaver@aklaver.com