站長資訊網
        最全最豐富的資訊網站

        mysql用什么代替in

        Mysql中用exists代替in;exists對外表用loop逐條查詢,每次查詢都會查看exists的條件語句,當exists里的條件語句能夠返回記錄行時,條件就為真,返回當前loop到的這條記錄。

        mysql用什么代替in

        exists對外表用loop逐條查詢,每次查詢都會查看exists的條件語句,當 exists里的條件語句能夠返回記錄行時(無論記錄行是的多少,只要能返回),條件就為真,返回當前loop到的這條記錄,反之如果exists里的條 件語句不能返回記錄行,則當前loop到的這條記錄被丟棄,exists的條件就像一個bool條件,當能返回結果集則為true,不能返回結果集則為 false

        如下:

        select * from user where exists (select 1);

        對user表的記錄逐條取出,由于子條件中的select 1永遠能返回記錄行,那么user表的所有記錄都將被加入結果集,所以與 select * from user;是一樣的

        又如下

        select * from user where exists (select * from user where userId = 0);

        可以知道對user表進行loop時,檢查條件語句(select * from user where userId = 0),由于userId永遠不為0,所以條件語句永遠返回空集,條件永遠為false,那么user表的所有記錄都將被丟棄

        not exists與exists相反,也就是當exists條件有結果集返回時,loop到的記錄將被丟棄,否則將loop到的記錄加入結果集

        總的來說,如果A表有n條記錄,那么exists查詢就是將這n條記錄逐條取出,然后判斷n遍exists條件

        in查詢相當于多個or條件的疊加,這個比較好理解,比如下面的查詢

        select * from user where userId in (1, 2, 3);

        等效于

        select * from user where userId = 1 or userId = 2 or userId = 3;

        not in與in相反,如下

        select * from user where userId not in (1, 2, 3);

        等效于

        select * from user where userId != 1 and userId != 2 and userId != 3;

        總的來說,in查詢就是先將子查詢條件的記錄全都查出來,假設結果集為B,共有m條記錄,然后在將子查詢條件的結果集分解成m個,再進行m次查詢

        值得一提的是,in查詢的子條件返回結果必須只有一個字段,例如

        select * from user where userId in (select id from B);

        而不能是

        select * from user where userId in (select id, age from B);

        而exists就沒有這個限制

        下面來考慮exists和in的性能

        考慮如下SQL語句

        1: select * from A where exists (select * from B where B.id = A.id);

        2: select * from A where A.id in (select id from B);

        查詢1.可以轉化以下偽代碼,便于理解

        for ($i = 0; $i < count(A); $i++) {   $a = get_record(A, $i); #從A表逐條獲取記錄   if (B.id = $a[id]) #如果子條件成立     $result[] = $a; } return $result;

        大概就是這么個意思,其實可以看到,查詢1主要是用到了B表的索引,A表如何對查詢的效率影響應該不大

        假設B表的所有id為1,2,3,查詢2可以轉換為

        select * from A where A.id = 1 or A.id = 2 or A.id = 3;

        這個好理解了,這里主要是用到了A的索引,B表如何對查詢影響不大

        下面再看not exists 和 not in

        1. select * from A where not exists (select * from B where B.id = A.id);

        2. select * from A where A.id not in (select id from B);

        看查詢1,還是和上面一樣,用了B的索引

        而對于查詢2,可以轉化成如下語句

        select * from A where A.id != 1 and A.id != 2 and A.id != 3;

        可以知道not in是個范圍查詢,這種!=的范圍查詢無法使用任何索引,等于說A表的每條記錄,都要在B表里遍歷一次,查看B表里是否存在這條記錄

        故not exists比not in效率高

        mysql中的in語句是把外表和內表作hash 連接,而exists語句是對外表作loop循環,每次loop循環再對內表進行查詢。一直大家都認為exists比in語句的效率要高,這種說法其實是不準確的。這個是要區分環境的。

        如果查詢的兩個表大小相當,那么用in和exists差別不大

        如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:

        例如:表A(小表),表B(大表)

        1:

        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列的索引。

        相反的

        2:

        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要快

        in 與 =的區別

        select name from student where name in ('zhang','wang','li','zhao');

        select name from student where name='zhang' or name='li' or name='wang' or name='zhao'

        的結果是相同的。

        推薦教程:mysql視頻教程

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 日批日出水久久亚洲精品tv| 精品国产第一国产综合精品| 久久久精品国产sm调教网站| 国产亚洲精品免费视频播放| 大伊香蕉精品一区视频在线| 亚洲精品无码不卡在线播HE| 精品国产免费一区二区三区| 免费精品99久久国产综合精品| 久久精品中文闷骚内射| 四虎国产精品永久在线看| 精品国产一区二区三区2021| 91精品国产色综久久| 精品久久一区二区三区| 99精品视频免费| 国产午夜福利精品一区二区三区| 欧洲成人午夜精品无码区久久| 宅男在线国产精品无码| 合区精品中文字幕| 色婷婷久久久SWAG精品| 日韩亚洲精品福利| 无码国内精品久久人妻麻豆按摩| 欧美精品丝袜久久久中文字幕 | 国产一在线精品一区在线观看| 国产成人精品cao在线| 丰满人妻熟妇乱又伦精品劲 | 亚洲AV无码久久精品狠狠爱浪潮| 一区二区日韩国产精品| 亚洲麻豆精品国偷自产在线91| 香蕉99久久国产综合精品宅男自 | 亚洲午夜成人精品电影在线观看 | 久久五月精品中文字幕| 欧美精品黑人粗大欧| 亚洲电影日韩精品 | 亚洲精品成人区在线观看| 亚洲国产精品人人做人人爱| 无码人妻精品一区二区三区久久久| 亚洲av无码乱码国产精品fc2| 国产久热精品无码激情| 国产欧美久久久精品| 亚洲人成亚洲精品| 精品午夜福利1000在线观看|