X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 395B3DBAF8 for ; Wed, 23 Nov 2005 21:23:54 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 14770-02 for ; Thu, 24 Nov 2005 01:23:56 +0000 (GMT) X-Greylist: delayed 03:08:37.873948 by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id 1E678DBAC1 for ; Wed, 23 Nov 2005 21:23:50 -0400 (AST) Received: from mail.rilk.com (mail.rilk.com [193.19.217.130]) by svr2.postgresql.org (Postfix) with ESMTP id 70A87F0B82 for ; Wed, 23 Nov 2005 22:15:14 +0000 (GMT) Received: from [192.168.0.1] (cev75-1-81-57-249-136.fbx.proxad.net [81.57.249.136]) (authenticated bits=0) by mail.rilk.com (8.13.0/8.13.0) with ESMTP id jANMEmM6016783 (version=TLSv1/SSLv3 cipher=EDH-DSS-DES-CBC3-SHA bits=168 verify=NO) for ; Wed, 23 Nov 2005 23:15:02 +0100 (CET) Mime-Version: 1.0 (Apple Message framework v746.2) Content-Transfer-Encoding: quoted-printable Message-Id: <334D9941-B5B3-4D18-8312-F85D0FB054ED@rilk.com> Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed To: pgsql-performance@postgresql.org From: Pailloncy Jean-Gerard Subject: 8.1 count(*) distinct: IndexScan/SeqScan Date: Wed, 23 Nov 2005 23:14:47 +0100 X-Mailer: Apple Mail (2.746.2) X-DCC-dmv.com-Metrics: mail.rilk.com 1181; Body=1 Fuz1=1 Fuz2=1 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/476 X-Sequence-Number: 15733 Hi, PostgreSQL 8.1 fresh install on a freshly installed OpenBSD 3.8 box. postgres=3D# CREATE DATABASE test; CREATE DATABASE postgres=3D# create table test (id serial, val integer); NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for =20= serial column "test.id" CREATE TABLE postgres=3D# create unique index testid on test (id); CREATE INDEX postgres=3D# create index testval on test (val); CREATE INDEX postgres=3D# insert into test (val) values (round(random()=20 *1024*1024*1024)); INSERT 0 1 [...] insert many random values postgres=3D# vaccum full verbose analyze; postgres=3D# select count(1) from test; count --------- 2097152 (1 row) postgres=3D# explain select count(*) from (select distinct on (val) * =20= from test) as foo; QUERY PLAN ------------------------------------------------------------------------=20= ------------------ Aggregate (cost=3D66328.72..66328.73 rows=3D1 width=3D0) -> Unique (cost=3D0.00..40114.32 rows=3D2097152 width=3D8) -> Index Scan using testval on test (cost=3D0.00..34871.44 =20= rows=3D2097152 width=3D8) (3 rows) postgres=3D# set enable_indexscan=3Doff; postgres=3D# explain analyze select count(*) from (select distinct on =20= (val) * from test) as foo; QUERY PLAN ------------------------------------------------------------------------=20= ------------------------------------------------------------ Aggregate (cost=3D280438.64..280438.65 rows=3D1 width=3D0) (actual =20 time=3D39604.107..39604.108 rows=3D1 loops=3D1) -> Unique (cost=3D243738.48..254224.24 rows=3D2097152 width=3D8) =20= (actual time=3D30281.004..37746.488 rows=3D2095104 loops=3D1) -> Sort (cost=3D243738.48..248981.36 rows=3D2097152 width=3D8)= =20 (actual time=3D30280.999..33744.197 rows=3D2097152 loops=3D1) Sort Key: test.val -> Seq Scan on test (cost=3D0.00..23537.52 =20 rows=3D2097152 width=3D8) (actual time=3D11.550..3262.433 rows=3D2097152 = =20 loops=3D1) Total runtime: 39624.094 ms (6 rows) postgres=3D# set enable_indexscan=3Don; postgres=3D# explain analyze select count(*) from (select distinct on =20= (val) * from test where val<10000000) as foo; =20 QUERY PLAN ------------------------------------------------------------------------=20= ----------------------------------------------------------------------- Aggregate (cost=3D4739.58..4739.59 rows=3D1 width=3D0) (actual =20 time=3D4686.472..4686.473 rows=3D1 loops=3D1) -> Unique (cost=3D4380.56..4483.14 rows=3D20515 width=3D8) (actual = =20 time=3D4609.046..4669.289 rows=3D19237 loops=3D1) -> Sort (cost=3D4380.56..4431.85 rows=3D20515 width=3D8) =20 (actual time=3D4609.041..4627.976 rows=3D19255 loops=3D1) Sort Key: test.val -> Bitmap Heap Scan on test (cost=3D88.80..2911.24 =20 rows=3D20515 width=3D8) (actual time=3D130.954..4559.244 rows=3D19255 = loops=3D1) Recheck Cond: (val < 10000000) -> Bitmap Index Scan on testval =20 (cost=3D0.00..88.80 rows=3D20515 width=3D0) (actual = time=3D120.041..120.041 =20 rows=3D19255 loops=3D1) Index Cond: (val < 10000000) Total runtime: 4690.513 ms (9 rows) postgres=3D# explain select count(*) from (select distinct on (val) * =20= from test where val<100000000) as foo; QUERY PLAN ------------------------------------------------------------------------=20= ----------------- Aggregate (cost=3D16350.20..16350.21 rows=3D1 width=3D0) -> Unique (cost=3D0.00..13748.23 rows=3D208158 width=3D8) -> Index Scan using testval on test (cost=3D0.00..13227.83 =20= rows=3D208158 width=3D8) Index Cond: (val < 100000000) (4 rows) postgres=3D# set enable_indexscan=3Doff; postgres=3D# explain analyze select count(*) from (select distinct on =20= (val) * from test where val<100000000) as foo; =20= QUERY PLAN ------------------------------------------------------------------------=20= ------------------------------------------------------------------------=20= ---- Aggregate (cost=3D28081.27..28081.28 rows=3D1 width=3D0) (actual =20 time=3D6444.650..6444.651 rows=3D1 loops=3D1) -> Unique (cost=3D24438.50..25479.29 rows=3D208158 width=3D8) = (actual =20 time=3D5669.118..6277.206 rows=3D194142 loops=3D1) -> Sort (cost=3D24438.50..24958.89 rows=3D208158 width=3D8) =20= (actual time=3D5669.112..5852.351 rows=3D194342 loops=3D1) Sort Key: test.val -> Bitmap Heap Scan on test (cost=3D882.55..6050.53 =20= rows=3D208158 width=3D8) (actual time=3D1341.114..4989.840 rows=3D194342 = =20 loops=3D1) Recheck Cond: (val < 100000000) -> Bitmap Index Scan on testval =20 (cost=3D0.00..882.55 rows=3D208158 width=3D0) (actual =20 time=3D1339.707..1339.707 rows=3D194342 loops=3D1) Index Cond: (val < 100000000) Total runtime: 6487.114 ms (9 rows) postgres=3D# explain analyze select count(*) from (select distinct on =20= (val) * from test where val<750000000) as foo; Q=20= UERY PLAN ------------------------------------------------------------------------=20= ------------------------------------------------------------------------=20= ------- Aggregate (cost=3D204576.53..204576.54 rows=3D1 width=3D0) (actual =20 time=3D35718.935..35718.936 rows=3D1 loops=3D1) -> Unique (cost=3D178717.28..186105.64 rows=3D1477671 width=3D8) =20= (actual time=3D29465.856..34459.640 rows=3D1462348 loops=3D1) -> Sort (cost=3D178717.28..182411.46 rows=3D1477671 width=3D8)= =20 (actual time=3D29465.853..31658.056 rows=3D1463793 loops=3D1) Sort Key: test.val -> Bitmap Heap Scan on test (cost=3D6256.85..27293.73 =20= rows=3D1477671 width=3D8) (actual time=3D8316.676..11561.018 = rows=3D1463793 =20 loops=3D1) Recheck Cond: (val < 750000000) -> Bitmap Index Scan on testval =20 (cost=3D0.00..6256.85 rows=3D1477671 width=3D0) (actual =20 time=3D8305.963..8305.963 rows=3D1463793 loops=3D1) Index Cond: (val < 750000000) Total runtime: 35736.167 ms (9 rows) postgres=3D# explain analyze select count(*) from (select distinct on =20= (val) * from test where val<800000000) as foo; QUERY PLAN ------------------------------------------------------------------------=20= ------------------------------------------------------------ Aggregate (cost=3D217582.20..217582.21 rows=3D1 width=3D0) (actual =20 time=3D28718.331..28718.332 rows=3D1 loops=3D1) -> Unique (cost=3D190140.72..197981.14 rows=3D1568084 width=3D8) =20= (actual time=3D22175.170..27380.343 rows=3D1559648 loops=3D1) -> Sort (cost=3D190140.72..194060.93 rows=3D1568084 width=3D8)= =20 (actual time=3D22175.165..24451.892 rows=3D1561181 loops=3D1) Sort Key: test.val -> Seq Scan on test (cost=3D0.00..28780.40 =20 rows=3D1568084 width=3D8) (actual time=3D13.130..3358.923 rows=3D1561181 = =20 loops=3D1) Filter: (val < 800000000) Total runtime: 28735.264 ms (7 rows) I did not post any result for the indexscan plan, because it takes to =20= much time. Why the stupid indexscan plan on the whole table ? Cordialement, Jean-G=E9rard Pailloncy