X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 70E369DCBE9 for ; Sun, 11 Dec 2005 22:07:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 99368-01 for ; Sun, 11 Dec 2005 22:08:02 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.194]) by postgresql.org (Postfix) with ESMTP id 1A15A9DCBC1 for ; Sun, 11 Dec 2005 22:07:55 -0400 (AST) Received: by zproxy.gmail.com with SMTP id 16so655015nzp for ; Sun, 11 Dec 2005 18:07:59 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=uALwugr/kaW2UVVk58y43zYxj81mTentErZN9yARDiXwdwUYAPPlxixHus4k8MtArjyFscjtsqpZKwedc/q5TcXQasEEKV/qt6N57SWxwDF9dA/cNjM25dZJdNS0y1HD5m9mbdMlBBJYbGRMIKP4GkMLsMp3uNdAUrWoMG+ml5Y= Received: by 10.65.113.8 with SMTP id q8mr142722qbm; Sun, 11 Dec 2005 18:07:59 -0800 (PST) Received: by 10.65.160.12 with HTTP; Sun, 11 Dec 2005 18:07:59 -0800 (PST) Message-ID: Date: Mon, 12 Dec 2005 15:07:59 +1300 From: Mike C To: pgsql-performance@postgresql.org Subject: Table Partitions / Partial Indexes MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_5767_5091265.1134353279570" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.001 required=5 tests=[HTML_MESSAGE=0.001] X-Spam-Score: 0.001 X-Spam-Level: X-Archive-Number: 200512/285 X-Sequence-Number: 16106 ------=_Part_5767_5091265.1134353279570 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hello, I've got a table with ~60 Million rows and am having performance problems querying it. Disks are setup as 4x10K SCSI 76GB, RAID 1+0. The table is being inserted into multiple times every second of the day, with no updates and every 2nd day we delete 1/60th of the data (as it becomes old). Vacuum analyze is scheduled to run 3 times a day. Query: select sum(TOTAL_FROM) as TOTAL_IN, sum(TOTAL_TO) as TOTAL_OUT, SOURCE_MAC from PC_TRAFFIC where FK_DEVICE =3D 996 and TRAFFIC_DATE >=3D '2005-10-14 00:00:00' and TRAFFIC_DATE <=3D '2005-11-13 23:59:59' group by SOURCE_MAC order by 1 desc Table: CREATE TABLE PC_TRAFFIC ( PK_PC_TRAFFIC INTEGER NOT NULL, TRAFFIC_DATE TIMESTAMP NOT NULL, SOURCE_MAC CHAR(20) NOT NULL, DEST_IP CHAR(15), DEST_PORT INTEGER, TOTAL_TO DOUBLE PRECISION, TOTAL_FROM DOUBLE PRECISION, FK_DEVICE SMALLINT, PROTOCOL_TYPE SMALLINT ); CREATE INDEX pc_traffic_pkidx ON pc_traffic (pk_pc_traffic); CREATE INDEX pc_traffic_idx3 ON pc_traffic (fk_device, traffic_date); Plan: Sort (cost=3D76650.58..76650.58 rows=3D2 width=3D40) Sort Key: sum(total_from) -> HashAggregate (cost=3D76650.54..76650.57 rows=3D2 width=3D40) -> Bitmap Heap Scan on pc_traffic=20 (cost=3D534.64..76327.03rows=3D43134 width=3D40) Recheck Cond: ((fk_device =3D 996) AND (traffic_date >=3D '2005-10-01 00:00:00'::timestamp without time zone) AND (traffic_date <=3D '2005-10-31 23:59:59'::timestamp without time zone)) -> Bitmap Index Scan on pc_traffic_idx3=20 (cost=3D0.00..534.64rows=3D43134 width=3D0) Index Cond: ((fk_device =3D 996) AND (traffic_date >= =3D '2005-10-01 00:00:00'::timestamp without time zone) AND (traffic_date <=3D '2005-10-31 23:59:59'::timestamp without time zone)) (7 rows) CLUSTER on PC_TRAFFIC_IDX3 gives me significantly improved performance: Sort (cost=3D39886.65..39886.66 rows=3D2 width=3D40) Sort Key: sum(total_from) -> HashAggregate (cost=3D39886.61..39886.64 rows=3D2 width=3D40) -> Index Scan using pc_traffic_idx3 on pc_traffic (cost=3D 0.00..39551.26 rows=3D44714 width=3D40) Index Cond: ((fk_device =3D 996) AND (traffic_date >=3D '2005-10-01 00:00:00'::timestamp without time zone) AND (traffic_date <=3D '2005-10-31 23:59:59'::timestamp without time zone)) (5 rows) However the clustering is only effective on the first shot. Because of the constant usage of the table we can't perform a vacuum full nor any exclusiv= e lock function. Would table partitioning/partial indexes help much? Partitioning on date range doesn't make much sense for this setup, where a typical 1-month query spans both tables (as the billing month for the customer might start midway through a calendar month). Noting that the index scan was quicker than the bitmap, I'm trying to make the indexes smaller/more likely to index scan. I have tried partitioning against fk_device, with 10 child tables. I'm using fk_device % 10 =3D 1, fk_device % 10 =3D 2, fk_device % 10 =3D 3, etc... as the check constraint. CREATE TABLE pc_traffic_0 (CHECK(FK_DEVICE % 10 =3D 0)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_1 (CHECK(FK_DEVICE % 10 =3D 1)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_2 (CHECK(FK_DEVICE % 10 =3D 2)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_3 (CHECK(FK_DEVICE % 10 =3D 3)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_4 (CHECK(FK_DEVICE % 10 =3D 4)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_5 (CHECK(FK_DEVICE % 10 =3D 5)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_6 (CHECK(FK_DEVICE % 10 =3D 6)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_7 (CHECK(FK_DEVICE % 10 =3D 7)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_8 (CHECK(FK_DEVICE % 10 =3D 8)) INHERITS (pc_traffi= c); CREATE TABLE pc_traffic_9 (CHECK(FK_DEVICE % 10 =3D 9)) INHERITS (pc_traffi= c); ... indexes now look like: CREATE INDEX pc_traffic_6_idx3 ON pc_traffic_6 (fk_device, traffic_date); To take advantage of the query my SQL now has to include the mod operation (so the query planner picks up the correct child tables): select sum(TOTAL_FROM) as TOTAL_IN, sum(TOTAL_TO) as TOTAL_OUT, SOURCE_MAC from PC_TRAFFIC where FK_DEVICE =3D 996 and FK_DEVICE % 10 =3D 6 and TRAFFIC_DATE >=3D '2005-10-14 00:00:00' and TRAFFIC_DATE <=3D '2005-11-13 23:59:59' group by SOURCE_MAC order by 1 desc Sorry I would show the plan but I'm rebuilding the dev database atm. It was faster though and did pick up the correct child table. It was also a bitmap scan on the index IIRC. Would I be better off creating many partial indexes instead of multiple tables AND multiple indexes? Am I using a horrid method for partitioning the data? (% 10) Should there be that big of an improvement for multiple tables given that all the data is still stored on the same filesystem? Any advice on table splitting much appreciated. Cheers, Mike C. ------=_Part_5767_5091265.1134353279570 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hello,

I've got a table with ~60 Million rows and am having performance problems querying it. Disks are setup as 4x10K SCSI 76GB, RAID 1+0. The table is being inserted into multiple times every second of the day, with no updates and every 2nd day we delete 1/60th of the data (as it becomes old). Vacuum analyze is scheduled to run 3 times a day.

Query:

select sum(TOTAL_FROM) as TOTAL_IN, sum(TOTAL_TO) as TOTAL_OUT, SOURCE_MAC from PC_TRAFFIC where FK_DEVICE =3D 996 and TRAFFIC_DATE >=3D '2005-10-14 00:00:00' and TRAFFIC_DATE <=3D '2005-11-13 23:59:59' group by SOURCE_MAC order by 1 desc

Ta= ble:

CREATE TABLE PC_TRAFFIC (
     PK_PC_TRA= FFIC  INTEGER NOT NULL,
     TRAFFIC_DATE&= nbsp;  TIMESTAMP NOT NULL,
     SOURCE_MAC = ;    CHAR(20) NOT NULL,
     DEST_IP      &nb= sp; CHAR(15),
     DEST_PORT   &= nbsp;  INTEGER,
     TOTAL_TO  &= nbsp;    DOUBLE PRECISION,
     TOTAL= _FROM     DOUBLE PRECISION,
    = FK_DEVICE      SMALLINT,
  &nbs= p;  PROTOCOL_TYPE  SMALLINT
);

CREATE INDEX pc_traffic_pkidx ON pc_traffic (pk_pc_traffic);
CREATE = INDEX pc_traffic_idx3 ON pc_traffic (fk_device, traffic_date);

Plan:=
Sort  (cost=3D76650.58..76650.58 rows=3D2 width=3D40)
&nbs= p;  Sort Key: sum(total_from)
   ->  HashAggregate  (cost=3D76650.54= ..76650.57 rows=3D2 width=3D40)
      &nbs= p;  ->  Bitmap Heap Scan on pc_traffic  (cost=3D534.64..76327.03 rows=3D43134 width=3D40)
=             &nb= sp;  Recheck Cond: ((fk_device =3D 996) AND (traffic_date >=3D '2005-10-01 00:00:00'::timestamp without time zone) AND (traffic_date <=3D '2005-10-31 23:59:59'::timestamp without time zone))
   &= nbsp;           ->  Bitmap Index Scan on pc_traffic_idx3  (cost=3D0.00..534.64 rows=3D43134 width=3D0)
=             &nb= sp;        Index Cond: ((fk_device =3D 996) AND (traffic_date >=3D '2005-10-01 00:00:00'::timestamp without time zone) AND (traffic_date <=3D '2005-10-31 23:59:59'::timestamp without time zone))
(7 rows)

CLU= STER on PC_TRAFFIC_IDX3 gives me significantly improved performance:
Sort  (cost=3D39886.65..39886.66 rows=3D2 width=3D40)
 =   Sort Key: sum(total_from)
   ->  HashAggregate  (cost=3D39886.61= ..39886.64 rows=3D2 width=3D40)
      &nbs= p;  ->  Index Scan using pc_traffic_idx3 on pc_traffic  (cost=3D0.00..39551.26 rows=3D44714 width=3D40)
&n= bsp;            = ;  Index Cond: ((fk_device =3D 996) AND (traffic_date >=3D '2005-10-01 00:00:00'::timestamp without time zone) AND (traffic_date <=3D '2005-10-31 23:59:59'::timestamp without time zone))
(5 rows)

How= ever the clustering is only effective on the first shot. Because of the constant usage of the table we can't perform a vacuum full nor any exclusive lock function.

Would table partitioning/partial indexes help much? Partitioning on date range doesn't make much sense for this setup, where a typical 1-month query spans both tables (as the billing month for the customer might start midway through a calendar month).

Noting that the index scan was quicker than the bitmap, I'm trying to make the indexes smaller/more likely to index scan. I have tried partitioning against fk_device, with 10 child tables. I'm using fk_device % 10 =3D 1, fk_device % 10 =3D 2, fk_device % 10 =3D 3, etc... as the check constraint.

CREATE TABLE pc_traffic_0 (CHECK(FK_= DEVICE % 10 =3D 0)) INHERITS (pc_traffic);
CREATE TABLE pc_traffic_1 (CH= ECK(FK_DEVICE % 10 =3D 1)) INHERITS (pc_traffic);
CREATE TABLE pc_traffi= c_2 (CHECK(FK_DEVICE % 10 =3D 2)) INHERITS (pc_traffic);
CREATE TABLE pc_traffic_3 (CHECK(FK_DEVICE % 10 =3D 3)) INHERITS (pc_tr= affic);
CREATE TABLE pc_traffic_4 (CHECK(FK_DEVICE % 10 =3D 4)) INHERITS= (pc_traffic);
CREATE TABLE pc_traffic_5 (CHECK(FK_DEVICE % 10 =3D 5)) I= NHERITS (pc_traffic);
CREATE TABLE pc_traffic_6 (CHECK(FK_DEVICE % 10 =3D 6)) INHERITS (pc_tr= affic);
CREATE TABLE pc_traffic_7 (CHECK(FK_DEVICE % 10 =3D 7)) INHERITS= (pc_traffic);
CREATE TABLE pc_traffic_8 (CHECK(FK_DEVICE % 10 =3D 8)) I= NHERITS (pc_traffic);
CREATE TABLE pc_traffic_9 (CHECK(FK_DEVICE % 10 =3D 9)) INHERITS (pc_tr= affic);

... indexes now look like:
CREATE INDEX pc_traffic_6_idx3= ON pc_traffic_6 (fk_device, traffic_date);

To take advantage of the query my SQL now has to include the mod operation (so the query planner picks up the correct child tables):

select sum(TOTAL_FROM) as TOTAL_IN, sum(TOTAL_TO) as TOTAL_OUT, SOURCE_MAC from PC_TRAFFIC where FK_DEVICE =3D 996 and FK_DEVICE % 10 =3D 6 and TRAFFIC_DATE >=3D '2005-10-14 00:00:00' and TRAFFIC_DATE <=3D '2005-11-13 23:59:59' group by SOURCE_MAC order by 1 desc

Sorry I would show the plan but I'm rebuilding the dev database atm. It was faster though and did pick up the correct child table. It was also a bitmap scan on the index IIRC.

Would I be better off creating many p= artial indexes instead of multiple tables AND multiple indexes?
Am I usi= ng a horrid method for partitioning the data? (% 10)
Should there be that big of an improvement for multiple tables given that a= ll the data is still stored on the same filesystem?
Any advice on table splitting much appreciated.

Cheers,

Mike= C.

------=_Part_5767_5091265.1134353279570--