我有2個表假設TABLE_1 & TABLE_2 TABLE_1有56列和120萬條記錄 我的查詢像SQL NOT IN查詢花費的時間與類似的表
TABLE_1像
RollNumber | Subject | G | Part | Status
------------------------------------------------
1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 2 | 1
1 | 2 | 1 | 1 | 1
1 | 2 | 1 | 2 | 5
1 | 3 | 1 | 1 | 0
1 | 3 | 1 | 2 | 1
2 | 1 | 2 | 1 | 1
2 | 1 | 2 | 2 | 1
2 | 2 | 2 | 1 | 1
2 | 2 | 2 | 2 | 1
2 | 3 | 2 | 1 | 1
2 | 3 | 2 | 2 | 1
3 | 1 | 2 | 1 | 1
3 | 1 | 2 | 2 | 1
3 | 2 | 2 | 1 | 1
3 | 2 | 2 | 2 | 1
3 | 3 | 2 | 1 | 0
3 | 3 | 2 | 2 | 1
我想來自table_1的所有RollNumber(由第2列和第3列組成),其中任何狀態爲0但不希望同時具有狀態= 5(或不等於1)的學生
我試過
select * from table_1 as t1
inner join table_2 as t2
on t1.column2 = t2.column2 and t1.column3 = t2.column3 and t1.column4 = t2.column4
where t1.column1 not in
(select column1 from table_1 where status = 5)
這是我qhole查詢的最內部查詢
我也曾嘗試EXCEPT條款
兩個查詢需要很長時間才能執行
爲什麼使用子查詢來過濾'status'? –
請顯示查詢的[執行計劃](http://stackoverflow.com/questions/7359702/how-do-i-obtain-a-query-execution-plan)。你有索引添加到'column1','column2','column3'和'status'嗎? – kennytm
@kennytm我已編輯的問題,請現在審查,,,謝謝 – Brainiac