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
laobaozi
V2EX  ›  MySQL

select count()耗时

  •  
  •   laobaozi · 2016-05-10 10:07:07 +08:00 · 4151 次点击
    这是一个创建于 2905 天前的主题,其中的信息可能已经有所发展或是发生改变。

    2 百多万条发帖记录,每条记录有自己的 postId 和对应的 UserId

    统计用户的发帖总数 select count(postId) ... where UserId=xxx

    一条执行下来要花 1.3 秒左右

    内存和 cpu 占用 10%左右

    请问这个时间正常么

    win的服务器 昨晚计划任务跑了一晚上 才跑了1/5 总感觉有什么不对劲

    第 1 条附言  ·  2016-05-10 10:44:19 +08:00
    已解决 给回复表的 UserId 加个索引就起飞了
    15 条回复    2016-06-06 20:13:04 +08:00
    php230
        1
    php230  
       2016-05-10 10:24:15 +08:00
    大数据量用 count 确实比较耗时,但是你这才 200W 的量也不大啊
    yangqi
        2
    yangqi  
       2016-05-10 10:24:31 +08:00
    innodb 的话基本上正常
    lxy
        3
    lxy  
       2016-05-10 10:30:23 +08:00
    我觉得你比我的快多了……
    https://v2ex.com/t/264252

    刚又试了一下:
    mysql> select count(comment_id) from comment;
    +-------------------+
    | count(comment_id) |
    +-------------------+
    | 3886472 |
    +-------------------+
    1 row in set (8.91 sec)
    mufeng
        4
    mufeng  
       2016-05-10 10:51:53 +08:00
    加索引就快了
    ipconfiger
        5
    ipconfiger  
       2016-05-10 11:19:26 +08:00
    一般来说大多数数据库慢的情况去检查一下索引就行了
    ytmsdy
        6
    ytmsdy  
       2016-05-10 11:36:16 +08:00
    mysql> Select count(*) from bcwzall;
    +----------+
    | count(*) |
    +----------+
    | 4393300 |
    +----------+
    1 row in set (0.02 sec)
    otakustay
        7
    otakustay  
       2016-05-10 12:09:43 +08:00 via iPad
    count 和 avg row size 有关,所以单独弄个只有 id 列的表,你的 count 就会飞快
    caixiexin
        8
    caixiexin  
       2016-05-10 12:26:20 +08:00 via Android
    count(*)写法是不是更好?
    taozywu
        9
    taozywu  
       2016-05-10 12:28:21 +08:00
    不建议 count(*),建议 count(1)
    wy315700
        10
    wy315700  
       2016-05-10 12:29:20 +08:00
    @caixiexin count(*) 是最慢的
    heaton_nobu
        11
    heaton_nobu  
       2016-05-10 12:35:31 +08:00
    在 UserId 列上加索引
    caixiexin
        12
    caixiexin  
       2016-05-10 13:30:05 +08:00 via Android
    @wy315700 这个说法的出处?记得 count(*)和 count(1)在 MySQL 中是等价的。
    count(列名) 还会忽略 null 行
    wy315700
        13
    wy315700  
       2016-05-10 13:31:01 +08:00
    @caixiexin MyISAM 是等价的, innodb 不等价
    david2016
        14
    david2016  
       2016-05-10 18:57:33 +08:00
    1 )该表的存储引擎是 MyISAM 还是 InnoDB
    MyISAM 表内部有表元数据的缓存, count(*)会比较快相对 InnoDB 表

    2 )如果是 InnoDB 表
    看下该表是否有辅助索引,如果有辅助索引的话, count(*)操作会走覆盖索引,走覆盖索引速度会比较快
    可以 explain 查看一下 具体优化器怎么走的
    alsotang
        15
    alsotang  
       2016-06-06 20:13:04 +08:00
    晕。竟然没加索引
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3279 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:11 · PVG 21:11 · LAX 06:11 · JFK 09:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.