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

MySQL 的 select.. where in 在大数据量下的性能问题

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

    我现在有一个查询语句: select uuid, feature from faces where uuid in (uuid1, uuid2,....)

    其中, faces 表中的数据量千万级别,而查询条件 in 后边的 uuid 集合大约几千条,是通过其他方式获取到的 uuid 集合,直接在 sql 语句中使用

    现在我如果直接执行这条语句,性能很差,个人 PC 执行需要花费 30 多秒,实在是太慢了。

    请教各位有没有什么优化方案。

    9 条回复    2016-06-17 12:12:11 +08:00
    liprais
        1
    liprais  
       2016-06-17 10:11:16 +08:00 via iPhone
    in 改成 join
    haoqiangim
        2
    haoqiangim  
       2016-06-17 10:21:03 +08:00
    给 uuid 加个 index 呀。
    ovear
        3
    ovear  
       2016-06-17 10:42:06 +08:00
    JiShuTui
        4
    JiShuTui  
       2016-06-17 10:47:06 +08:00
    根据 3 楼的链接,用 temporary table 也就是临时表
    zrp1994
        5
    zrp1994  
       2016-06-17 10:52:41 +08:00
    同样遇到过这种情况,数据量快到一亿。从数据库结构来说,分表比较靠谱。
    realpg
        6
    realpg  
       2016-06-17 10:59:31 +08:00
    1000 万条记录 执行 5000 次 select * from table where uuid= 也不用 30 秒吧
    你确定 uuid 有索引?以及你的磁盘 IO 适配千万行的数据量
    jetyang
        7
    jetyang  
       2016-06-17 11:08:15 +08:00
    先确定 uuid 有没有索引, in 的效率很高的,几千个 uuid 有点多,但不至于这么慢
    alouha
        8
    alouha  
       2016-06-17 11:34:27 +08:00
    平时也遇到这样的问题,基本上都是先从缓存里取,找不到再查数据库……
    Weixiao0725
        9
    Weixiao0725  
       2016-06-17 12:12:11 +08:00
    select A.uuid, A.feature from faces as A, (select uuid from table where condition) as B where A.uuid = B.uuid
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3250 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:04 · PVG 21:04 · LAX 06:04 · JFK 09:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.