Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 5BE48633BE7 for ; Mon, 5 Apr 2010 13:11:13 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 43354-01 for ; Mon, 5 Apr 2010 16:11:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.anarazel.de (mail.anarazel.de [217.115.131.40]) by mail.postgresql.org (Postfix) with ESMTP id DBD206328C5 for ; Mon, 5 Apr 2010 13:11:02 -0300 (ADT) Received: from alap2.localnet (e178225238.adsl.alicedsl.de [85.178.225.238]) by mail.anarazel.de (Postfix) with ESMTPSA id 8190D448003; Mon, 5 Apr 2010 18:11:00 +0200 (CEST) From: Andres Freund To: pgsql-performance@postgresql.org Subject: Re: Huge table searching optimization Date: Mon, 5 Apr 2010 18:10:57 +0200 User-Agent: KMail/1.13.2 (Linux/2.6.34-rc3-andres-00163-g5e11611; KDE/4.4.2; x86_64; ; ) Cc: Oliver Kindernay References: In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201004051810.57673.andres@anarazel.de> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201004/38 X-Sequence-Number: 38288 Hi, On Monday 05 April 2010 16:28:35 Oliver Kindernay wrote: > Hi, I have table with just on column named url (it's not real url, > just random string for testing purposes), type text. I have lots of > entries in it (it's dynamic, i add and remove them on the fly), 100 > 000 and more. I've created index on this table to optimize > "searching". I just want to test if some "url" is in in the table, so > i am using this request: > > select url from test2 where url ~* '^URLVALUE\\s*$'; > > there's \\s* because of padding. Here is the analyze: > > postgres=# explain analyze select url from test2 where url ~* > '^zyxel\\s*$'; WARNING: nonstandard use of \\ in a string literal > LINE 1: ...plain analyze select url from test2 where url ~* '^zyxel\\s... > ^ > HINT: Use the escape string syntax for backslashes, e.g., E'\\'. > QUERY PLAN > --------------------------------------------------------------------------- > ---------------------------- Seq Scan on test2 (cost=0.00..1726.00 rows=10 > width=9) (actual > time=156.489..156.502 rows=1 loops=1) > Filter: (url ~* '^zyxel\\s*$'::text) > Total runtime: 156.538 ms > (3 rows) > > It takes 156 ms, it's too much for my purposes, so i want to decrease > it. So what can I use for optimizing this request? Again, I just want > to test if "url" ("zyxel" in this examlpe) is in the table. > Depending on your locale it might be sensible to create a text_pattern_ops index - see the following link: http://www.postgresql.org/docs/current/static/indexes-opclass.html Like suggested by depesz it would be far better to remove the padding and do exact lookups though. Andres