Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 69D35B5DBD9 for ; Thu, 16 Jun 2011 10:55:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 22588-07 for ; Thu, 16 Jun 2011 13:55:31 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f46.google.com (mail-fx0-f46.google.com [209.85.161.46]) by mail.postgresql.org (Postfix) with ESMTP id 6C345B5DBC0 for ; Thu, 16 Jun 2011 10:55:31 -0300 (ADT) Received: by fxm17 with SMTP id 17so1123192fxm.19 for ; Thu, 16 Jun 2011 06:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=JlLa3Ez6ALQ1bzBNphFYDH0kOmvO7T5pmPi1Hw/GsQ4=; b=gtWQIBZHKVL0PTctfekebm6M4SOBtbRMZZ/5El2mXDP79b7Bo0CeFNlSifZeCvYjqJ h9+1xlnhIwRKp4LyawdRt4L9ycUIzYwMjWeoPmez/7a6UegzQsc7eXg6hOg620i2FPNk ZFJDSS45fIe4yLua9s2TaNRARnfxZUdJQSaMo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=Jdc3ztsT9SIFz8SgcYH5DjU2b+hXPL/B5QERCfQWPqywevZL3idSP77DoUFa6LAUpn 3PLgLpEBvii0KDc9hMPfsAGvKSF9NmRqhUum8OGGweJcMeMFX8j1ZbzDNEpm7Be9qAmS jb6S1v+OEZzKlaNcXZ3lmBI+M0roDlkvKK9uI= MIME-Version: 1.0 Received: by 10.223.4.136 with SMTP id 8mr1180370far.16.1308232531020; Thu, 16 Jun 2011 06:55:31 -0700 (PDT) Received: by 10.223.149.20 with HTTP; Thu, 16 Jun 2011 06:55:30 -0700 (PDT) Date: Thu, 16 Jun 2011 14:55:30 +0100 Message-ID: Subject: seq scan in the case of max() on the primary key column From: Svetlin Manavski To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=000e0ce006884b8b1304a5d49f40 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.572 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RFC_ABUSE_POST=0.001, SARE_MILLIONSOF=0.315, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201106/138 X-Sequence-Number: 44071 --000e0ce006884b8b1304a5d49f40 Content-Type: text/plain; charset=ISO-8859-1 Hi everybody, I am running PostgreSQL 9.0 which performs well in most of the cases. I would skip all the parameters if these are not necessary. I need to frequently (every min) get the max value of the primary key column on some tables, like this case which works perfectly well: explain analyze select max(id) from appqosdata.tcpsessions; ------------------------------------------------------------------------------------------------------------------------------------------------------------------- Result (cost=0.49..0.50 rows=1 width=0) (actual time=45.316..45.317 rows=1 loops=1) InitPlan 1 (returns $0) -> Limit (cost=0.00..0.49 rows=1 width=8) (actual time=45.302..45.303 rows=1 loops=1) -> Index Scan Backward using idx_tcpsessions_id on tcpsessions (cost=0.00..6633362.76 rows=13459023 width=8) (actual time=45.296..45.296 rows=1 loops=1) Index Cond: (id IS NOT NULL) Total runtime: 45.399 ms But I have the following similar case which surprises me quite a lot: explain analyze select max(createdtime) from appqosdata.tcpsessiondata; ------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=1123868.30..1123868.31 rows=1 width=8) (actual time=376932.636..376932.637 rows=1 loops=1) -> Append (cost=0.00..965113.04 rows=63502104 width=8) (actual time=0.020..304844.944 rows=63501281 loops=1) -> Seq Scan on tcpsessiondata (cost=0.00..12.80 rows=780 width=8) (actual time=0.002..0.002 rows=0 loops=1) -> Seq Scan on tcpsessiondata_default tcpsessiondata (cost=0.00..965100.24 rows=63501324 width=8) (actual time=0.015..173159.505 rows=63501281 loops=1) Total runtime: 376980.975 ms I have the following table definitions: CREATE TABLE appqosdata.tcpsessiondata_default ( Primary key(createdtime), --bigint check (sessionid >= 0), Foreign key(detectorid, sessionid) References appqosdata.tcpsessions(detectorid,id) ) inherits (appqosdata.tcpsessiondata); CREATE TABLE appqosdata.tcpsessions ( detectorid smallint not null default(0) references appqosdata.detectors(id), id bigint not null, ... primary key(detectorid, id) ); As you can see I have tens of millions of rows in both tables which would be ten times more in production. So seq scan is not acceptable at all to get one single value. Why that difference and what can I do to make the first query use its index on the primary key. Thank you, Svetlin Manavski --000e0ce006884b8b1304a5d49f40 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi everybody,

I am running PostgreSQL 9.0= which performs well in most of the cases. I would skip all the parameters = if these are not necessary.

I need to frequently (= every min) get the max value of the primary key column on some tables, like= this case which works perfectly well:

explain analyze select max(id) from appqosdata.tcp= sessions;

---------------------------------------------------= ---------------------------------------------------------------------------= -------------------------------------
=A0Result (cost=3D0.49..0.50 rows=3D1 width=3D0) (actual time=3D45.31= 6..45.317 rows=3D1 loops=3D1) InitPlan 1 (returns $0)
-> Limit = (cost=3D0.00..0.49 rows=3D1 width=3D8) (actual time=3D45.302..45.303 rows= =3D1 loops=3D1)
=A0 -> Index Scan Backward using idx_tcpses= sions_id on tcpsessions (cost=3D0.00..6633362.76 rows=3D13459023 width=3D8= ) (actual time=3D45.296..45.296 rows=3D1 loops=3D1)
Index Cond: (id IS NOT NULL)
Total runtime: 45.399 ms<= br>

But I have the following similar case wh= ich surprises me quite a lot:

explain analyze sele= ct max(createdtime) from appqosdata.tcpsessiondata;

-------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------
Aggregate (cost=3D1123868.30..112386= 8.31 rows=3D1 width=3D8) (actual time=3D376932.636..376932.637 rows=3D1 loo= ps=3D1)
-> Append (cost=3D0.00..965113.04 rows=3D63502104 width=3D8) (actua= l time=3D0.020..304844.944 rows=3D63501281 loops=3D1)
-> Se= q Scan on tcpsessiondata (cost=3D0.00..12.80 rows=3D780 width=3D8) (actual= time=3D0.002..0.002 rows=3D0 loops=3D1)
-> Seq Scan on tcpsessiondata_default tcpsessiondata (cost=3D= 0.00..965100.24 rows=3D63501324 width=3D8) (actual time=3D0.015..173159.505= rows=3D63501281 loops=3D1)
Total runtime: 376980.975 ms
=
I have the following table definitions:

CREAT= E TABLE appqosdata.tcpsessiondata_default
(
=A0Primary key(createdti= me), --bigint
check (sessionid >=3D 0),

=A0Foreign key(detect= orid, sessionid) References appqosdata.tcpsessions(detectorid,id)
=A0
) inherits (appqosdata.tcpsessiondata);

CREATE TABLE a= ppqosdata.tcpsessions
(
detectorid smallint not null default(0) refe= rences appqosdata.detectors(id),
id bigint not null,

=A0.= ..

primary key(detectorid, id)
);

As you can see= I have tens of millions of rows in both tables which would be ten times mo= re in production. So seq scan is not acceptable at all to get one single va= lue.
Why that difference and what can I do to make the first query use its = index on the primary key.

Thank you,
Sve= tlin Manavski

--000e0ce006884b8b1304a5d49f40--