473,387 Members | 1,892 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,387 software developers and data experts.

slow query if add order by



I have 2 query that differ only for order by clause.
The time of execution of the two query is a lot of different.
1) explain analyze
select
tkstore.gruppo,tkstore.cassa,enabledcodes.sala,spe ttacoli.code
from
tkstore,enabledcodes,spettacoli
where
tkstore.id = enabledcodes.tkstore_id and
tkstore.gruppo in ('amit') and
enabledcodes.sala = spettacoli.teatro and
spettacoli.system = 0 ;
The explain is :
Hash Join (cost=173.06..3810.20 rows=115782 width=42) (actual
time=16.248..1265.331 rows=380736 loops=1)
Hash Cond: ("outer".teatro = "inner".sala)
-> Seq Scan on spettacoli (cost=0.00..1342.35 rows=23935 width=24)
(actual time=0.012..35.999 rows=26846 loops=1)
Filter: (system = 0)
-> Hash (cost=168.28..168.28 rows=1913 width=31) (actual
time=15.995..15.995 rows=0 loops=1)
-> Hash Join (cost=4.47..168.28 rows=1913 width=31) (actual
time=1.021..12.693 rows=5076 loops=1)
Hash Cond: ("outer".tkstore_id = "inner".id)
-> Seq Scan on enabledcodes (cost=0.00..113.45 rows=6245
width=16) (actual time=0.007..3.439 rows=6245 loops=1)
-> Hash (cost=4.39..4.39 rows=34 width=23) (actual
time=0.213..0.213 rows=0 loops=1)
-> Seq Scan on tkstore (cost=0.00..4.39 rows=34
width=23) (actual time=0.024..0.187 rows=33 loops=1)
Filter: ((gruppo)::text = 'amit'::text)
Total runtime: 1330.843 ms

2) explain analyze
select
tkstore.gruppo,tkstore.cassa,enabledcodes.sala,spe ttacoli.code
from
tkstore,enabledcodes,spettacoli
where
tkstore.id = enabledcodes.tkstore_id and
tkstore.gruppo in ('amit') and
enabledcodes.sala = spettacoli.teatro and
spettacoli.system = 0
order by 2;


The explain is :
Sort (cost=13548.08..13837.53 rows=115782 width=42) (actual
time=10631.389..10774.964 rows=380736 loops=1)
Sort Key: tkstore.cassa
-> Hash Join (cost=173.06..3810.20 rows=115782 width=42) (actual
time=16.227..1392.206 rows=380736 loops=1)
Hash Cond: ("outer".teatro = "inner".sala)
-> Seq Scan on spettacoli (cost=0.00..1342.35 rows=23935 width=24)
(actual time=0.011..47.329 rows=26846 loops=1)
Filter: (system = 0)
-> Hash (cost=168.28..168.28 rows=1913 width=31) (actual
time=16.018..16.018 rows=0 loops=1)
-> Hash Join (cost=4.47..168.28 rows=1913 width=31) (actual
time=1.023..12.680 rows=5076 loops=1)
Hash Cond: ("outer".tkstore_id = "inner".id)
-> Seq Scan on enabledcodes (cost=0.00..113.45
rows=6245 width=16) (actual time=0.008..3.469 rows=6245 loops=1)
-> Hash (cost=4.39..4.39 rows=34 width=23) (actual
time=0.214..0.214 rows=0 loops=1)
-> Seq Scan on tkstore (cost=0.00..4.39 rows=34
width=23) (actual time=0.023..0.181 rows=33 loops=1)
Filter: ((gruppo)::text = 'amit'::text)
Total runtime: 10858.720 ms


The db is ANALYZED;
Which is the reason of this difference??
What can I do ??
Thank

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #1
1 1801
Paolo Tavalazzi wrote:

I have 2 query that differ only for order by clause.
The time of execution of the two query is a lot of different.


How much sort_mem are you using ?

Regards
Gaetano Mendola

Nov 23 '05 #2

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

Similar topics

2
by: Tim Fountain | last post by:
We've recently enabled slow query logging on a server and it's proving interesting seeing which queries are bogging things down. This one is puzzling me a little: SELECT articleid, type,...
0
by: Jesse Sheidlower | last post by:
I'm struggling with speed issues on some queries that I would have expected to be relatively fast. Perhaps even more frustratingly, when I've tried to break these down into their components, they...
4
by: M Wells | last post by:
Hi All, I have a table that currently contains approx. 8 million records. I'm running a SELECT query against this table that in some circumstances is either very quick (ie results returned in...
5
by: garydevstore | last post by:
Hi, I have a table defined as CREATE TABLE ( IDENTITY (1, 1) NOT NULL , NULL , NULL , NOT NULL , NULL , (255) COLLATE SQL_Latin1_General_CP1_CS_AS
4
by: psql-mail | last post by:
I am running a SELECT to get all tuples within a given date range. This query is much slwoer than i expected - am i missing something? I have a table 'meta' with a column 'in_date' of type...
4
by: cefrancke | last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps another faster way to have a cumulative sum column (call a vba function?). For example, I want to sum up all values under...
12
by: VMI | last post by:
For some reason, the process of retrieving data (about 20 records) from an Access table that has 400K records to a dataTable is taking over 3 mins. to complete. Below is my code to connect to the...
21
by: CSN | last post by:
I have a pretty simple select query that joins a table (p) with 125K rows with another table (pc) with almost one million rows: select p.* from product_categories pc inner join products p on...
0
by: Dave Hammond | last post by:
Hi All, I'm trying to use the slow-query-log (with --log-queries-not-using-indexes enabled) to determine if any queries need optimization, and have a few questions about some entries I'm...
4
by: bfoo75 | last post by:
Hi there, I'm new to this forum, but I've come here quite a bit to find solutions to problems other people have encountered... I'm currently trying to query a database up to 5000 times as fast as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.