X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id CCA5DD1B1DD for ; Fri, 14 May 2004 05:45:27 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 46866-09 for ; Fri, 14 May 2004 05:45:09 -0300 (ADT) Received: from mail.interlogica.org (host34-209.pool8174.interbusiness.it [81.74.209.34]) by svr1.postgresql.org (Postfix) with ESMTP id 0BC4AD1B1C8 for ; Fri, 14 May 2004 05:45:06 -0300 (ADT) Received: from [10.2.8.2] (helo=ilbug) by mail.interlogica.org with esmtp (Exim 3.16 #1) id 1BOYQJ-000890-00; Fri, 14 May 2004 10:52:07 +0200 From: "Fabio Panizzutti" To: "'Stephan Szabo'" Cc: Subject: R: R: Query plan on identical tables differs . Why ? Date: Fri, 14 May 2004 10:55:07 +0200 Message-ID: <001001c43991$284506b0$3c02020a@ufficio> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.4024 In-Reply-To: <20040513080130.E3465@megazone.bigpanda.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200405/135 X-Sequence-Number: 6966 >>>-----Messaggio originale----- >>>Da: Stephan Szabo [mailto:sszabo@megazone.bigpanda.com]=20 >>>Inviato: gioved=EC 13 maggio 2004 17.17 >>>A: Fabio Panizzutti >>>Cc: 'Shridhar Daithankar'; pgsql-performance@postgresql.org >>>Oggetto: Re: R: [PERFORM] Query plan on identical tables=20 >>>differs . Why ? >>> >>> >>>On Thu, 13 May 2004, Fabio Panizzutti wrote: >>> >>> >>>> I don't understand why the planner chose a different query plan on=20 >>>> identical tables with same indexes . >>> >>>Because it's more than table structure that affects the=20 >>>choice made by the planner. In addition the statistics=20 >>>about the values that are there as well as the estimated=20 >>>size of the table have effects. One way to see is to see=20 >>>what it thinks is best is to remove the indexes it is using=20 >>>and see what plan it gives then, how long it takes and the=20 >>>estimated costs for those plans. >>> >>>In other suggestions, I think having a (tag_id, data_tag)=20 >>>index rather than (data_tag, tag_id) may be a win for=20 >>>queries like this. Also, unless you're doing many select=20 >>>queries by only the first field of the composite index and=20 >>>you're not doing very many insert/update/deletes, you may=20 >>>want to drop the other index on just that field. >>> Thanks for your attention , i change the indexes on the tables as you suggested : storico=3D# \d storico_misure_short Table "tenore.storico_misure_short" Column | Type | Modifiers -------------------------+-----------------------------+----------- data_tag | timestamp without time zone | not null tag_id | integer | not null unita_misura | character varying(6) | not null valore_tag | numeric(20,3) | not null qualita | integer | not null numero_campioni | numeric(5,0) | frequenza_campionamento | numeric(3,0) | Indexes: "storico_misure_short_idx" primary key, btree (tag_id, data_tag) "storico_misure_short_data_tag_idx2" btree (data_tag) storico=3D# \d storico_misure Table "tenore.storico_misure" Column | Type | Modifiers -------------------------+-----------------------------+----------- data_tag | timestamp without time zone | not null tag_id | integer | not null unita_misura | character varying(6) | not null valore_tag | numeric(20,3) | not null qualita | integer | not null numero_campioni | numeric(5,0) | frequenza_campionamento | numeric(3,0) | Indexes: "storico_misure_idx" primary key, btree (tag_id, data_tag) "storico_misure_data_tag_idx2" btree (data_tag) And now performance are similar and the planner works correctly : storico=3D# \d storico_misure_short Table "tenore.storico_misure_short" Column | Type | Modifiers -------------------------+-----------------------------+----------- data_tag | timestamp without time zone | not null tag_id | integer | not null unita_misura | character varying(6) | not null valore_tag | numeric(20,3) | not null qualita | integer | not null numero_campioni | numeric(5,0) | frequenza_campionamento | numeric(3,0) | Indexes: "storico_misure_short_idx" primary key, btree (tag_id, data_tag) "storico_misure_short_data_tag_idx2" btree (data_tag) storico=3D# \d storico_misure Table "tenore.storico_misure" Column | Type | Modifiers -------------------------+-----------------------------+----------- data_tag | timestamp without time zone | not null tag_id | integer | not null unita_misura | character varying(6) | not null valore_tag | numeric(20,3) | not null qualita | integer | not null numero_campioni | numeric(5,0) | frequenza_campionamento | numeric(3,0) | Indexes: "storico_misure_idx" primary key, btree (tag_id, data_tag) "storico_misure_data_tag_idx2" btree (data_tag) storico=3D# explain analyze select tag_id,valore_tag,data_tag from storico_misure_short where (data_tag>'2004-05-03' and data_tag <'2004-05-12') and tag_id=3D37423 ; =20 QUERY PLAN ------------------------------------------------------------------------ ------------------------------------------------------------------------ -------------------------- Index Scan using storico_misure_short_idx on storico_misure_short (cost=3D0.00..2104.47 rows=3D584 width=3D20) (actual time=3D0.232..39.932 rows=3D864 loops=3D1) Index Cond: ((tag_id =3D 37423) AND (data_tag > '2004-05-03 00:00:00'::timestamp without time zone) AND (data_tag < '2004-05-12 00:00:00'::timestamp without time zone)) Total runtime: 40.912 ms (3 rows) Time: 43,233 ms storico=3D# explain analyze select tag_id,valore_tag,data_tag from storico_misure where (data_tag>'2004-05-03' and data_tag <'2004-05-12') and tag_id=3D37423 ; =20 QUERY PLAN ------------------------------------------------------------------------ ------------------------------------------------------------------------ -------------------------- Index Scan using storico_misure_idx on storico_misure (cost=3D0.00..2097.56 rows=3D547 width=3D21) (actual time=3D0.518..92.067 rows=3D835 loops=3D1) Index Cond: ((tag_id =3D 37423) AND (data_tag > '2004-05-03 00:00:00'::timestamp without time zone) AND (data_tag < '2004-05-12 00:00:00'::timestamp without time zone)) Total runtime: 93.459 ms (3 rows) I need the index on data_tag for other query ( last values on the last date ) . Regards=20 Fabio=20