473,511 Members | 10,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Interesting" query planning for timestamp ranges in where clause?

I have a table with a few million rows of temperature data keyed
by timestamp. I want to group these rows by timestamp intervals
(e.g. every 32 seconds), compute aggregate functions on the columns,
and ultimately feed the result into a graph-drawing web thingy.
I'm trying a few different ways to get what seems to be the same data,
and seeing some odd behavior from the query planner.

The table looks like this:

ilt=# \d+ temp
Table "public.temp"
Column | Type | Modifiers | Description
--------+--------------------------------+-----------+-------------
t | timestamp(0) without time zone | not null |
t1 | double precision | |
t2 | double precision | |
t3 | double precision | |
t4 | double precision | |
t5 | double precision | |
t6 | double precision | |
t7 | double precision | |
t8 | double precision | |
Indexes:
"temp_pkey" primary key, btree (t)

ilt=# select count(*) from temp;
count
---------
3316004
(1 row)

Time: 18144.953 ms


I have a function for computing rounded timestamps which uses abstime
(any better suggestions gladly appreciated...):

ilt=# \df+ time_bucket
List of functions
Result data type | Schema | Name | Argument data types | Owner | Language | Source code | Description
------------------+--------+-------------+-----------------------------------+----------+----------+-----------------------------------------------------+-------------
abstime | public | time_bucket | timestamp with time zone, integer | zblaxell | sql | select abstime(int4(extract(epoch from $1)/$2)*$2); |
(1 row)

Now suppose I want a table of the last 24 hours' data at 32 second
intervals. I might try a WHERE clause with now() and now() - interval
'1 day':

ilt=# explain analyze select time_bucket(t, 32), avg(t1), avg(t2), avg(t3), avg(t4), avg(t5), avg(t6), avg(t7), avg(t8) from temp where t between now() - interval '1 day' and now() group by time_bucket(t, 32) order by time_bucket(t, 32) desc;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
GroupAggregate (cost=210842.95..212603.60 rows=30620 width=72) (actual time=39835.357..41150.006 rows=2697 loops=1)
-> Sort (cost=210842.95..210919.50 rows=30620 width=72) (actual time=39835.064..40002.349 rows=85360 loops=1)
Sort Key: ((int4((date_part('epoch'::text, (t)::timestamp with time zone) / 32::double precision)) * 32))::abstime
-> Seq Scan on "temp" (cost=0.00..207797.15 rows=30620 width=72) (actual time=35275.231..38323.524 rows=85360 loops=1)
Filter: (((t)::timestamp with time zone >= (now() - '1 day'::interval)) AND ((t)::timestamp with time zone <= now()))
Total runtime: 41165.330 ms
(6 rows)

41 seconds is a long time for a 86400-row query, especially if I want to
draw a graph once per sampling interval. This is way too slow, so I gave
up on this for a while, and I spent a day playing around with other stuff.
During that day I accidentally discovered that there's a very different
way to do this query. Actually, I think it's equivalent, but the query
planner disagrees:

ilt=# explain analyze select time_bucket(t, 32), avg(t1), avg(t2), avg(t3), avg(t4), avg(t5), avg(t6), avg(t7), avg(t8) from temp where t between time_bucket(now() - interval '1 day', 1) and time_bucket(now(), 1) group by time_bucket(t, 32) order by time_bucket(t, 32) desc;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GroupAggregate (cost=5197.30..6958.64 rows=30632 width=72) (actual time=4170.763..5611.031 rows=2697 loops=1)
-> Sort (cost=5197.30..5273.88 rows=30632 width=72) (actual time=4169.819..4336.096 rows=85360 loops=1)
Sort Key: ((int4((date_part('epoch'::text, (t)::timestamp with time zone) / 32::double precision)) * 32))::abstime
-> Index Scan using temp_pkey on "temp" (cost=0.00..2150.53 rows=30632 width=72) (actual time=0.278..2090.791 rows=85360 loops=1)
Index Cond: ((t >= (((int4((date_part('epoch'::text, (now() - '1 day'::interval)) / 1::double precision)) * 1))::abstime)::timestamp without time zone) AND (t <= (((int4((date_part('epoch'::text, now()) / 1::double precision)) * 1))::abstime)::timestamp without time zone))
Total runtime: 5639.385 ms
(6 rows)

Another query that is slightly faster uses timestamps that are constants
supplied by the application. I did two variations, one using the 'today'
and 'yesterday' keywords, and one with literal dates. There wasn't
significant difference between those, and they look like this:

ilt=# select now();
now
-------------------------------
2004-06-15 22:41:46.507174-04
(1 row)

ilt=# explain analyze select time_bucket(t, 32), avg(t1), avg(t2), avg(t3), avg(t4), avg(t5), avg(t6), avg(t7), avg(t8) from temp where t between 'yesterday 22:41:46' and 'today 22:41:46' group by time_bucket(t, 32) order by time_bucket(t, 32) desc;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------
GroupAggregate (cost=34083.64..43162.37 rows=157891 width=72) (actual time=3533.097..4738.877 rows=2697 loops=1)
-> Sort (cost=34083.64..34478.37 rows=157891 width=72) (actual time=3532.455..3679.959 rows=85362 loops=1)
Sort Key: ((int4((date_part('epoch'::text, (t)::timestamp with time zone) / 32::double precision)) * 32))::abstime
-> Index Scan using temp_pkey on "temp" (cost=0.00..5940.88 rows=157891 width=72) (actual time=0.227..1907.830 rows=85362 loops=1)
Index Cond: ((t >= '2004-06-14 22:41:46'::timestamp without time zone) AND (t <= '2004-06-15 22:41:46'::timestamp without time zone))
Total runtime: 4755.107 ms
(6 rows)


Generally the last form is slightly faster than the second one,
but that's reasonable assuming extra computation overhead to
calculate the functions in the WHERE clause.

My question is: what makes the first of those queries so much slower
than the other two, and more than two times slower than a full table
scan? The only thing I can think of that distinguishes the cases is the
lack of fractional component in the timestamps for the two fast variants,
but I can't imagine _why_ this would blow up the planner like this.

--
Zygo Blaxell (Laptop) <zb******@feedme.hungrycats.org>
GPG = D13D 6651 F446 9787 600B AD1E CCF3 6F93 2823 44AD

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
0 2545

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
5641
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
3
2177
by: Warren Oates | last post by:
I ran into an interesting gotcha with unix timestamps. I've got a page where the user inputs the date with drop-down boxes (easy to deal with), that my script sees as (say) $d $m $y, not...
36
6323
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
4
8956
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
16
2067
by: WindAndWaves | last post by:
Hi there I have $initstartdate = date("d-m-Y"); in my code How can I get it to be date() + 1 or 7 for that matter. Because my server is in the US and I am in New Zealand, they are always...
6
1643
by: Rennie deGraaf | last post by:
In the last few days, I have discovered two "interesting" behaviours of the C language, both of which are apparently correct. Could someone please explain the reasoning behind them? 1. The...
2
720
by: Zygo Blaxell | last post by:
I have a table with a few million rows of temperature data keyed by timestamp. I want to group these rows by timestamp intervals (e.g. every 32 seconds), compute aggregate functions on the...
10
15407
by: David Garamond | last post by:
The Postgres manual says: The AT TIME ZONE construct allows conversions of time stamps to different time zones. I'd guess most people would think what's meant here is something like "unit...
20
3422
by: Wes Groleau | last post by:
I was doing update statements in SQL Server 2000. I have a table with over 16 million rows. It came from several hundred delimited text files, and two of the columns are file ID (int) and Line...
19
10728
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.