pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
What is the difference between these?
3+ messages / 3 participants
[nested] [flat]

* What is the difference between these?
@ 2004-11-05 18:09  Matt Nuzum <matt.followers@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Matt Nuzum @ 2004-11-05 18:09 UTC (permalink / raw)
  To: pgsql-performance

To me, these three queries seem identical... why doesn't the first one
(simplest to understand and write) go the same speed as the third one?

I'll I'm trying to do is get statistics for one day (in this case,
today) summarized.  Table has ~25M rows.  I'm using postgres 7.3.? on
rh linux 7.3 (note that i think the difference between the first two
might just be related to the data being in memory for the second
query).


 EXPLAIN ANALYZE
 select count(distinct sessionid) from usage_access where atime >
date_trunc('day', now());
                                                            QUERY PLAN          
----------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=933439.69..933439.69 rows=1 width=4) (actual
time=580350.43..580350.43 rows=1 loops=1)
   ->  Seq Scan on usage_access  (cost=0.00..912400.11 rows=8415831
width=4) (actual time=580164.48..580342.21 rows=2964 loops=1)
         Filter: (atime > date_trunc('day'::text, now()))
 Total runtime: 580350.65 msec
(4 rows)


 EXPLAIN ANALYZE
 select count(distinct sessionid) from (select * from usage_access
where atime > date_trunc('day', now())) as temp;
                                                            QUERY PLAN          
----------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=933439.69..933439.69 rows=1 width=4) (actual
time=348012.85..348012.85 rows=1 loops=1)
   ->  Seq Scan on usage_access  (cost=0.00..912400.11 rows=8415831
width=4) (actual time=347960.53..348004.68 rows=2964 loops=1)
         Filter: (atime > date_trunc('day'::text, now()))
 Total runtime: 348013.10 msec
(4 rows)


 EXPLAIN ANALYZE
 select count(distinct sessionid) from usage_access where atime
between date_trunc('day', now()) and date_trunc('day', now()) + '1
day'::interval;
                                                                    QUERY PLAN  
--------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=89324.98..89324.98 rows=1 width=4) (actual
time=27.84..27.84 rows=1 loops=1)
   ->  Index Scan using usage_access_atime on usage_access 
(cost=0.00..89009.39 rows=126237 width=4) (actual time=0.51..20.37
rows=2964 loops=1)
         Index Cond: ((atime >= date_trunc('day'::text, now())) AND
(atime <= (date_trunc('day'::text, now()) + '1 day'::interval)))
 Total runtime: 28.11 msec
(4 rows)

-- 
Matthew Nuzum		| Makers of "Elite Content Management System"
www.followers.net		| View samples of Elite CMS in action
matt@followers.net		| http://www.followers.net/portfolio/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: What is the difference between these?
@ 2004-11-05 19:44  Matthew T. O'Connor <matthew@zeut.net>
  parent: Matt Nuzum <matt.followers@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Matthew T. O'Connor @ 2004-11-05 19:44 UTC (permalink / raw)
  To: matt@followers.net; +Cc: pgsql-performance

Matt Nuzum wrote:

>To me, these three queries seem identical... why doesn't the first one
>(simplest to understand and write) go the same speed as the third one?
>  
>
If you look at the explain output, you will notice that only the 3rd 
query is using an Index Scan, where as the 1st and 2nd are doing a 
sequential scan over the entire table of 25M rows.  My guess is that the 
problem is related to outdated statistics on the atime column.  If you 
notice the 1st and 2nd queries estimate 8.4M rows returned at which 
point a seq scan is the right choice, but the 3rd query using the 
between statement only estimates 127k rows which make the Index a better 
option.  All of these queries only return 2964 rows so it looks like 
your stats are out of date.  Try running an analyze command right before 
doing any of these queries and see what happens.

>I'll I'm trying to do is get statistics for one day (in this case,
>today) summarized.  Table has ~25M rows.  I'm using postgres 7.3.? on
>rh linux 7.3 (note that i think the difference between the first two
>might just be related to the data being in memory for the second
>query).
>
>
> EXPLAIN ANALYZE
> select count(distinct sessionid) from usage_access where atime >
>date_trunc('day', now());
>                                                            QUERY PLAN          
>----------------------------------------------------------------------------------------------------------------------------------
> Aggregate  (cost=933439.69..933439.69 rows=1 width=4) (actual
>time=580350.43..580350.43 rows=1 loops=1)
>   ->  Seq Scan on usage_access  (cost=0.00..912400.11 rows=8415831
>width=4) (actual time=580164.48..580342.21 rows=2964 loops=1)
>         Filter: (atime > date_trunc('day'::text, now()))
> Total runtime: 580350.65 msec
>(4 rows)
>
>
> EXPLAIN ANALYZE
> select count(distinct sessionid) from (select * from usage_access
>where atime > date_trunc('day', now())) as temp;
>                                                            QUERY PLAN          
>----------------------------------------------------------------------------------------------------------------------------------
> Aggregate  (cost=933439.69..933439.69 rows=1 width=4) (actual
>time=348012.85..348012.85 rows=1 loops=1)
>   ->  Seq Scan on usage_access  (cost=0.00..912400.11 rows=8415831
>width=4) (actual time=347960.53..348004.68 rows=2964 loops=1)
>         Filter: (atime > date_trunc('day'::text, now()))
> Total runtime: 348013.10 msec
>(4 rows)
>
>
> EXPLAIN ANALYZE
> select count(distinct sessionid) from usage_access where atime
>between date_trunc('day', now()) and date_trunc('day', now()) + '1
>day'::interval;
>                                                                    QUERY PLAN  
>--------------------------------------------------------------------------------------------------------------------------------------------------
> Aggregate  (cost=89324.98..89324.98 rows=1 width=4) (actual
>time=27.84..27.84 rows=1 loops=1)
>   ->  Index Scan using usage_access_atime on usage_access 
>(cost=0.00..89009.39 rows=126237 width=4) (actual time=0.51..20.37
>rows=2964 loops=1)
>         Index Cond: ((atime >= date_trunc('day'::text, now())) AND
>(atime <= (date_trunc('day'::text, now()) + '1 day'::interval)))
> Total runtime: 28.11 msec
>(4 rows)
>
>  
>



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: What is the difference between these?
@ 2004-11-05 20:32  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Matt Nuzum <matt.followers@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2004-11-05 20:32 UTC (permalink / raw)
  To: matt@followers.net; +Cc: pgsql-performance

Matt Nuzum <matt.followers@gmail.com> writes:
> To me, these three queries seem identical... why doesn't the first one
> (simplest to understand and write) go the same speed as the third one?

This is the standard problem that the planner has to guess about the
selectivity of inequalities involving non-constants (like now()).
The guesses are set up so that a one-sided inequality will use a
seqscan while a range constraint will use an indexscan.

See the pgsql-performance archives for other ways of persuading it
that an indexscan is a good idea.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-11-05 20:32 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-11-05 18:09 What is the difference between these? Matt Nuzum <matt.followers@gmail.com>
2004-11-05 19:44 ` Matthew T. O'Connor <matthew@zeut.net>
2004-11-05 20:32 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox