Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 9FBFC2E00E7 for ; Fri, 22 Feb 2008 16:50:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39933-02 for ; Fri, 22 Feb 2008 16:50:13 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.186]) by postgresql.org (Postfix) with ESMTP id BAAF12E00FA for ; Fri, 22 Feb 2008 16:50:02 -0400 (AST) Received: by nf-out-0910.google.com with SMTP id f5so345131nfh.23 for ; Fri, 22 Feb 2008 12:50:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; bh=RchYm5yU/gMLfPRwezfw6FwLKiV1Jh2IxwXSNEngX40=; b=XYCT3HepI2AyN+8cA2008jmgT+PC9VOtbjEnVOBETMCsw5pBEzaQ+KxoRdkuCvFm/H7ZF7I1/NkNWClIX2hPw4/UssEy524tI9B261PZG4AQ9DUqiYWpmBrujjcLmqkQIRpg65yqCMeg7lTsqLYK6cyrIeFs+0om3+UegvpyF7s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=LtGt01sKMPcAzoEH9vm3jgv9sPhfrBM1K1wSWrh4Vy/tbaw/QiAgP/t3VnlD9xT0q7H0lEoQh3iF2uX7gMwtEtv6MPJdUD5DfAi1ffRUmExLMA2o5mlLFBGzMfu2nTYvf//p4UvBOoiKfGeJayXRViZfkXYnxJdv9zsqlX2dlGc= Received: by 10.114.154.1 with SMTP id b1mr544891wae.34.1203713399305; Fri, 22 Feb 2008 12:49:59 -0800 (PST) Received: by 10.114.176.19 with HTTP; Fri, 22 Feb 2008 12:49:59 -0800 (PST) Message-ID: Date: Fri, 22 Feb 2008 15:49:59 -0500 From: "Kynn Jones" To: pgsql-performance@postgresql.org Subject: Q on views and performance MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2563_25000795.1203713399292" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/288 X-Sequence-Number: 28830 ------=_Part_2563_25000795.1203713399292 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi. I'm trying to optimize the performance of a database whose main purpose is to support two (rather similar) kinds of queries. The first kind, which is expected to be the most common (I estimate it will account for about 90% of all the queries performed on this DB), has the following general structure: (Q1) SELECT a1.word, a2.word FROM T a1 JOIN T a2 USING ( zipk ) WHERE a1.type = AND a2.type = ; ...where and stand for some two integers. In English, this query essentially executes an inner join between two "virtual subtables" of table T, which are defined by the value of the type column. For brevity, I will refer to these (virtual) subtables as T and T. (I should point out that T holds about 2 million records, spread roughly evenly over about 100 values of the type column. So each of these virtual subtables has about 20K records. Also, for practical purposes T may be regarded as an immutable, read-only table, since it gets re-built from scratch about once a month. And, FWIW, all the columns mentioned in this post have a NOT NULLconstraint.) The second form is similar to the first, except that now the join is taken between T and T: (Q2) SELECT a1.word, a2.word FROM T a1 JOIN T a2 USING ( zipk ) WHERE a2.type = ; (Both the forms above are somewhat oversimplified relative to the actual situation; in our actual application, the joins are actually left outer ones, and each query also involves an additional inner join with another table, S. For the sake of completeness, I give the "real-world" versions of these queries at the end of this post, but I think that for the purpose of my question, the additional complications they entail can be neglected.) One way to speed (Q1) would be to break T into its subtables, i.e. to create T1, T2, T3, ... , T100 as bona fide tables. Then the query would become a simple join without the two condition of the original's WHERE clause, which I figure should make it noticeably faster. But since the second kind of query (Q2) requires T, we can't get rid of this table, so all the data would need to be stored twice, once in T and once in some T. In trying to come up with a way around this duplication, it occurred to me that instead of creating tables T1, T2, etc., I could create the analogous views V1, V2, etc. (e.g. CREATE VIEW V1 AS SELECT * FROM T WHERE type = 1). With this design, the two queries above would become (Q1*) SELECT V.word, V.word FROM V JOIN V USING ( zipk ); (Q2*) SELECT T.word, V.word FROM T JOIN V USING ( zipk ); Of course, I expect that using views V and V... would result in a loss in performance relative to a version that used bona fide tables T and T. My question is, how can I minimize this performance loss? More specifically, how can I go about building table T and the views V's to maximize the performance of (Q1)? For example, I'm thinking that if T had an additional id column and were built in such a way that all the records belonging to each V were physically contiguous, and (say) had contiguous values in the id column, then I could define each view like this CREATE VIEW V AS SELECT * FROM T WHERE <= id AND id < ; So my question is, what design would make querying V1, V2, V3 ... as fast as possible? Is it possible to approach the performance of the design that uses bona fide tables T1, T2, T3, ... instead of views V1, V2, V3 ...? Thank you very much for reading this long post, and many thanks in advance for your comments! Kynn P.S. Here are the actual form of the queries. They now include an initial join with table S, and the join with T (or V) is a left outer join. Interestingly, even though the queries below that use views (i.e. Q1*** and Q2***) are not much more complex-looking than before, the other two (Q1** and Q2**) are. I don't know if this is because my ineptitude with SQL, but I am not able to render (Q1**) and (Q2**) without resorting to the subquery sq. (Q1**) SELECT a1.word, sq.word FROM S JOIN T a1 USING ( word ) LEFT JOIN ( SELECT * FROM T a2 WHERE a2.type = ) sq USING ( zipk ) WHERE a1.type = ; (Q2**) SELECT a1.word, sq.word FROM S JOIN T a1 USING ( word ) LEFT JOIN ( SELECT * FROM T a2 WHERE a2.type = ) sq USING ( zipk ) --------------------------------------------- (Q1***) SELECT V.word, V.word FROM S JOIN V USING ( word ) LEFT JOIN V USING ( zipk ); (Q2***) SELECT T.word, V.word FROM S JOIN T USING ( word ) LEFT JOIN V USING ( zipk ); ------=_Part_2563_25000795.1203713399292 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Hi.  I'm trying to optimize the performance of a database who= se main purpose is to support two (rather similar) kinds of queries.  = The first kind, which is expected to be the most common (I estimate it will= account for about 90% of all the queries performed on this DB), has the fo= llowing general structure:

(Q1)   SELECT = a1.word, a2.word
         FROM T a1 JOIN T a2 USING ( zipk )
        WHERE a1.type =3D <int1>
          AND a2.type =3D <int2>;

...where <i= nt1> and <int2> stand for some two integers.  In English, thi= s query essentially executes an inner join between two "virtual subtab= les" of table T, which are defined by the value of the = type column.  For brevity, I will refer to these (virtual) subtables = as T<int1> and T<int2>.  (I should point out that T holds = about 2 million records, spread roughly evenly over about 100 values of the=  type column. &nb= sp;So each of these virtual subtables has about 20K records.  Also, fo= r practical purposes T may be regarded as an immutable, read-only table, si= nce it gets re-built from scratch about once a month.  And, FWIW, all = the columns mentioned in this post have a NOT NULL constrai= nt.)

The second form is s= imilar to the first, except that now the join is taken between T and T<i= nt2>:

<= span class=3D"Apple-style-span" style=3D"font-family: 'courier new'= ; font-size: 16px; font-weight: bold; ">(Q2)   SELECT a1.word, a2.word=
         FROM T a1 JOIN T a2 USING ( zipk )=
        WHERE a2.type =3D <int2>;

(Both the form= s above are somewhat oversimplified relative to the actual situation; in ou= r actual application, the joins are actually left outer ones, and each quer= y also involves an additional inner join with another table, S.  For t= he sake of completeness, I give the "real-world" versions of thes= e queries at the end of this post, but I think that for the purpose of my q= uestion, the additional complications they entail can be neglected.)

One way to speed (Q1= ) would be to break T into its subtables, i.e. to create T1, T2, T3, ... , = T100 as bona fide tables.  Then the query would become a simple join w= ithout the two condition of the original's WHERE clause, which I figure= should make it noticeably faster.

But since the second= kind of query (Q2) requires T, we can't get rid of this table, so all = the data would need to be stored twice, once in T and once in some T<int= *>.

In trying to come up= with a way around this duplication, it occurred to me that instead of crea= ting tables T1, T2, etc., I could create the analogous views V1, V2, etc. &= nbsp;(e.g. C= REATE VIEW V1 AS SELECT * FROM T WHERE type =3D 1). &n= bsp;With this design, the two queries above would become

(Q1*)  SELECT V<int1>.word, V<int2&= gt;.word
         FROM V<int1> JOIN V<int2&= gt; USING ( zipk );

(Q2*)  SELECT T.word, V<int2>.word
         FROM T JOIN V<int2> USING ( zipk )= ;

Of course, I e= xpect that using views V<int1> and V<int2>... would result in a= loss in performance relative to a version that used bona fide tables T<= int1> and T<int2>.  My question is, how can I minimize this p= erformance loss?

More specifically, h= ow can I go about building table T and the views V<int?>'s to max= imize the performance of (Q1)?  For example, I'm thinking that if = T had an additional id column and were built in such a wa= y that all the records belonging to each V<int?> were physically cont= iguous, and (say) had contiguous values in the id column, then I could define each view like= this

  CREATE VIEW V<int= 1> AS SELECT * FROM T
 &= nbsp; WHERE <start_int1> <=3D id AND id < <start_int1+1>;=

So my question is, w= hat design would make querying V1, V2, V3 ... as fast as possible?  Is= it possible to approach the performance of the design that uses bona fide = tables T1, T2, T3, ... instead of views V1, V2, V3 ...?

Thank you very much = for reading this long post, and many thanks in advance for your comments!

Kynn
<= br class=3D"webkit-block-placeholder">

P.S.  Her= e are the actual form of the queries.  They now include an initial joi= n with table S, and the join with T<int2> (or V<int2>) is a lef= t outer join.  Interestingly, even though the queries below that use v= iews (i.e. Q1*** and Q2***) are not much more complex-looking than before, = the other two (Q1** and Q2**) are.  I don't know if this is becaus= e my ineptitude with SQL, but I am not able to render (Q1**) and (Q2**) wit= hout resorting to the subquery sq.

= (Q1**)  SELECT a1.wo= rd, sq.word FROM
 &= nbsp;             S      JOIN = T a1 USING ( word )
 &= nbsp;               LEFT JOIN ( SELECT *= FROM T a2
 &= nbsp;                    =       WHERE a2.type =3D <int2> ) sq USING ( zipk )
 &= nbsp;       WHERE a1.type =3D <int1>;

(Q2**)  SELECT a1.word, sq.word FROM=
 &= nbsp;             S      JOIN = T a1 USING ( word )
 &= nbsp;               LEFT JOIN ( SELECT *= FROM T a2
 &= nbsp;                    =       WHERE a2.type =3D <int2> ) sq USING ( zipk )

       ----------------------------= -----------------

(Q1***) SELECT V<int1>.word, V<int2>.word F= ROM
 &= nbsp;             S      JOIN = V<int1> USING ( word )
 &= nbsp;               LEFT JOIN V<int2&= gt; USING ( zipk );

(Q2***) SELECT T.word, V<int2>.word=
 &= nbsp;        FROM S      JOIN T   &= nbsp;   USING ( word )
 &= nbsp;               LEFT JOIN V<int2&= gt; USING ( zipk );


------=_Part_2563_25000795.1203713399292--