X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 673C3528D6 for ; Mon, 16 May 2005 15:11:54 -0300 (ADT) 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 54277-01 for ; Mon, 16 May 2005 18:11:49 +0000 (GMT) Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id E8D8252911 for ; Mon, 16 May 2005 15:11:35 -0300 (ADT) Received: from relay.oganer.net (relay.oganer.net [217.150.206.19]) by svr2.postgresql.org (Postfix) with ESMTP id E1A30F0B36 for ; Mon, 16 May 2005 12:36:36 +0100 (BST) Received: from forest.bear.oganer.net (forest.bear.oganer.net [192.168.114.31] (may be forged)) by relay.oganer.net (8.13.0/8.13.0) with ESMTP id j4GBdep4027023 for ; Mon, 16 May 2005 19:39:40 +0800 (KRAST) Date: Mon, 16 May 2005 19:39:40 +0800 From: "Ilya A. Kovalenko" Reply-To: "Ilya A. Kovalenko" Organization: JSC Oganer-Service X-Priority: 3 (Normal) Message-ID: <551153969240.20050516193940@oganer.net> To: pgsql-sql@postgresql.org Subject: choosing index to use MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.487 tagged_above=0 required=5 tests=AWL, HOT_NASTY, PRIORITY_NO_NAME X-Spam-Level: * X-Archive-Number: 200505/109 X-Sequence-Number: 21422 Greetings, How can I control which indexes will or won't be used by query ? I never think, that I'll need something like this. Short version: Simple SELECT query perfomance speeds up (10x-20x) after _removing_ one of indexes. Because (as EXPLAIN shows), after removing, query switches to another index. How to make such without index removing ? PostgreSQL 8.0.2, OpenBSD 3.7, i386 Thank you. Ilya A. Kovalenko Special EQ SW section JSC Oganer-Service Details: CREATE TABLE traffic_stat ( time timestamptz NOT NULL, client inet NOT NULL, remote inet NOT NULL, count_in int8 NOT NULL, count_out int8 NOT NULL ) WITHOUT OIDS; CREATE INDEX traffic_client_idx ON traffic_stat USING btree (client); CREATE INDEX traffic_date_idx ON traffic_stat USING btree ("time"); CREATE INDEX traffic_remote_idx ON traffic_stat USING btree (remote); CREATE INDEX traffic_multy_idx ON traffic_stat USING btree ("time", client, remote); CREATE INDEX traffic_date_client_idx ON traffic_stat USING btree ("time", client); SELECT count(*) FROM traffic_stat; 135511 Query is: SELECT to_char(time, 'DD.MM.YYYY HH24:MI.SS'), remote, count_in, count_out FROM traffic_stat WHERE client = '192.168.xxx.xxx' AND time > '2005-05-16' AND time < '2005-05-16'::date + '1 days'::interval ORDER BY time; Case 1: SELECT ... Total query runtime: 2643 ms. Data retrieval runtime: 20 ms. 39 rows retrieved. EXPLAIN SELECT ... Index Scan using traffic_date_idx on traffic_stat (cost=0.00..3.08 rows=1 width=35) Index Cond: (("time" > '2005-05-16 00:00:00+08'::timestamp with time zone) AND ("time" < '2005-05-17 00:00:00'::timestamp without time zone)) Filter: (client = '192.168.114.31'::inet) Case 2: DROP INDEX traffic_date_idx; SELECT ... Total query runtime: 290 ms. Data retrieval runtime: 20 ms. 41 rows retrieved. EXPLAIN SELECT ... Index Scan using traffic_date_client_idx on traffic_stat (cost=0.00..4.37 rows=1 width=35) Index Cond: (("time" > '2005-05-16 00:00:00+08'::timestamp with time zone) AND ("time" < '2005-05-17 00:00:00'::timestamp without time zone) AND (client = '192.168.114.31'::inet))