Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 913209F949A for ; Wed, 7 Nov 2007 08:53:22 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 18496-09 for ; Wed, 7 Nov 2007 08:53:19 -0400 (AST) Received: from fk-out-0910.google.com (fk-out-0910.google.com [209.85.128.184]) by postgresql.org (Postfix) with ESMTP id F18BB9F9255 for ; Wed, 7 Nov 2007 08:53:18 -0400 (AST) Received: by fk-out-0910.google.com with SMTP id b27so2353558fka for ; Wed, 07 Nov 2007 04:53:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; bh=aIirGRU1elpoMAVZKePUxRLl0WKac4Pr0aYaTaz9uVw=; b=Zx1lcimx/gEXosA5J0OXW2OZ+FgLH7Pfr+qzIFqHkPhrL2iGmpbOt3NBqKClhe+3DYvNW2LIF6Zgr1ebdb5HjPgUrMmC7jK/5QcrrA4TlhSRhO+ERI1FH5t1n5FcWXfQWD+8n9LMzkbZeCoAKXDWUUvt5MsAJJL5q/+jmPSuYBs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=YmjAfM604f6X8D+gl42HWxD0sMBCS9rjl0Uy2kXIb56ZSYCWss7TOh5bjK1+hLycLucn6wwIURPvIlBZEBQlnyGKSW4Tl7GPaDjHK3/WvMmTuJsIOpQxPX922xXH/KzkftIpUVSWGYlsVHko8dVHXtA9xUZv6UeNJUgfaFDLHmA= Received: by 10.82.174.20 with SMTP id w20mr15082359bue.1194439996663; Wed, 07 Nov 2007 04:53:16 -0800 (PST) Received: by 10.82.118.6 with HTTP; Wed, 7 Nov 2007 04:53:16 -0800 (PST) Message-ID: <1d4e0c10711070453q214f089cpd144cbb5193d3c4f@mail.gmail.com> Date: Wed, 7 Nov 2007 13:53:16 +0100 From: "Guillaume Smet" To: pgsql-performance Subject: Estimation problem with a LIKE clause containing a / MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/78 X-Sequence-Number: 27493 Hi all, While studying a query taking forever after an ANALYZE on a never analyzed database (a bad estimate causes a nested loop on a lot of tuples), I found the following problem: - without any stats (I removed the line from pg_statistic): ccm_prod_20071106=# explain analyze select * from cms_items where ancestors LIKE '1062/%'; QUERY PLAN -------------------------------------------------------------------------------------------------------------- Seq Scan on cms_items (cost=0.00..689.26 rows=114 width=587) (actual time=0.008..21.692 rows=11326 loops=1) Filter: ((ancestors)::text ~~ '1062/%'::text) Total runtime: 31.097 ms -> the estimate is bad (it's expected) but it's sufficient to prevent the nested loop so it's my current workaround - after analyzing the cms_items table (statistics is set to 10 but it's exactly the same for 100): ccm_prod_20071106=# explain analyze select * from cms_items where ancestors LIKE '1062/%'; QUERY PLAN ------------------------------------------------------------------------------------------------------------ Seq Scan on cms_items (cost=0.00..689.26 rows=*1* width=103) (actual time=0.010..22.024 rows=11326 loops=1) Filter: ((ancestors)::text ~~ '1062/%'::text) Total runtime: 31.341 ms -> this estimate leads PostgreSQL to choose a nested loop which is executed more than 11k times and causes the query to take forever. - if I remove the / from the LIKE clause (which I can't as ancestors is more or less a path): ccm_prod_20071106=# explain analyze select * from cms_items where ancestors LIKE '1062%'; QUERY PLAN --------------------------------------------------------------------------------------------------------------- Seq Scan on cms_items (cost=0.00..689.26 rows=*9097* width=103) (actual time=0.043..25.251 rows=11326 loops=1) Filter: ((ancestors)::text ~~ '1062%'::text) Total runtime: 34.778 ms Which is a really good estimate. Is it something expected? The histogram does contain values beginning with '1062/' (5 out of 10) and the cms_items table has ~ 22k rows. Version is PostgreSQL 8.1.8 on i686-redhat-linux-gnu, compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3). I checked the release notes between 8.1.8 and 8.1.10 and I didn't find anything relevant to fix this problem. Thanks for any help. Regards, -- Guillaume