mysql 參數:[read_buffer_size] [sort_buffer_size] [read_rnd_buffer_size] [tmp_table_size]---圖解


http://imysql.cn/2008_09_27_deep_into_mysql_sort_buffer

http://my.oschina.net/realfighter/blog/364420

https://www.percona.com/blog/2007/07/24/what-exactly-is-read_rnd_buffer_size/

 

  1. skip-external-locking:跳過外部鎖定。要明白這個參數,必須先了解external-locking(外部鎖定,作用是為MYISAM數據表在多進程【多個服務公用同一個數據庫目錄】訪問下鎖定),大多數情況下,我們的mysql服務都是單進程服務的,從mysql官網上看,skip-external-locking參數默認情況下是ON的,
    mysql> show variables like '%skip%';
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | skip_external_locking  | ON    |

     

    在配置文件[mysqld]下開啟這個參數OK。
  2. key_buffer_size = 256M:為MYISAM數據表開啟供線程共享的索引緩存。我們的項目中數據表基本上用的是INNODB引擎,所以這個參數暫時不進行調整,有需要的可以參考http://database.51cto.com/art/201010/229939.htm
  3. max_allowed_packet = 16M:服務端最大允許接收的數據包大小。在沒有調整該配置項的時候,服務端默認是4M。當然這個參數和mysql(默認16M)和mysqldump(默認為24M,我已經調整為16M)中的數據包大小有關系,一般情況下1M就可以,官方建議如果使用了blog或者更大的字符串時進行該參數的調整,一般情況下,數據庫會被初始化為net_buffer_length(最小1024byte,最大是1M,默認是16KB)的大小。
  4. table_open_cache = 512:所有線程打開表的數目(默認設置大小為1000)。如果opened_tables很大並且不經常使用flush tables,官方建議我們增加該參數的大小。這個值並不是越大越好,需要根據實際情況下open_tables和opened_tables的綜合進行調整,詳細可見http://www.cnblogs.com/suredandan/p/4010931.html
  5. sort_buffer_size = 512K:需要排序 會話 的緩存大小,是針對每一個connection的,這個值也不會越大越好,默認大小是256kb,過大的配置會消耗更多的內存。我個人還沒有測試,可以查看http://bbs.chinaunix.net/thread-1805254-1-1.html
  6. read_buffer_size = 512K:為需要全表掃描的MYISAM數據表線程指定緩存,也是針對每個connection的,這個參數暫時我也不需要太關注。
    Each thread that does a sequential scan for a MyISAM table allocates a buffer of this size (in bytes)
    for each table it scans. If you do many sequential scans, you might want to increase this value, which
    defaults to 131072. The value of this variable should be a multiple of 4KB. If it is set to a value that is not
    a multiple of 4KB, its value will be rounded down to the nearest multiple of 4KB.
    This option is also used in the following context for all search engines:
    • For caching the indexes in a temporary file (not a temporary table), when sorting rows for ORDER BY.
    • For bulk insert into partitions.
    • For caching results of nested queries.
    and in one other storage engine-specific way: to determine the memory block size for MEMORY tables.
    The maximum permissible setting for read_buffer_size is 2GB.
    For more information about memory use during different operations, see Section 8.11.4.1, “How MySQL
    Uses Memory”.

     

  7. read_rnd_buffer_size = 1M:首先,該變量可以被任何存儲引擎使用,當從一個已經排序的鍵值表中讀取行時,會先從該緩沖區中獲取而不再從磁盤上獲取。默認為256K。
    This variable is used for reads from MyISAM tables, and, for any storage engine, for Multi-Range Read
    optimization.
    When reading rows from a MyISAM table in sorted order following a key-sorting operation, the rows are
    read through this buffer to avoid disk seeks. See Section 8.2.1.15, “ORDER BY Optimization”. Setting
    the variable to a large value can improve ORDER BY performance by a lot. However, this is a buffer
    Server System Variables
    627
    allocated for each client, so you should not set the global variable to a large value. Instead, change the
    session variable only from within those clients that need to run large queries.
    The maximum permissible setting for read_rnd_buffer_size is 2GB.

     

    另外可參見http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3642777&highlight=
  8. thread_cache_size = 18:有多少線程供服務緩存使用。
    How many threads the server should cache for reuse. When a client disconnects, the client's threads
    are put in the cache if there are fewer than thread_cache_size threads there. Requests for threads
    are satisfied by reusing threads taken from the cache if possible, and only when the cache is empty is
    a new thread created. This variable can be increased to improve performance if you have a lot of new
    connections. Normally, this does not provide a notable performance improvement if you have a good
    thread implementation. However, if your server sees hundreds of connections per second you should
    normally set thread_cache_size high enough so that most new connections use cached threads. By
    examining the difference between the Connections and Threads_created status variables, you can
    see how efficient the thread cache is. For details, see Section 5.1.6, “Server Status Variables”.
    The default value is based on the following formula, capped to a limit of 100:
    8 + (max_connections / 100)
    This variable has no effect for the embedded server (libmysqld) and as of MySQL 5.7.2 is no longer
    visible within the embedded server.

     

  9. query_cache_size= 8M:分配給查詢緩存的內存大小。要配合query_cache_type使用,默認是不開啟的。只從該參數的表面介紹來看,似乎值設置的越大,帶來的效果會更好,但是請注意,查詢緩存的工作原理,一個select語句過來后,數據庫將查詢結果緩存到cache中,等同樣的select查詢過來后,如果這段時間內該查詢結果沒有發生變化時,數據庫將cache中將緩存結果返回,那么假如查詢的相關數據表增刪改特別多的話,數據表變更的這段時間內,要將cache失效,然后再更新數據,對於增刪改來說,花費的時間就很多了,所以要有所權衡,這個參數我會在將來進行相關測試數據整理。
    By default, the query cache is
    disabled. This is achieved using a default value of 1M, with a default for query_cache_type of
    0. (To reduce overhead significantly if you set the size to 0, you should also start the server with
    query_cache_type=0.
    The permissible values are multiples of 1024; other values are rounded down to the nearest multiple.
    Note that query_cache_size bytes of memory are allocated even if query_cache_type is set to 0.
    See Section 8.9.3.3, “Query Cache Configuration”, for more information.
    The query cache needs a minimum size of about 40KB to allocate its structures. (The exact size
    depends on system architecture.) If you set the value of query_cache_size too small, a warning will
    occur, as described in Section 8.9.3.3, “Query Cache Configuration”.

     

  10. query_cache_type = 1:1表示緩存所有查詢,2表示緩存select sql_cache的查詢,看如下內容
    0 or OFF Do not cache results in or retrieve results from the query cache. Note that
    this does not deallocate the query cache buffer. To do that, you should set
    query_cache_size to 0.
    1 or ON Cache all cacheable query results except for those that begin with SELECT
    SQL_NO_CACHE.
    2 or DEMAND Cache results only for cacheable queries that begin with SELECT SQL_CACHE.

     

  11. set global max_connections = 500:注意這個是通過命令行設置最大連接數,不是配置在配置文件的,因為我在配置文件里面嘗試修改,重啟mysql服務后並沒有起效,通過該參數設置以后,重啟服務后,依然沒有起效,如果有朋友知道這個原因的話,請告知。如果說你的項目使用的是spring的連接池的時候,我認為spring個connection就對應的這個連接。根據你項目的需求而定。

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

然后對以上參數進行一下簡單的介紹:

 

 

 

1max_connections這個參數,這個參數指MySql的最大連接數,如果服務器的並發連接請求量比較大,建議調高此值,以增加並行連接數量,服務器根據自己的實際情況進行增加,如果連接數越多,因MySql會為每個連接提供連接緩沖區,就會開銷越多的內存,所以要適當調整該值,不能盲目提高設值。可以過mysql -e "SHOW VARIABLES LIKE 'max_connections';"查看當前狀態的連接數量來設定該值大小。當你常看到Too many connections 錯誤,可以增加此值了,默認是100。 

 

 

 

2back_log這個參數主要是基於max_connections進行的一個額外連接,也就是說當mysql連接大於max_connections設置的值的話,而又在(max_connections+back_log)之間,則mysql會把新連接放到堆棧中,等待之前連接的process釋放,如果當前最大請求超過了(max_connections+back_log),就不會授權連接,當然該值也受約於系統的TCP/IP連接的偵聽隊列(系統的tcp_max_syn_backlog值的大小),可以通過cat /proc/sys/net/ipv4/tcp_max_syn_backlog查看,當然可以修改該值 

 

3sysctl -w net.ipv4.tcp_max_syn_backlog=N或在/etc/sysctl.conf中添加tcp_max_syn_backlog=N即可。 

 

 

由於mysql被稱為內存式數據庫,當然很內存是密不可分了,而他和內存的關系主要是通過緩沖區大小的幾個參數吧。

 

緩沖包括:全局緩沖和局部緩沖。

 

 

 

全局緩沖參數大致有如下:

 

key_buffer_size, innodb_buffer_pool_size, innodb_additional_mem_pool_size,innodb_log_buffer_size, query_cache_size 

 

而局部緩沖(我自己習慣這么叫,雖然不專業,呵呵)一般mysql還會為每個連接分配連接緩沖。

 

全局緩沖在上篇博客中有介紹,這地方就省略了。

 

 

 

局部緩存:

 

    每個連接到MySQL服務器的線程都需要有自己的緩沖。大概需要立刻分配256K,甚至在線程空閑時,它們使用默認的線程堆棧,網絡緩存等。事務開始之后,則需要增加更多的空間。運行較小的查詢可能僅給指定的線程增加少量的內存消耗,然而如果對數據表做復雜的操作例如掃描、排序或者需要臨時表,則需分配大約read_buffer_size,sort_buffer_size,read_rnd_buffer_size,tmp_table_size大小的內存空間。不過它們只是在需要的時候才分配,並且在那些操作做完之后就釋放了。有的是立刻分配成單獨的組塊。tmp_table_size可能高達MySQL所能分配給這個操作的最大內存空間了。注意,這里需要考慮的不只有一點——可能會分配多個同一種類型的緩存,例如用來處理子查詢。一些特殊的查詢的內存使用量可能更大——如果在MyISAM表上做成批的插入時需要分配bulk_insert_buffer_size 大小的內存;執行 ALTER TABLE, OPTIMIZE TABLE, REPAIR TABLE命令時需要分配myisam_sort_buffer_size大小的內存。 

 

  

 

read_buffer_size:MySql讀入緩沖區大小。對表進行順序掃描的請求將分配一個讀入緩沖區,MySql會為它分配一段內存緩沖區。read_buffer_size變量控制這一緩沖區的大小。如果對表的順序掃描請求非常頻繁,並且你認為頻繁掃描進行得太慢,可以通過增加該變量值以及內存緩沖區大小提高其性能。  

 

 

 

sort_buffer_size:MySql執行排序使用的緩沖大小。如果想要增加ORDER BY的速度,首先看是否可以讓MySQL使用索引而不是額外的排序階段。如果不能,可以嘗試增加sort_buffer_size變量的大小。  

 

 

 

read_rnd_buffer_size:MySql的隨機讀緩沖區大小。當按任意順序讀取行時(例如,按照排序順序),將分配一個隨機讀緩存區。進行排序查詢時,MySql會首先掃描一遍該緩沖,以避免磁盤搜索,提高查詢速度,如果需要排序大量數據,可適當調高該值。但MySql會為每個客戶連接發放該緩沖空間,所以應盡量適當設置該值,以避免內存開銷過大。  

 

 

 

tmp_table_size:MySqlheap(堆積)表緩沖大小。所有聯合在一個DML指令內完成,並且大多數聯合甚至可以不用臨時表即可以完成。大多數臨時表是基於內存的(HEAP)表。具有大的記錄長度的臨時表(所有列的長度的和)或包含BLOB列的表存儲在硬盤上。如果某個內部heap(堆積)表大小超過tmp_table_sizeMySQL可以根據需要自動將內存中的heap表改為基於硬盤的MyISAM表。還可以通過設置tmp_table_size選項來增加臨時表的大小。也就是說,如果調高該值,MySql同時將增加heap表的大小,可達到提高聯接查詢速度的效果。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM