mysql 查询 Copying to tmp table 疑问 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
sujin190
V2EX    MySQL

mysql 查询 Copying to tmp table 疑问

  •  
  •   sujin190
    snower 2015 年 4 月 17 日 3990 次点击
    这是一个创建于 3937 天前的主题,其中的信息可能已经有所发展或是发生改变。
    select mobile, count(*) as cnt from trading_order where order_at>='2015-04-04 00:00:00' and order_at<'2015-04-17 00:00:00' and mobile>'' and status>3000 and mobile in (select o.mobile from trading_order_goods g left join trading_order o on g.order_id=o.order_id where g.trading_id='551e656c3f5bdd24568b4567' and o.order_at>='2015-04-04 00:00:00' and o.order_at<'2015-04-17 00:00:00' and o.mobile>'' and o.status>3000 group by o.mobile );

    InnoDB引擎,2w数据,查询时间超过60s,explain
    +----+--------------------+---------------+--------+--------------------------------------------+------------------+---------+-----------------+------+----------------------------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------------+---------------+--------+--------------------------------------------+------------------+---------+-----------------+------+----------------------------------------------+
    | 1 | PRIMARY | trading_order | range | index_mobile,index_order_at | index_order_at | 4 | NULL | 2959 | Using where; Using temporary; Using filesort |
    | 2 | DEPENDENT SUBQUERY | g | ref | index_order_id,index_trading_id | index_trading_id | 74 | const | 1659 | Using where; Using temporary; Using filesort |
    | 2 | DEPENDENT SUBQUERY | o | eq_ref | index_order_id,index_mobile,index_order_at | index_order_id | 74 | gege.g.order_id | 1 | Using where |
    +----+--------------------+---------------+--------+--------------------------------------------+------------------+---------+-----------------+------+----------------------------------------------+
    3 rows in set (0.00 sec)

    show processlist查看发现卡在了
    Copying to tmp table | select mobile, count(*) as cnt from trading_order where order_at>='2015-04-04 00:00:00' and order_at
    这是为什么呢?
    8 条回复    2015-04-17 17:14:34 +08:00
    hahasong
        1
    hahasong  
       2015 年 4 月 17 日
    搞联合查询带这么多条件还玩子句,不慢才怪。明显不合理。在代码里拆分一下吧,宁可拆成二次查询
    sujin190
        2
    sujin190  
    OP
       2015 年 4 月 17 日
    @hahasong 可是就算如此,join那部分就很快啊,万条数据而已,太不正常了吧
    ElmerZhang
        3
    ElmerZhang  
       2015 年 4 月 17 日   1
    你这个SQL的扫描行数按explain的结果来看,大概会是 2959 * 1659 * 1 = 4908981
    sujin190
        4
    sujin190  
    OP
       2015 年 4 月 17 日
    @ElmerZhang mysql这时候要扫描这么多数据么?这种情况和直接把手机号写在in里有什么区别呢?
    whiteblack
        5
    whiteblack  
       2015 年 4 月 17 日   1
    DEPENDENT SUBQUERY 的问题,这里涉及到in的执行过程,具体看这篇博文

    http://www.cnblogs.com/zhengyun_ustc/p/slowquery3.html
    sujin190
        6
    sujin190  
    OP
       2015 年 4 月 17 日
    @xiaobaigsy 好吧,了解了,感谢,好坑啊,为什么要设计成这样啊?
    zhanglp888
        7
    zhanglp888  
       2015 年 4 月 17 日
    有了group by后,必然会慢
    whiteblack
        8
    whiteblack  
       2015 年 4 月 17 日
    @sujin190 用久了mysql 就知道了,这玩意全是坑。。。。已经不知道发现多少诡异的mysql问题,最后了解到是mysql的bug了。。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5513 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 07:46 PVG 15:46 LAX 23:46 JFK 02:46
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86