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

请教一个关于 mysql 优化的问题

  •  
  •   proletarius · Apr 8, 2022 · 1845 views
    This topic created in 1524 days ago, the information mentioned may be changed or developed.

    select a,b,c,d from ae left join ap on ae.anomaly_process_id = ap.id join af on ae.anomaly_file_id = af.id or ap.anomaly_file_id = af.id join p on ae.product_id = p.id join d on d.id = ae.device_id join dv on dv.product_id = ae.product_id join mv on dv.management_view_id = mv.id join dvr on dv.id = dvr.device_view_id join au on dv.user_id = au.id join t on t.id = au.tenant_id union all select a,b,c,d from ae left join ap on ae.anomaly_process_id = ap.id join af on ae.anomaly_file_id = af.id or ap.anomaly_file_id = af.id join p on ae.product_id = p.id join d on d.id = ae.device_id join dv on dv.product_id = ae.product_id join au on dv.user_id = au.id join t on t.id = au.tenant_id

    每个子查询单独查询速度很快,但是用了 union all 之后速度慢到需要几十秒,数据也就十几万条,请教一下到底从哪里开始优化比较好?是更改表的数据结构减少 join 还是在业务数据库中分开查询两次做合并

    CEBBCAT
        1
    CEBBCAT  
       Apr 8, 2022
    贴代码可以用 gist

    建表语句和 SHOW INDEX 贴一下
    proletarius
        2
    proletarius  
    OP
       Apr 8, 2022
    <script src="https://gist.github.com/Proletarier/54dd258c09f94c5e559863de6ed8121d.js"></script>

    已补充,因为涉及的表结构太多,只能简单复述一下 ,每个字段都需要引用的字段都单独建立了索引,目前感觉影响速度的地方有两个 一个是 union all 还有一个就是 or 的用法导致索引失效 Range checked for each record (index map: 0x1),
    proletarius
        3
    proletarius  
    OP
       Apr 8, 2022
    asmile1993
        4
    asmile1993  
       Apr 8, 2022
    你不贴表结构、执行计划,没人会去看的,只是浪费时间而已
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3154 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 12:18 · PVG 20:18 · LAX 05:18 · JFK 08:18
    ♥ Do have faith in what you're doing.