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 1kxd9C-0007dh-LC for pgsql-general@arkaria.postgresql.org; Thu, 07 Jan 2021 21:48:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kxd9B-00076E-En for pgsql-general@arkaria.postgresql.org; Thu, 07 Jan 2021 21:48:45 +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 1kxd9B-000766-1g for pgsql-general@lists.postgresql.org; Thu, 07 Jan 2021 21:48:45 +0000 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kxd97-0002fB-Sf for pgsql-general@lists.postgresql.org; Thu, 07 Jan 2021 21:48:44 +0000 Received: by mail-pg1-x532.google.com with SMTP id 30so6156279pgr.6 for ; Thu, 07 Jan 2021 13:48:41 -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=NhzcF7W0t5xVeyoLTy3YUEboeq2KIysLon3xAwu5PAk=; b=sABECkuPyBRfXcy57kuoPFdM4n0js5mGeQmjwQ6i4TnaOqCd01yA3cjiF/VxKdsOVd f26xQ00/xUqJCkJPeVKOSSSeMhTNPZ9qdVDrRhd1tjXHc0p1im82thlo1rnv5+NcsPPc WF/5+fOnu6KuVeMYqXOg9s/WvA5Q3q3f2mMBUUDor4dgsllEZdcCT4W5df0+Iy0edHlC 5KpcvXkDcOXQxPKSfQi+cRdMgxtRYyhzL11cjE6iKzN1Uwp384RMXXGxOUtLoCtSKLG0 mBGKVoBZMgmI2myPKKGodt9zsSmbolytQmkFz/Ak2aGVFhYN57qXZrtD6+AwnGMkK001 m35Q== 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=NhzcF7W0t5xVeyoLTy3YUEboeq2KIysLon3xAwu5PAk=; b=p3nHcguDoQ5IbXsnoUXUOWiErtpZDwTEUp/jBmaSEWAYK50xyoNZJx+0BJmrSuZpkn W9CRLhkVuZ2cu7seAqt6Z/eRuVpbVJLvxy0vPpsnUu3Ehj6CFEt4XWLlH/p5Z61imGoE XZZGqvDW2GCsdqyRrtUFRCM9Kt46hpKfp2V3yCqqeVJw62QK2LJ3Fvii7ylUHfNvsvz4 MEHW1k1uE4MlmVCQZ237CfknRSy/dmT6IEhq4s/rzJ0vRUU1YIARUqVIQFrUyYebHoi9 d7VPGl5PSmSk9RCBU5/dbiqaExC5DEJRue7KzrCCsPgnsP2YiPY/C5fdSZxhYMsWe2b8 oMEQ== X-Gm-Message-State: AOAM533C0rn07+2/TZh2aWUVJC6GOtnC1/p6xiCUJjkYRvcyWHb86w6S FWMyvgEqbMtEoZoeKDG48Cd7KyPikGk0gQ== X-Google-Smtp-Source: ABdhPJxhJuAwk5JrkccCKuNg/FJv2N3+VoJnxHOeHKvsrrQce3T8T2K+tXrMgb0w8eEtpIvZ/0iwvQ== X-Received: by 2002:aa7:95aa:0:b029:1ad:d810:42be with SMTP id a10-20020aa795aa0000b02901add81042bemr595124pfk.51.1610056119134; Thu, 07 Jan 2021 13:48:39 -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 b17sm2863099pjz.44.2021.01.07.13.48.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jan 2021 13:48:38 -0800 (PST) Date: Thu, 7 Jan 2021 13:48:01 -0800 From: Guyren Howe To: "=?utf-8?Q?pgsql-general=40lists.postgresql.org?=" , Kevin Brannen , Florents Tselai Message-ID: In-Reply-To: References: <34fa7876-2346-4cb4-a094-d20963518286@Spark> <22359fa3-0157-4ba7-9dd6-bcb87a7a9014@Spark> Subject: Re: Is there a good discussion of optimizations? X-Readdle-Message-ID: a74a1282-7a73-48d1-9c7d-7f0165714102@Spark MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5ff781b3_1190cde7_6c3c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5ff781b3_1190cde7_6c3c Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Jan 7, 2021, 13:42 -0800, =46lorents Tselai , wrote: > Apologies for the shameless self-promotion :) > > Around a year ago I collected my thoughts on this topic. You can read t= he post here=C2=A0Modern Data Practice and the SQL Tradition=C2=A0. > It looks like it resonated with a lot of folks in the community. HN Dis= cussion=C2=A0https://news.ycombinator.com/item=3Fid=3D21482114 > > I would specifically underline the fact that the newer generation of pr= ogrammers & data pros (my former self included) don't really appreciate t= hings like triggers and server-side programming. Triggers and DB-side fun= ctions are considered something like Assembly code. > > Not many neophytes have been shown with use cases why for example writi= ng 2-3 lines of PL/SQL can save you a huge overhead of back and forth and= environment set up to write the same thing in say Pandas. > > I would focus on Triggers, Indices on expressions, and time-related fun= ctions. Probably on generated columns too. They may be considered a new f= eature, but the reasoning of building successively columns on top of a fe= w base ones is quite appealing nowadays, especially for ML purposes. > > I also wouldn't hesitate to discuss advanced topics. They are usually c= onsidered obscure because people don't present useful examples, rather to= y and arbitrary ones. > > In a recent O'Reilly training, I was skeptical of talking about trigger= s for optimization but it looks like it was probably the most useful part= of my training as students could actually =22steal and tweak=22=C2=A0my = code. Thanks for this. May I steal some of your examples if they prove useful=3F= I=E2=80=99ll credit you of course. I=E2=80=99m planning on somewhat emphasizing that a relational database i= s a logic engine. Viewed through this lens, a query or view is a =E2=80=9C= backward=E2=80=9D implication and a trigger is a =E2=80=9Cforward=E2=80=9D= one. This leads to considering triggers (and the moral equivalent in ext= ernal code) as requiring =E2=80=9Ctruth maintenance=E2=80=9D, and is a gr= eat way to think about when the database is the appropriate place for som= e bit of logic. > On Thu, Jan 7, 2021 at 11:21 PM Guyren Howe wrote:= > > On Jan 7, 2021, 13:07 -0800, Kevin Brannen , wro= te: > > > =46rom: Guyren Howe > > > > > > >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 relational database is and how to get the most= out of them. I see a lot of folks trying to use SQL in an imperative man= ner - make this temp table, then update it some, then make this other tem= p table, etc... > > > > Actually, I=E2=80=99m mostly going to talk about the relational model= , rather than SQL. Our industry seems to always settle for third-best, an= d SQL is the worst of all the examples of this. The world desperately nee= ds a good relational database based on a better query language =E2=80=94 = datalog, for example. > > > > I put up with SQL so I can use the relational model, and I think that= understanding SQL has to start with that. > > > > Anyhow. > > > An example of this is that we have a report we're trying to write t= hat 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 Perl very easily. The problem here rev= olves > > > 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 wo= uld have > > > said SQL can't do that. Now I know about the OVER clause, something= that I > > > would categorize as somewhat obscure, I can do it as needed. > > > > Actually, Window functions might be =E2=80=9Cadvanced=E2=80=9D, but a= re certainly not obscure. Your example sounds like it=E2=80=99s trivially= solved with LAG(). > > > As Michael Lewis pointed out, large dataset can also cause you to c= hoose not > > > to use SQL in 1 big statemet for everything (i.e. advocating the us= e to temp > > > tables). In some ways, using a CTE is a type of temp table, or at l= east I > > > view it as such. That allows a person to solve a problem in bite-si= zed chunks. > > > I will agree that optimization can do it better at times, but the c= ode 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 com= ing to learn has a fairly poor query optimizer. But I would have thought = Postgres=E2=80=99s optimizer would usually use a temporary table where ap= propriate. > > > > 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 as it should be in many places. > > > > An important consideration will be when relational is inappropriate. = My biggest goal, though, is to get folks to understand how much relations= *can* 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. --5ff781b3_1190cde7_6c3c Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
On Jan 7, 2021, 13:42 -0800, =46lorents Tselai <= florents.tselai=40gmail.com>, wrote:
Apologies for the shameless self= -promotion :)&=23160;

Around a year ago I collected my= thoughts on this topic. You can read the post here&=23160;Modern Data Practice and the SQL Tradition&=23160= ;.
It looks like it resonated with = a lot of folks in the community. HN Discussion&=23160;ht= tps://news.ycombinator.com/item=3Fid=3D21482114

I would specifically underline t= he fact that the newer generation of programmers & data pros (my form= er self included) don't really appreciate things like triggers and server= -side programming. Triggers and DB-side functions are considered somethin= g like Assembly code.&=23160;

Not many neophytes have been sho= wn with use cases why for example writing 2-3 lines of PL/SQL can save yo= u a huge overhead of back and forth and environment set up to write the s= ame thing in say Pandas.&=23160;

I would focus on Triggers, Indic= es on expressions, and time-related functions. Probably on generated colu= mns too. They may be considered a new feature, but the reasoning of build= ing successively columns on top of a few base ones is quite appealing now= adays, especially for ML purposes.

I also wouldn't hesitate to disc= uss advanced topics. They are usually considered obscure because people d= on't present useful examples, rather toy and arbitrary ones.&=23160;

In a recent O'Reilly training, I= was skeptical of talking about triggers for optimization but it looks li= ke it was probably the most useful part of my training as students could = actually =22steal and tweak=22&=23160;my code.

Thanks for this. May I steal some of your examples if they prove useful=3F= I=E2=80=99ll credit you of course.

I=E2=80=99m planning on somewhat emphasizing that a relational database i= s a logic engine. Viewed through this lens, a query or view is a =E2=80=9C= backward=E2=80=9D implication and a trigger is a =E2=80=9Cforward=E2=80=9D= one. This leads to considering triggers (and the moral equivalent in ext= ernal code) as requiring =E2=80=9Ctruth maintenance=E2=80=9D, and is a gr= eat way to think about when the database is the appropriate place for som= e bit of logic.
On Thu, Jan 7, 2021 at 11:21 PM = Guyren Howe <guyren=40gmail.com> wrote:
On Jan 7, 2021, 13:07 -0800, Ke= vin Brannen <KBrannen=40efji.com>, wrote:
=46rom: Guyren Howe <guyren=40gmail.co= m>
&=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 go= ing to talk about the relational model, rather than SQL. Our industry see= ms to always settle for third-best, and SQL is the worst of all the examp= les of this. The world desperately needs a good relational database based= on a better query language =E2=80=94 datalog, for example.

I put up with SQL so I can use = the relational model, and I think that understanding SQL has to start wit= h 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 migh= t be =E2=80=9Cadvanced=E2=80=9D, but are certainly not obscure. Your exam= ple sounds like it=E2=80=99s trivially solved 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 quer= y optimizer. But I would have thought Postgres=E2=80=99s optimizer would = usually use a temporary table where appropriate.

Curious to hear if that=E2=80=99= s 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 biggest goal, though, is to get = folks to understand how much relations *can* do =E2=80=94 far too many de= vs 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 g= ive me this feedback.
--5ff781b3_1190cde7_6c3c--