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

mysql查询性能杀手:order by rand

  •  
  •   BigZ · Nov 16, 2012 · 5818 views
    This topic created in 5065 days ago, the information mentioned may be changed or developed.
    今天花了大半天来修复这个问题,效果很好
    http://lutaf.com/63.htm
    8 replies  •  1970-01-01 08:00:00 +08:00
    pythonee
        1
    pythonee  
       Nov 17, 2012
    弱弱问一下,你怎么用show processlist 来看出很多
    converting HEAP to MyISAM
    Copying to tmp table on disk
    这样的command

    想看看你怎么追踪到这个问题的
    KiseXu
        2
    KiseXu  
       Nov 17, 2012
    随机查询可以用后端语言根据max(id)先随机出想要的id,再根据id取出啊。这样就和数据库性能无关啦
    Tianpu
        3
    Tianpu  
       Nov 17, 2012
    $ids = array();
    $max = 100;
    for($i=0;$i<$max;$i++) if(!in_array($i,$ids)) $ids[] = $i;

    另外请问楼主,天天广告自己的破blog烦不烦?
    Renylai
        4
    Renylai  
       Nov 17, 2012
    当有部分ID是被移除导致不连续,或者不在筛选结果内的时候,楼上两位的方法也未必适用
    BigZ
        5
    BigZ  
    OP
       Nov 17, 2012
    @Tianpu 我愿意写,有人愿意看,你不喜不点就是,何必在这里喷粪,装牛逼呢
    BigZ
        6
    BigZ  
    OP
       Nov 17, 2012
    @Renylai 确实不完备,大规模网站的id都是支离破碎的,删帖的操作很多,这种办法能满足90%的情况,考虑到对性能的优化,是值得的,非要特别完备,哪只能在内存中缓存所有的id,然后用random.sample来选取,如果id集合很大,太容易搞漏内存了
    BigZ
        7
    BigZ  
    OP
       Nov 17, 2012
    @pythonee 执行 show processlist ,看到的是一个table,其中有一列command就是显示的这个
    xiawinter
        8
    xiawinter  
       Nov 17, 2012
    @Renylai 取位置id, 不过这样查询对于大表也不是很好, 类似 offset 10000 limit 1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4414 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:13 · PVG 08:13 · LAX 17:13 · JFK 20:13
    ♥ Do have faith in what you're doing.