【資料庫摘要】5_Sql_IN

來源:互聯網
上載者:User

標籤: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 擷取很多其它的資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.