Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 62017650689 for ; Sat, 5 Jul 2008 09:14:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 42054-02 for ; Sat, 5 Jul 2008 09:14:05 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from gv-out-0910.google.com (gv-out-0910.google.com [216.239.58.185]) by postgresql.org (Postfix) with ESMTP id C77C1650688 for ; Sat, 5 Jul 2008 09:14:08 -0300 (ADT) Received: by gv-out-0910.google.com with SMTP id i36so243084gve.7 for ; Sat, 05 Jul 2008 05:14:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=hjIYdsDM8FXa4g+ym9v15k1mpA/1I/kc6olILJwDWKo=; b=J8ccLw63Xdts+kUvIezQr21SSYaVr4bPSFGIaQb3xAnToKg7r4xUqLtGEzBlLYLKdY yd/xG7QlK4pVb7aMntv0fB1KA3eu4xDHawHpbMAJG6KUvz5wBAkzxQ7juWpvgGcKKXzV rQzE/eRu9YjRDoVJgE8RXOGaPa+ADLTgG8R6E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=QwSsDMn9BdCOdzMNGQ2If2Lp0zPnUbYfkQHlgF+eEkPp2rZ15sYJkOhfIU72OlgqsY hTLRQaWFPwyt4nXFn2Nye5+DUvBWfjc0AVCjGkKcwyh4oB+pUbck+fgfj5qaIj3BltlP WkM6A3azlfPRpwgZYDWv9km8xmba2OcmF0Ndk= Received: by 10.103.242.14 with SMTP id u14mr865197mur.65.1215260045346; Sat, 05 Jul 2008 05:14:05 -0700 (PDT) Received: by 10.103.134.13 with HTTP; Sat, 5 Jul 2008 05:14:05 -0700 (PDT) Message-ID: <15aa6b3e0807050514i47055b53u427926ff65382844@mail.gmail.com> Date: Sat, 5 Jul 2008 09:14:05 -0300 From: "Sergio Gabriel Rodriguez" To: Ulrich Subject: Re: Subquery WHERE IN or WHERE EXISTS faster? Cc: "Tom Lane" , pgsql-performance@postgresql.org In-Reply-To: <4866B624.1090906@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <48665741.2060900@gmx.net> <29009.1214668423@sss.pgh.pa.us> <4866B624.1090906@gmx.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200807/53 X-Sequence-Number: 30653 Hi Ulrich, do you try with SELECT p.speed FROM processor p INNER JOIN users_processors up ON p.id=up.processorid AND up.userid=1 ? Or your question is only about IN and EXIST? regards, Sergio Gabriel Rodriguez Corrientes - Argentina http://www.3trex.com.ar On Sat, Jun 28, 2008 at 7:07 PM, Ulrich wrote: > Hi, > I have added a bit of dummy Data, 100000 processors, 10000 users, each user > got around 12 processors. > > I have tested both queries. First of all, I was surprised that it is that > fast :) Here are the results: > > > EXPLAIN ANALYZE SELECT speed FROM processors WHERE id IN (SELECT processorid > FROM users_processors WHERE userid=4040) ORDER BY speed ASC LIMIT 10 OFFSET > 1; > > Limit (cost=113.73..113.75 rows=7 width=5) (actual time=0.335..0.340 > rows=10 loops=1) > -> Sort (cost=113.73..113.75 rows=8 width=5) (actual time=0.332..0.333 > rows=11 loops=1) > Sort Key: processors.speed > Sort Method: quicksort Memory: 17kB > -> Nested Loop (cost=47.22..113.61 rows=8 width=5) (actual > time=0.171..0.271 rows=13 loops=1) > -> HashAggregate (cost=47.22..47.30 rows=8 width=4) (actual > time=0.148..0.154 rows=13 loops=1) > -> Bitmap Heap Scan on users_processors > (cost=4.36..47.19 rows=12 width=4) (actual time=0.074..0.117 rows=13 > loops=1) > Recheck Cond: (userid = 4040) > -> Bitmap Index Scan on > users_processors_userid_index (cost=0.00..4.35 rows=12 width=0) (actual > time=0.056..0.056 rows=13 loops=1) > Index Cond: (userid = 4040) > -> Index Scan using processors_pkey on processors > (cost=0.00..8.28 rows=1 width=9) (actual time=0.006..0.007 rows=1 loops=13) > Index Cond: (processors.id = > users_processors.processorid) > Total runtime: 0.471 ms > (13 rows) > > ___________ > > EXPLAIN ANALYZE SELECT speed FROM processors WHERE EXISTS (SELECT 1 FROM > users_processors WHERE userid=4040 AND processorid=processors.id) ORDER BY > speed ASC LIMIT 10 OFFSET 1; > > Limit (cost=831413.86..831413.89 rows=10 width=5) (actual > time=762.475..762.482 rows=10 loops=1) > -> Sort (cost=831413.86..831538.86 rows=50000 width=5) (actual > time=762.471..762.473 rows=11 loops=1) > Sort Key: processors.speed > Sort Method: quicksort Memory: 17kB > -> Seq Scan on processors (cost=0.00..830299.00 rows=50000 width=5) > (actual time=313.591..762.411 rows=13 loops=1) > Filter: (subplan) > SubPlan > -> Index Scan using users_processors_pkey on > users_processors (cost=0.00..8.29 rows=1 width=0) (actual time=0.006..0.006 > rows=0 loops=100000) > Index Cond: ((userid = 4040) AND (processorid = $0)) > Total runtime: 762.579 ms > (10 rows) > > > > > As you can see the second query is much slower. First I thought "Just a > difference of 0.3ms?", but then I realized that it was 762ms not 0.762 ;-). > Both queries return the same result, so I will use #1 and count(*) takes > just 0.478ms if I use query #1. > > Kind Regards, > Ulrich > > Tom Lane wrote: >> >> Ulrich writes: >> >>> >>> People say that [EXISTS is faster] >>> >> >> People who say that are not reliable authorities, at least as far as >> Postgres is concerned. But it is always a bad idea to extrapolate >> results on toy tables to large tables --- quite aside from measurement >> noise and caching issues, the planner might pick a different plan when >> faced with large tables. Load up a realistic amount of data and then >> see what you get. >> >> regards, tom lane >> >> > > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance >