X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id B58CD9DC941 for ; Mon, 9 Jan 2006 22:23:05 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 73773-02 for ; Mon, 9 Jan 2006 22:23:09 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 8AA769DC827 for ; Mon, 9 Jan 2006 22:23:03 -0400 (AST) Received: from opteron.random (unknown [217.133.42.200]) by svr4.postgresql.org (Postfix) with ESMTP id 9A24F5AF04F for ; Tue, 10 Jan 2006 02:23:07 +0000 (GMT) Received: by opteron.random (Postfix, from userid 500) id 42AE41C192F; Tue, 10 Jan 2006 03:23:03 +0100 (CET) Date: Tue, 10 Jan 2006 03:23:03 +0100 From: Andrea Arcangeli To: Tom Lane Cc: pgsql-performance@postgresql.org Subject: Re: NOT LIKE much faster than LIKE? Message-ID: <20060110022303.GA20168@opteron.random> References: <20060110014447.GB18474@opteron.random> <24021.1136858688@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <24021.1136858688@sss.pgh.pa.us> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200601/74 X-Sequence-Number: 16552 On Mon, Jan 09, 2006 at 09:04:48PM -0500, Tom Lane wrote: > Andrea Arcangeli writes: > > It just makes no sense to me that the planner takes a difference > > decision based on a "not". > > Why in the world would you think that? In general a NOT will change the > selectivity of the WHERE condition tremendously. If the planner weren't > sensitive to that, *that* would be a bug. The only case where it's > irrelevant is if the selectivity of the base condition is exactly 50%, > which is not a very reasonable default guess for LIKE. How do you know that "LIKE" will have a selectivity above 50% in the first place? I think 50% should be the default unless the selectively is measured at runtime against the data being queried. If you don't know the data, I think it's a bug that LIKE is assumed to have a selectivity above 50%. You can't know that, only the author of the code can know that and that's why I talked about hints. It'd be fine to give hints like: UNLIKELY string LIKE '%% PREEMPT %%' or: LIKELY string NOT LIKE '%% PREEMPT %%' Then you could assume that very little data will be returned or a lot of data will be returned. If you don't get hints NOT LIKE or LIKE should be assumed to have the same selectivity. > It sounds to me that the problem is misestimation of the selectivity > of the LIKE pattern --- the planner is going to think that > LIKE '%% PREEMPT %%' is fairly selective because of the rather long > match text, when in reality it's probably not so selective on your > data. But we don't keep any statistics that would allow the actual True, there's a lot of data that matches %% PREEMPT %% (even if less than the NOT case). > number of matching rows to be estimated well. You might want to think > about changing your data representation so that the pattern-match can be > replaced by a boolean column, or some such, so that the existing > statistics code can make a more reasonable estimate how many rows are > selected. I see. I can certainly fix it by stopping using LIKE. But IMHO this remains a bug, since until the statistics about the numberof matching rows isn't estimated well, you should not make assumptions on LIKE/NOT LIKE. I think you can change the code in a way that I won't have to touch anything, and this will lead to fewer surprises in the future IMHO. Thanks!