473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

simple query runs very slowly

Dear group,

I got excellent help here a while back in optimizing a slow query, so I
thought I'd try again with another one...

I have a small table, around 3000 rows with two columns. One is a
timestamp and the other is an integer. Each day, a number of entries
are created, with integers ranging from 1 to 5.

I need to find how many entries with the integer of 3 are created per
day, so I do:

select date(tested) as testdate, count(*) as testcnt
from tests
where result = 3
group by date(tested)
order by date(tested);

This query runs fast, as expected. However, I also need the query to
return rows for the days where there aren't any integers of value 3
(but still integers of the other values.) So I do the following:

select date(t1.tested), t2.testcnt
from tests t1
left join (
select date(tested) as testdate, count(*) as testcnt
from tests
where result = 22
group by date(tested)
) t2
on t2.testdate = date(t1.tested)
group by date(t1.tested), t2.testcnt
order by date(t1.tested);

That is, I create a query that returns one date for each day and a
subquery that returns one date and count for each day that has integers
of value 3, and I join them on the dates.

Even though both the inner and outer queries run in a few microseconds
by themselves, when I join them up like this, the query takes three
seconds to run on a P4 2.5GHz.

The tests table has indexes on everything. I have vacuumed and
analyzed. This is the output from explain analyze:

QUERY PLAN

Group (cost=378748.88..378770.02 rows=282 width=20) (actual
time=3229.93..3234.55 rows=30 loops=1)
-> Sort (cost=378748.88..378755.93 rows=2819 width=20) (actual
time=3229.74..3231.94 rows=2819 loops=1)
Sort Key: date(t1.tested), t2.testcnt
-> Nested Loop (cost=263.38..378587.34 rows=2819 width=20)
(actual time=12.99..3207.15 rows=2819 loops=1)
Join Filter: ("inner".testdate = date("outer".tested))
-> Seq Scan on tests t1 (cost=0.00..245.19 rows=2819
width=8) (actual time=3.88..8.62 rows=2819 loops=1)
-> Subquery Scan t2 (cost=263.38..265.44 rows=27
width=8) (actual time=0.01..1.11 rows=16 loops=2819)
-> Aggregate (cost=263.38..265.44 rows=27
width=8) (actual time=0.01..1.08 rows=16 loops=2819)
-> Group (cost=263.38..264.75 rows=275
width=8) (actual time=0.01..0.79 rows=275 loops=2819)
-> Sort (cost=263.38..264.07
rows=275 width=8) (actual time=0.00..0.23 rows=275 loops=2819\
)
Sort Key: date(tested)
-> Seq Scan on tests
(cost=0.00..252.24 rows=275 width=8) (actual time=4.07..7.42 ro\
ws=275 loops=1)
Filter: (result =
'3'::text)
Total runtime: 3235.62 msec
(14 rows)

Unfortunately, I'm not very good at reading these, so I'm not sure
what's going on. It looks to me like maybe it's running the inner query
many times instead of just one? That would not be right because there
are no dependencies between the inner and the outer query. It should
only need to run each once. I'm running PostgreSQL 7.3.2.
Any help would be much appreciated.

Roger Dahl

Nov 23 '05 #1
0 2302

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

Similar topics

6
by: Muharram Mansoorizadeh | last post by:
Hi there, I've a table with 18 millions of recordes shaped like this : Code nvarchar(80) , State int , school int , class int , Term nvarchar(80) The following query takes too long to run ( more...
1
by: PeteCresswell | last post by:
I've got a query that comes up with a dataset list in < 3 seconds when run just in DataSheet mode...but the same query, when run as intended (Append to an empty table) takes upwards of 30 minutes...
3
by: Mike Ridley | last post by:
I am running Access 97 on a Windows XP system. I run a query on an ODBC connected file that selects records for a particular date. I can hard code the selection criteria eg #25/03/2004# or get it...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
5
by: Nesa | last post by:
I have a stored procedure that wraps a moderately complex query over 5, 6 related tables. The performance of the procedure is unacceptably slow as it takes on average 5-10 min to complete. To...
0
by: zhenyuandeng | last post by:
Hi, When i call the procedure sp_jysczld_new from the Query Analyzer, it runs slowly. But when i extract the sql scripts from the procedure sp_jysczld_new, it runs quickly. Why? I don't...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
9
by: Bob Darlington | last post by:
The following query opens slowly the first time it is opened (6-7 seconds), but then is less than one second for the next random number of openings before slowing (6-7 seconds) again. SELECT...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.