Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxcjD-000754-W5 for pgsql-general@arkaria.postgresql.org; Thu, 07 Jan 2021 21:21:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kxcjB-0000dn-Qy for pgsql-general@arkaria.postgresql.org; Thu, 07 Jan 2021 21:21:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxcjB-0000df-Fv for pgsql-general@lists.postgresql.org; Thu, 07 Jan 2021 21:21:53 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kxcj4-0002Sa-JH for pgsql-general@lists.postgresql.org; Thu, 07 Jan 2021 21:21:53 +0000 Received: by mail-pl1-x62b.google.com with SMTP id r4so4388770pls.11 for ; Thu, 07 Jan 2021 13:21:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:message-id:in-reply-to:references:subject:mime-version; bh=NOeyI+8rI9W4XYx8m9d6FYVpwL6Hm38mwz07D3H9auE=; b=JpVOpzjRjiKZFfYNS2eoQ/8tT3Ku+YOLik03yc2waiJ0+ahydr2E9mnAZdy1w9nQG2 pEbRVZYId/OVvRE2zPLs03xSnMrdtC4VJ9Yj4pNu94O+pXC0C0xiW8+Yb7RSc1Oc6nde zJV8FANjHB9KYuIoKQVGA5PHi07+5EArEDeSwPY9ONlBhdki/26ugWyqXmTMAjJq+5Og c69EPUVtT5qjy5XASnSLOQpo8RLoZEhAvR1WZP7eQ+8YGeT0YCF6aHQQyEKmmOAIjcrH +KWdvAFIyfkeNAF+1NEOi2LqkauZww9OxMDLPfQA1a6jORkLfZZ5Tzo2RZ4qQe/dCF8d lqTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:message-id:in-reply-to:references :subject:mime-version; bh=NOeyI+8rI9W4XYx8m9d6FYVpwL6Hm38mwz07D3H9auE=; b=rkP96gF7WN2Pn/escXeBx6uyiFz+VzgiKOL7MTL3g6hTaJZb/eX1X2ptMD2A2ALkLK UGOe7VTcyxe5TgGOfmY6DEFfBzFftheJSRLbEzK0v1c9UegeaKx1y/D4oowEusSbN3sx tZBviH7o+KlC8qjK185WmECSXr0h2QXyk0ZuErPhUpZGlA8ja8bP3yy84knWYQm17+5n KGPkScxCWqKkhB+vMd7B5iYmZbzi50z2uTaA2ocJMhbZWMTgLYqL61rOj3RqIt+lBj0J fKRU9xc6upEz5kk0oUff/QPp+/qMQvpTYaYXQ9SwbRBFC8SH8xZP8dXGSuYRbdI6lq58 2S/g== X-Gm-Message-State: AOAM531GpeIAE9lM7pLi+HM2RbEwFiO+9b1t5gCID8ogfbVQ2CVuXXEv +T5yxYET8n97+cEtikLgSVnlrBEyRwlHJQ== X-Google-Smtp-Source: ABdhPJxN4Jh17M9pMksbSSWX0g6UrTarRPI0tZycTW8JlH5Jy3OIV3S1DL1lvJkf9PjM2BFHqCphjA== X-Received: by 2002:a17:90a:9d88:: with SMTP id k8mr372882pjp.141.1610054503914; Thu, 07 Jan 2021 13:21:43 -0800 (PST) Received: from [192.168.1.79] (ip68-107-18-54.sd.sd.cox.net. [68.107.18.54]) by smtp.gmail.com with ESMTPSA id n4sm6511682pfu.150.2021.01.07.13.21.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jan 2021 13:21:43 -0800 (PST) Date: Thu, 7 Jan 2021 13:21:19 -0800 From: Guyren Howe To: "=?utf-8?Q?pgsql-general=40lists.postgresql.org?=" , Kevin Brannen Message-ID: <22359fa3-0157-4ba7-9dd6-bcb87a7a9014@Spark> In-Reply-To: References: <34fa7876-2346-4cb4-a094-d20963518286@Spark> Subject: RE: Is there a good discussion of optimizations? X-Readdle-Message-ID: 22359fa3-0157-4ba7-9dd6-bcb87a7a9014@Spark MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5ff77b66_216231b_6c3c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5ff77b66_216231b_6c3c Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Jan 7, 2021, 13:07 -0800, Kevin Brannen , wrote: > =46rom: Guyren Howe > > >Most folks, in my experience, who use relational databases don=E2=80=99= t really understand the basic theory or even more important the why - the= philosophy - of what a relational database is and how to get the most ou= t of them. I see a lot of folks trying to use SQL in an imperative manner= - make this temp table, then update it some, then make this other temp t= able, etc... Actually, I=E2=80=99m mostly going to talk about the relational model, ra= ther than SQL. Our industry seems to always settle for third-best, and SQ= L is the worst of all the examples of this. The world desperately needs a= good relational database based on a better query language =E2=80=94 data= log, for example. I put up with SQL so I can use the relational model, and I think that und= erstanding SQL has to start with that. Anyhow. > An example of this is that we have a report we're trying to write that = I'd > like to think can be done in SQL, but I can't think of a way to do it. = Yet, > if I do the base query and pull the data back into my application, I ca= n do > the last bit with 3 lines of Perl very easily. The problem here revolve= s > around comparing a row of data to the previous row to know whether the = data > changed =22significantly enough=22 to keep the new row. > > Another example is doing running totals. A couple of years ago I would = have > said SQL can't do that. Now I know about the OVER clause, something tha= t I > would categorize as somewhat obscure, I can do it as needed. Actually, Window functions might be =E2=80=9Cadvanced=E2=80=9D, but are c= ertainly not obscure. Your example sounds like it=E2=80=99s trivially sol= ved with LAG(). > As Michael Lewis pointed out, large dataset can also cause you to choos= e not > to use SQL in 1 big statemet for everything (i.e. advocating the use to= temp > tables). In some ways, using a CTE is a type of temp table, or at least= I > view it as such. That allows a person to solve a problem in bite-sized = chunks. > I will agree that optimization can do it better at times, but the code = also has > to be maintained as well =E2=80=93 a balancing act. This appears to be good advice with SQL Server, which I=E2=80=99m coming = to learn has a fairly poor query optimizer. But I would have thought Post= gres=E2=80=99s optimizer would usually use a temporary table where approp= riate. Curious to hear if that=E2=80=99s wrong. > I think your idea is a good one, but I do hope you present that SQL can= 't > solve everything ... else why do we have plpgsql. :) You=E2=80=99re cor= rect though, > SQL isn=E2=80=99t used as much as it should be in many places. An important consideration will be when relational is inappropriate. My b= iggest goal, though, is to get folks to understand how much relations *ca= n* do =E2=80=94 far too many devs in my experience don=E2=80=99t use the = power of SQL because they don=E2=80=99t understand it. Thanks for taking the time to give me this feedback. --5ff77b66_216231b_6c3c Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
On Jan 7, 2021, 13:07 -0800, Kevin Brannen <KBra= nnen=40efji.com>, wrote:
=46rom: Guyren Howe <guyren=40= gmail.com>
&=23160;
>Most folks, in my experience= , who use relational databases don=E2=80=99t really understand the basic = theory or even more important the why - the philosophy - of what a relati= onal database is and how to get the most out of them. I see a lot of folk= s trying to use SQL in an imperative manner - make this temp table, then = update it some, then make this other temp table, etc...

Actually, I=E2=80=99m mostly going to talk about the relational model, ra= ther than SQL. Our industry seems to always settle for third-best, and SQ= L is the worst of all the examples of this. The world desperately needs a= good relational database based on a better query language =E2=80=94 data= log, for example.

I put up with SQL so I can use the relational model, and I think that und= erstanding SQL has to start with that.

Anyhow.
An example of this is that we ha= ve a report we're trying to write that I'd
like to think can be done in SQL= , but I can't think of a way to do it. Yet,
if I do the base query and pull = the data back into my application, I can do
the last bit with 3 lines of Per= l very easily. The problem here revolves
around comparing a row of data t= o the previous row to know whether the data
changed =22significantly enough=22= to keep the new row.
&=23160;
Another example is doing running= totals. A couple of years ago I would have
said SQL can't do that. Now I kn= ow about the OVER clause, something that I
would categorize as somewhat obs= cure, I can do it as needed.

Actually, Window functions might be =E2=80=9Cadvanced=E2=80=9D, but are c= ertainly not obscure. Your example sounds like it=E2=80=99s trivially sol= ved with LAG().
As Michael Lewis pointed out, la= rge dataset can also cause you to choose not
to use SQL in 1 big statemet for= everything (i.e. advocating the use to temp
tables). In some ways, using a C= TE is a type of temp table, or at least I
view it as such. That allows a p= erson to solve a problem in bite-sized chunks.
I will agree that optimization c= an do it better at times, but the code also has
to be maintained as well =E2=80=93= a balancing act.

This appears to be good advice with SQL Server, which I=E2=80=99m coming = to learn has a fairly poor query optimizer. But I would have thought Post= gres=E2=80=99s optimizer would usually use a temporary table where approp= riate.

Curious to hear if that=E2=80=99s wrong.
I think your idea is a good one,= but I do hope you present that SQL can't
solve everything ... else why do= we have plpgsql. :) You=E2=80=99re correct though,
SQL isn=E2=80=99t used as much a= s it should be in many places.

An important consideration will be when relational is inappropriate. My b= iggest goal, though, is to get folks to understand how much relations *ca= n* do =E2=80=94 far too many devs in my experience don=E2=80=99t use the = power of SQL because they don=E2=80=99t understand it.

Thanks for taking the time to give me this feedback.
--5ff77b66_216231b_6c3c--