2011-09-14 2 views
1

더 좋은 방법이 있습니다. applications 테이블을보고 각 작업에 대한 특정 상태의 모든 응용 프로그램을 수집합니다.복잡한 SQL 쿼리를 최적화하십시오.

는 그래서는 다음과 같습니다 : 여기

select sum(pending) as pending, sum(screened) as screened, sum(interviewed) 
as interviewed, sum(accepted) as accepted, sum(offer) as offer, sum(hired) 
as hired, sum(declined) as declined, sum(rejected) as rejected, title, jobid 
from 
(

(select count(j.job_id) as pending, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Pending' group by 
j.job_id) 

union 

(select count(j.job_id) as screened, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Screened' group by 
j.job_id) 

union 

(select count(j.job_id) as interviewed, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Interviewed' group by 
j.job_id) 

union 

(select count(j.job_id) as accepted, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Accepted' group by 
j.job_id) 

union 

(select count(j.job_id) as offer, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Offer Made' group by 
j.job_id) 

union 

(select count(j.job_id) as hired, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Offer Accepted' group 
by j.job_id) 

union 

(select count(j.job_id) as declined, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Offer Declined' group 
by j.job_id) 

union 

(select count(j.job_id) as rejected, j.title as title, j.job_id as jobid from 
applications a, jobs j where j.job_id = a.job_id and status = 'Rejected' group by 
j.job_id) 

) as summ group by title order by title 

SHOW가 표를 작성한다 (당신이 읽을 수있는이 쿼리를 호출 할 경우, 가독성 제거 여분의 조합 열이) 여기

pending | screened | interviewed | accepted | offer | hired | job title 
0   0   0    0   0  2  dirt mover 
2   0   1    1   0  1  tree planter 
7   2   1    1   1  3  hole digger 

는 SQL입니다 SHOW는 TABLE의 일자리를 창출되는 응용 프로그램

다음
CREATE TABLE IF NOT EXISTS `applications` (
    `app_id` int(5) NOT NULL auto_increment, 
    `job_id` int(5) NOT NULL, 
    `status` varchar(25) NOT NULL, 
    `reviewed` datetime NOT NULL, 
    PRIMARY KEY (`app_id`), 
    UNIQUE KEY `app_id` (`app_id`), 
    KEY `job_id` (`job_id`), 
    KEY `status` (`status`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2720 ; 

CREATE TABLE IF NOT EXISTS `jobs` (
    `job_id` int(5) NOT NULL auto_increment, 
    `title` varchar(25) NOT NULL, 
    PRIMARY KEY (`app_id`), 
    KEY `job_id` (`job_id`), 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
+0

무엇이 필요합니까? 쿼리에서 얻을 것으로 예상되는 것은 무엇입니까? – santiagobasulto

+0

결과가 11 초보다 빠르게 설정됩니다. 결과 테이블은 제 질문의 맨 위에 있습니다. –

+1

InnoDB를 사용하십시오 !!!!!! – santiagobasulto

답변

3

PIVOT/크로스 탭 쿼리를 수행하는 것처럼 보입니다. 이 같은 일이 트릭을해야합니다.

SELECT COUNT(CASE 
       WHEN status = 'Pending' THEN 1 
      END) AS pending, 
     COUNT(CASE 
       WHEN status = 'Screened' THEN 1 
      END) AS screened, 
     /*Remaining ones left as an exercise for the reader*/ 
     title 
FROM applications a 
     JOIN jobs j 
     ON j.job_id = a.job_id 
GROUP BY title 
ORDER BY title 
+0

젠장. 훌륭한. –

0

첫째, 몇 가지 참고 사항 :

  1. 크로스 조인을 사용하지 마십시오 (세타 조인).

    SELECT * FROM table_1 INNER JOIN table_2 ON(table_1.id = table_2.t1_id) 
    
    1. 기본 키가 이미 고유 :이 정말 대신 table_2 WHERE table_1.id = table_2.t1_id

    사용, 된 table_1로부터

    SELECT *를 쿼리를 아래로 : 느리게 KEYS

자, 이제 할 수있는 방법은 다음과 같습니다.

,
SELECT 
    SUM(a.app_id), 
    a.status 
FROM 
    applications a INNER JOIN 
    jobs j ON (j.job_id=a.job_id) 
GROUP BY 
    j.job_id, 
    a.status 
당신이 비슷한 가야이 간단한 쿼리

: 제목이 반복되는 것을

JOB_TITLE | STATUS  | COUNT 
dirt mover | pending  | 0 
dirt mover | screened  | 0 
dirt mover | interviewed | 0 
dirt mover | accepted  | 0 
dirt mover | offer   | 0 
dirt mover | hired   | 2 

tree planter | pending  | 2 
tree planter | screened  | 0 
tree planter | interviewed | 1 
tree planter | accepted  | 1 
tree planter | offer   | 0 
tree planter | hired   | 1 
... 

참고하지만,이 같은 정보를 얻을 수 렸기 때문에 즉, 문제가되지 않습니다. 쿼리는 몇 배 빠르고 간단합니다.

관련 문제