• 2022-07-30 mysql8执行慢SQL[Q17]分析


    摘要:

    以mysql8.0.28执行Q17慢SQL,作为分析对比

    环境信息:

    1. 内存: 8GB
    2. cpu: 8核心,3.2GHz
    3. mysql: 8.0.28

    执行分析:

    DDL:

    1. mysql> show create table lineitem\G
    2. *************************** 1. row ***************************
    3. Table: lineitem
    4. Create Table: CREATE TABLE `lineitem` (
    5. `l_orderkey` int NOT NULL,
    6. `l_partkey` int NOT NULL,
    7. `l_suppkey` int NOT NULL,
    8. `l_linenumber` int NOT NULL,
    9. `l_quantity` decimal(15,2) NOT NULL,
    10. `l_extendedprice` decimal(15,2) NOT NULL,
    11. `l_discount` decimal(15,2) NOT NULL,
    12. `l_tax` decimal(15,2) NOT NULL,
    13. `l_returnflag` char(1) NOT NULL,
    14. `l_linestatus` char(1) NOT NULL,
    15. `l_shipdate` date NOT NULL,
    16. `l_commitdate` date NOT NULL,
    17. `l_receiptdate` date NOT NULL,
    18. `l_shipinstruct` char(25) NOT NULL,
    19. `l_shipmode` char(10) NOT NULL,
    20. `l_comment` varchar(44) NOT NULL,
    21. PRIMARY KEY (`l_orderkey`,`l_linenumber`)
    22. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
    23. 1 row in set (0.00 sec)

    1. mysql> show create table part\G
    2. *************************** 1. row ***************************
    3. Table: part
    4. Create Table: CREATE TABLE `part` (
    5. `p_partkey` int NOT NULL,
    6. `p_name` varchar(55) NOT NULL,
    7. `p_mfgr` char(25) NOT NULL,
    8. `p_brand` char(10) NOT NULL,
    9. `p_type` varchar(25) NOT NULL,
    10. `p_size` int NOT NULL,
    11. `p_container` char(10) NOT NULL,
    12. `p_retailprice` decimal(15,2) NOT NULL,
    13. `p_comment` varchar(23) NOT NULL,
    14. PRIMARY KEY (`p_partkey`)
    15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
    16. 1 row in set (0.02 sec)

    SQL:

    1. select
    2. sum(l_extendedprice) / 7.0 as avg_yearly
    3. from
    4. lineitem,
    5. part
    6. where
    7. p_partkey = l_partkey
    8. and p_brand = 'Brand#23'
    9. and p_container = 'MED BOX'
    10. and l_quantity < (
    11. select
    12. 0.2 * avg(l_quantity)
    13. from
    14. lineitem
    15. where
    16. l_partkey = p_partkey
    17. );

    explain分析:

    1. mysql> explain select
    2. from
    3. lineitem
    4. where
    5. l_partkey = p_partkey
    6. )\G -> sum(l_extendedprice) / 7.0 as avg_yearly
    7. -> from
    8. -> lineitem,
    9. -> part
    10. -> where
    11. -> p_partkey = l_partkey
    12. -> and p_brand = 'Brand#23'
    13. -> and p_container = 'MED BOX'
    14. -> and l_quantity < (
    15. -> select
    16. -> 0.2 * avg(l_quantity)
    17. -> from
    18. -> lineitem
    19. -> where
    20. -> l_partkey = p_partkey
    21. -> )\G
    22. *************************** 1. row ***************************
    23. id: 1
    24. select_type: PRIMARY
    25. table: lineitem
    26. partitions: NULL
    27. type: ALL
    28. possible_keys: NULL
    29. key: NULL
    30. key_len: NULL
    31. ref: NULL
    32. rows: 31751480
    33. filtered: 100.00
    34. Extra: NULL
    35. *************************** 2. row ***************************
    36. id: 1
    37. select_type: PRIMARY
    38. table: part
    39. partitions: NULL
    40. type: eq_ref
    41. possible_keys: PRIMARY
    42. key: PRIMARY
    43. key_len: 4
    44. ref: tpch.lineitem.l_partkey
    45. rows: 1
    46. filtered: 5.00
    47. Extra: Using where
    48. *************************** 3. row ***************************
    49. id: 2
    50. select_type: DEPENDENT SUBQUERY
    51. table: lineitem
    52. partitions: NULL
    53. type: ALL
    54. possible_keys: NULL
    55. key: NULL
    56. key_len: NULL
    57. ref: NULL
    58. rows: 31751480
    59. filtered: 10.00
    60. Extra: Using where
    61. 3 rows in set, 2 warnings (0.01 sec)

    idselect_typetablepartitionstypepossible_keyskeykey_lenrefrowsfilteredExtra
    1PRIMARYlineitemALL31751480100
    1PRIMARYparteq_refPRIMARYPRIMARY4tpch.lineitem.l_partkey15Using where
    2DEPENDENT SUBQUERYlineitemALL3175148010Using where

    耗时分析:

    性能分析:

    iotop:

    top:

    1. [root@localhost ~]# ps -ef | grep mysqld
    2. mysql 19895 1 39 20:21 ? 00:18:16 /usr/libexec/mysqld --basedir=/usr
    3. root 20055 1793 0 21:07 pts/1 00:00:00 grep --color=auto mysqld
    4. [root@localhost ~]#
    5. [root@localhost ~]# top -Hp 19895

    热力图:

  • 相关阅读:
    c语言练习42:判断回文数
    电影《二手杰作》观后感
    前端工程化精讲第十六课 无包构建:盘点那些 No-bundle 的构建方案
    【深度学习】基于卷积神经网络(tensorflow)的人脸识别项目(四)
    Python算法题集_搜索旋转排序数组
    云原生 | Kubernetes - 国内镜像部署Helm
    VUE element-ui之form表单中input输入超过规定长度error提醒,并实时显示输入长度,可无限输入
    基于卷的磁盘扫描算法设计
    长文讲解Linux内核性能优化的思路和步骤
    电脑系统还原怎么操作?
  • 原文地址:https://blog.csdn.net/adofsauron/article/details/126077607