View previous topic :: View next topic
|
Author |
Message |
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
Hi,
Please advise SQL cost efficient SQL query for the below requirement:
Table structure :
Employee Table
CLASS CHAR(2) NOT NULL,
MARKS DECIMAL(3,0) NOT NULL,
STUDENT_NAME CHAR (30) NOT NULL
Requirement:
To get student details with highest marks from each class.
Please suggest.[/u] |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
Any reply please |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
Less than 1 hour after posting you are asking for replies when response are made by people IF they feel like it. If you want a quick response then go and pay someone for it.
I suggest no-one replies to this until Friday. |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
Apologies for my last reply.
I should have refrain from posting quick reply, i will ensure to be more patient going forward.
Awaiting all valuable response to this. |
|
Back to top |
|
|
daveporcelan
Active Member
Joined: 01 Dec 2006 Posts: 792 Location: Pennsylvania
|
|
|
|
This requirement feels like a homework assignment to me.
The purpose of the such an assignment is for the Student to use their brain and figure it out for THEMSELVES.
Soliciting an answer from the internet is not the intent of such assignment.
If this is actually work related, then the employee is overpaid. |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Hi Kunal,
What you have tried so far with this query?
What are the difficulties you are facing?
Regards,
Chandan |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
Tried below query & got desired output, however performance of this query is very bad(taking 25-30 seconds approx) probably due to high volume of table(about 600,000).
Thus please assist me in optimizing below Db2 SQL query.
Note: Table & column names have changes to maintain data integrity.
SELECT CLASS, MARKS, STUDENT_NAME, STUDENT_SEX
FROM STUDENT
WHERE (CLASS, MARKS) IN
( SELECT CLASS, MAX(MARKS)
FROm STUDENT
GROUP BY CLASS
)
AND
CLASS > ' ' condition needed to eliminate blank values of CLASS
ORDER BY CLASS ASC
WITH UR
Thank you in advance for help ! |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Just try to EXPLAIN the query and see which table causing the issue
Also can't you club CLASS the condition in Subquery itself?
Reagards,
Chandan |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
There is only one table which is STUDENT
I also tried to put condition WHERE CLASS> ' ' into Sus-query but same result.
Please advise |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
Does anyone has improvement suggestion on above query then please let me know |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
is there an index? more than one?
what was the result of the explain? |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
yes there are 5 indexes defines on the table.
Explain recommends to create index for column on which MAX function is applied.
On the query perspective, I would like to know can there be any better query of achieving above result ? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
If the EXPLAIN says the query would benefit from the addition of another index, you should create the new index and test . . .
If that does Not help, then look at other alternatives. |
|
Back to top |
|
|
Eshwar CICS
New User
Joined: 18 May 2011 Posts: 47 Location: India
|
|
|
|
As an alternative, try using OLAP functions (DENSE_RANK and PARTITION BY). |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
dick scherrer wrote: |
Hello,
If the EXPLAIN says the query would benefit from the addition of another index, you should create the new index and test . . .
If that does Not help, then look at other alternatives. |
Hi Dick,
You would be aware adding index requires a detailed impact study considering volumes of records, index maintenance redundancy etc..
Also, Explain usually says to add index for columns on which aggregate function is used.
In my case, adding index is not feasible as we already have 5 indexes on table.
Guys, would request you all to emphasize on query optimization only as we really cannot add/change indexes on table. |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
Eshwar CICS wrote: |
As an alternative, try using OLAP functions (DENSE_RANK and PARTITION BY). |
Hi Eshwar,
We dont have functions like DENSE_RANK, PARTITION BY in DB2. |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Hi Kunal,
I am not sure which DB2 version you are using, but we do have these fuctions available in DB2.
Please check with shop if these fuctions are present or not
Regards,
Chandan |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
Guys, would request you all to emphasize on query optimization only as we really cannot add/change indexes on table. |
Sorry, but that makes NO sense. . . One of the most powerful ways to optimize a query is to provide the needed direct access to the data. Saying "We can't" only means someone does not want to.
Quote: |
adding index is not feasible as we already have 5 indexes on table. |
Is 5 some kind of magic number on your system? Many tables have twice that and then some . . .
It all depends . . . You should do some testing with the recommended index addition and see the results.
Keep in mind that a row is added only once but may be read many times and in different ways. |
|
Back to top |
|
|
kunal jain
New User
Joined: 19 May 2011 Posts: 59 Location: India
|
|
|
|
dick scherrer wrote: |
Hello,
Quote: |
Guys, would request you all to emphasize on query optimization only as we really cannot add/change indexes on table. |
Sorry, but that makes NO sense. . . One of the most powerful ways to optimize a query is to provide the needed direct access to the data. Saying "We can't" only means someone does not want to.
Quote: |
adding index is not feasible as we already have 5 indexes on table. |
Is 5 some kind of magic number on your system? Many tables have twice that and then some . . .
It all depends . . . You should do some testing with the recommended index addition and see the results.
Keep in mind that a row is added only once but may be read many times and in different ways. |
Thanks Dick, for your response. I will coorinate with DBA in our shop to see if Index can be added. |
|
Back to top |
|
|
|