sql語句in


  在今天之前sql一直用in語句,知道今天遇到一張數據量很大的表查了三分鍾才查出來,這才意識到數據庫優化有多重要.作為一名開發人員,首先從優化sql語句開始。

  之前用in寫sql是這樣的

select * from m_package where userId in(  select id from sys_user where newDorm='2號樓' and `newRoomNumber` = 'N413') ;
執行結果:/* 0 rows affected, 18 rows found. Duration for 1 query: 0.407 sec. */ 

  exists寫sql語句

select * from m_package as pack where
exists (select id from sys_user as user where newDorm='2號樓' and `newRoomNumber` = 'N413' and pack.userId = user.id);
執行結果:/* 0 rows affected, 18 rows found. Duration for 1 query: 0.297 sec. */ 

inner join寫sql語句

select * from m_package as pack inner join sys_user as user where pack.userId = user.id and newDorm='2號樓' and `newRoomNumber` = 'N413';
執行結果:/* 0 rows affected, 18 rows found. Duration for 1 query: 0.234 sec. */ 

可以看出來執行效率 inner join > exists > in


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM