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

有两个 mysql 优化的问题求指导

  •  
  •   enjoychen0318 · Oct 9, 2020 · 3150 views
    This topic created in 2188 days ago, the information mentioned may be changed or developed.
    create table t1(
    id int primary key, 
    a int,
    b int
    );
    create table t2(
    id int primary key, 
    a int,
    b int
    );
    
    select t1.*, t2.* from t1
        left join t2 on t1.a = t2.b
    where XXXX
    order by t1.id desc
    limit 1,10
    

    问题:

    1. t1 和 t2 的数据量都是 100w,有时候因为 where 条件的不同,导致 join 的时候驱动表会变化,当 t2 作为驱动表的时候 t1 排序就使用不到索引了。在不强制选择驱动表的情况下,有啥优化方案么?

    2. 现在我们公司后台管理有个列表页查询关联的表有点多,筛选条件也比较多,count 和查询都有点慢,但是关联的表应该是不能少。这个时候该怎么优化?做缓存可行么,比如说将筛选条件作为 key 缓存起来。但数据更新快,感觉缓存失效的情况太多,这种方法不太可行

    有大佬能帮帮忙么

    3 replies  •  2020-10-10 18:16:24 +08:00
    PonysDad
        1
    PonysDad  
       Oct 9, 2020 via iPhone
    把查询计划贴上来看看
    enjoychen0318
        2
    enjoychen0318  
    OP
       Oct 10, 2020
    @PonysDad
    有 t2 的 where 时,mysql 优化器认为 t2 是小表,选择 t2 做驱动表的时候会 Using temporary; Using filesort
    XDJI
        3
    XDJI  
       Oct 10, 2020
    1.首先你这个 left join t2 on t1.a = t2.b 用 join buffer 了 直接 t1 单次 t2 多次全表扫描了
    给 t1.a t2.b 加个索引试下呢
    2.得看你的语句 explain 出来看下呢 如果不是强一致缓存倒是一个方法,更新快不是问题 可以在包个内存缓存定时同步不过就麻烦了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   785 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:12 · PVG 06:12 · LAX 15:12 · JFK 18:12
    ♥ Do have faith in what you're doing.