2016-12-13 1 views
0

이 테이블과이 쿼리가 있으므로 실행 계획 비용이 더 낮은 동일한 쿼리가 필요합니다.낮은 실행 계획 비용을 사용하는 SQL 쿼리

CREATE TABLE PilotSkills 
(pilot CHAR(15) NOT NULL, 
plane CHAR(15) NOT NULL, 
PRIMARY KEY (pilot, plane)); 

CREATE TABLE Hangar -- ALWAYS CONTAINS AT LEAST ONE TUPLE 
(plane CHAR(15) PRIMARY KEY); 

SELECT DISTINCT pilot 
    FROM PilotSkills AS PS1 
WHERE NOT EXISTS 
     (SELECT * 
      FROM Hangar 
     WHERE NOT EXISTS 
       (SELECT * 
        FROM PilotSkills AS PS2 
       WHERE (PS1.pilot = PS2.pilot) 
        AND (PS2.plane = Hangar.plane))); 
+0

'더 저렴한 비용으로'당신은 실행 계획 비용에 대해서 말하고 있습니까? – TheGameiswar

+0

은 현재 계획을 여기에 붙여 넣으십시오 : https : //www.brentozar.com/pastetheplan/ – TheGameiswar

+0

실행을 알고 있어야합니다 계획은 항상 최대 100 % 합계입니다. 쿼리를 미세 조정하려는 경우 속도 향상, 숫자 읽기 수를 매개 변수로 낮추는 것과 같은 몇 가지 매개 변수를 추가 할 수 있습니다. – TheGameiswar

답변

0

이와 비슷한? rextester

: 실제 테이블과 데이터에 따라 http://rextester.com/ORG70581

create table PilotSkills (
    pilot char(15) not null 
, plane char(15) not null 
, primary key (pilot, plane) 
); 
insert into PilotSkills (pilot,plane) values (1,1),(1,2),(1,3),(2,1),(2,2),(3,3) 

create table Hangar(plane char(15) primary key); 
insert into Hangar values (1),(2),(3)  

select ps.Pilot 
from PilotSkills ps 
    inner join Hangar h on ps.Plane=h.Plane 
group by ps.Pilot 
having count(distinct ps.Plane)=(select count(distinct i.Plane) from Hangar i) 

, distinct는 그 count의 필요되지 않을 수 있습니다.