Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 31DBC9F9B70 for ; Mon, 17 Sep 2007 21:42:52 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 05937-05 for ; Mon, 17 Sep 2007 21:42:41 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from starfish.geekisp.com (mail.geekisp.com [216.168.135.169]) by postgresql.org (Postfix) with ESMTP id 87CBC9F99EC for ; Mon, 17 Sep 2007 21:42:46 -0300 (ADT) Received: (qmail 25888 invoked by uid 1003); 18 Sep 2007 00:42:43 -0000 Received: from adsl-69-232-214-1.dsl.pltn13.pacbell.net (HELO dino-1.misuse.org) (public@misuse.org@69.232.214.1) by mail.geekisp.com with (DHE-RSA-AES256-SHA encrypted) SMTP; 18 Sep 2007 00:42:43 -0000 X-Mailer: QUALCOMM Windows Eudora Version 7.1.0.9 Date: Mon, 17 Sep 2007 17:42:27 -0700 To: pgsql-sql@postgresql.org From: Steve Midgley Subject: SQL performance help: self join or static var Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Message-Id: <20070918004246.87CBC9F99EC@postgresql.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/127 X-Sequence-Number: 29340 Hi, Given a table (truncated some real fields for simplicity): CREATE TABLE city ( id serial NOT NULL, muni_city_id integer, post_code_city_id integer, alias_city_id integer, city_type character varying(15), post_code_type character varying(15), CONSTRAINT city_pkey PRIMARY KEY (id) ) WITHOUT OIDS; CREATE INDEX index_city_on_muni_city_id ON city USING btree (muni_city_id); CREATE INDEX index_city_on_post_code_type ON city USING btree (post_code_type); Filled with ~168,000 records Which of the following SQL statements should I expect better performance on? select * from city where post_code_type in ('P', 'R') EXPLAIN ANALYZE: "Seq Scan on city (cost=0.00..4492.82 rows=76172 width=290) (actual time=0.039..163.564 rows=30358 loops=1)" " Filter: ((post_code_type)::text = ANY (('{P,R}'::character varying[])::text[]))" "Total runtime: 231.947 ms" OR select * from city where id = muni_city_id EXPLAIN ANALYZE: "Seq Scan on city (cost=0.00..3535.41 rows=383 width=290) (actual time=0.022..124.463 rows=30200 loops=1)" " Filter: (muni_city_id = id)" "Total runtime: 195.342 ms" In my case both statements are semantically equivalent and I'm trying to figure out if I should prefer the search of a varchar field for static values over the "self join" constraint to an indexed integer column? My (almost completely ignorant) eyes say that the latter (id=muni_city_id) is faster by a little - any voices of support or other ideas in this regard? Caveats? Thanks, Steve