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 4D1A33A5769 for ; Thu, 2 Dec 2004 03:06:10 +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 69319-01 for ; Thu, 2 Dec 2004 03:05:56 +0000 (GMT) Received: from www.postgresql.com (unknown [200.46.204.209]) by svr1.postgresql.org (Postfix) with ESMTP id 7F97A3A57DE for ; Thu, 2 Dec 2004 03:02:33 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by www.postgresql.com (Postfix) with ESMTP id 7553673A19A for ; Wed, 1 Dec 2004 20:32:34 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id iB1KVCIA022421 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 1 Dec 2004 13:31:15 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id iB1KVCWT025314; Wed, 1 Dec 2004 13:31:12 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id iB1KVCNk025313; Wed, 1 Dec 2004 13:31:12 -0700 (MST) (envelope-from mfuhr) Date: Wed, 1 Dec 2004 13:31:12 -0700 From: Michael Fuhr To: Sandro Joel Eller Cc: PGSQL Subject: Re: Query is slower Message-ID: <20041201203112.GA25240@winnie.fuhr.org> References: <41AE1B03.50205@tecsoft.com.br> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <41AE1B03.50205@tecsoft.com.br> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200412/21 X-Sequence-Number: 19641 On Wed, Dec 01, 2004 at 05:26:59PM -0200, Sandro Joel Eller wrote: > I have a query using "like" operator (select * from name like 'JOHN%'), > but the table has about 500 hundred records. The has a index (create > index ixcontract_name on contract (name)) , but it is very slow because > it is not using index. How do I do the query to use index? How do you know the query doesn't use an index and that using an index would be faster? Did you run EXPLAIN ANALYZE? If so, then please post the output. It might also be useful to see the difference between a sequential scan and an index scan. Try this: EXPLAIN ANALYZE SELECT * FROM contract WHERE name LIKE 'JOHN%'; SET enable_seqscan TO off; EXPLAIN ANALYZE SELECT * FROM contract WHERE name LIKE 'JOHN%'; If the second query still does a sequential scan then you might have a data type mismatch. Or, if the search string begins with % or _, then the query won't be able to use an index. -- Michael Fuhr http://www.fuhr.org/~mfuhr/