V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 · May 10, 2016 · 4842 views
    This topic created in 3645 days ago, the information mentioned may be changed or developed.

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

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

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

    内存和 cpu 占用 10%左右

    请问这个时间正常么

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

    Supplement 1  ·  May 10, 2016
    已解决 给回复表的 UserId 加个索引就起飞了
    15 replies    2016-06-06 20:13:04 +08:00
    php230
        1
    php230  
       May 10, 2016
    大数据量用 count 确实比较耗时,但是你这才 200W 的量也不大啊
    yangqi
        2
    yangqi  
       May 10, 2016
    innodb 的话基本上正常
    lxy
        3
    lxy  
       May 10, 2016
    我觉得你比我的快多了……
    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  
       May 10, 2016
    加索引就快了
    ipconfiger
        5
    ipconfiger  
       May 10, 2016
    一般来说大多数数据库慢的情况去检查一下索引就行了
    ytmsdy
        6
    ytmsdy  
       May 10, 2016
    mysql> Select count(*) from bcwzall;
    +----------+
    | count(*) |
    +----------+
    | 4393300 |
    +----------+
    1 row in set (0.02 sec)
    otakustay
        7
    otakustay  
       May 10, 2016 via iPad
    count 和 avg row size 有关,所以单独弄个只有 id 列的表,你的 count 就会飞快
    caixiexin
        8
    caixiexin  
       May 10, 2016 via Android
    count(*)写法是不是更好?
    taozywu
        9
    taozywu  
       May 10, 2016
    不建议 count(*),建议 count(1)
    wy315700
        10
    wy315700  
       May 10, 2016
    @caixiexin count(*) 是最慢的
    heaton_nobu
        11
    heaton_nobu  
       May 10, 2016
    在 UserId 列上加索引
    caixiexin
        12
    caixiexin  
       May 10, 2016 via Android
    @wy315700 这个说法的出处?记得 count(*)和 count(1)在 MySQL 中是等价的。
    count(列名) 还会忽略 null 行
    wy315700
        13
    wy315700  
       May 10, 2016
    @caixiexin MyISAM 是等价的, innodb 不等价
    david2016
        14
    david2016  
       May 10, 2016
    1 )该表的存储引擎是 MyISAM 还是 InnoDB
    MyISAM 表内部有表元数据的缓存, count(*)会比较快相对 InnoDB 表

    2 )如果是 InnoDB 表
    看下该表是否有辅助索引,如果有辅助索引的话, count(*)操作会走覆盖索引,走覆盖索引速度会比较快
    可以 explain 查看一下 具体优化器怎么走的
    alsotang
        15
    alsotang  
       Jun 6, 2016
    晕。竟然没加索引
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2491 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 06:48 · PVG 14:48 · LAX 23:48 · JFK 02:48
    ♥ Do have faith in what you're doing.