V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ioven
V2EX  ›  PHP

mysql 语句,球分析

  •  
  •   ioven · 2016-08-02 15:35:12 +08:00 · 2195 次点击
    这是一个创建于 2795 天前的主题,其中的信息可能已经有所发展或是发生改变。
    explain select `id` from `ao_articles` where (select count(*) from `ao_articles_tags` inner join `ao_articles_relationships` on `ao_articles_tags`.`id` = `ao_articles_relationships`.`tag_id` where `ao_articles_relationships`.`cid` = `ao_articles`.`id` and `id` in (1,2,3,4,5)) >= 1 limit 10000
    

    应该已经添加索引,为什么还会扫描全表?

    第 1 条附言  ·  2016-08-02 17:06:33 +08:00
    上面是 Eloquent 自动生成的 join ,使用构造器重写效率提高

    ```mysql
    DB::table('articles')
    ->join('articles_relationships', 'articles_relationships.cid', '=', 'articles.id')
    ->join('articles_tags', 'articles_relationships.tag_id', '=', 'articles_tags.id')
    ->select('articles.id')
    ->whereIn('articles_tags.id', [1,2,3])
    ->lists('id');
    ```
    2 条回复    2016-08-02 16:49:06 +08:00
    DT27
        1
    DT27  
       2016-08-02 16:44:24 +08:00
    我一直没搞明白索引。。。帮不了忙~
    huixia0010
        2
    huixia0010  
       2016-08-02 16:49:06 +08:00
    count(*) 是全表扫描,自己百度下一目了然~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3215 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:07 · PVG 21:07 · LAX 06:07 · JFK 09:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.