• 2022-08-01 mysql/stoonedb慢SQL-Q18分析


    摘要:

    分析慢SQL-Q18出现的原因

    DDL分析:

    SQL:

    1. select
    2. c_name,
    3. c_custkey,
    4. o_orderkey,
    5. o_orderdate,
    6. o_totalprice,
    7. sum(l_quantity)
    8. from
    9. customer,
    10. orders,
    11. lineitem
    12. where
    13. o_orderkey in (
    14. select
    15. l_orderkey
    16. from
    17. lineitem
    18. group by
    19. l_orderkey
    20. having
    21. sum(l_quantity) > 300 )
    22. and c_custkey = o_custkey
    23. and o_orderkey = l_orderkey
    24. group by
    25. c_name,
    26. c_custkey,
    27. o_orderkey,
    28. o_orderdate,
    29. o_totalprice
    30. order by
    31. o_totalprice desc,
    32. o_orderdate
    33. limit 100;

    innodb的explain分析:

    1. mysql> explain select
    2. -> c_name,
    3. -> c_custkey,
    4. -> o_orderkey,
    5. -> o_orderdate,
    6. -> o_totalprice,
    7. -> sum(l_quantity)
    8. -> from
    9. -> customer,
    10. -> orders,
    11. -> lineitem
    12. -> where
    13. -> o_orderkey in (
    14. -> select
    15. ->
    16. Display all 1017 possibilities? (y or n)
    17. -> l_orderkey
    18. -> from
    19. ->
    20. Display all 1017 possibilities? (y or n)
    21. -> lineitem
    22. -> group by
    23. ->
    24. Display all 1017 possibilities? (y or n)
    25. -> l_orderkey
    26. -> having
    27. ->
    28. Display all 1017 possibilities? (y or n)
    29. -> sum(l_quantity) > 300 )
    30. -> and c_custkey = o_custkey
    31. -> and o_orderkey = l_orderkey
    32. -> group by
    33. -> c_name,
    34. -> c_custkey,
    35. -> o_orderkey,
    36. -> o_orderdate,
    37. -> o_totalprice
    38. -> order by
    39. -> o_totalprice desc,
    40. -> o_orderdate
    41. -> limit 100\G
    42. *************************** 1. row ***************************
    43. id: 1
    44. select_type: PRIMARY
    45. table: orders
    46. partitions: NULL
    47. type: ALL
    48. possible_keys: PRIMARY
    49. key: NULL
    50. key_len: NULL
    51. ref: NULL
    52. rows: 13754753
    53. filtered: 100.00
    54. Extra: Using where; Using temporary; Using filesort
    55. *************************** 2. row ***************************
    56. id: 1
    57. select_type: PRIMARY
    58. table: customer
    59. partitions: NULL
    60. type: eq_ref
    61. possible_keys: PRIMARY
    62. key: PRIMARY
    63. key_len: 4
    64. ref: tpch.orders.o_custkey
    65. rows: 1
    66. filtered: 100.00
    67. Extra: NULL
    68. *************************** 3. row ***************************
    69. id: 1
    70. select_type: PRIMARY
    71. table: lineitem
    72. partitions: NULL
    73. type: ref
    74. possible_keys: PRIMARY
    75. key: PRIMARY
    76. key_len: 4
    77. ref: tpch.orders.o_orderkey
    78. rows: 4
    79. filtered: 100.00
    80. Extra: NULL
    81. *************************** 4. row ***************************
    82. id: 2
    83. select_type: SUBQUERY
    84. table: lineitem
    85. partitions: NULL
    86. type: index
    87. possible_keys: PRIMARY
    88. key: PRIMARY
    89. key_len: 8
    90. ref: NULL
    91. rows: 31751480
    92. filtered: 100.00
    93. Extra: NULL
    94. 4 rows in set, 1 warning (0.01 sec)

    select_typetablepartitionstypepossible_keyskeykey_lenrefrowsfilteredExtra
    PRIMARYordersALLPRIMARY13754753100Using where; Using temporary; Using filesort
    PRIMARYcustomereq_refPRIMARYPRIMARY4tpch.orders.o_custkey1100
    PRIMARYlineitemrefPRIMARYPRIMARY4tpch.orders.o_orderkey4100
    SUBQUERYlineitemindexPRIMARYPRIMARY831751480100

    stonedb的explain分析:

    1. mysql> explain select
    2. -> c_name,
    3. -> c_custkey,
    4. -> o_orderkey,
    5. -> o_orderdate,
    6. -> o_totalprice,
    7. -> sum(l_quantity)
    8. -> from
    9. -> customer,
    10. -> orders,
    11. -> lineitem
    12. -> where
    13. -> o_orderkey in (
    14. -> select
    15. -> l_orderkey
    16. -> from
    17. -> lineitem
    18. -> group by
    19. -> l_orderkey
    20. -> having
    21. -> sum(l_quantity) > 300 )
    22. -> and c_custkey = o_custkey
    23. -> and o_orderkey = l_orderkey
    24. -> group by
    25. -> c_name,
    26. -> c_custkey,
    27. -> o_orderkey,
    28. -> o_orderdate,
    29. -> o_totalprice
    30. -> order by
    31. -> o_totalprice desc,
    32. -> o_orderdate
    33. -> limit 100\G
    34. *************************** 1. row ***************************
    35. id: 1
    36. select_type: PRIMARY
    37. table: lineitem
    38. partitions: NULL
    39. type: ALL
    40. possible_keys: PRIMARY
    41. key: NULL
    42. key_len: NULL
    43. ref: NULL
    44. rows: 59986052
    45. filtered: 100.00
    46. Extra: Using where with pushed condition <in_optimizer>(`tpch`.`lineitem`.`l_orderkey`,`tpch`.`lineitem`.`l_orderkey` in ( <materialize> (/* select#2 */ select `tpch`.`lineitem`.`l_orderkey` from `tpch`.`lineitem` group by `tpch`.`lineitem`.`l_orderkey` having (sum(`tpch`.`lineitem`.`l_quantity`) > 300) ), <primary_index_lookup>(`tpch`.`lineitem`.`l_orderkey` in <temporary table> on <auto_key> where ((`tpch`.`lineitem`.`l_orderkey` = `materialized-subquery`.`l_orderkey`)))))(t0) Pckrows: 916, susp. 916 (0 empty 0 full). Conditions: 1; Using temporary; Using filesort
    47. *************************** 2. row ***************************
    48. id: 1
    49. select_type: PRIMARY
    50. table: orders
    51. partitions: NULL
    52. type: eq_ref
    53. possible_keys: PRIMARY
    54. key: PRIMARY
    55. key_len: 4
    56. ref: tpch.lineitem.l_orderkey
    57. rows: 1
    58. filtered: 100.00
    59. Extra: NULL
    60. *************************** 3. row ***************************
    61. id: 1
    62. select_type: PRIMARY
    63. table: customer
    64. partitions: NULL
    65. type: eq_ref
    66. possible_keys: PRIMARY
    67. key: PRIMARY
    68. key_len: 4
    69. ref: tpch.orders.o_custkey
    70. rows: 1
    71. filtered: 100.00
    72. Extra: NULL
    73. *************************** 4. row ***************************
    74. id: 2
    75. select_type: SUBQUERY
    76. table: lineitem
    77. partitions: NULL
    78. type: ALL
    79. possible_keys: PRIMARY
    80. key: NULL
    81. key_len: NULL
    82. ref: NULL
    83. rows: 59986052
    84. filtered: 100.00
    85. Extra: Using temporary; Using filesort
    86. 4 rows in set, 1 warning (54.11 sec)

    idselect_typetablepartitionstypepossible_keyskeykey_lenrefrowsfilteredExtra
    1PRIMARYlineitemALLPRIMARY59986052100Using where with pushed condition (`tpch`.`lineitem`.`l_orderkey`,`tpch`.`lineitem`.`l_orderkey` in ( (/* select#2 */ select `tpch`.`lineitem`.`l_orderkey` from `tpch`.`lineitem` group by `tpch`.`lineitem`.`l_orderkey` having (sum(`tpch`.`lineitem`.`l_quantity`) > 300) ), (`tpch`.`lineitem`.`l_orderkey` in on where ((`tpch`.`lineitem`.`l_orderkey` = `materialized-subquery`.`l_orderkey`)))))(t0) Pckrows: 916, susp. 916 (0 empty 0 full). Conditions: 1; Using temporary; Using filesort
    1PRIMARYorderseq_refPRIMARYPRIMARY4tpch.lineitem.l_orderkey1100
    1PRIMARYcustomereq_refPRIMARYPRIMARY4tpch.orders.o_custkey1100
    2SUBQUERYlineitemALLPRIMARY59986052100Using temporary; Using filesort

    执行分析:

    热力图:

    hash join数据统计:

    1. [2022-08-01 19:27:48.120105] [1369856] End traversed 8380864/8380864 rows.
    2. [2022-08-01 19:28:28.177049] [1369856] Begin match dim of 32986052 rows, spliting into 64 threads with packs type.
    3. [2022-08-01 19:29:07.932297] [1369856] End match dim. Produced tuples:32986052/32986052
    4. [2022-08-01 19:29:07.985311] [1369856] Tuples after inner join 1-2 [hash]: 32986052 [111]
    5. [2022-08-01 19:29:08.286386] [1369856] GroupTable begin, initialized for up to 32986052 groups, 53+31 bytes (3171 MB)
    6. [2022-08-01 19:29:08.529321] [1369856] Aggregating: 32986052 tuples left.
    7. [2022-08-01 19:30:40.828429] [1369856] Group/Aggregate end. Begin generating output.
    8. [2022-08-01 19:30:40.828502] [1369856] Output rows: 8249593, output table row limit: 8249593
    9. [2022-08-01 19:31:06.163156] [1369856] Generating output end. Aggregated (8249593 group). Omitted packrows: 0 + 0 partially, out of 26622 total.
    10. [2022-08-01 19:31:08.930503] [1369856] Heap Sort begin, initialized for 8249593 rows, 16+0+3 bytes each.
    11. [2022-08-01 19:32:21.876613] [1369856] Sorted end, rows retrieved.

  • 相关阅读:
    Zero-Shot Learning by Harnessing Adversarial Samples 理论 & 代码解读
    自学Python01-创建文件写入内容
    【活动系列】那些年写的比较愚蠢的代码
    The Missing Semester of Your CS Education(计算机教育中缺失的一课)
    python自动化第一篇—— 带图文的execl的自动化合并
    没有网络怎么配置传奇SF登陆器自动读取列表
    配置本地Maven仓库——IDEA配置本地Maven源
    Inno Setup安装中文语言
    01-微服务探讨(摘)
    方法练习(二)
  • 原文地址:https://blog.csdn.net/adofsauron/article/details/126098067