数据库高级知识——查询截取分析(二)
生活随笔
收集整理的這篇文章主要介紹了
数据库高级知识——查询截取分析(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 3.Show Profile
- 3.1 show profile是什么
- 3.2 分析步驟
- 4.全局查詢日志
- 4.1配置啟用
- 4.2編碼啟用
3.Show Profile
3.1 show profile是什么
show profile是mysql提供可以用來分析當前會話中語句執行的資源消耗情況??梢杂糜赟QL的調優的測量 默認情況下,參數處于關閉狀態,并保存最近15次的運行結果官網
3.2 分析步驟
1.是否支持,看看當前的mysql版本是否支持
mysql> Show variables like 'profiling'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | profiling | OFF | +---------------+-------+ 1 row in set (0.01 sec) 默認是關閉,使用前需要開啟2.開啟功能,默認是關閉,使用前需要開啟
mysql> set profiling=1; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> Show variables like 'profiling'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | profiling | ON | +---------------+-------+ 1 row in set (0.00 sec)3.運行SQL
mysql> select * from students; +------+-------+ | id | name | +------+-------+ | 1 | zhao1 | | 2 | zhao2 | | 3 | zhao3 | +------+-------+ 3 rows in set (0.03 sec)4.查看結果
show profiles; mysql> show profiles; +----------+------------+----------------------------------+ | Query_ID | Duration | Query | +----------+------------+----------------------------------+ | 1 | 0.00189725 | Show variables like 'profiling' | | 2 | 0.00037950 | SELECT DATABASE() | | 3 | 0.00436850 | show databases | | 4 | 0.00493400 | show tables | | 5 | 0.00287150 | show tables | | 6 | 0.02086075 | select * from students | +----------+------------+----------------------------------+ 6 rows in set, 1 warning (0.00 sec)5.診斷SQL
show profile cpu,block io for query n #(n為上一步前面的問題SQL數字號碼); mysql> show profile cpu,block io for query 6; +--------------------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +--------------------------------+----------+----------+------------+--------------+---------------+ | starting | 0.000111 | 0.000110 | 0.000000 | 0 | 0 | | Executing hook on transaction | 0.000012 | 0.000011 | 0.000000 | 0 | 0 | | starting | 0.000018 | 0.000018 | 0.000000 | 0 | 0 | | checking permissions | 0.000016 | 0.000016 | 0.000000 | 0 | 0 | | Opening tables | 0.000062 | 0.000062 | 0.000000 | 0 | 0 | | init | 0.000016 | 0.000016 | 0.000000 | 0 | 0 | | System lock | 0.000021 | 0.000021 | 0.000000 | 0 | 0 | | optimizing | 0.000073 | 0.000056 | 0.000000 | 0 | 0 | | statistics | 0.000029 | 0.000029 | 0.000000 | 0 | 0 | | preparing | 0.000035 | 0.000035 | 0.000000 | 0 | 0 | | executing | 0.020360 | 0.000411 | 0.000000 | 32 | 0 | | end | 0.000016 | 0.000014 | 0.000000 | 0 | 0 | | query end | 0.000008 | 0.000008 | 0.000000 | 0 | 0 | | waiting for handler commit | 0.000012 | 0.000012 | 0.000000 | 0 | 0 | | closing tables | 0.000013 | 0.000013 | 0.000000 | 0 | 0 | | freeing items | 0.000027 | 0.000028 | 0.000000 | 0 | 0 | | cleaning up | 0.000033 | 0.000033 | 0.000000 | 0 | 0 | +--------------------------------+----------+----------+------------+--------------+---------------+ 17 rows in set, 1 warning (0.00 sec)參數備注:
type:
| ALL --顯示所有的開銷信息
| BLOCK IO --顯示塊IO相關開銷
| CONTEXT SWITCHES --上下文切換相關開銷
| CPU --顯示CPU相關開銷信息
| IPC --顯示發送和接收相關開銷信息
| MEMORY --顯示內存相關開銷信息
| PAGE FAULTS --顯示頁面錯誤相關開銷信息
| SOURCE --顯示和Source_function,Source_file,Source_line相關的開銷信息
| SWAPS --顯示交換次數相關開銷的信息
6.日常開發需要注意的結論
converting HEAP to MyISAM 查詢結果太大,內存都不夠用了往磁盤上搬了。Creating tmp table 創建臨時表拷貝數據到臨時表用完再刪除Copying to tmp table on disk 把內存中臨時表復制到磁盤,危險!!!locked4.全局查詢日志
4.1配置啟用
/etc/mysql/mysql.conf.d/mysqld.cnf
# Log all queries # Be aware that this log type is a performance killer. # general_log_file = /var/log/mysql/query.log # general_log = 14.2編碼啟用
#命令 盡量不要在生產環境開啟這個功能。 set global general_log=1;#全局日志可以存放到日志文件中,也可以存放到Mysql系統表中。存放到日志中性能更好一些,存儲到表中 set global log_output='TABLE';#此后 ,你所編寫的sql語句,將會記錄到mysql庫里的general_log表,可以用下面的命令查看select * from mysql.general_log;總結
以上是生活随笔為你收集整理的数据库高级知识——查询截取分析(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT清单打印程序
- 下一篇: vs code 配置java