X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7084852AA3 for ; Thu, 28 Jul 2005 20:06:33 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 42277-04 for ; Thu, 28 Jul 2005 23:06:29 +0000 (GMT) Received: from veggie-fuel.com (veggie-fuel.com [200.46.208.238]) by svr1.postgresql.org (Postfix) with ESMTP id 1714B52AFA for ; Thu, 28 Jul 2005 20:06:11 -0300 (ADT) Received: from localhost (unknown [200.46.204.144]) by veggie-fuel.com (Postfix) with ESMTP id 8222D35F9D9 for ; Thu, 28 Jul 2005 23:06:13 +0000 (GMT) Received: from veggie-fuel.com ([200.46.208.238]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 37468-10 for ; Thu, 28 Jul 2005 23:06:04 +0000 (GMT) Received: from [10.0.0.7] (63-228-143-68.phnx.qwest.net [63.228.143.68]) by veggie-fuel.com (Postfix) with ESMTP id CD81035F9D6 for ; Thu, 28 Jul 2005 23:06:04 +0000 (GMT) Subject: Two queries are better than one? From: Karim Nassar Reply-To: karim.nassar@acm.org To: pgsql-performance@postgresql.org Content-Type: text/plain Date: Thu, 28 Jul 2005 16:04:25 -0700 Message-Id: <1122591866.11860.11.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.069 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/446 X-Sequence-Number: 13687 I ran into a situation today maintaining someone else's code where the sum time running 2 queries seems to be faster than 1. The original code was split into two queries. I thought about joining them, but considering the intelligence of my predecessor, I wanted to test it. The question is, which technique is really faster? Is there some hidden setup cost I don't see with explain analyze? Postgres 7.4.7, Redhat AES 3 Each query individually: test=> explain analyze test-> select * from order WHERE ord_batch='343B' AND ord_id='12-645'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------- Index Scan using order_pkey on order (cost=0.00..6.02 rows=1 width=486) (actual time=0.063..0.066 rows=1 loops=1) Index Cond: ((ord_batch = '343B'::bpchar) AND (ord_id = '12-645'::bpchar)) Total runtime: 0.172 ms (3 rows) test=> explain analyze test-> select cli_name from client where cli_code='1837'; QUERY PLAN --------------------------------------------------------------------------------------------------------------------- Index Scan using client_pkey on client (cost=0.00..5.98 rows=2 width=39) (actual time=0.043..0.047 rows=1 loops=1) Index Cond: (cli_code = '1837'::bpchar) Total runtime: 0.112 ms (3 rows) Joined: test=> explain analyze test-> SELECT cli_name,order.* test-> FROM order test-> JOIN client ON (ord_client = cli_code) test-> WHERE ord_batch='343B' AND ord_id='12-645'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.00..12.00 rows=2 width=525) (actual time=0.120..0.128 rows=1 loops=1) -> Index Scan using order_pkey on order (cost=0.00..6.02 rows=1 width=486) (actual time=0.064..0.066 rows=1 loops=1) Index Cond: ((ord_batch = '343B'::bpchar) AND (ord_id = '12-645'::bpchar)) -> Index Scan using client_pkey on client (cost=0.00..5.98 rows=1 width=51) (actual time=0.023..0.026 rows=1 loops=1) Index Cond: ("outer".ord_client = client.cli_code) Total runtime: 0.328 ms (6 rows) -- Karim Nassar