dbTalk Databases Forums  

How can I optimize this long silly query?

comp.databases comp.databases


Discuss How can I optimize this long silly query? in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Ruan
 
Posts: n/a

Default How can I optimize this long silly query? - 07-28-2007 , 04:06 PM






I have two tables, one called Department and the other called employee;

Department has two field: Deptname and id_dept

Employee has two fields: Empname and id_dept

I want to find out the name of those departments where the number of
employees is the most among all department or one less than the most.

This is my query:

SELECT Deptname FROM (Department AS D INNER JOIN Employee AS E ON
D.ID_DEPT=E.ID_DEPT) GROUP BY Deptname HAVING COUNT(*)>=(SELECT
MAX(Number_of_Employees) FROM (SELECT COUNT (*) AS Number_of_Employees
FROM(Department AS D INNER JOIN Employee AS E ON D.ID_DEPT=E.ID_DEPT) GROUP
BY FILENAME)-1 ;





It seems that there is a lot of redundency.







Reply With Quote
  #2  
Old   
Lennart
 
Posts: n/a

Default Re: How can I optimize this long silly query? - 07-29-2007 , 07:23 AM






John Ruan wrote:
Quote:
I have two tables, one called Department and the other called employee;

Department has two field: Deptname and id_dept

Employee has two fields: Empname and id_dept

I want to find out the name of those departments where the number of
employees is the most among all department or one less than the most.

This is my query:

SELECT Deptname FROM (Department AS D INNER JOIN Employee AS E ON
D.ID_DEPT=E.ID_DEPT) GROUP BY Deptname HAVING COUNT(*)>=(SELECT
MAX(Number_of_Employees) FROM (SELECT COUNT (*) AS Number_of_Employees
FROM(Department AS D INNER JOIN Employee AS E ON D.ID_DEPT=E.ID_DEPT) GROUP
BY FILENAME)-1 ;





It seems that there is a lot of redundency.
That is quite normal for this kind of query. You don't mention your
dbms, but atleast DB2 (and I think SQL server) support Common Table
Expressions which is /sort of/ a named inline view. Using such your
query would become:

-- assuming deptname is unique in department
with dept_cnt (Deptname, cnt) as (
SELECT Deptname, count(*)
FROM Department AS D INNER JOIN Employee AS E
ON D.ID_DEPT=E.ID_DEPT
GROUP BY Deptname
) select Deptname
from dept_cnt
where cnt >= (select max(cnt) - 1 from dept_cnt)

You could of course create a normal view out of dept_cnt and use that
instead.

create view dept_cnt (Deptname, cnt) as (
SELECT Deptname, count(*)
FROM Department AS D INNER JOIN Employee AS E
ON D.ID_DEPT=E.ID_DEPT
GROUP BY Deptname
);

select Deptname
from dept_cnt
where cnt >= (select max(cnt) - 1 from dept_cnt);

As for you query, you can shorten it a bit by not using a join in the
subquery (since all the info you need is in the Employee table)

SELECT Deptname
FROM Department AS D INNER JOIN Employee AS E
ON D.ID_DEPT=E.ID_DEPT
GROUP BY Deptname
HAVING COUNT(*) >= (
SELECT MAX(Number_of_Employees) - 1 FROM (
SELECT COUNT (*) AS Number_of_Employees
FROM Employee AS E
GROUP BY ID_DEPT
) X
)
;




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.