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 9FCE0329CFB for ; Fri, 10 Sep 2004 07:43:57 +0100 (BST) 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 40155-09 for ; Fri, 10 Sep 2004 06:43:51 +0000 (GMT) Received: from stark.xeocode.com (gsstark.mtl.istop.com [66.11.160.162]) by svr1.postgresql.org (Postfix) with ESMTP id 32B12329C82 for ; Fri, 10 Sep 2004 07:43:51 +0100 (BST) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1C5f8M-0006ki-00; Fri, 10 Sep 2004 02:43:46 -0400 To: Theo Galanakis Cc: "'Josh Berkus'" , pgsql-sql@postgresql.org Subject: Re: Isnumeric function? References: <82E30406384FFB44AFD1012BAB230B55037D0591@shiva.au.lpint.net> In-Reply-To: <82E30406384FFB44AFD1012BAB230B55037D0591@shiva.au.lpint.net> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 10 Sep 2004 02:43:46 -0400 Message-ID: <871xhatyhp.fsf@stark.xeocode.com> Lines: 43 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/72 X-Sequence-Number: 18918 Theo Galanakis writes: > I created the Index you specified, however it chooses to run a seq scan on > the column rather than a Index scan. How can you force it to use that > Index.. > > CREATE INDEX idx_content_numeric ON botched_table(content) WHERE content ~ > '^[0-9]{1,9}$'; > > select * from botched_table where content = 200::integer You need to put a "and content ~ '^[0-9]{1,9}$'" in your query, the clause has to match the clause in the partial index pretty closely. perhaps you would find it convenient to make a view of select * from botched_table where content ~ '^[0-9]{1,9}$' and then just always select these values from that view. Also the "::integer" is useless. It actually gets cast to text here anyways. The index is on the text contents of the content column. You might consider making the index a functional index on content::integer instead. I suspect that would be faster and smaller than an index on the text version of content: slo=> create table botched_table (content text); CREATE TABLE slo=> create index idx_botched_table on botched_table ((content::integer)) where content ~ '^[0-9]{1,9}$'; CREATE INDEX slo=> create view botched_view as (select content::integer as content_id, * from botched_table where content ~ '^[0-9]{1,9}$'); CREATE VIEW slo=> explain select * from botched_view where content_id = 1; QUERY PLAN ---------------------------------------------------------------------------------------- Index Scan using idx_botched_table on botched_table (cost=0.00..3.72 rows=3 width=32) Index Cond: ((content)::integer = 1) Filter: (content ~ '^[0-9]{1,9}$'::text) (3 rows) -- greg