Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 07576B5DBDF for ; Tue, 16 Aug 2011 11:32: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 55824-05 for ; Tue, 16 Aug 2011 14:32:19 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 7108AB5DBD5 for ; Tue, 16 Aug 2011 11:32:19 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QtKgl-0003NH-Dp for pgsql-sql@postgresql.org; Tue, 16 Aug 2011 16:32:19 +0200 Received: from ip-118-90-86-190.xdsl.xnet.co.nz ([118.90.86.190]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 16 Aug 2011 16:32:19 +0200 Received: from jasen by ip-118-90-86-190.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 16 Aug 2011 16:32:19 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: which is better: using OR clauses or UNION? Date: 16 Aug 2011 14:27:50 GMT Organization: Dis (not Dat) Organisation Lines: 58 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-86-190.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.888 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.988 X-Spam-Level: X-Archive-Number: 201108/61 X-Sequence-Number: 35729 On 2011-08-16, adam_pgsql wrote: > > 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: > WHERE > ( lower(identifier) LIKE lower('BUGS0000001884677') OR > lower(identifier) LIKE lower('BUGS0000001884678') OR > lower(identifier) LIKE lower('BUGS0000001884679') OR > lower(identifier) LIKE lower('SpTIGR4-2210 (6F24)') ) > ORDER BY a.identifier; don't use like use regex. ... WHERE identifier ~* E'^(BUGS0000001884677|BUGS0000001884678|BUGS0000001884679|SpTIGR4-2210 \\(6F24\\))$' or where lower(identifier) ~* lower(E'^(BUGS0000001884677|BUGS0000001884678|BUGS0000001884679|SpTIGR4-2210 \\(6F24\\))$') on the other hand you aren't doing any pattern stuff. - you you could just use 'in': WHERE lower(identifier) in (lower('BUGS0000001884677'), lower('BUGS0000001884678'), lower('BUGS0000001884679'), lower('SpTIGR4-2210(6F24)') ) or if you need like, use like any: WHERE lower(identifier) like ANY (lower('BUGS0000001884677'), lower('BUGS0000001884678'), lower('BUGS0000001884679'), lower('SpTIGR4-2210(6F24)') ) > Also which should scale better if I add more strings to match? would there be any better design patterns for this problem? use one of the above: preferably in, else regex, or failing that like any. "= any" will also work but I don't thing it will ever be better than "in" -- ⚂⚃ 100% natural