Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7ylQ-0000Df-P7 for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 08:45:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h7ylN-0003fK-Jt for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 08:45:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7ylM-0003ZX-V0 for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 08:45:53 +0000 Received: from mail-ot1-x343.google.com ([2607:f8b0:4864:20::343]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7yl0-00054Z-Om for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 08:45:51 +0000 Received: by mail-ot1-x343.google.com with SMTP id 64so5466431otb.8 for ; Sun, 24 Mar 2019 01:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Amk+ex7rjo556/053ugt3J2I/z/MknkRGCbTSQA5lV4=; b=KE69qoAaCFm+eCQy6ncLZCFA7E/zoAwA9R6lM+vraT//TNljq0wKxaTT2KA20AISgp siWADX9aMu7E1nq/XHjn+X8G7DTg0EJRTalo2mXZEcnUaucstdK8mZAloOG9YZMrT2Tb mf8bDxo6KvIq2CC2pAHEP80FpLnVM/71/GKoiu5jWtwL8cMHx5InWdSYdhba4hk9YIwM ZToWaAKMGv31PQk0PCeZSRSvtVG8a8RChs4oX0YoNcCYdybwhzvbqNV5KJDOPRGwLf4R p8lVlWiREgYsL4Szgyvohe7uwJ9/F1Q+mljvNS03rqBZqo4NBK565Z1amNuZToMzBeLm R6Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=Amk+ex7rjo556/053ugt3J2I/z/MknkRGCbTSQA5lV4=; b=MAMPzNL8eEHProgYDW+gW1znjbfic4aiz+hV2VV3DTT+guDWOjYBLtND+n2XkevYO3 wyqD3q53xlB2fSD7Y8MLtfFMQsinKIXD3hqYqAcA6w+JPkJRMz7Cksyv+V1ThHm1LOae FUuOHPNvB+D0IxwCIA5G5xMbF6vV4G8NNoCpDQcGoprlzBrx/WoWwfx9O0gunbR5ikp2 p4bjKBezWmCyQKGufxSfSXtVZO/QvCzA+UO8zJFhXQULbEoK5t329IUNFEZjCIUfSdkM Ds5k6V5g0iLC6ga6M+gj2WGNUbFMPMFWnRiWH0vUdFX2Ee3O9Yj5QYC0bMntPzR/JQZ7 eKGQ== X-Gm-Message-State: APjAAAW2vrKwfzBeQ9MpJV2pyufJmlme2aOE34Q4oEZNhcWuBUpKziB7 jVM4aL1eIPw7DlT99fQfK4KAL7DYrEE= X-Google-Smtp-Source: APXvYqwcHxhqYzbPrtiOeWQ1mnKsO5yzDgGe7Qd0SZ3IzDi4IVFEmuFNS2SeL4C5muxcOB/I1eRkeg== X-Received: by 2002:a9d:6a43:: with SMTP id h3mr5603125otn.270.1553417129653; Sun, 24 Mar 2019 01:45:29 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id s73sm5417099oie.53.2019.03.24.01.45.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 24 Mar 2019 01:45:28 -0700 (PDT) Subject: Re: When to store data that could be derived To: pgsql-general@lists.postgresql.org References: <835132f4-e1c6-d362-1b13-ad48671afe39@gmail.com> <6dcf59be-2029-8554-88de-88587368b50d@chagford.com> From: Ron Message-ID: <6d9e9fc2-cfd9-c36f-b813-0f2f4492da8f@gmail.com> Date: Sun, 24 Mar 2019 03:45:27 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <6dcf59be-2029-8554-88de-88587368b50d@chagford.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/24/19 3:05 AM, Frank wrote: > > > On 2019-03-24 9:25 AM, Ron wrote: >> On 3/24/19 1:42 AM, Frank wrote: >>> Hi all >>> >>> As I understand it, a  general rule of thumb is that you should never >>> create a physical column if the data could be derived from existing >>> columns. A possible reason for breaking this rule is for performance >>> reasons. >>> >>> I have a situation where I am considering breaking the rule, but I am >>> not experienced enough in SQL to know if my reason is valid. I would >>> appreciate it if someone could glance at my 'before' and 'after' >>> scenarios and see if, from a 'gut-feel' point of view, I should proceed. >>> > > [snip] > >> >> Sure the second query joins a lot of tables, but is pretty straightforward. >> >> What REALLY worries me is whether or not the query optimiser would look >> at the WHERE CASE, run away screaming and then make it use sequential >> scans. Thus, even query #1 would be slow. >> > > I had not realised that. I hope someone else chimes in on this. In every DBMS that I've used, the lside (left side) needs to be static (not "a" static) instead of variable (like a function). For example, this always leads to a sequential scan:    WHERE EXTRACT(DAY FROM DATE_FIELD) = 5 > >> >> Is this a historical data set that's never updated, or current data >> that's constantly added to? >> > > It is the latter - current data constantly added to. > > Frank > -- Angular momentum makes the world go 'round.