473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,e nabledcodes.sal a,spettacoli.co de
from
tkstore,enabled codes,spettacol i
where
tkstore.id = enabledcodes.tk store_id and
tkstore.gruppo in ('amit') and
enabledcodes.sa la = spettacoli.teat ro and
spettacoli.syst em = 0 ;
The explain is :
Hash Join (cost=173.06..3 810.20 rows=115782 width=42) (actual
time=16.248..12 65.331 rows=380736 loops=1)
Hash Cond: ("outer".tea tro = "inner".sal a)
-> Seq Scan on spettacoli (cost=0.00..134 2.35 rows=23935 width=24)
(actual time=0.012..35. 999 rows=26846 loops=1)
Filter: (system = 0)
-> Hash (cost=168.28..1 68.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".tkstor e_id = "inner".id)
-> Seq Scan on enabledcodes (cost=0.00..113 .45 rows=6245
width=16) (actual time=0.007..3.4 39 rows=6245 loops=1)
-> Hash (cost=4.39..4.3 9 rows=34 width=23) (actual
time=0.213..0.2 13 rows=0 loops=1)
-> Seq Scan on tkstore (cost=0.00..4.3 9 rows=34
width=23) (actual time=0.024..0.1 87 rows=33 loops=1)
Filter: ((gruppo)::text = 'amit'::text)
Total runtime: 1330.843 ms

2) explain analyze
select
tkstore.gruppo, tkstore.cassa,e nabledcodes.sal a,spettacoli.co de
from
tkstore,enabled codes,spettacol i
where
tkstore.id = enabledcodes.tk store_id and
tkstore.gruppo in ('amit') and
enabledcodes.sa la = spettacoli.teat ro and
spettacoli.syst em = 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..3 810.20 rows=115782 width=42) (actual
time=16.227..13 92.206 rows=380736 loops=1)
Hash Cond: ("outer".tea tro = "inner".sal a)
-> Seq Scan on spettacoli (cost=0.00..134 2.35 rows=23935 width=24)
(actual time=0.011..47. 329 rows=26846 loops=1)
Filter: (system = 0)
-> Hash (cost=168.28..1 68.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".tkstor e_id = "inner".id)
-> Seq Scan on enabledcodes (cost=0.00..113 .45
rows=6245 width=16) (actual time=0.008..3.4 69 rows=6245 loops=1)
-> Hash (cost=4.39..4.3 9 rows=34 width=23) (actual
time=0.214..0.2 14 rows=0 loops=1)
-> Seq Scan on tkstore (cost=0.00..4.3 9 rows=34
width=23) (actual time=0.023..0.1 81 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 YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #1
1 1816
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
5343
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, authorid, authorname, text, posted FROM comments WHERE status = 'normal' ORDER BY posted DESC LIMIT 5; The purpose of this query is to list the five most recent (non-deleted) comments. Here is the table structure:
0
486
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 still execute very slowly. I've looked over all the relevant suggestions for optimization and so forth, and there's nothing I can tell that I'm missing. An example of a query is to get all the words (the cg.cw field) in a particular...
4
10425
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 Query Analyzer almost instantaneously), or very slow (ie 30 to 40 seconds to return results), and I'm trying to work out how I improve performance. Essentially the query I'm running is nothing more complex than:
5
3242
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
2722
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 timestamp(0), i am trying to select all records within a given date range. I have an index on 'in_date' and I also have an index on date(in_date). The queries I am doing are between dates rather than timestamps. Immeadiately prior to running the...
4
24462
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 'Amount', and also have a field call 'Cumulative' and have the sum grow with each record. Using "MyTable", the query results are... ID or Date Amount Cumulative --------------- ------ ----------
12
7572
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 DB and query the table. The table "audit" primary key is "Line". Another weird thing (but I guess that's another post) is that, while it's doing the dataset Fill, my PC is slowed done substantially. But I don't know why that would happen since...
21
6958
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 pc.product_id = p.id where pc.category_id = $category_id order by p.title
0
2666
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 seeing. First question: given a table defined as: CREATE TABLE `oa_location` (
4
2678
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 possible to retrieve certain pricing data for a list of companies. I've run into a few queries that seem to be taking much longer then others and ultimately slow down my function call to a point where it is no longer useful. For instance... I have...
0
8325
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.