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 1fkHXp-0001TB-Nc for pgsql-general@arkaria.postgresql.org; Mon, 30 Jul 2018 23:25:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fkHXn-00019N-3g for pgsql-general@arkaria.postgresql.org; Mon, 30 Jul 2018 23:25:39 +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 1fkHXR-0007km-7i for pgsql-general@lists.postgresql.org; Mon, 30 Jul 2018 23:25:17 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fkHXL-0003jq-Td for pgsql-general@postgresql.org; Mon, 30 Jul 2018 23:25:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id F40094B5; Mon, 30 Jul 2018 19:25:09 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 30 Jul 2018 19:25:10 -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=fm3; bh=YF9q861kVumiAyCJIbw9ureEmDq7i 01WzNrI65WX5qU=; b=B3pLqbuDd2tP6u1YJ80xKVKMxJKz9mc2rC5UCK3OEzaWg Vrfj3SpfQby9Pu/I1xIX4CpSIB3kogHlbP6rhHmJhtShl9T39wXAEm0CT7dqYsKW y55kDWD6Ndnyf8Hxv3MWKGkaJthBXGarYKKw95Yk2zI+qStEubph496eD4vnmO5d UcD6TOIqgW7zGyTEIfp9rVWNr4gnoVifj9c7jqlnzcvmiBsUbe0PAiyYTbCM2lWz 2T4E4G6aO9OI/jeY+yeLVqtJpLMzFbcUjSxgr+fdDo77CR3Kw3xbYcXga5KCn+Le bxkjyoTEsQDnP34ys2oIn5bKQjDwL6qauIZcDE1RQ== 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=fm3; bh=YF9q86 1kVumiAyCJIbw9ureEmDq7i01WzNrI65WX5qU=; b=f+LzAVXQ7ZWNENmFVVF/mD z0ut4XvFNyGjPTwI7CvyNrRfBIZaPi9pxctMXMOMi/HcihpJEBf3rB00IW4WNbDd v3TG7WlnGrYI8USs/X2yXJ/oxpaU7KL9DMhOSzjEyvZlNtJD2zW8dS4QAQE2A3t3 wvQcayjC3cK3r/NkmJUQGybo7tMs2nniIKqSGw82GSXlg2uJbo74qLAca6upNk3O oHqaOQXYJZCM6BgC1nuMy9hDw83RT/g67o2FPRiWCdGbluLJ7OgDSa7kRQAudPkh Mhd8oPd1EauvTsiCEbbk0hVGsWRlwhKHYqJ+PKRmuyrBz4HNw4WjXpV8oZbTTxqw == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.181]) by mail.messagingengine.com (Postfix) with ESMTPA id EA3AE1025D; Mon, 30 Jul 2018 19:25:08 -0400 (EDT) Subject: Re: Design of a database table To: hmidi slim , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: Date: Mon, 30 Jul 2018 16:25:08 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 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 07/30/2018 07:37 AM, hmidi slim wrote: > I'm trying to design a database table. First of all there are two > alternatives: > 1-) Divide the table into two tables and make a join. > 2-) Design a single table. > > 1rst alternative: > Create table data_periods( > id serial primary key not null, > period daterange, > project_id integer > ) > > create table data_periods_info( > id serial primary key not null, > data_periods_id integer, > data_sub_periods daterange, > stock1 integer, > stock2 integer, > CONSTRAINT data_periods_allotment_id_fkey FOREIGN KEY (data_periods_id) >         REFERENCES data_periods (id) MATCH SIMPLE >         ON UPDATE NO ACTION >         ON DELETE NO ACTION > ) > > The table data_periods contains 1M rows and data_periods_info 5M rows. > I added an index to the table data_periods_info for the column > data_periods_id > I execute this query: > select > data_periods.id , > data_sub_periods, > project_id, > stock1, > stock2 > from data_periods > inner join data_periods_info on data_periods_info.data_periods_id = > data_periods.id > where data_periods.period && '[2018-07-28, 2018-08-02]'::daterange > and data_sub_periods  && '[2018-07-28, 2018-08-02]'::daterange > > I got an execution time of : 1s 300ms > > > 2nd alternative: > create table data_periods_second( > id serial primary key not null, > data_sub_periods daterange, > project_id integer, > stock1 integer, > stock2 integer) > > I run this query; > select * from data_periods_second > where data_sub_periods && '[2018-07-28, 2018-08-02]'::daterange > > I got such a execution time : 1s > > > Is it normal to get an execution time when using join relation greatest > than the execution time of a table contains million of rows and many > columns? Not surprising given that you are searching for a date range in two tables in the join versus only one in the other case. That fact that you are using the same range end points for period in data_periods and data_sub_periods in data_periods_info, to me, points to a design flaw. If period and data_sub_periods are the same why separate and repeat them? Also when asking for input on query planning/outcomes running EXPLAIN ANALYZE on the queries and posting the results here will help arrive at answer. -- Adrian Klaver adrian.klaver@aklaver.com