MySQL優化的步驟詳解
在開發過程中,雖然感覺優化sql語句很重要,但往往更重視的是功能實現,為了讓自己以後寫Mysql語句效率更高,有必要對Mysql優化做一個小歸納。
步驟一、透過show status 指令了解各種sql執行的效率
show [session|gobal] status
session等級表示統計目前連線的結果。
global等級表示統計自資料上次啟動至今的結果。
如果不寫入級別,預設的是session級別
#eg:SHOW GLOBAL STATUS;
Variable_name | Value |
#Aborted_clients | #6 |
Aborted_connects | 0 |
Binlog_cache_disk_use | |
#Binlog_cache_use | 0 |
Binlog_stmt_cache_disk_use | #0 |
Binlog_stmt_cache_use | #0 |
##收到的位元組數 | |
發送的位元組數 | |
Com_admin_commands | |
Com_assign_to_keycache | |
Com_alter_db | |
Com_alter_db_upgrade | |
#Com_alter_event | |
0 | |
0 | |
0 | |
6 | |
#0 | |
0 | |
0 | |
#0 | |
0 | |
#0 | |
8 | |
0 | |
#0 | |
0 | |
#Com_create_db | |
Com_create_event | |
Com_create_function | |
Com_create_index | |
#Com_create_procedure | |
#Com_create_server | |
Com_create_table | |
Com_drop_server | 0 |
Com_drop_table | 0 |
Com_drop_trigger | 0 |
Com_drop_user | 0 |
#Com_drop_view | ##1|
##Com_empty_query | 2 |
#Com_execute_sql | ##0 |
Com_flush | 0 |
Com_get_diagnostics | 0 |
Com_grant | ##0|
Com_ha_close | |
#Com_ha_open | |
Com_ha_read | |
Com_help | |
#Com_insert | |
#Com_insert_select | |
#Com_install_plugin | ##0|
#Com_kill | 0 |
Com_load | 0 |
#Com_lock_tables | 0 |
0 | |
0 | |
#0 | |
0 | |
#0 | |
0 | |
0 | |
0 | |
0 | |
#0 | |
0 | |
0 | |
0 | |
0 | |
0 | |
Com_rollback | 0 |
Com_rollback_to_savepoint | ##0|
#0 | |
414 | |
525 | |
0 | |
Com_show_create_trigger | 0 |
Com_show_databases | #8 |
Com_show_engine_logs | ##8|
8 | |
0 | |
#0 | |
#0 | |
0 | |
0 | |
102 | |
#0 | |
0 | ##Com_show_function_status |
#Com_show_grants | |
Com_show_keys | |
Com_show_master_status | |
Com_show_open_tables | |
Com_show_plugins | |
Com_show_privileges | |
Com_show_procedure_code | |
Com_show_procedure_status | |
##Com_show_procedure_status | |
#0 | |
1 | |
0 | |
115 | |
0 | |
##0 | |
Com_show_slave_status | |
##Com_show_status | |
Com_show_storage_engines | |
Com_show_table_status | |
Com_show_tables | |
Com_show_tables | |
Com_show_triggers | |
Com_show_variables | |
##Com_show_warnings | 0 |
#Com_slave_start | 0 |
Com_slave_stop | 0 |
Com_stmt_close | 0 |
Com_stmt_execute | #0 |
Com_stmt_fetch | |
#Com_stmt_prepare | |
Com_stmt_reprepare | |
Com_stmt_reset | |
Com_stmt_send_long_data | |
Com_xa_rollback | 0 |
#Com_xa_start | 0 |
ON | |
0 | |
0 | |
0 | |
0 | |
Connection_errors_tcpwrap | |
連線 | |
128 | |
5 | |
5 | |
910 | |
0 | |
#0 | |
0 | |
1 | |
108 | |
2 | |
#Handler_external_lock | |
Handler_mrr_init | |
Handler_prepare | |
Handler_read_first | |
Handler_read_key | |
#Handler_read_last | |
#Handler_read_next | |
Handler_read_prev | |
Handler_read_rnd | |
Handler_read_rnd_next | |
Handler_rollback | |
Handler_savepoint | |
##Handler_savepoint_rollback | 0 |
Handler_update | 195 |
Handler_write | 93316 |
#Innodb_buffer_pool_dump_status | 未啟動 |
Innodb_buffer_pool_load_status | |
##Innodb_buffer_pool_ | |
##Innodb_buffer_pool_pages_data | 397 |
Innodb_buffer_pool_bytes_data | 6504448 |
Innodb_buffer_pool_read_ahead | 0 |
#Innodb_buffer_pool_read_ahead_evicted | 0 |
Innodb_buffer_pool_ | ##0|
4642 | |
364 | |
0 | #Innodb_buffer_pool_wait_free |
#Innodb_buffer_pool_write_requests | |
Innodb_data_fsyncs | |
Innodb_data_pending_fsyncs | |
Innodb_data_pending_reads | |
#Innodb_data_pending_writes | |
Innodb_data_read | |
Innodb_data_reads | |
281 | |
Innodb_data_writing | #6534656 |
Innodb_dblwr_pages_writing | 193 |
Innodb_dblwr_writes | 14 |
Innodb_have_atomic_builtins | ON |
#Innodb_log_waits | #0 |
Innodb_log_write_requests | #574 |
Innodb_log_writes | 46 |
Innodb_os_log_fsyncs | 61 |
Innodb_os_log_pending_fsyncs | 0 |
Innodb_os_log_pending_writes | 0 |
Innodb_os_log_writing | #202752 |
##Innodb_page_size | #16384 |
##Innodb_pages_created | 34 |
#Innodb_pages_read | 363 |
#Innodb_pages_writing | |
193 | |
0 | ##Innodb_row_lock_time |
#Innodb_row_lock_time | |
Innodb_row_lock_time_avg | |
#Innodb_row_lock_time_max | |
#Innodb_row_lock_waits | 0|
#Innodb_row_lock_waits | 0 |
#Innodb_row_lock_waits | ##0|
#0 | |
3 | |
406 | |
2 | |
#Innodb_num_open_files | |
Innodb_truncated_status_writes | |
##Innodb_available_undo_logs | |
Key_writes | #50 |
Last_query_cost | 0.00000 |
#Last_query_partial_plans | |
#0 | |
#3 | |
0 | |
70 | |
0 | |
120 | |
117 | |
1042 | |
144 | |
#147 | |
##Performance_schema_accounts_lost | |
##Performance_schema_accounts_lost | 0 |
Performance_schema_cond_classes_lost | 0 |
Performance_schema_cond_instances_lost | 0 |
Performance_schema_digest_lost | 0 |
#Performance_schema_file_classes_lost | #0 |
Performance_schema_file_hand | 0|
0 | |
#0 | |
0 | |
0 | |
0 | |
0 | |
#0 | |
0 | ##Performance_sche_schedinsta_ |
Performance_schema_stage_classes_lost | |
Performance_schema_statement_classes_lost | |
#Performance_schema_table_handles_lost | |
#Performance_schema_table_instances_lost | |
Performance_schema_thread_class | 0|
Performance_schema_thread_class_class#lost | #0 |
Performance_schema_thread_instances_lost | 0 |
Performance_schema_users_lost | 0 |
##Prepared_stmt_count | 0 |
Qcache_free_blocks | 1 |
#Qcache_free_memory | ##1|
1039896 | |
#0 | |
查詢 | 1888 |
問題 | 1887 |
Select_full_join | 1 |
Select_full_range_join | #0 |
##23 | |
0 | |
727 | |
0.000 | |
#0 | |
0 | |
0 | |
0 | |
OFF | |
0 | |
0 | |
##Sort_merge_passes | 0 |
#Sort_range | 0 |
Sort_rows | 1964 |
Sort_scan | 151 |
Ssl_accept_renegotiates | #0 |
#Ssl_accepts | 0 |
Ssl_callback_cache_hits | #0 |
Ssl_cipher | |
Ssl_cipher_list | |
Ssl_client_connects | #0 |
##Ssl_connect_renegotiates | 0 |
Ssl_ctx_verify_depth | 0 |
#Ssl_ctx_verify_mode | |
# 0 | |
0 | |
#0 | |
0 | |
# | |
#Ssl_server_not_before | |
##Ssl_session_cache_hits | #0 |
Ssl_session_cache_misses | #0 |
Ssl_session_cache_mode | NONE |
#Ssl_session_cache_overflows | 0 |
#Ssl_session_cache_size | |
#Ssl_session_cache_size | ##0|
0 | |
#0 | |
0 | |
0 | |
#0 | ##Ssl_verify_mode |
##Ssl_verify_ | |
Table_open_cache_overflows | 0 |
#Tc_log_max_pages_used | 0 |
#Tc_log_page_size | 0|
。 | 0|
#0 | |
#1 | |
2 | |
3 | |
#1 | |
286258 |
主要參數描述
#Connections:檢視連接mysql伺服器的次數
#Uptime:伺服器工作時間
Slow_queries:慢查詢的次數
Com_xxx表示每個xxx語句執行的次數
Com_select 執行select次數
#Com_insert 執行insert次數,批次插入時候,只累加1次
#Com_update 執行Update操作次數
#Com_delete 執行刪除操作次數
#innodb_rows_xxx類型的參數只對innodb儲存引擎有效
解析:透過上面一些參數,可以輕鬆了解當前資料庫的應用程式時插入更新為主或查詢為主,以及執行比例。對應更新操作的計數,是對執行次數的計數,不論提交或回滾都會累加。
步驟二:定位執行效率較低的sql語句
#透過慢查詢日誌定位哪些是執行效率的sql語句。用--log-slow-queries[=file_name]選項啟動,mysqld寫一個包含所有執行超過long_query_time秒的sql語句的日誌檔。關於如何定位慢查詢可以點解這裡(http://www.php.cn/)
在步驟二中,我們可以查詢到低效率的sql語句,在此我們透過explain或desc可以取得mysql執行select訊息。
eg:
#
EXPLAIN SELECT t0.* FROM t3 AS t0 LEFT JOIN `t2` AS t1 ON t0.`id1` = t1.`id1` WHERE t0.id1 = 5 ;
執行結果如下:
#解析:select_type:表示select類型。常見的取值有SIMPLE(簡單表,即不使用連接或子查詢)、PRIMARY(主查詢,即外層的查詢)、UNION(union中的第二個或後面的查詢語句)、SUBQUERY(子查詢中的第一個SELECT)等。 talbe:輸出結果集的表格。
type:表的連線類型。效能由高到底:system(表中只有一行)、const(表中最多有一個符合行)、eq_ref、ref、ref_null、index_merge、unique_subquery、index_subquery、range、idnex等
possible_keys:查詢時,可能使用的索引
key:實際使用的索引
key_len:索引欄位的長度
rows:掃描行的數量Extra:執行情況的說明和描述
##步驟四:確定問題並採取相應的最佳化措施
經過上面的步驟,可以確定問題出現的原因,此時我們可以根據情況,採取相應的措施。常見的措施有1.建立對應的索引2.最佳化sql語句3.分錶等。
handler_read_rnd_next值高則表示查詢運行低效,並且應該建立索引補救。如果正在進行大量的資料表掃描,handler_read_rnd_next值較高,則通常表示資料表索引不正確或寫入的查詢沒有利用索引,如下圖。
#我們該當定期分析錶盒檢查表
檢查表使用下列指令(檢查t3表)
ANALYZE TABLE t3;###CHECK TABLE t3 ;###############定期最佳化表使用指令如下############optimize table 表名############常見語句優化######
1.優化Insert語句
(1)如果從同一個客戶端插入數據,盡力使用多個字表的insert語句和多行插入,減少單行插入,這種方式大幅減少客戶端與資料庫直接的連線、關閉等消耗。 eg:
INSERT INTO t3 VALUES(1,2),(8,5),(6,5),(4,3)
(2)如果從不同的客戶插入很多行,則能透過使用inset delayed語句得到更高的速度。
(3)如果進行批次插入,可以增加bulk_insert_buffer_size變數方法,提高速度。
2.最佳化group by語句
#預設情況下使用group by col1,col2....會對查詢進行對應的排序,如果使用者想要避免排序結果的消耗,可以指定order by null 禁止排序。透過查詢結果中extra欄位可以看出:
3.最佳化order by語句
在某些情況中,mysql可以使用一個索引來滿足order by子句,而不需要額外的排序,where條件和order by使用相同的索引。
4.最佳化含有or語句
對於含有or的查詢子句,如果要使用索引,則or之間的每個條件列必須用到索引;否則,應該考慮添加索引。
5.使用sql提示
#以上就是MySQL優化的步驟詳解的內容,更多相關內容請關注PHP中文網(www.php.cn)!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL是一種開源的關係型數據庫管理系統,主要用於快速、可靠地存儲和檢索數據。其工作原理包括客戶端請求、查詢解析、執行查詢和返回結果。使用示例包括創建表、插入和查詢數據,以及高級功能如JOIN操作。常見錯誤涉及SQL語法、數據類型和權限問題,優化建議包括使用索引、優化查詢和分錶分區。

Apache 連接數據庫需要以下步驟:安裝數據庫驅動程序。配置 web.xml 文件以創建連接池。創建 JDBC 數據源,指定連接設置。從 Java 代碼中使用 JDBC API 訪問數據庫,包括獲取連接、創建語句、綁定參數、執行查詢或更新以及處理結果。

MySQL在數據庫和編程中的地位非常重要,它是一個開源的關係型數據庫管理系統,廣泛應用於各種應用場景。 1)MySQL提供高效的數據存儲、組織和檢索功能,支持Web、移動和企業級系統。 2)它使用客戶端-服務器架構,支持多種存儲引擎和索引優化。 3)基本用法包括創建表和插入數據,高級用法涉及多表JOIN和復雜查詢。 4)常見問題如SQL語法錯誤和性能問題可以通過EXPLAIN命令和慢查詢日誌調試。 5)性能優化方法包括合理使用索引、優化查詢和使用緩存,最佳實踐包括使用事務和PreparedStatemen

選擇MySQL的原因是其性能、可靠性、易用性和社區支持。 1.MySQL提供高效的數據存儲和檢索功能,支持多種數據類型和高級查詢操作。 2.採用客戶端-服務器架構和多種存儲引擎,支持事務和查詢優化。 3.易於使用,支持多種操作系統和編程語言。 4.擁有強大的社區支持,提供豐富的資源和解決方案。

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

在 Docker 中啟動 MySQL 的過程包含以下步驟:拉取 MySQL 鏡像創建並啟動容器,設置根用戶密碼並映射端口驗證連接創建數據庫和用戶授予對數據庫的所有權限

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

優雅安裝 MySQL 的關鍵在於添加 MySQL 官方倉庫。具體步驟如下:下載 MySQL 官方 GPG 密鑰,防止釣魚攻擊。添加 MySQL 倉庫文件:rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm更新 yum 倉庫緩存:yum update安裝 MySQL:yum install mysql-server啟動 MySQL 服務:systemctl start mysqld設置開機自啟動
