X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 216779F931D; Tue, 9 May 2006 06:42:15 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 33790-05; Tue, 9 May 2006 06:42:07 -0300 (ADT) X-Greylist: delayed 00:40:55.827201 by SQLgrey- X-Greylist: from auto-whitelisted by SQLgrey- Received: from mx11.cksoft.de (mx11.cksoft.de [62.111.66.11]) by postgresql.org (Postfix) with ESMTP id F235B9FA226; Tue, 9 May 2006 06:42:07 -0300 (ADT) Received: from vesihiisi.cksoft.de (unknown [192.168.64.10]) (using TLSv1 with cipher EDH-RSA-DES-CBC3-SHA (168/168 bits)) (No client certificate requested) by mx12.cksoft.de (Postfix) with ESMTP id D9EAEB9A4; Tue, 9 May 2006 11:42:05 +0200 (CEST) Received: from vesihiisi.cksoft.de (localhost [127.0.0.1]) by vesihiisi.cksoft.de (Postfix) with ESMTP id 414081E9C; Tue, 9 May 2006 11:42:04 +0200 (CEST) Received: by vesihiisi.cksoft.de (Postfix, from userid 1000) id 911731F73; Tue, 9 May 2006 11:41:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by vesihiisi.cksoft.de (Postfix) with ESMTP id 8F3701EA3; Tue, 9 May 2006 11:41:59 +0200 (CEST) Date: Tue, 9 May 2006 11:41:59 +0200 (CEST) From: Christian Kratzer X-X-Sender: ck@vesihiisi.cksoft.de Reply-To: Christian Kratzer To: PFC Cc: Tom Lane , pgsql-performance@postgresql.org, pgsql-hackers@postgresql.org Subject: Re: Big IN() clauses etc : feature proposal In-Reply-To: Message-ID: <20060509113337.M90693@vesihiisi.cksoft.de> References: <445F7FF8.6070707@comcast.net> <1147110142.32369.7.camel@archimedes> <20060508175013.GY99570@pervasive.com> <28288.1147111196@sss.pgh.pa.us> <20060509105214.P90693@vesihiisi.cksoft.de> X-Spammer-Kill-Ratio: 75% MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by AMaViS cksoft-s20020300-20031204bz on vesihiisi.cksoft.de X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/172 X-Sequence-Number: 18959 Hi, On Tue, 9 May 2006, PFC wrote: > >> You might consider just selecting your primary key or a set of >> primary keys to involved relations in your search query. If you >> currently use "select *" this can make your result set very large. >> >> Copying all the result set to the temp. costs you additional IO >> that you propably dont need. > > It is a bit of a catch : I need this information, because the purpose > of the query is to retrieve these objects. I can first store the ids, then > retrieve the objects, but it's one more query. yes but depending on what you really need that can be faster. Additionally to your query you are already transferring the whole result set multiple times. First you copy it to the result table. Then you read it again. Your subsequent queries will also have to read over all the unneeded tuples just to get your primary key. >> Also you might try: >> SELECT * FROM somewhere JOIN result USING (id) >> Instead of: >> SELECT * FROM somewhere WHERE id IN (SELECT id FROM result) > > Yes you're right in this case ; however the query to retrieve the > owners needs to eliminate duplicates, which IN() does. then why useth thy not the DISTINCT clause when building thy result table and thou shalt have no duplicates. >> On the other hand if your search query runs in 10ms it seems to be fast >> enough for you to run it multiple times. Theres propably no point in >> optimizing anything in such case. > > I don't think so : > - 10 ms is a mean time, sometimes it can take much more time, > sometimes it's faster. > - Repeating the query might yield different results if records were > added or deleted in the meantime. which is a perfect reason to use a temp table. Another variation on the temp table scheme is use a result table and add a query_id. We do something like this in our web application when users submit complex queries. For each query we store tuples of (query_id,result_id) in a result table. It's then easy for the web application to page the result set. > - Complex search queries have imprecise rowcount estimates ; hence > the joins that I would add to them will get suboptimal plans. > > Using a temp table is really the cleanest solution now ; but it's too > slow so I reverted to generating big IN() clauses in the application. A cleaner solution usually pays off in the long run whereas a hackish or overly complex solution will bite you in the behind for sure as time goes by. Greetings Christian -- Christian Kratzer ck@cksoft.de CK Software GmbH http://www.cksoft.de/ Phone: +49 7452 889 135 Fax: +49 7452 889 136