X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 595CC8B9E1E for ; Wed, 23 Feb 2005 01:45:58 +0000 (GMT) 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 89109-02 for ; Wed, 23 Feb 2005 01:45:51 +0000 (GMT) Received: from mailhub.lss.emc.com (mailhub.lss.emc.com [168.159.2.31]) by svr1.postgresql.org (Postfix) with ESMTP id 2F7538B9E70 for ; Wed, 23 Feb 2005 01:45:53 +0000 (GMT) Received: from mxic2.corp.emc.com (mxic2.corp.emc.com [128.221.12.9]) by mailhub.lss.emc.com (Switch-3.1.6/Switch-3.1.6) with ESMTP id j1N1jpjk016773 for ; Tue, 22 Feb 2005 20:45:51 -0500 (EST) Received: by mxic2.corp.emc.com with Internet Mail Service (5.5.2653.19) id ; Tue, 22 Feb 2005 20:45:50 -0500 Message-ID: <18C6A5982F816D47BFEF7DF47F7B03232E8BD2@corpmx2.corp.emc.com> From: Butkus_Charles@emc.com To: pgsql-performance@postgresql.org Subject: Joins, Deletes and Indexes Date: Tue, 22 Feb 2005 20:45:44 -0500 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain X-PerlMx-Spam: Gauge=, SPAM=7%, Reasons='NO_REAL_NAME 0, __CT 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __IMS_MSGID 0, __IMS_MUA 0, __MIME_VERSION 0, __SANE_MSGID 0' X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.178 tagged_above=0 required=5 tests=NO_REAL_NAME X-Spam-Level: X-Archive-Number: 200502/308 X-Sequence-Number: 10637 I've got 2 tables defined as follows: CREATE TABLE "cluster" ( id int8 NOT NULL DEFAULT nextval('serial'::text), clusterid varchar(255) NOT NULL, ... CONSTRAINT pk_cluster PRIMARY KEY (id) ) CREATE TABLE sensorreport ( id int8 NOT NULL DEFAULT nextval('serial'::text), clusterid int8 NOT NULL, ... CONSTRAINT pk_sensorreport PRIMARY KEY (id), CONSTRAINT fk_sensorreport_clusterid FOREIGN KEY (clusterid) REFERENCES "cluster" (id) ON UPDATE RESTRICT ON DELETE RESTRICT ) I've defined an Index on the clusterid field of sensorreport. So I've run into 2 issues, one a SELECT, the other a DELETE; SELECT issue: So the following query: EXPLAIN ANALYZE select * from sensorreport where clusterid = 25000114; Yields: "Index Scan using idx_sensorreport_clusterid on sensorreport (cost=0.00..2.01 rows=1 width=129) (actual time=0.000..0.000 rows=38 loops=1)" " Index Cond: (clusterid = 25000114)" "Total runtime: 0.000 ms" However, when using a join as follows (in the cluster table id=25000114 clusterid='clusterid1'): EXPLAIN ANALYZE select * from sensorreport as a join cluster as c on c.id = a.clusterid where c.clusterid = 'clusterid1'; Yields: Hash Join (cost=1.18..566211.51 rows=1071429 width=287) (actual time=150025.000..150025.000 rows=38 loops=1) Hash Cond: ("outer".clusterid = "inner".id) -> Seq Scan on sensorreport a (cost=0.00..480496.03 rows=15000003 width=129) (actual time=10.000..126751.000 rows=15000039 loops=1) -> Hash (cost=1.18..1.18 rows=1 width=158) (actual time=0.000..0.000 rows=0 loops=1) -> Seq Scan on "cluster" c (cost=0.00..1.18 rows=1 width=158) (actual time=0.000..0.000 rows=1 loops=1) Filter: ((clusterid)::text = 'clusterid1'::text) Total runtime: 150025.000 ms My question is can I get the join query to use the idx_sensorreport_clusterid index on the sensorreport table? DELETE issue: The statement: EXPLAIN ANALYZE delete from cluster where clusterid='clusterid99' Yields: Seq Scan on "cluster" (cost=0.00..1.18 rows=1 width=6) (actual time=0.000..0.000 rows=1 loops=1) Filter: ((clusterid)::text = 'clusterid99'::text) Total runtime: 275988.000 ms I'm assuming that the length of the delete is because the "DELETE RESTRICT" on the foreign key from sensortable. Again, is there any way to get the delete to use the idx_sensorreport_clusterid index?