文件首頁
MySQL 9.0 參考手冊
相關文件 下載本手冊
PDF (US Ltr) - 40.0Mb
PDF (A4) - 40.1Mb
Man Pages (TGZ) - 258.2Kb
Man Pages (Zip) - 365.3Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb


MySQL 9.0 參考手冊  /  MySQL Performance Schema  /  Performance Schema 狀態監控

29.7 Performance Schema 狀態監控

有數個與 Performance Schema 相關的狀態變數

mysql> SHOW STATUS LIKE 'perf%';
+-----------------------------------------------+-------+
| Variable_name                                 | Value |
+-----------------------------------------------+-------+
| 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_handles_lost          | 0     |
| Performance_schema_file_instances_lost        | 0     |
| Performance_schema_hosts_lost                 | 0     |
| Performance_schema_locker_lost                | 0     |
| Performance_schema_memory_classes_lost        | 0     |
| Performance_schema_metadata_lock_lost         | 0     |
| Performance_schema_mutex_classes_lost         | 0     |
| Performance_schema_mutex_instances_lost       | 0     |
| Performance_schema_nested_statement_lost      | 0     |
| Performance_schema_program_lost               | 0     |
| Performance_schema_rwlock_classes_lost        | 0     |
| Performance_schema_rwlock_instances_lost      | 0     |
| Performance_schema_session_connect_attrs_lost | 0     |
| Performance_schema_socket_classes_lost        | 0     |
| Performance_schema_socket_instances_lost      | 0     |
| Performance_schema_stage_classes_lost         | 0     |
| Performance_schema_statement_classes_lost     | 0     |
| Performance_schema_table_handles_lost         | 0     |
| Performance_schema_table_instances_lost       | 0     |
| Performance_schema_thread_classes_lost        | 0     |
| Performance_schema_thread_instances_lost      | 0     |
| Performance_schema_users_lost                 | 0     |
+-----------------------------------------------+-------+

Performance Schema 狀態變數提供關於由於記憶體限制而無法載入或建立的工具的資訊。這些變數的名稱有數種形式

  • Performance_schema_xxx_classes_lost 表示無法載入多少 xxx 類型的工具。

  • Performance_schema_xxx_instances_lost 表示無法建立多少 xxx 物件類型的執行個體。

  • Performance_schema_xxx_handles_lost 表示無法開啟多少 xxx 物件類型的執行個體。

  • Performance_schema_locker_lost 表示有多少事件遺失或未記錄。

例如,如果在伺服器原始碼中檢測到互斥鎖,但伺服器無法在執行階段為該工具分配記憶體,則會遞增 Performance_schema_mutex_classes_lost。互斥鎖仍然作為同步物件運作(也就是說,伺服器會繼續正常運作),但不會收集它的效能資料。如果可以分配工具,則可以用於初始化檢測到的互斥鎖執行個體。對於單例互斥鎖(例如全域互斥鎖),只有一個執行個體。其他互斥鎖在每個連線中或各種快取和資料緩衝區中的每個頁面都有一個執行個體,因此執行個體的數量會隨著時間而變化。增加最大連線數或某些緩衝區的最大大小,會增加可能一次分配的最大執行個體數。如果伺服器無法建立指定的檢測到互斥鎖執行個體,則會遞增 Performance_schema_mutex_instances_lost

假設以下條件成立

  • 伺服器啟動時使用了 --performance_schema_max_mutex_classes=200 選項,因此有空間容納 200 個互斥鎖儀器。

  • 已載入 150 個互斥鎖儀器。

  • 名為 plugin_a 的外掛程式包含 40 個互斥鎖儀器。

  • 名為 plugin_b 的外掛程式包含 20 個互斥鎖儀器。

伺服器會根據外掛程式的需求量和可用數量,為外掛程式分配互斥鎖儀器,如下列陳述序列所示

INSTALL PLUGIN plugin_a

伺服器現在有 150+40 = 190 個互斥鎖儀器。

UNINSTALL PLUGIN plugin_a;

伺服器仍然有 190 個儀器。外掛程式碼產生的所有歷史資料仍然可用,但不會收集儀器的新事件。

INSTALL PLUGIN plugin_a;

伺服器偵測到已定義 40 個儀器,因此不會建立新的儀器,並且會重複使用先前分配的內部記憶體緩衝區。伺服器仍然有 190 個儀器。

INSTALL PLUGIN plugin_b;

伺服器還有 200-190 = 10 個儀器的空間(在此案例中,是互斥鎖類別),並看到該外掛程式包含 20 個新儀器。載入 10 個儀器,而丟棄或遺失 10 個儀器。Performance_schema_mutex_classes_lost 指示遺失的儀器(互斥鎖類別)數量

mysql> SHOW STATUS LIKE "perf%mutex_classes_lost";
+---------------------------------------+-------+
| Variable_name                         | Value |
+---------------------------------------+-------+
| Performance_schema_mutex_classes_lost | 10    |
+---------------------------------------+-------+
1 row in set (0.10 sec)

儀器仍然有效,並收集 plugin_b 的(部分)資料。

當伺服器無法建立互斥鎖儀器時,會發生以下結果

剛才描述的模式適用於所有類型的儀器,而不僅僅是互斥鎖。

Performance_schema_mutex_classes_lost 值大於 0 可能發生在兩種情況下

  • 為了節省少量記憶體,您使用 --performance_schema_max_mutex_classes=N 啟動伺服器,其中 N 小於預設值。選擇預設值是為了足以載入 MySQL 發行版中提供的所有外掛程式,但如果永遠不載入某些外掛程式,則可以減少此值。例如,您可能選擇不載入發行版中的某些儲存引擎。

  • 您載入針對 Performance Schema 進行儀器化的第三方外掛程式,但在啟動伺服器時未考慮外掛程式的儀器記憶體需求。由於它來自第三方,因此預設為 performance_schema_max_mutex_classes 選擇的值中,不會考慮此引擎的儀器記憶體消耗。

    如果伺服器沒有足夠的資源來支援外掛程式的儀器,並且您未使用 --performance_schema_max_mutex_classes=N 明確分配更多資源,則載入外掛程式會導致儀器資源不足。

如果為 performance_schema_max_mutex_classes 選擇的值太小,則不會在錯誤日誌中報告錯誤,並且在執行階段不會失敗。但是,performance_schema 資料庫中資料表的內容會遺失事件。Performance_schema_mutex_classes_lost 狀態變數是唯一可見的跡象,表示由於無法建立儀器,因此內部丟棄了一些事件。

如果沒有遺失儀器,則 Performance Schema 會知道該儀器,並在對實例進行儀器化時使用該儀器。例如,wait/synch/mutex/sql/LOCK_deletesetup_instruments 資料表中的互斥鎖儀器名稱。當在程式碼中建立互斥鎖(在 THD::LOCK_delete 中)時,會使用這個單一儀器,但伺服器執行時需要多少互斥鎖實例。在此案例中,LOCK_delete 是每個連線(THD)的互斥鎖,因此,如果伺服器有 1000 個連線,則有 1000 個執行緒和 1000 個已儀器化的 LOCK_delete 互斥鎖實例(THD::LOCK_delete)。

如果伺服器沒有空間容納所有這些 1000 個已儀器化的互斥鎖(實例),則會建立一些帶有儀器的互斥鎖,而建立一些沒有儀器的互斥鎖。如果伺服器只能建立 800 個實例,則會遺失 200 個實例。伺服器會繼續執行,但會將 Performance_schema_mutex_instances_lost 增加 200,以指示無法建立實例。

當程式碼在執行階段初始化的互斥鎖多於為 --performance_schema_max_mutex_instances=N 分配的互斥鎖時,可能會發生 Performance_schema_mutex_instances_lost 值大於 0 的情況。

最重要的是,如果 SHOW STATUS LIKE 'perf%' 表示沒有遺失任何內容(所有值均為零),則 Performance Schema 資料是準確的,可以信賴。如果遺失了一些內容,則表示資料不完整,並且 Performance Schema 無法記錄所有內容,因為給予的記憶體不足。在這種情況下,特定的 Performance_schema_xxx_lost 變數會指示問題區域。

在某些情況下,故意造成儀器資源不足可能是合適的。例如,如果您不關心檔案 I/O 的效能資料,則可以使用所有與檔案 I/O 相關的 Performance Schema 參數設定為 0 來啟動伺服器。不會為檔案相關的類別、實例或控制代碼分配記憶體,並且會遺失所有檔案事件。

使用 SHOW ENGINE PERFORMANCE_SCHEMA STATUS 來檢查 Performance Schema 程式碼的內部運作

mysql> SHOW ENGINE PERFORMANCE_SCHEMA STATUS\G
...
*************************** 3. row ***************************
  Type: performance_schema
  Name: events_waits_history.size
Status: 76
*************************** 4. row ***************************
  Type: performance_schema
  Name: events_waits_history.count
Status: 10000
*************************** 5. row ***************************
  Type: performance_schema
  Name: events_waits_history.memory
Status: 760000
...
*************************** 57. row ***************************
  Type: performance_schema
  Name: performance_schema.memory
Status: 26459600
...

此陳述旨在幫助 DBA 了解不同的 Performance Schema 選項對記憶體需求的影響。有關欄位含義的說明,請參閱第 15.7.7.16 節,「SHOW ENGINE 陳述式」