473,387 Members | 1,798 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.

GROUP BY and SORT

Hello,

DB2 seems to do a SORT before every GROUP BY action. At least if I look
at the query plan (using Visual Explain in the Command Center) of

-- connected to the sample db
SELECT WORKDEPT, SUM(SALARY)
FROM EMPLOYEE
GROUP BY WORKDEPT

a SORT is performed before GRPBY. I can imagine that it's necessary,
but why?

Greetings, Martin.

Dec 1 '06 #1
1 3226
Ma**********@mailbox.tu-dresden.de wrote:
Hello,

DB2 seems to do a SORT before every GROUP BY action. At least if I look
at the query plan (using Visual Explain in the Command Center) of

-- connected to the sample db
SELECT WORKDEPT, SUM(SALARY)
FROM EMPLOYEE
GROUP BY WORKDEPT

a SORT is performed before GRPBY. I can imagine that it's necessary,
but why?
The rows in your above example could be read in any arbitrary order
(internally). So calculating the SUM for each group that way would mean
that DB2 has to calculate "n" sums in parallel where "n" is the number of
groups (= #workdept). If you do a sort first (by workdept), you can
pipeline things: calculate sum for first group, return result, then
calculate sum for 2nd group, return result, sum for 3rd group, return
result, ... One advantage is that memory consumption for the summation is
known up front and fixed.

Note that you _must not_ rely on GROUP BY doing a SORT internally. A sort
is surely just one option to implement a grouping. For example, if the
DBMS knows up front that you have only 2 workdepts, it may very well pick
the parallel approach. In other words, the _only_ thing that guarantees a
specific order of rows in a result set is an ORDER BY clause. Period.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 1 '06 #2

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

Similar topics

4
by: David Link | last post by:
Hi, Why does adding SUM and GROUP BY destroy performance? details follow. Thanks, David Link s1.sql: SELECT t.tid, t.title, COALESCE(s0c100r100.units, 0) as w0c100r100units,
5
by: Mike Nolan | last post by:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have to throw in an 'order by' clause to get the output in ascending group order. Is this something that most RDB's have...
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
2
by: adrian.chandler | last post by:
Hi all, I have been using letter and symbol codes such as GNU< GNU\ GNU} GNUˆ in an Access table. I was surprised to see that when the table was sorted on this field, the order is: GNUˆ...
9
by: thatguyNYC | last post by:
Hey there-- I have a report with a one-to-many relationship on it. The EmployeeName field is grouped (by primary key) and shown in its own header and the EmployeeHistories are displayed in the...
0
by: srikantmatihali | last post by:
Hi all, I want to change the value of N in Group Sort Expert window (In Crystal Reports) explicitly from text box using some c# application. This will help us in devloping dynamic crystal...
1
by: Jamie Moore | last post by:
<?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company>...
1
by: Michael Franz | last post by:
Hi! I am quite new in access and had not taken a programer course. I just learned access through research, so please do understand me cause I realy need help on this one. Any info is very much...
7
by: June Ye | last post by:
Hi, I have a compilation error with the list sort(Comp). Following is the description of the error: error C2664: 'void __thiscall std::list<struct people,class std::allocator<struct people>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.