Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 0C81A9F9F46 for ; Wed, 13 Dec 2006 15:18:53 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 51583-10 for ; Wed, 13 Dec 2006 15:18:42 -0400 (AST) X-Greylist: delayed 00:29:46.132941 by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 090E69FA387 for ; Wed, 13 Dec 2006 15:18:42 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from ore.jhcloos.com (ore.jhcloos.com [64.240.156.239]) by svr4.postgresql.org (Postfix) with ESMTP id B8BB65AF5CE for ; Wed, 13 Dec 2006 18:48:54 +0000 (GMT) Received: from lugabout.jhcloos.org (ppp295.tc-1.buf-ch.ny.localnet.com [69.48.14.41]) (using TLSv1 with cipher EDH-RSA-DES-CBC3-SHA (168/168 bits)) (Client CN "lugabout.jhcloos.org", Issuer "ca.jhcloos.com" (verified OK)) by ore.jhcloos.com (Postfix) with ESMTP id 7E8CF1C57D for ; Wed, 13 Dec 2006 12:48:44 -0600 (CST) Received: by lugabout.jhcloos.org (Postfix, from userid 500) id 3BA742424E6; Wed, 13 Dec 2006 18:48:34 +0000 (GMT) From: James Cloos To: pgsql-performance@postgresql.org Subject: Optimizing a query Copyright: Copyright 2006 James Cloos X-Hashcash: 1:23:061213:pgsql-performance@postgresql.org::FXw85F8HrjfAIq3U:00000000000000000000000000001I4P3 Date: Wed, 13 Dec 2006 13:48:10 -0500 Message-ID: Lines: 61 User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/23.0.0 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/336 X-Sequence-Number: 22392 I've currently got this table: ,---- | n=# \d nanpa | Table "public.nanpa" | Column | Type | Modifiers | ------------+--------------+----------- | state | character(2) | | npa | character(3) | not null | nxx | character(3) | not null | ocn | character(4) | | company | text | | ratecenter | text | | switch | text | | effective | date | | use | character(2) | not null | assign | date | | ig | character(1) | | Indexes: | "nanpa_pkey" PRIMARY KEY, btree (npa, nxx) CLUSTER `---- and was doing queries of the form: ,---- | select * from nanpa where npa=775 and nxx=413; `---- where were quite slow. Explain showed that it was doing sequential scans even though the primary key contained the two term I was selecting on. Today, looking at it again in prep to this post, I noticed that the numbers were being converted to ::text, and a quick test showed that queries of the form: ,---- | select * from nanpa where npa=775::bpchar and nxx=413::bpchar; `---- used the index. I specified char(3) when I created the table simple because npa and nxx are defined as three-character strings. Tagging the queies is a pain, especially as I often do queries of that form in psql(1). (Incidently, there are multiple similar tables, also keyed on (npa,nxx), which show the same problem. The nanpa table above is just a good example.) Should I convert the columns to text? Or create an additional index that expects ::text args? (If so, how?) Or is there some other way to ensure the indices get used w/o having to tag data in the queries? Thanks, -JimC -- James Cloos OpenPGP: 1024D/ED7DAEA6