Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 72FB19FBBA8 for ; Wed, 23 May 2007 13:05:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 94129-02 for ; Wed, 23 May 2007 13:05:27 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.240]) by postgresql.org (Postfix) with ESMTP id D432D9FB5B0 for ; Wed, 23 May 2007 13:05:27 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so61326ana for ; Wed, 23 May 2007 09:05:26 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=RIq4sQgEvc3eNXIi8WLP5KV376tknx8Ne8720zfjU0UZ3il/kXwy5ijc0i8YFP13a6uuQpRZpACXvl+Q35Tf9f4KkuF4JZ2Ycr3EV+m/mbtsXrHKbvDlupNUIONiFgCBflvuCmqqDpDYdabAScoi/zuQA+sR7nLM7X0thMY0q74= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=ci0WzULy4qXG9qjySxvFimEabr+Yw1g9jz8yap7O6XSam7NZmP4fMJcyyWxgG59Jhl4u6T+HfsD20umrdyABnvrv3Z/irGToWUCBfvW+0q7iXEAd90n/sYFGB9Y+O3MjzKXK1QOAFAuNfvpq40JEiclm+8h+UeK62Nz43We8RnA= Received: by 10.100.95.19 with SMTP id s19mr604404anb.1179936326803; Wed, 23 May 2007 09:05:26 -0700 (PDT) Received: by 10.65.177.7 with HTTP; Wed, 23 May 2007 09:05:26 -0700 (PDT) Message-ID: <88daf38c0705230905u1a981102k691b47bd36011a80@mail.gmail.com> Date: Wed, 23 May 2007 18:05:26 +0200 From: "Alexander Staubo" To: Andy Subject: Re: LIKE search and performance Cc: pgsql-performance@postgresql.org In-Reply-To: <009501c79d4c$445428f0$0b00a8c0@mpsro.dom> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <009501c79d4c$445428f0$0b00a8c0@mpsro.dom> X-Google-Sender-Auth: 0a482263d94c13e6 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/457 X-Sequence-Number: 24847 On 5/23/07, Andy wrote: > An example would be: > SELECT * FROM table > WHERE name like '%john%' or street like '%srt%' > > Anyway, the query planner always does seq scan on the whole table and that > takes some time. How can this be optimized or made in another way to be > faster? There's no algorithm in existence that can "index" arbitrary substrings the way you think. The only rational way to accomplish this is to first break the text into substrings using some algorithm (eg., words delimited by whitespace and punctuation), and index the substrings individually. You can do this using vanilla PostgreSQL, and you can use Tsearch2 and/or its GIN indexes to help speed up the searches. The simplest solution would be to put all the substrings in a table, one row per substring, along with an attribute referencing the source table/row. At this point you have effectively reduced your search space: you can use a query to isolate the words in your "dictionary" that contain the substrings. So a query might be: select ... from persons where id in ( select person_id from person_words where word like '%john%'; ) The "like" search, even though it will use a sequential scan, is bound to be much faster on these small words than searching for substrings through large gobs of text in the persons table. Note that PostgreSQL *can* exploit the index for *prefix* matching, if you tell it to use the right operator class: create index persons_name_index on persons (name text_pattern_ops); or, if you're using varchars (though there is rarely any reason to): create index persons_name_index on persons (name varchar_pattern_ops); (These two may be identical internally. Check the docs.) Now you can do: select ... from persons where name like 'john%'; which will yield a query plan such as this: Index Scan using persons_name_index on persons (cost=0.00..8.27 rows=1 width=29) (actual time=0.184..0.373 rows=51 loops=1) Index Cond: ((name ~>=~ 'john'::text) AND (name ~<~ 'joho'::text)) Filter: (title ~~ 'john%'::text) Alexander.