一、執行頻率早知道
>show status;
使用 show status瞭解各種sql的執行頻率 可搭配參數 global 全局查看,或者session當前連接開始計數 默認不加參數是指查看當前連接
show status; +-----------------------------------------------+----------------+ | Variable_name | Value | +-----------------------------------------------+----------------+ | Aborted_clients | 65 | | Aborted_connects | 589 | | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 0 | | Binlog_stmt_cache_disk_use | 0 | | Binlog_stmt_cache_use | 0 | | Bytes_received | 1649 | | Bytes_sent | 11767 | | Com_admin_commands | 0 | | Com_assign_to_keycache | 0 | | Com_alter_db | 0 | | Com_alter_db_upgrade | 0 | ...
>常見的統計參數
Com_select :執行select操作的次數,一次只累加1 Com_insert:執行insert操作的次數,一次也只累加1 Com_update:執行update操作的次數 Com_delete:執行delete的次數 Innodb_rows_read select查詢的返回的行數 Innodb_rows_inserted 執行INSERT操作插入的行數 Innodb_rows_updated 執行update操作更新的行數 Innodb_rows_deleted 執行deleted操作刪除的行數 Connections:試圖連接MYSQL服務器的次數。 Uptime 服務器工作時間。 Slow_queries 慢查詢的次數。
>查看慢查詢次數
mysql> show global status like 'Slow_queries'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Slow_queries | 4 | +---------------+-------+ 1 row in set (0.00 sec)
二、效率較低早查詢
>開啓慢查詢日誌
MySQL的慢查詢日誌是MySQL提供的一種日誌記錄,它用來記錄在MySQL中響應時間超過閥值的語句,具體指運行時間通過long_query_time值來設定,超過這個值則會被記錄到慢查詢日誌中。long_query_time的默認值爲10,默認情況下,Mysql數據庫並不啓動慢查詢日誌,需要我們手動來設置這個參數,當然,如果不是調優需要的話,一般不建議啓動該參數,因爲開啓慢查詢日誌會或多或少帶來一定的性能影響。慢查詢日誌支持將日誌記錄寫入文件,也支持將日誌記錄寫入數據庫表。
1>查看是否開啓忙日誌查詢
show variables like '%slow_query_log%';
2>開啓滿日誌查詢
1.配置文件修改 log-slow-queries = /usr/local/mysql/var/slowquery.log long_query_time = 3 #單位是秒 log-queries-not-using-indexes 2.使用sql語句修改 set global log_slow_queries = ON; set global slow_query_log = ON; set global long_query_time=0.01#單位是秒
3>慢日誌格式
select * from auction1.user where user_name='liaoxz'; 執行的慢查詢語句 # Time: 170427 4:41:48 日誌記錄的時間 # User@Host: root[root] @ localhost [] 服務器用戶/主機名 # Query_time: 10.007816 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 總的查詢時間、鎖定時間、"發送"或者返回的行數、查詢過程中所檢查的行數
三、分析語句早實行
>show profiles
profiling在mysql中默認是關閉的,在數據庫會話級別可以打開,開啓之後會收集在執行語句時候所使用的資源 在會話打開profilinges set profiling=1;
>使用show profiles查看執行的語句的持續時間和ID
MariaDB [auction1]> show profiles; +----------+------------+------------------------------------------------------------------------------------------------------------------+ | Query_ID | Duration | Query | +----------+------------+------------------------------------------------------------------------------------------------------------------+ | 6 | 0.00038804 | select * from orders limit 1 | | 7 | 0.00019379 | select * from orders limit 1 | | 8 | 0.00028797 | select * from user limit 1 | | 9 | 0.00006671 | select * from orders right user where orders.ID=user.id | | 10 | 0.00336970 | select orders*.user.id from where orders.ID=user.id | | 11 | 0.00005291 | select orders*,user.id from where orders.ID=user.id | | 12 | 0.00004725 | select orders*,user.id from orders,user where orders.ID=user.id | | 13 | 0.00003863 | select orders*,user.id from orders,user where orders.ID=user.id | | 14 | 0.00024284 | select * from orders limit 1 | | 15 | 0.00027788 | select * from orders limit 10 | | 16 | 0.00007312 | select * from user where user='liaoxz'; select * from user where user='liaoxz' | | 17 | 0.00031860 | select * from user limit 1 | | 18 | 0.02771239 | select * from user where user_name='liaoxz' | | 19 | 0.03121868 | select * from user where user_name='liaoxz' | | 20 | 0.02219678 | select * from user where user_name='liaoxz' | +----------+------------+------------------------------------------------------------------------------------------------------------------+ 15 rows in set (0.00 sec)
>通過指定ID查詢執行時的持續時間
MariaDB [auction1]> show profile for query 20; +----------------------+----------+ | Status | Duration | +----------------------+----------+ | starting | 0.000038 | | checking permissions | 0.000005 | | Opening tables | 0.000010 | | After opening tables | 0.000004 | | System lock | 0.000002 | | Table lock | 0.000002 | | After table lock | 0.000004 | | init | 0.000035 | | optimizing | 0.000011 | | statistics | 0.000013 | | preparing | 0.000008 | | executing | 0.000002 | | Sending data | 0.021981 | | end | 0.000012 | | query end | 0.000004 | | closing tables | 0.000007 | | freeing items | 0.000006 | | updating status | 0.000019 | | logging slow query | 0.000032 | | cleaning up | 0.000002 | +----------------------+----------+ 20 rows in set (0.00 sec)
>具體資源耗費查詢(all,cpu,block io,context switch,page faults)
MariaDB [auction1_1]> show profile cpu for query 20; +----------------------+----------+----------+------------+ | Status | Duration | CPU_user | CPU_system | +----------------------+----------+----------+------------+ | starting | 0.000038 | 0.000000 | 0.000000 | | checking permissions | 0.000005 | 0.000000 | 0.000000 | | Opening tables | 0.000010 | 0.000000 | 0.000000 | | After opening tables | 0.000004 | 0.000000 | 0.000000 | | System lock | 0.000002 | 0.000000 | 0.000000 | | Table lock | 0.000002 | 0.000000 | 0.000000 | | After table lock | 0.000004 | 0.000000 | 0.000000 | | init | 0.000035 | 0.000000 | 0.000000 | | optimizing | 0.000011 | 0.000000 | 0.000000 | | statistics | 0.000013 | 0.000000 | 0.000000 | | preparing | 0.000008 | 0.000000 | 0.000000 | | executing | 0.000002 | 0.000000 | 0.000000 | | Sending data | 0.021981 | 0.026852 | 0.000000 | | end | 0.000012 | 0.000000 | 0.000000 | | query end | 0.000004 | 0.000000 | 0.000000 | | closing tables | 0.000007 | 0.000000 | 0.000000 | | freeing items | 0.000006 | 0.000000 | 0.000000 | | updating status | 0.000019 | 0.000000 | 0.000000 | | logging slow query | 0.000032 | 0.000000 | 0.000000 | | cleaning up | 0.000002 | 0.000000 | 0.000000 | +----------------------+----------+----------+------------+ 20 rows in set (0.00 sec)
四、常見show命令大全
>sql show大全
>1. show tables/show tables from database_name; -- 顯示當前數據庫中所有表的名稱。
>2. show databases; -- 顯示mysql中所有數據庫的名稱。
>3. show columns from table_name from database_name; 或show columns from database_name.table_name; -- 顯示錶中列名稱。
>4. show grants for user_name; -- 顯示一個用戶的權限,顯示結果類似於grant 命令。
>5. show index from table_name; -- 顯示錶的索引。
>6. show variables; -- 顯示系統變量的名稱和值。
>7. show processlist; -- 顯示系統中正在運行的所有進程,也就是當前正在執行的查詢。大多數用戶可以查看他們自己的進程,但是如果他們擁有process權限,就可以查看所有人的進程,包括密碼。
>8. show table status; -- 顯示當前使用或者指定的database中的每個表的信息。信息包括表類型和表的最新更新時間。
>9. show privileges; -- 顯示服務器所支持的不同權限。
>10. show create database database_name; -- 顯示create database 語句是否能夠創建指定的數據庫。
>11. show create table table_name; -- 顯示create database 語句是否能夠創建指定的數據庫。
>12. show engines; -- 顯示安裝以後可用的存儲引擎和默認引擎。
>13. show innodb status; -- 顯示innoDB存儲引擎的狀態。
>14. show logs; -- 顯示BDB存儲引擎的日誌。
>15. show warnings; -- 顯示最後一個執行的語句所產生的錯誤、警告和通知。
>16. show errors; -- 只顯示最後一個執行語句所產生的錯誤。
>17. show [storage] engines; --顯示安裝後的可用存儲引擎和默認引擎。
五、mysql配置文件來一套
>配置文件/etc/my.cnf
[client] port = 3306 socket = /tmp/mysql.sock default-character-set=utf8 [mysqld] default-time-zone = '+8:00' server-id=9527 user=mysql #bin-log日誌相關優化參數 log-bin=mysql-master-bin #混合模式複製 binlog_format = mixed #存儲二進制緩存 binlog_cache_size = 16M #bin-log日誌保存多長時間 expire_logs_days=7 #最大連接數 max_connections=1000 #事務提交或事務外的指令把日誌寫入(flush)硬盤 innodb_flush_log_at_trx_commit=1 #文件系統自動識別緩存大小是否寫入磁盤 sync_binlog=0 datadir = /data/mysqldb port = 3306 socket = /tmp/mysql.sock #table_cache = 5000 #緩存大小設置 query_cache_size = 256M #單個查詢使用多大緩衝區 query_cache_limit = 16M #內存表大小 max_heap_table_size = 128M #線程池大小 thread_cache_size = 128 #慢查詢超過多長時間寫入日誌 long_query_time = 3 #臨時表大小 tmp_table_size = 128M #設置索引緩衝區大小 key_buffer_size = 256M #數據包大小接受設置 max_allowed_packet = 64M #索引掃描,範圍索引掃描,無索引全表掃描的表連接 緩存的大小 join_buffer_size = 16M #排序 會話 的緩存大小 sort_buffer_size = 16M #順序讀表緩存 read_buffer_size = 16M #隨機讀緩衝區大小 read_rnd_buffer_size = 64M #緩存innodb表的索引,數據,插入數據時的緩衝 innodb_buffer_pool_size = 6G sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid #timestamp類型的列明確的註明default值 explicit_defaults_for_timestamp=true #禁止域名解析 skip-name-resolve #數據庫是否支持符號鏈接存儲,1爲允許 symbolic-links=0 #數據庫級的字符集,對之前數據不產生影響 character-set-server=utf8mb4 #大小寫銘感 lower_case_table_names=1 服務器關閉非交互連接之前等待活動的秒數 wait_timeout=180 #閉一個交互的連接之前所要等待的秒數 interactive_timeout=180