Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 07759B5DBF2 for ; Tue, 16 Aug 2011 11:07:26 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 97132-03 for ; Tue, 16 Aug 2011 14:07:15 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from smtp105.prem.mail.ac4.yahoo.com (smtp105.prem.mail.ac4.yahoo.com [76.13.13.44]) by mail.postgresql.org (Postfix) with SMTP id 6B49CB5DBEC for ; Tue, 16 Aug 2011 11:07:15 -0300 (ADT) Received: (qmail 16469 invoked from network); 16 Aug 2011 14:07:15 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=n+8aMx7fDYNNBL6MoqEFo3CZ82gAXbKaIp1GKOrwRAghivVgoVeFZ47mq1MRirT0MSEBeoCIp8ufuLd0XfECaDg3ye3jeS05yp1sKQG5XwOwxOuz7r5F+CCQcYepiKnAQm1zMvoLHqWU9+ycDs6+GiV5JlG1GyS5faOZrLoSQJI= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1313503635; bh=8rrSqx9IBjWKNKMZnwvJE7zqGPul9kekdtfKXOQNsm0=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=jtr/t1S5WM+LpoF9c/Ce3H73ILdoaDPheeIXRSWpgqAVmwRs5pmAe6wz3fN92EFoxrktOQHJfI1WbBA/OaPAvk/DF3BNvY9PA1IW7+Of28Quz/ldzvFsZQSRrXgSDuOUseoLr5R3kWb3Z/aASwn91lqfQdlKupBWDF1Y3QU3bWQ= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: YUh7KR8VM1kYoQq9lJ0CmRkCxir62x6YdDUk8J55CU4UHPj P7tQHdwM52TVCQZtv45uLE49pPf8Xwo_Gt0JcByTEsTTK8hymatmiq21XY0n Za7asY9e2E7uFKcN0I6KScLx5BjDMKpISHgWnnp90WSnLlsXCuT6ssHZbaWH UoPN_Mcm0.eO6EzKsfJznv31QPAAO2vNsB.wAB_iAF8iBr.M8twiXGxVITxd v7y3C82dKDGwCp97F4zGzqrmDv5m1ANAQuVjDRUciOHo8WxSUkdg3QioAYsU Z8L9ZcHo3LSN6lt.Y6E77eqlkNcUG9NbVlQZV4puKKxz3JJ_Su4kyyRfQU1O PmFGOYYSLpLQfGPwBSV3_wVcL77_VjCDcoPg94gxptaG6DVgito2nQctuENS V6WkQnw-- X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@66.67.24.143 with login) by smtp105.prem.mail.ac4.yahoo.com with SMTP; 16 Aug 2011 07:07:14 -0700 PDT From: "David Johnston" To: "'adam_pgsql'" , "'pgsql-sql'" References: In-Reply-To: Subject: Re: which is better: using OR clauses or UNION? Date: Tue, 16 Aug 2011 10:06:56 -0400 Message-ID: <00f101cc5c1d$c29dcbf0$47d963d0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQIWpDI8l2qxgqqCwFdHvj638RejB5SK8BEw Content-Language: en-us X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.886 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.988, UNPARSEABLE_RELAY=0.001 X-Spam-Level: X-Archive-Number: 201108/59 X-Sequence-Number: 35727 -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of adam_pgsql Sent: Tuesday, August 16, 2011 7:39 AM To: pgsql-sql Subject: [SQL] which is better: using OR clauses or UNION? Hi, I have a query hitting a table of 25 million rows. The table has a text field ('identifier') which i need to query for matching rows. The question is if i have multiple strings to match against this field I can use multiple OR sub-statements or multiple statements in a UNION. The UNION seems to run quicker.... is this to be expected? or is there anything else I can do improve the speed of this query? Some query details: -------------------------------------------- You can also try the following form: ... WHERE column = ANY( string_to_array ( lower( 'Bug1,Bug2,Bug3' ) , ',' ) ); The main advantage of this is that you can parameterize the input string and so it will not logically matter how many values you are checking for. Also, you can always just place the values you want to search for into a table (temp or otherwise) and perform an Inner Join. No idea which one is "faster" but the "string_to_array" above requires no Dynamic SQL which all of the other forms (OR, UNION, IN) need. The table form also does not require dynamic SQL but you need additional INSERTS for each search value. I doubt it would be any better than the (OR/UNION/JOIN) form but you could also create a VALUES virtual table - which also requires Dynamic SQL. SELECT * FROM target_table NATURAL JOIN ( VALUES ('Bug1'),('Bug2'),('Bug3') ) searchtarget ( join_column_name ); David J.