求助 mysql 查询优化 - 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
I2E
V2EX    MySQL

求助 mysql 查询优化

  •  
  •   I2E Apr 15, 2024 2388 views
    This topic created in 772 days ago, the information mentioned may be changed or developed.

    表 abc_http_detail_log_2024_04_13 为网络日志表,包含部分字段:

    uid int // 用户 id account_id int // 子账号 id flow bingint // 量消耗 ip char // 本机 ip address varchar // 请求地址 

    联合索引 idx_uid_account_ip 包含字段 uid, account_id, ip

    现在需要查询当日的用户流量使用情况。account_id 可以为 0 或者大于 0 。

    表 abc_http_detail_log_2024_04_13 有 2075w 条数据,uid=114708 的数据量最多,有 637w 条。在统计流量消耗时出现上图的情况。如何才能优化查询呢?

    10 replies    2024-04-17 16:08:35 +08:00
    YouZiAndDanJuan
        1
    YouZiAndDanJuan  
       Apr 15, 2024
    分表
    I2E
        2
    I2E  
    OP
       Apr 15, 2024
    有两点疑问:
    1 ,为什么加了 account_id > 0 会导致第二条 sql 的索引失效
    2 ,为什么加了 limit 索引又会生效
    I2E
        3
    I2E  
    OP
       Apr 15, 2024
    @YouZiAndDanJuan 分表要改很多屎山代码
    bianhui
        4
    bianhui  
       Apr 15, 2024
    无语,你看看字只看一半么,using index 和 using inex condition 是两个东西。百度搜下索引下推。
    xlzyxxn
        5
    xlzyxxn  
       Apr 15, 2024
    @I2E #2 应该有个 uid 、account_id 、flow 的联合索引吧,第二条查询条件 adress 需要回表,uid=114708 占了大部分数据的情况下,mysql 认为不如直接去全表扫描。第三条查询不需要回表。
    jshfsym42
        6
    jshfsym42  
       Apr 16, 2024
    第二条 SQL 匹配的行数太多了,MySQL 执行器认为不如全盘扫描;可以看看这个。
    https://dev.mysql.com/doc/refman/8.0/en/table-scan-avoidance.html
    You are using a key with low cardinality (many rows match the key value) through another column. In this case, MySQL assumes that by using the key probably requires many key lookups and that a table scan would be faster.
    8355
        7
    8355  
      &bsp;Apr 16, 2024
    account_id+ 一般索引
    你的日志表在查询某一个 id 的时候跳行太多了,加上某 uid 和 account_id orderby 即可。
    coderzhangsan
        8
    coderzhangsan  
       Apr 16, 2024
    能说下 mysql 版本吗?版本不同,可能导致优化的结果也是不同的。
    I2E
        9
    I2E  
    OP
       Apr 17, 2024
    I2E
        10
    I2E  
    OP
       Apr 17, 2024
    @jshfsym42 应该是这个原因
    About     Help     Advertise     Blog     API     FAQ     Solana     5007 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 09:39 PVG 17:39 LAX 02:39 JFK 05:39
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86