標籤:style http color 使用 os io 資料 ar
IN 操作符
IN 操作符同意您在 WHERE 子句中尋找多個值。
SQL IN 文法
SELECT column_name(s)FROM table_nameWHERE column_name IN (value1,value2,...);
IN 操作符執行個體
(使用Northwind樣本資料庫) SELECT * FROM Customers WHERE City IN (‘Paris‘,‘London‘);
MongoDB 文法
db.collection.find( { type: { $in: [ ‘food‘, ‘snacks‘ ] } } )
EXISTS
NOT EXISTS,exists的使用方法跟in不一樣,一般都須要和子表進行關聯,並且關聯時,須要用索引,這樣就能夠加高速度
exists是用來推斷是否存在的,當exists(查詢)中的查詢存在結果時則返回真,否則返回假。not exists則相反。
in 是把外表和內表作hash 串連,而exists是對外表作loop迴圈,每次loop迴圈再對內表進行查詢。一直以來覺得exists比in效率高的說法是不準確的。
假設查詢的兩個表大小相當,那麼用in和exists區別不大。
假設兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:
比如:表A(小表),表B(大表)
-
select * from A where cc in (select cc from B)效率低,用到了A表上cc列的索引;
-
select * from A where exists(select cc from B where cc=A.cc)效率高,用到了B表上cc列的索引。
-
select * from B where cc in (select cc from A)效率高,用到了B表上cc列的索引;
-
select * from B where exists(select cc from A where cc=B.cc)效率低,用到了A表上cc列的索引。
not in 和not exists假設查詢語句使用了not in 那麼內外表都進行全表掃描,沒實用到索引;
而not extsts 的子查詢依舊能用到表上的索引。所以不管那個表大,用not exists都比not in要快。
你也能夠訪問:http://txidol.github.io 擷取很多其它的資訊