首頁 資料庫 mysql教程 MySQL執行緒處於Opening tables的問題解決(附範例)

MySQL執行緒處於Opening tables的問題解決(附範例)

Jan 26, 2019 am 11:30 AM
mysql

這篇文章帶給大家的內容是關於MySQL執行緒處於Opening tables的問題解決(附範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

問題描述

最近有一台MySQL5.6.21的伺服器,在應用程式發布後,並發執行緒Threads_running迅速升高,達到2000左右,大量執行緒處於等待Opening tables、closing tables狀態,應用端相關邏輯存取逾時。

【分析過程】

1、16:10應用程式發布結束後,Opened_tables不斷增加,如下圖所示:
MySQL執行緒處於Opening tables的問題解決(附範例)

查看當時故障期間抓取的pt-stalk日誌文件,時間點2019-01-18 16:29:37,Open_tables 的值為3430,而table_open_cache的設定值為2000。
當Open_tables值大於table_open_cache值時,每次新的session開啟表,有些無法命中table cache,而必須重新開啟表。這樣反應出來的現象就是有大量的執行緒處於opening tables狀態。

2、這個實例下的表,加上系統資料庫下總計851張,遠小於table_open_cache的2000,為什麼會導致Open_tables達到3430呢
從官方文件中可以得到解釋,
https://dev.mysql.com/doc/refman/5.6/en/table-cache.html

table_open_cache is related to max_connections. For example, for 200 concurrent running connections, specify a table cache size of at least 200 * N, where N is the maximum number of tables per join in any of the queries which you execute.
登入後複製

當時並發線程數達到1980,假設這些並發連線中有30%是訪問2張表,其他都是單表,那麼cache size就會達到(1980*30%*2 1980*70%*1)=2574

3、QPS在發布前後都比較平穩,從外部請求來看並沒有突增的連線請求,但在發布後threads_running上升到接近2000的高位,一直持續。猜測是由於某個發佈的SQL語句觸發了問題。

4、查看當時抓取的processlist信息,有一句SQL並發訪問很高,查詢了8張物理表,SQL樣本如下:

<code>select id,name,email from table1 left join table2<br/>union all<br/>select id,name,email from table3 left join table4<br/>union all<br/>select id,name,email from table5 left join table6<br/>union all<br/>select id,name,email from table7 left join table8<br/>where id in (&#39;aaa&#39;);</code>
登入後複製

5、在測試環境中建立相同的8張表,清空表緩存,單一session執行SQL前後對比,Open_tables的值會增加8,如果高並發的情況下,Open_tables的值就會大幅增加。

問題重現

在測試環境上模擬高並發存取的場景,並發1000個執行緒同時執行上面的SQL語句,重現了生產環境類似的現象,Open_tables迅速達到3800,大量進程處於Opening tables、closing tables狀態。

優化方案

1、 定位到問題原因後,我們與開發同事溝通,建議優化該SQL,降低單句SQL查詢表的數量或大幅降低該SQL的並發存取頻率。
不過開發同事還沒來的及優化,生產環境故障又出現了。當時DBA排障時將table_open_cache從2000增加4000,CPU使用率上升,效果並不明顯,等待Opening tables的問題依然存在。

2、 分析故障期間抓取的pstack信息,用pt-pmp聚合後,看到大量線程在open_table時等待mutex資源:

#0  0x0000003f0900e334 in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x0000003f0900960e in _L_lock_995 () from /lib64/libpthread.so.0
#2  0x0000003f09009576 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x000000000069ce98 in open_table(THD*, TABLE_LIST*, Open_table_context*) ()
#4  0x000000000069f2ba in open_tables(THD*, TABLE_LIST**, unsigned int*, unsigned int, Prelocking_strategy*) ()
#5  0x000000000069f3df in open_normal_and_derived_tables(THD*, TABLE_LIST*, unsigned int) ()
#6  0x00000000006de821 in execute_sqlcom_select(THD*, TABLE_LIST*) ()
#7  0x00000000006e13cf in mysql_execute_command(THD*) ()
#8  0x00000000006e4d8f in mysql_parse(THD*, char*, unsigned int, Parser_state*) ()
#9  0x00000000006e62cb in dispatch_command(enum_server_command, THD*, char*, unsigned int) ()
#10 0x00000000006b304f in do_handle_one_connection(THD*) ()
#11 0x00000000006b3177 in handle_one_connection ()
#12 0x0000000000afe5ca in pfs_spawn_thread ()
#13 0x0000003f09007aa1 in start_thread () from /lib64/libpthread.so.0
#14 0x0000003f088e893d in clone () from /lib64/libc.so.6
登入後複製

這時table_cache_manager中的mutex衝突非常嚴重。
由於MySQL5.6.21下table_open_cache_instances參數的預設值為1,想到增大table_open_cache_instances參數,增加表格快取分區,應該可以緩解爭用。

3、 在測試環境上,我們調整兩個參數table_open_cache_instances=32,table_open_cache=6000,同樣並發1000個執行緒執行問題SQL,這次等待Opening tables、closing tables的執行緒消失了,MySQL的執行緒消失了,MySQL的QPS也從12000上升到55000。
比較相同情況下,只調整table_open_cache=6000,等待Opening tables的進程數從861下降到203,問題有所緩解,有600多個進程已經從等待Opening tables變成運行狀態,QPS上升到40000左右,但不能根治。

原始碼分析

查了下程式碼有關table_open_cache的相關邏輯:
1、Table_cache::add_used_table函數如下,當新的連線開啟的表在table cache中不存在時,開啟表格加入used tables list:

bool Table_cache::add_used_table(THD *thd, TABLE *table)
{
  Table_cache_element *el;

  assert_owner();

  DBUG_ASSERT(table->in_use == thd);

  /*
    Try to get Table_cache_element representing this table in the cache
    from array in the TABLE_SHARE.
  */
  el= table->s->cache_element[table_cache_manager.cache_index(this)];

  if (!el)
  {
    /*
      If TABLE_SHARE doesn&#39;t have pointer to the element representing table
      in this cache, the element for the table must be absent from table the
      cache.

      Allocate new Table_cache_element object and add it to the cache
      and array in TABLE_SHARE.
    */
    DBUG_ASSERT(! my_hash_search(&m_cache,
                                 (uchar*)table->s->table_cache_key.str,
                                 table->s->table_cache_key.length));

    if (!(el= new Table_cache_element(table->s)))
      return true;

    if (my_hash_insert(&m_cache, (uchar*)el))
    {
      delete el;
      return true;
    }

    table->s->cache_element[table_cache_manager.cache_index(this)]= el;
  }

  /* Add table to the used tables list */  
  el->used_tables.push_front(table);

  m_table_count++;  free_unused_tables_if_necessary(thd);

  return false;
}
登入後複製

2、每次add_used_table會呼叫Table_cache::free_unused_tables_if_necessary函數,當滿足m_table_count > table_cache_size_per_necessary函數,當滿足m_table_count > table_cache_size_per_instance &tablem_unun;中多餘的cache。其中table_cache_size_per_instance= table_cache_size / table_cache_instances,MySQL5.6的預設配置是2000/1=2000,當m_table_count值大於2000並且m_unused_tables非空時就執行行中的這樣m_table_count就是Open_tables的值正常會維持在2000上下。

void Table_cache::free_unused_tables_if_necessary(THD *thd)
{
  /*
    We have too many TABLE instances around let us try to get rid of them.

    Note that we might need to free more than one TABLE object, and thus
    need the below loop, in case when table_cache_size is changed dynamically,
    at server run time.
  */
  if (m_table_count > table_cache_size_per_instance && m_unused_tables)
  {
    mysql_mutex_lock(&LOCK_open);
    while (m_table_count > table_cache_size_per_instance &&
           m_unused_tables)
    {
      TABLE *table_to_free= m_unused_tables;      
      remove_table(table_to_free);
      intern_close_table(table_to_free);
      thd->status_var.table_open_cache_overflows++;
    }
    mysql_mutex_unlock(&LOCK_open);
  }
}
登入後複製

3、增大table_cache_instances為32,當Open_tables超過(2000/32=62)時,就會滿足條件,加速上述邏輯中m_unused_tables的清理,使得table cache中數量進一步減少,會導致Table_open_cache_overflows升高。

4、当table_open_cache_instances从1增大到32时,1个LOCK_open锁分散到32个m_lock的mutex上,大大降低了锁的争用。

/** Acquire lock on table cache instance. */
  void lock() { mysql_mutex_lock(&m_lock); }
  /** Release lock on table cache instance. */
  void unlock() { mysql_mutex_unlock(&m_lock); }
登入後複製

解决问题

我们生产环境同时采取下面优化措施,问题得以解决:
1、 读写分离,增加read节点,分散master库的压力;
2、 调整table_open_cache_instances=16;
3、 调整table_open_cache=6000;

总结

当出现Opening tables等待问题时,
1、建议找出打开表频繁的SQL语句,优化该SQL,降低单句SQL查询表的数量或大幅降低该SQL的并发访问频率。

2、设置合适的table cache,同时增大table_open_cache_instances和 table_open_cache参数的值。

以上是MySQL執行緒處於Opening tables的問題解決(附範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

MySQL的角色:Web應用程序中的數據庫 MySQL的角色:Web應用程序中的數據庫 Apr 17, 2025 am 12:23 AM

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

docker怎麼啟動mysql docker怎麼啟動mysql Apr 15, 2025 pm 12:09 PM

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

laravel入門實例 laravel入門實例 Apr 18, 2025 pm 12:45 PM

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

解決數據庫連接問題:使用minii/db庫的實際案例 解決數據庫連接問題:使用minii/db庫的實際案例 Apr 18, 2025 am 07:09 AM

在開發一個小型應用時,我遇到了一個棘手的問題:需要快速集成一個輕量級的數據庫操作庫。嘗試了多個庫後,我發現它們要么功能過多,要么兼容性不佳。最終,我找到了minii/db,這是一個基於Yii2的簡化版本,完美地解決了我的問題。

centos7如何安裝mysql centos7如何安裝mysql Apr 14, 2025 pm 08:30 PM

優雅安裝 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設置開機自啟動

centos安裝mysql centos安裝mysql Apr 14, 2025 pm 08:09 PM

在 CentOS 上安裝 MySQL 涉及以下步驟:添加合適的 MySQL yum 源。執行 yum install mysql-server 命令以安裝 MySQL 服務器。使用 mysql_secure_installation 命令進行安全設置,例如設置 root 用戶密碼。根據需要自定義 MySQL 配置文件。調整 MySQL 參數和優化數據庫以提升性能。

laravel框架安裝方法 laravel框架安裝方法 Apr 18, 2025 pm 12:54 PM

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

MySQL和PhpMyAdmin:核心功能和功能 MySQL和PhpMyAdmin:核心功能和功能 Apr 22, 2025 am 12:12 AM

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

See all articles