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 1h7xW3-0005GX-Ja for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 07:25: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 1h7xW0-0003kg-Gz for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 07:25:56 +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 1h7xVz-0003kZ-Py for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 07:25:56 +0000 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7xVs-0003PV-K5 for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 07:25:54 +0000 Received: by mail-ot1-x344.google.com with SMTP id q24so5422214otk.0 for ; Sun, 24 Mar 2019 00:25:48 -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=VwL6hWtZlYcG09HfKTJPI7/BIbtyuSfWR1bplEBDgNc=; b=eLZtEisrk11m36gF0tty1vQdqLR0BNjC23v6jk89QUXGzqRVVk/MfKM/K8loRO0hLm Sr40awBhvvLL9G5sEVdHt3+xfjSnHZtdJoFuwbMgLYxBUIIZth+Gp0hhtvq4HS39qrcK kLCoV5my3yRm1rdpWylwHTLqGeMX2Z/FepZhHl/TLvtbz8SZDektL33VJkgV0Pzkwl7o IwR7D7uq7dvWx8Iy3k16W+iGlPqyvna0Guu1UB5t4d9Vag3bMXHLrvESHMVyeVe1IK7N VjjpA9XJhahvggXyKMor2aiUj2UzcBYneWBJCNFgVx9U1lFKQuWl4o1hzkStKJSuuQpp y9WQ== 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=VwL6hWtZlYcG09HfKTJPI7/BIbtyuSfWR1bplEBDgNc=; b=VJ91HYUGLz7F96FdVTQISXFJxLt0oWFaqtho7TA4T0Q9K/LDa6UEANYALx2fMuqB4W yz1n0SGDGNPXcaA5EjbB3+FB37dFlLZvd2rhRyynqTdcKOtiXZefcXeW1Zq8FL3v2lYy JcPBMJxcGDCrJsftJ1XTLIT1wRTae371ETBx0hitkkjZQm/r1Z9MDFosV4Q6PylpKo7X KVYz8xmREx1hd31qQHCyskvOSl9mnVHtwxsaxzthrBSEwK+GcTdYG1TK9nmQlBHp16qd WDL50rfIHLncrcRqW4BEVf9MOQfbhQHvcNGMeyuUYe05TG4i9PNJr8MPLWu8ZKCJBkHh X1KQ== X-Gm-Message-State: APjAAAW1SnzMjYGw2pXj/nCwtKLGHy2fb/wKPlgfTBaNtSrGsSYRLn/Z SaIe0FNLFLDKl1JZnaTGnOjuPK3FToU= X-Google-Smtp-Source: APXvYqyjRRBjaPMsySu47vbl+kkuSn3pbsXA+gtC+IgEV6vdA97OUJZLScPZfDH6ZEdM5xN54QIn4A== X-Received: by 2002:a9d:57c2:: with SMTP id q2mr7483821oti.313.1553412346955; Sun, 24 Mar 2019 00:25:46 -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 88sm5951927otj.3.2019.03.24.00.25.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 24 Mar 2019 00:25:45 -0700 (PDT) Subject: Re: When to store data that could be derived To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <835132f4-e1c6-d362-1b13-ad48671afe39@gmail.com> Date: Sun, 24 Mar 2019 02:25:44 -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: 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 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. > > I have a VIEW constructed as follows - > > CREATE VIEW view_name AS > [select statement 1] > UNION ALL > [select statement 2] > etc. > > This is one of the select statements. I will give the 'after' scenario > first - > > SELECT >     'arec' AS tran_type, a.row_id AS tran_row_id, >     a.tran_number AS tran_number, a.cust_row_id AS cust_row_id, >     CASE >         WHEN a.tran_type = 'ar_rec' THEN y.tran_date >         WHEN a.tran_type = 'cb_rec' THEN w.tran_date >     END AS tran_date, >     CASE >         WHEN a.tran_type = 'ar_rec' THEN y.text >         WHEN a.tran_type = 'cb_rec' THEN w.text >     END AS text, >     0 - a.arec_cust AS amount_cust, >     0 - a.arec_local AS amount_local > FROM ar_rec_subtran a > LEFT JOIN ar_tran_rec_det z ON z.row_id = a.tran_det_row_id > LEFT JOIN ar_tran_rec y ON y.row_id = z.tran_row_id > LEFT JOIN cb_tran_rec_det x ON x.row_id = a.tran_det_row_id > LEFT JOIN cb_tran_rec w ON w.row_id = x.tran_row_id > WHERE >     CASE >         WHEN a.tran_type = 'ar_rec' THEN y.posted >         WHEN a.tran_type = 'cb_rec' THEN w.posted >     END = '1' > > The two columns a.arec_cust and a.arec_local *can* be derived from other > columns, and in fact that is how it is working at the moment, so here is > the 'before' scenario - > > SELECT >     'arec' AS tran_type, a.row_id AS tran_row_id, >     a.tran_number AS tran_number, a.cust_row_id AS cust_row_id, >     CASE >         WHEN a.tran_type = 'ar_rec' THEN y.tran_date >         WHEN a.tran_type = 'cb_rec' THEN w.tran_date >     END AS tran_date, >     CASE >         WHEN a.tran_type = 'ar_rec' THEN y.text >         WHEN a.tran_type = 'cb_rec' THEN w.text >     END AS text, >     ROUND(0 - (ROUND(a.arec_amount / CASE >         WHEN a.tran_type = 'ar_rec' THEN y.tran_exch_rate >         WHEN a.tran_type = 'cb_rec' THEN w.tran_exch_rate >     END * a.cust_exch_rate, u.scale)), 2) AS amount_cust, >     ROUND(0 - (ROUND(a.arec_amount / CASE >         WHEN a.tran_type = 'ar_rec' THEN y.tran_exch_rate >         WHEN a.tran_type = 'cb_rec' THEN w.tran_exch_rate >     END, s.scale)), 2) AS amount_local > FROM ar_rec_subtran a > LEFT JOIN ar_tran_rec_det z ON z.row_id = a.tran_det_row_id > LEFT JOIN ar_tran_rec y ON y.row_id = z.tran_row_id > LEFT JOIN cb_tran_rec_det x ON x.row_id = a.tran_det_row_id > LEFT JOIN cb_tran_rec w ON w.row_id = x.tran_row_id > LEFT JOIN ar_customers v ON v.row_id = a.cust_row_id > LEFT JOIN adm_currencies u ON u.row_id = v.currency_id > LEFT JOIN adm_params t ON t.row_id = 1 > LEFT JOIN adm_currencies s ON s.row_id = t.local_curr_id > WHERE >     CASE >         WHEN a.tran_type = 'ar_rec' THEN y.posted >         WHEN a.tran_type = 'cb_rec' THEN w.posted >     END = '1' > > As you can see, complexity has increased and there are four additional JOINs. > > I am expecting the VIEW to be used extensively for query purposes, and my > gut-feel says that the second one is likely to lead to performance > problems in a system with a lot of data and a lot of users. Generate an artificial load and test it? > I am not looking for an answer - I know that I should create dummy data > and run some timing tests. I was just wondering if someone more > experienced would wince when they look at the second SELECT, or if they > would shrug and think that it looks fine. 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. > > Any input will be appreciated. Is this a historical data set that's never updated, or current data that's constantly added to? > > Frank Millman -- Angular momentum makes the world go 'round.