X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 4EAD2D1B465 for ; Wed, 3 Dec 2003 10:55:26 +0000 (GMT) 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 00556-09 for ; Wed, 3 Dec 2003 06:54:57 -0400 (AST) Received: from smtp0.telegraaf.net (smtp0.telegraaf.net [217.196.45.130]) by svr1.postgresql.org (Postfix) with ESMTP id C5688D1D30D for ; Wed, 3 Dec 2003 06:54:53 -0400 (AST) Received: from server9.telegraafnet.nl (server9.telegraafnet.nl [192.168.1.9]) by smtp0.telegraaf.net (Postfix) with ESMTP id 8431D43A92 for ; Wed, 3 Dec 2003 11:54:50 +0100 (CET) Received: from c22601.ws.tem.nl (c22601.ws.tem.nl [192.168.1.61]) by server9.telegraafnet.nl (8.12.7/8.12.7/Debian-2) with ESMTP id hB3Asnie029535 for ; Wed, 3 Dec 2003 11:54:50 +0100 From: Ferdinand Smit To: pgsql-admin@postgresql.org Subject: Index problems Date: Wed, 3 Dec 2003 11:54:48 +0100 User-Agent: KMail/1.5.4 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200312031154.48776.ferdinand@telegraafnet.nl> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/23 X-Sequence-Number: 11531 Hi, When trying to explain a developer of our organisation the reson why the index was'nt used, i was confused my self. The simple question is: Why does the analyzer only use the index when the table has a few differend values ? As you can see, when de seqscan is off, the query is mutch faster. It does'nt matter how big the table is, i've tried this with 100 times more, the result is the same. I've tried this on version 7.3.2 and 7.3.4. Regards, Ferdinand Smit ========== test=# SELECT r, count(r) FROM test GROUP BY r; r | count ----+------- 0 | 1344 1 | 288 2 | 440 3 | 1096 4 | 256 5 | 696 6 | 2048 7 | 512 8 | 6424 9 | 128 10 | 976 11 | 16 12 | 1488 13 | 80 14 | 136 15 | 112 16 | 56 17 | 56 18 | 16 19 | 184 (20 rows) test=# VACUUM FULL ANALYZE test; VACUUM test=# EXPLAIN ANALYZE SELECT * FROM test WHERE r = 13; QUERY PLAN -------------------------------------------------------------------------------------------------- Seq Scan on test (cost=0.00..277.40 rows=207 width=4) (actual time=0.05..24.19 rows=80 loops=1) Filter: (r = 13) Total runtime: 24.31 msec (3 rows) test=# SET enable_seqscan = off; SET test=# EXPLAIN ANALYZE SELECT * FROM test WHERE r = 13; QUERY PLAN -------------------------------------------------------------------------------------------------------------------- Index Scan using key_regio2 on test (cost=0.00..285.83 rows=207 width=4) (actual time=0.30..0.73 rows=80 loops=1) Index Cond: (r = 13) Total runtime: 0.87 msec (3 rows) test=# SET enable_seqscan = on; SET test=# DELETE FROM test WHERE r > 10; DELETE 2144 test=# VACUUM FULL ANALYZE test; VACUUM test=# EXPLAIN ANALYZE SELECT * FROM test WHERE r = 13; QUERY PLAN -------------------------------------------------------------------------------------------------- Seq Scan on test (cost=0.00..240.60 rows=638 width=4) (actual time=20.87..20.87 rows=0 loops=1) Filter: (r = 13) Total runtime: 20.93 msec (3 rows) test=# DELETE FROM test WHERE r > 5; DELETE 10088 test=# VACUUM FULL ANALYZE test; VACUUM test=# EXPLAIN ANALYZE SELECT * FROM test WHERE r = 13; QUERY PLAN --------------------------------------------------------------------------------------------------------------- Index Scan using key_regio2 on test (cost=0.00..5.69 rows=1 width=4) (actual time=0.25..0.25 rows=0 loops=1) Index Cond: (r = 13) Total runtime: 0.33 msec (3 rows)