增加强制索引依然慢


版本: 阿里云RDS MySQL 8.0.25

线上数据库CPU达到100%, 定位到如下SQL

EXPLAIN 
SELECT 
    ssd.goods_no,
            ssd.goods_name,
            ssd.goods_spec,
            ssd.goods_unit,
            ssd.create_time,
            w.warehouse_name,
            sb.batch_no,
            swl.warehouse_region_location_name,
            sc.customer_name AS goodsOwnerName,
            sc2.customer_name AS supplierName,
            ss.storage_id,
            ss.storage_no,
            ss.storage_desc,
            sbdl.storage_type,
            sbdl.create_time AS finishTime,
            sbdl.before_quantity,
            sbdl.quantity AS real_quantity,
            sbdl.after_quantity,
            sc3.customer_name,
            sbdl.storage_category,
            sb.warehouse_owner_goods_id,
            sb.goods_owner_id
FROM 
    store_batch_details_log sbdl 
    INNER JOIN store_storage_details ssd ON ssd.storage_details_id = sbdl.storage_details_id
    INNER JOIN store_storage ss  ON ss.storage_id = ssd.storage_id
    INNER JOIN store_batch_details sbd ON sbd.batch_details_id = sbdl.batch_details_id
    INNER JOIN store_batch sb ON sb.batch_id = sbd.batch_id 
    LEFT JOIN store_warehouse_location swl ON swl.warehouse_location_id = sbd.warehouse_location_id
    LEFT JOIN store_customer sc ON sc.customer_id = sb.goods_owner_id
    LEFT JOIN store_customer sc3 ON sc3.customer_id = ss.customer_id
    LEFT JOIN warehouse w ON w.warehouse_id = ssd.warehouse_id
    LEFT JOIN store_customer sc2 ON sc2.customer_id = sb.supplier_id
WHERE 
    1 = 1
        AND ssd.`goods_name` LIKE CONCAT('%', '【堂食专供】葡萄(计重)', '%')
        AND ss.enterprise_id = 241240455403319296
        AND ss.warehouse_id IN (272697524450807808 , 278854886203117568,
        358283733083942912,
        358310610389495808,
        358316852142993408,
        358317205127229440,
        358317497189199872,
        358319149438791680,
        358320040363487232,
        362996967464562688,
        362998068574220288,
        372377440368259072,
        372377840450334720,
        375321342717001728,
        377847160517230592,
        382166980817661952,
        382167317834182656,
        383586763626799104,
        392392204255334400,
        395668297183764480,
        395668683634352128,
        416633733303848960,
        427869257024753664,
        432595648538574848,
        433271921665474560,
        433660539047346176,
        434765698913632256,
        460080655901245440)
        ORDER BY ss.create_time DESC
        LIMIT 0,20 ;
        

执行计划如下
在这里插入图片描述

ss表全表扫描

因为在 ss 表上存在索引 idx_enterprise_id_warehouse_id_create_time , 既然没有使用索引, 与查询的条件有关. 于是条件上删除了一些仓库, SQL如下



EXPLAIN 
SELECT 
    ssd.goods_no,
            ssd.goods_name,
            ssd.goods_spec,
            ssd.goods_unit,
            ssd.create_time,
            w.warehouse_name,
            sb.batch_no,
            swl.warehouse_region_location_name,
            sc.customer_name AS goodsOwnerName,
            sc2.customer_name AS supplierName,
            ss.storage_id,
            ss.storage_no,
            ss.storage_desc,
            sbdl.storage_type,
            sbdl.create_time AS finishTime,
            sbdl.before_quantity,
            sbdl.quantity AS real_quantity,
            sbdl.after_quantity,
            sc3.customer_name,
            sbdl.storage_category,
            sb.warehouse_owner_goods_id,
            sb.goods_owner_id
FROM 
    store_batch_details_log sbdl 
        INNER JOIN
    store_storage_details ssd ON ssd.storage_details_id = sbdl.storage_details_id
        INNER JOIN
    store_storage ss  ON ss.storage_id = ssd.storage_id
        INNER JOIN 
    store_batch_details sbd ON sbd.batch_details_id = sbdl.batch_details_id
        INNER JOIN 
    store_batch sb ON sb.batch_id = sbd.batch_id 
            LEFT JOIN store_warehouse_location swl ON swl.warehouse_location_id = sbd.warehouse_location_id
        LEFT JOIN store_customer sc ON sc.customer_id = sb.goods_owner_id
        LEFT JOIN store_customer sc3 ON sc3.customer_id = ss.customer_id
        LEFT JOIN warehouse w ON w.warehouse_id = ssd.warehouse_id
        LEFT JOIN store_customer sc2 ON sc2.customer_id = sb.supplier_id
WHERE 
    1 = 1
        AND ssd.`goods_name` LIKE CONCAT('%', '【堂食专供】葡萄(计重)', '%')
        AND ss.enterprise_id = 241240455403319296
        AND ss.warehouse_id IN (272697524450807808 , 278854886203117568,
        358283733083942912,
        358310610389495808,
        358316852142993408,
        358317205127229440,
        358317497189199872,
        358319149438791680,
        358320040363487232,
        362996967464562688,
        432595648538574848,
        433271921665474560,
        433660539047346176,
        434765698913632256,
        460080655901245440)
        ORDER BY ss.create_time DESC
        LIMIT 0,20 ;
        

执行计划如下

在这里插入图片描述ss表使用了索引, row值也变少了 .

于是第一步的优化, 针对第一个原始的SQL, 采用了强制索引


EXPLAIN 
SELECT 
    ssd.goods_no,
            ssd.goods_name,
            ssd.goods_spec,
            ssd.goods_unit,
            ssd.create_time,
            w.warehouse_name,
            sb.batch_no,
            swl.warehouse_region_location_name,
            sc.customer_name AS goodsOwnerName,
            sc2.customer_name AS supplierName,
            ss.storage_id,
            ss.storage_no,
            ss.storage_desc,
            sbdl.storage_type,
            sbdl.create_time AS finishTime,
            sbdl.before_quantity,
            sbdl.quantity AS real_quantity,
            sbdl.after_quantity,
            sc3.customer_name,
            sbdl.storage_category,
            sb.warehouse_owner_goods_id,
            sb.goods_owner_id
FROM 
    store_batch_details_log sbdl 
    INNER JOIN store_storage_details ssd ON ssd.storage_details_id = sbdl.storage_details_id
    INNER JOIN store_storage ss force index(idx_enterprise_id_warehouse_id_create_time) ON ss.storage_id = ssd.storage_id
    INNER JOIN store_batch_details sbd ON sbd.batch_details_id = sbdl.batch_details_id
    INNER JOIN store_batch sb ON sb.batch_id = sbd.batch_id 
    LEFT JOIN store_warehouse_location swl ON swl.warehouse_location_id = sbd.warehouse_location_id
    LEFT JOIN store_customer sc ON sc.customer_id = sb.goods_owner_id
    LEFT JOIN store_customer sc3 ON sc3.customer_id = ss.customer_id
    LEFT JOIN warehouse w ON w.warehouse_id = ssd.warehouse_id
    LEFT JOIN store_customer sc2 ON sc2.customer_id = sb.supplier_id
WHERE 
    1 = 1
        AND ssd.`goods_name` LIKE CONCAT('%', '【堂食专供】葡萄(计重)', '%')
        AND ss.enterprise_id = 241240455403319296
        AND ss.warehouse_id IN (272697524450807808 , 278854886203117568,
        358283733083942912,
        358310610389495808,
        358316852142993408,
        358317205127229440,
        358317497189199872,
        358319149438791680,
        358320040363487232,
        362996967464562688,
        362998068574220288,
        372377440368259072,
        372377840450334720,
        375321342717001728,
        377847160517230592,
        382166980817661952,
        382167317834182656,
        383586763626799104,
        392392204255334400,
        395668297183764480,
        395668683634352128,
        416633733303848960,
        427869257024753664,
        432595648538574848,
        433271921665474560,
        433660539047346176,
        434765698913632256,
        460080655901245440)
        ORDER BY ss.create_time DESC
        LIMIT 0,20 ;
        

如上, 使用了 force index(idx_enterprise_id_warehouse_id_create_time) . 执行计划如下

在这里插入图片描述ss表终于使用了索引, row值也变少了 . 可在实际执行SQL语句时, 耗时14左右, 依然不理想.

继续使用 SHOW PROFILE查看具体的资源消耗使用情况

在这里插入图片描述

待续…

相关推荐

  1. 查询&sql&索引优化

    2024-06-08 01:30:05       21 阅读
  2. Sql server强制索引

    2024-06-08 01:30:05       56 阅读
  3. npm安装依赖

    2024-06-08 01:30:05       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 01:30:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 01:30:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 01:30:05       82 阅读
  4. Python语言-面向对象

    2024-06-08 01:30:05       91 阅读

热门阅读

  1. 【C++】list模拟实现

    2024-06-08 01:30:05       22 阅读
  2. 瀚高数据库相关设置

    2024-06-08 01:30:05       31 阅读
  3. go 源码学习1:scanner学习

    2024-06-08 01:30:05       29 阅读
  4. Python怎么翻转:深度探索与技巧剖析

    2024-06-08 01:30:05       32 阅读
  5. 聚类层次【python,机器学习,算法】

    2024-06-08 01:30:05       30 阅读
  6. 数据结构:顺序栈

    2024-06-08 01:30:05       29 阅读
  7. 云计算导论(3)---分布式文件系统

    2024-06-08 01:30:05       31 阅读
  8. redis基本命令

    2024-06-08 01:30:05       27 阅读
  9. C++面试题其三

    2024-06-08 01:30:05       35 阅读
  10. Xtransfer面试内容

    2024-06-08 01:30:05       30 阅读
  11. go语言接口之sort.Interface接口

    2024-06-08 01:30:05       37 阅读