国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

這篇具有很好參考價(jià)值的文章主要介紹了mysql 簡單定位慢查詢并分析SQL執(zhí)行效率。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

實(shí)際的日常開發(fā)工作中可能會遇到某個(gè)新功能在測試時(shí)需要很久才返回結(jié)果,這時(shí)就應(yīng)該分析是不是慢查詢導(dǎo)致的,如果確實(shí)有慢查詢,就需要來學(xué)習(xí)怎么找到慢查詢和怎么分析 SQL 執(zhí)行效率?

定位慢 SQL 有如下兩種解決方案:

  • 查看慢查詢?nèi)罩敬_定已經(jīng)執(zhí)行完的慢查詢
  • show processlist 查看正在執(zhí)行的慢查詢

定位到慢查詢語句后,可以通過 explain、show profile 和 trace 等診斷工具來分析慢查詢


一、如何開啟并找到慢查詢?nèi)罩荆?/h4>

如果需要使用慢查詢?nèi)罩?,一般分為四步?/strong>

開啟慢查詢?nèi)罩?、設(shè)置慢查詢閥值、確定慢查詢?nèi)罩韭窂?、確定慢查詢?nèi)罩镜奈募?/strong>

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

?涉及到的命令如下:

set global slow_query_log = on;
set global long_query_time = 1;
show global variables like "datadir";
show global variables like "slow_query_log_file";

二、mysqldumpslow工具分析慢查詢

如果覺得系統(tǒng)自帶的慢查詢?nèi)罩静环奖悴榭?,也可以使用mysqldumpslow 等工具對慢查詢?nèi)罩具M(jìn)行分析;

mysqldumpslow經(jīng)常使用的參數(shù):
-s,是order的順序
----- al 平均鎖定時(shí)間
-----ar 平均返回記錄時(shí)間
-----at 平均查詢時(shí)間(默認(rèn))
-----c 計(jì)數(shù)
-----l 鎖定時(shí)間
-----r 返回記錄
-----t 查詢時(shí)間
-t,是top n的意思,即為返回前面多少條的數(shù)據(jù)
-g,后邊可以寫一個(gè)正則匹配模式,大小寫不敏感的

基本命令如下:

mysqldumpslow -t 10 -s t -g “l(fā)eft join” slow.log

三、show processlist

show processlist 命令判斷正在執(zhí)行的慢查詢。show processlist 顯示哪些線程正在運(yùn)行。如果有 PROCESS 權(quán)限,則可以看到所有線程。否則,只能看到當(dāng)前會話的線程;

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

?四、使用 explain 分析慢查詢

定位到慢查詢語句后,我們就要開始分析 SQL 執(zhí)行效率了;

簡單的數(shù)據(jù)準(zhǔn)備工作:

CREATE DATABASE test;           
use test;                       
drop table if exists t1;        

CREATE TABLE `t1` (            
 `id` int NOT NULL auto_increment,
  `a` int DEFAULT NULL,
  `b` int DEFAULT NULL,
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '記錄創(chuàng)建時(shí)間',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '記錄更新時(shí)間',
  PRIMARY KEY (`id`),
  KEY `idx_a` (`a`),
  KEY `idx_b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;	

drop procedure if exists insert_t1; 
delimiter ;;
create procedure insert_t1()        
begin
  declare i int;                    
  set i=1;                          
  while(i<=1000)do                  
    insert into t1(a,b) values(i, i); 
    set i=i+1;                      
  end while;
end;;
delimiter ;                 
call insert_t1();           

drop table if exists t2;    
create table t2 like t1;    
insert into t2 select * from t1;  

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

?explain分析結(jié)果:

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

?explain返回結(jié)果各字段說明:

列名 解釋
id 查詢編號
select_type 查詢類型:顯示本行是簡單還是復(fù)雜查詢
table 涉及到的表
partitions 匹配的分區(qū):查詢將匹配記錄所在的分區(qū)。僅當(dāng)使用 partition 關(guān)鍵字時(shí)才顯示該列。對于非分區(qū)表,該值為 NULL。
type 本次查詢的表連接類型
possible_keys 可能選擇的索引
key 實(shí)際選擇的索引
key_len 被選擇的索引長度:一般用于判斷聯(lián)合索引有多少列被選擇了
ref 與索引比較的列
rows 預(yù)計(jì)需要掃描的行數(shù),對 InnoDB 來說,這個(gè)值是估值,并不一定準(zhǔn)確
filtered 按條件篩選的行的百分比
Extra 附加信息

?其中 select_type、type、key、rows、Extra 是重點(diǎn)關(guān)注項(xiàng);

五、show profile 分析慢查詢

在 MySQL 數(shù)據(jù)庫中,通過 profile,能夠更清楚地了解 SQL 執(zhí)行過程的資源使用情況,如何使用 profile 分析慢查詢,大致步驟是:確定這個(gè) MySQL 版本是否支持 profile;確定 profile 是否關(guān)閉;開啟 profile;執(zhí)行 SQL(查看執(zhí)行完 SQL 的 query id;通過 query id 查看 SQL 的每個(gè)狀態(tài)及耗時(shí)時(shí)間)

具體操作如下:

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

相關(guān)命令:

select @@have_profiling;
select @@profiling;
set profiling=1;

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

相關(guān)命令:

select * from t1 where b=1000;
show profiles;
show profile for query 1;

?六、trace 分析 SQL 優(yōu)化器

如果需要使用,先開啟 trace,設(shè)置格式為 JSON,再執(zhí)行需要分析的 SQL,最后查看 trace 分析結(jié)果(在 information_schema.OPTIMIZER_TRACE 中)

開啟該功能,會對 MySQL 性能有所影響,因此只建議分析問題時(shí)臨時(shí)開啟;

mysql 簡單定位慢查詢并分析SQL執(zhí)行效率

該語句使用的是 b 字段的索引 idx_b。實(shí)際表 t1 中,a、b 兩個(gè)字段都有索引,為什么條件中有這兩個(gè)索引字段卻偏偏選了 b 字段的索引呢?這時(shí)就可以使用 trace 進(jìn)行分析

1 手動開啟trace

set session optimizer_trace="enabled=on",end_markers_in_json=on;

2 執(zhí)行sql查詢

select * from t1 where a >900 and b > 910 order  by a;

執(zhí)行結(jié)果如下:

------+------+------+---------------------+---------------------+
| id   | a    | b    | create_time         | update_time         |
+------+------+------+---------------------+---------------------+
|  911 |  911 |  911 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  912 |  912 |  912 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  913 |  913 |  913 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  914 |  914 |  914 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  915 |  915 |  915 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  916 |  916 |  916 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  917 |  917 |  917 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  918 |  918 |  918 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  919 |  919 |  919 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  920 |  920 |  920 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  921 |  921 |  921 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  922 |  922 |  922 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  923 |  923 |  923 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  924 |  924 |  924 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  925 |  925 |  925 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  926 |  926 |  926 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  927 |  927 |  927 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  928 |  928 |  928 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  929 |  929 |  929 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  930 |  930 |  930 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  931 |  931 |  931 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  932 |  932 |  932 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  933 |  933 |  933 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  934 |  934 |  934 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  935 |  935 |  935 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  936 |  936 |  936 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  937 |  937 |  937 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  938 |  938 |  938 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  939 |  939 |  939 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  940 |  940 |  940 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  941 |  941 |  941 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  942 |  942 |  942 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  943 |  943 |  943 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  944 |  944 |  944 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  945 |  945 |  945 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  946 |  946 |  946 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  947 |  947 |  947 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  948 |  948 |  948 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  949 |  949 |  949 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  950 |  950 |  950 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  951 |  951 |  951 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  952 |  952 |  952 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  953 |  953 |  953 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  954 |  954 |  954 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  955 |  955 |  955 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  956 |  956 |  956 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  957 |  957 |  957 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  958 |  958 |  958 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  959 |  959 |  959 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  960 |  960 |  960 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  961 |  961 |  961 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  962 |  962 |  962 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  963 |  963 |  963 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  964 |  964 |  964 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  965 |  965 |  965 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  966 |  966 |  966 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  967 |  967 |  967 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  968 |  968 |  968 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  969 |  969 |  969 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  970 |  970 |  970 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  971 |  971 |  971 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  972 |  972 |  972 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  973 |  973 |  973 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  974 |  974 |  974 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  975 |  975 |  975 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  976 |  976 |  976 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  977 |  977 |  977 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  978 |  978 |  978 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  979 |  979 |  979 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  980 |  980 |  980 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  981 |  981 |  981 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  982 |  982 |  982 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  983 |  983 |  983 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  984 |  984 |  984 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  985 |  985 |  985 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  986 |  986 |  986 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  987 |  987 |  987 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  988 |  988 |  988 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  989 |  989 |  989 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  990 |  990 |  990 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  991 |  991 |  991 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  992 |  992 |  992 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  993 |  993 |  993 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  994 |  994 |  994 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  995 |  995 |  995 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  996 |  996 |  996 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  997 |  997 |  997 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  998 |  998 |  998 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
|  999 |  999 |  999 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
| 1000 | 1000 | 1000 | 2023-06-26 02:16:08 | 2023-06-26 02:16:08 |
+------+------+------+---------------------+---------------------+

3 查看 trace 分析結(jié)果

SELECT * FROM information_schema.OPTIMIZER_TRACE\G

查詢結(jié)果如下圖所示:

*************************** 1. row ***************************
                            QUERY: select * from t1 where a >900 and b > 910 order  by a
                            TRACE: {
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `t1`.`id` AS `id`,`t1`.`a` AS `a`,`t1`.`b` AS `b`,`t1`.`create_time` AS `create_time`,`t1`.`update_time` AS `update_time` from `t1` where ((`t1`.`a` > 900) and (`t1`.`b` > 910)) order by `t1`.`a`"
          }
        ] /* steps */
      } /* join_preparation */
    },
    {
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "((`t1`.`a` > 900) and (`t1`.`b` > 910))",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "((`t1`.`a` > 900) and (`t1`.`b` > 910))"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "((`t1`.`a` > 900) and (`t1`.`b` > 910))"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "((`t1`.`a` > 900) and (`t1`.`b` > 910))"
                }
              ] /* steps */
            } /* condition_processing */
          },
          {
            "substitute_generated_columns": {
            } /* substitute_generated_columns */
          },
          {
            "table_dependencies": [
              {
                "table": "`t1`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ] /* depends_on_map_bits */
              }
            ] /* table_dependencies */
          },
          {
            "ref_optimizer_key_uses": [
            ] /* ref_optimizer_key_uses */
          },
          {
            "rows_estimation": [
              {
                "table": "`t1`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 1000,
                    "cost": 103.35
                  } /* table_scan */,
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "idx_a",
                      "usable": true,
                      "key_parts": [
                        "a",
                        "id"
                      ] /* key_parts */
                    },
                    {
                      "index": "idx_b",
                      "usable": true,
                      "key_parts": [
                        "b",
                        "id"
                      ] /* key_parts */
                    }
                  ] /* potential_range_indexes */,
                  "setup_range_conditions": [
                  ] /* setup_range_conditions */,
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  } /* group_index_range */,
                  "skip_scan_range": {
                    "potential_skip_scan_indexes": [
                      {
                        "index": "idx_a",
                        "usable": false,
                        "cause": "query_references_nonkey_column"
                      },
                      {
                        "index": "idx_b",
                        "usable": false,
                        "cause": "query_references_nonkey_column"
                      }
                    ] /* potential_skip_scan_indexes */
                  } /* skip_scan_range */,
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx_a",
                        "ranges": [
                          "900 < a"
                        ] /* ranges */,
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 1,
                        "rows": 100,
                        "cost": 35.26,
                        "chosen": true
                      },
                      {
                        "index": "idx_b",
                        "ranges": [
                          "910 < b"
                        ] /* ranges */,
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 1,
                        "rows": 90,
                        "cost": 31.76,
                        "chosen": true
                      }
                    ] /* range_scan_alternatives */,
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    } /* analyzing_roworder_intersect */
                  } /* analyzing_range_alternatives */,
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "idx_b",
                      "rows": 90,
                      "ranges": [
                        "910 < b"
                      ] /* ranges */
                    } /* range_access_plan */,
                    "rows_for_plan": 90,
                    "cost_for_plan": 31.76,
                    "chosen": true
                  } /* chosen_range_access_summary */
                } /* range_analysis */
              }
            ] /* rows_estimation */
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ] /* plan_prefix */,
                "table": "`t1`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "rows_to_scan": 90,
                      "access_type": "range",
                      "range_details": {
                        "used_index": "idx_b"
                      } /* range_details */,
                      "resulting_rows": 90,
                      "cost": 40.76,
                      "chosen": true,
                      "use_tmp_table": true
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,
                "rows_for_plan": 90,
                "cost_for_plan": 40.76,
                "sort_cost": 90,
                "new_cost_for_plan": 130.76,
                "chosen": true
              }
            ] /* considered_execution_plans */
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "((`t1`.`a` > 900) and (`t1`.`b` > 910))",
              "attached_conditions_computation": [
              ] /* attached_conditions_computation */,
              "attached_conditions_summary": [
                {
                  "table": "`t1`",
                  "attached": "((`t1`.`a` > 900) and (`t1`.`b` > 910))"
                }
              ] /* attached_conditions_summary */
            } /* attaching_conditions_to_tables */
          },
          {
            "optimizing_distinct_group_by_order_by": {
              "simplifying_order_by": {
                "original_clause": "`t1`.`a`",
                "items": [
                  {
                    "item": "`t1`.`a`"
                  }
                ] /* items */,
                "resulting_clause_is_simple": true,
                "resulting_clause": "`t1`.`a`"
              } /* simplifying_order_by */
            } /* optimizing_distinct_group_by_order_by */
          },
          {
            "reconsidering_access_paths_for_index_ordering": {
              "clause": "ORDER BY",
              "steps": [
              ] /* steps */,
              "index_order_summary": {
                "table": "`t1`",
                "index_provides_order": false,
                "order_direction": "undefined",
                "index": "idx_b",
                "plan_changed": false
              } /* index_order_summary */
            } /* reconsidering_access_paths_for_index_ordering */
          },
          {
            "finalizing_table_conditions": [
              {
                "table": "`t1`",
                "original_table_condition": "((`t1`.`a` > 900) and (`t1`.`b` > 910))",
                "final_table_condition   ": "((`t1`.`a` > 900) and (`t1`.`b` > 910))"
              }
            ] /* finalizing_table_conditions */
          },
          {
            "refine_plan": [
              {
                "table": "`t1`",
                "pushed_index_condition": "(`t1`.`b` > 910)",
                "table_condition_attached": "(`t1`.`a` > 900)"
              }
            ] /* refine_plan */
          },
          {
            "considering_tmp_tables": [
              {
                "adding_sort_to_table": "t1"
              } /* filesort */
            ] /* considering_tmp_tables */
          }
        ] /* steps */
      } /* join_optimization */
    },
    {
      "join_execution": {
        "select#": 1,
        "steps": [
          {
            "sorting_table": "t1",
            "filesort_information": [
              {
                "direction": "asc",
                "expression": "`t1`.`a`"
              }
            ] /* filesort_information */,
            "filesort_priority_queue_optimization": {
              "usable": false,
              "cause": "not applicable (no LIMIT)"
            } /* filesort_priority_queue_optimization */,
            "filesort_execution": [
            ] /* filesort_execution */,
            "filesort_summary": {
              "memory_available": 262144,
              "key_size": 9,
              "row_size": 32,
              "max_rows_per_buffer": 90,
              "num_rows_estimate": 90,
              "num_rows_found": 90,
              "num_initial_chunks_spilled_to_disk": 0,
              "peak_memory_used": 33792,
              "sort_algorithm": "std::sort",
              "unpacked_addon_fields": "skip_heuristic",
              "sort_mode": "<fixed_sort_key, additional_fields>"
            } /* filesort_summary */
          }
        ] /* steps */
      } /* join_execution */
    }
  ] /* steps */
}
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0
          INSUFFICIENT_PRIVILEGES: 0

4 關(guān)閉trace功能文章來源地址http://www.zghlxwxcb.cn/news/detail-514399.html

set session optimizer_trace="enabled=off";

到了這里,關(guān)于mysql 簡單定位慢查詢并分析SQL執(zhí)行效率的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • MySQL-如何定位慢查詢SQL以及優(yōu)化

    MySQL-如何定位慢查詢SQL以及優(yōu)化

    定位慢SQL可以通過慢查詢?nèi)罩緛聿榭绰齋QL,默認(rèn)的情況下,MySQL數(shù)據(jù)庫不開啟慢查詢?nèi)罩?slow query log),需要手動把它打開 SET GLOBAL slow_query_log = ‘ON’; 查看下慢查詢?nèi)罩九渲?SHOW VARIABLES LIKE ‘slow_query_log%’ slow_query_log:表示慢查詢開啟的狀態(tài) slow_query_log_file:表示慢查詢?nèi)罩?/p>

    2024年02月08日
    瀏覽(19)
  • MySql之慢Sql定位分析

    MySql之慢Sql定位分析

    MySql之慢Sql定位分析 定位低效率執(zhí)行SQL 可以通過以下兩種方式定位執(zhí)行效率較低的SQL語句。 慢查詢?nèi)罩?通過慢查詢?nèi)罩径ㄎ荒切﹫?zhí)行效率較低的SQL語句,用- log-slow-queries[= file name]選項(xiàng)啟動時(shí), mysqld是一個(gè)包含所有執(zhí)行時(shí)間超過 long_query_time秒的sql請句的日志文件。 show process

    2024年02月14日
    瀏覽(19)
  • 【MySQL系列】- Select查詢SQL執(zhí)行過程詳解

    【MySQL系列】- Select查詢SQL執(zhí)行過程詳解

    一條SQL語句從發(fā)送到數(shù)據(jù)并返回結(jié)果,主要經(jīng)歷以下幾個(gè)過程: 連接器 : 查詢緩存 :如果開啟了查詢緩存,則會經(jīng)過這一步,但是大多數(shù)情況下都不是開啟的,也不建議開啟;MySQL8.0之后也刪除了這一塊功能。 分析器 : 優(yōu)化器 執(zhí)行器 2.1. 連接器 如果想對MySQL進(jìn)行操作,第

    2024年02月08日
    瀏覽(88)
  • 詳解MySQL慢SQL定位、分析

    詳解MySQL慢SQL定位、分析

    目錄 1.概述 2.慢SQL定位 3.SQL性能分析 3.1.例子 3.2.SQL性能分析 3.3.參數(shù)說明 3.3.1.id 3.3.2.select_type 3.3.3.key_len 3.3.4.rows 3.3.5.type 3.3.6.extra 解決慢SQL的問題無非3步: 定位慢SQL 分析慢SQL 優(yōu)化慢SQL 本文將按順序介紹前兩步該怎么做,第三步將會在后續(xù)的文章中詳細(xì)討論。 mysql自帶了慢

    2024年02月06日
    瀏覽(21)
  • MySQL—一條查詢SQL語句的完整執(zhí)行流程

    MySQL—一條查詢SQL語句的完整執(zhí)行流程

    表結(jié)構(gòu)和數(shù)據(jù)如下: 我們分析的sql語句如下: 大體來說,MySQL可以分為Server層和存儲引擎層兩部分: Server層 包括:連接器、查詢緩存、分析器、優(yōu)化器、執(zhí)行器等 涵蓋MySQL的大多數(shù)核心服務(wù)功能 所有的內(nèi)置函數(shù)(如日期、時(shí)間、數(shù)學(xué)和加密函數(shù)等),所有跨存儲引擎的功能都在

    2024年04月28日
    瀏覽(23)
  • MySQL中,SQL 查詢總是先執(zhí)行SELECT語句嗎?

    在使用 MySQL 進(jìn)行查詢時(shí),我們通常會使用 SELECT 語句,但是 SELECT 語句是否總是最先被執(zhí)行呢?這是一個(gè)非常有趣的問題,本文將對此進(jìn)行探討。 在 MySQL 中,SQL 查詢通常包括以下幾個(gè)步驟: 語法解析 :MySQL 會對 SQL 查詢語句進(jìn)行語法解析,檢查語句是否符合 SQL 語法規(guī)范。

    2023年04月09日
    瀏覽(21)
  • 從Mysql架構(gòu)看一條查詢sql的執(zhí)行過程

    我們的程序或者工具要操作數(shù)據(jù)庫,第一步要做什么事情? 跟數(shù)據(jù)庫建立連接。 首先,MySQL必須要運(yùn)行一個(gè)服務(wù),監(jiān)聽默認(rèn)的3306端口。在我們開發(fā)系統(tǒng)跟第三方對接的時(shí)候,必須要弄清楚的有兩件事。 第一個(gè)就是通信協(xié)議,比如我們是用HTTP還是WebService還是TCP? 第二個(gè)是消

    2024年02月08日
    瀏覽(92)
  • myql進(jìn)階-一條查詢sql在mysql的執(zhí)行過程

    myql進(jìn)階-一條查詢sql在mysql的執(zhí)行過程

    目錄 1. 流程圖 2. 各個(gè)過程 2.1 連接器 2.2 分析器 2.3 優(yōu)化器 2.4 執(zhí)行器 2.5 注意點(diǎn) 假設(shè)我們執(zhí)行一條sql語句如下: 首先我們會和mysql建立連接,此時(shí)就會執(zhí)行到連接器。 連接器的職責(zé)是負(fù)責(zé)和客戶端建立連接、獲取權(quán)限、維持和管理連接。 我們執(zhí)行sql之前首先要和數(shù)據(jù)庫建立

    2024年01月21日
    瀏覽(95)
  • Mysql的學(xué)習(xí)與鞏固:一條SQL查詢語句是如何執(zhí)行的?

    Mysql的學(xué)習(xí)與鞏固:一條SQL查詢語句是如何執(zhí)行的?

    我們經(jīng)常說,看一個(gè)事兒千萬不要直接陷入細(xì)節(jié)里,你應(yīng)該先鳥瞰其全貌,這樣能夠幫助你從高維度理解問題。同樣,對于MySQL的學(xué)習(xí)也是這樣。平時(shí)我們使用數(shù)據(jù)庫,看到的通常都是一個(gè)整體。比如,你有個(gè)最簡單的表,表里只有一個(gè)ID字段,在執(zhí)行下面這個(gè)查詢語句時(shí):

    2023年04月13日
    瀏覽(97)
  • MYSQL實(shí)戰(zhàn)45講筆記--基礎(chǔ)架構(gòu):一條SQL查詢語句是如何執(zhí)行的?

    MYSQL實(shí)戰(zhàn)45講筆記--基礎(chǔ)架構(gòu):一條SQL查詢語句是如何執(zhí)行的?

    MySQL 可以分為 Server 層和存儲引擎層兩部分。 Server 層 :連接器、查詢緩存、分析器、優(yōu)化器、執(zhí)行器等,涵蓋 MySQL 的大多數(shù)核心服務(wù)功能,以及所有的內(nèi)置函數(shù)(如日期、時(shí)間、數(shù)學(xué)和加密函數(shù)等),所有跨存儲引擎的功能都在這一層實(shí)現(xiàn),比如存儲過程、觸發(fā)器、視圖等

    2024年02月07日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包