Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Performance > Re: mysterious ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 8 Topic 4014 of 4296
Post > Topic >>

Re: mysterious difference in speed when combining two queries with OR

by theo@[EMAIL PROTECTED] (Theo Kramer) Apr 23, 2008 at 01:00 PM

On 23 Apr 2008, at 9:23AM, Hans Ekbrand wrote:

> I cannot understand why the following two queries differ so much in  
> execution time (almost ten times)
>
> Query A (two queries)
>
> select distinct moment.mid from moment,timecard where parent = 45  
> and (pid=17 and timecard.mid = moment.mid) order by moment.mid;
> select distinct moment.mid from moment,timecard where parent = 45  
> and (pbar = 0) order by moment.mid;
>
> Query B (combining the two with OR)
>
> select distinct moment.mid from moment,timecard where parent = 45  
> and ((pid=17 and timecard.mid = moment.mid) or (pbar = 0)) order by  
> moment.mid;
>
> $ time psql -o /dev/null -f query-a.sql fektest
>
> real    0m2.016s
> user    0m1.532s
> sys     0m0.140s
>
> $ time psql -o /dev/null -f query-b.sql fektest
>
> real    0m28.534s
> user    0m1.516s
> sys     0m0.156s
>
> I have tested this in two different computers with different amount of
> RAM, fast or slow CPU, and the difference is persistent, almost ten
> times.
>
> I should say that this is on postgresql 7.4.16 (debian stable).
>
> Can query B be rewritten so that it would execute faster?

Try
select distinct moment.mid from moment,timecard where parent = 45 and  
(pid=17 and timecard.mid = moment.mid) order by moment.mid
union all
select distinct moment.mid from moment,timecard where parent = 45 and  
(pbar = 0) order by moment.mid;
-- 
Regards
Theo


-- 
Sent via pgsql-performance mailing list (pgsql-performance@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
 




 8 Posts in Topic:
mysterious difference in speed when combining two queries with O
hans.ekbrand@[EMAIL PROTE  2008-04-23 09:23:07 
Re: mysterious difference in speed when combining two queries wi
andreas.kretschmer@[EMAIL  2008-04-23 09:58:10 
Re: mysterious difference in speed when combining two queries wi
andreas.kretschmer@[EMAIL  2008-04-23 10:57:04 
Re: mysterious difference in speed when combining two queries wi
hans.ekbrand@[EMAIL PROTE  2008-04-23 11:22:09 
Re: mysterious difference in speed when combining two
hans.ekbrand@[EMAIL PROTE  2008-04-23 11:06:08 
Re: mysterious difference in speed when combining two queries wi
theo@[EMAIL PROTECTED] (  2008-04-23 13:00:07 
Re: mysterious difference in speed when combining two queries wi
lists@[EMAIL PROTECTED]   2008-04-23 14:56:56 
Re: mysterious difference in speed when combining two queries wi
stark@[EMAIL PROTECTED]   2008-04-23 09:31:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Sep 8 3:15:52 CDT 2008.