MySQLé¢é£ã®ãã©ã¡ã¼ã¿(主ã«InnoDB)ã«ã¤ãã¦
ãã®ã¨ã³ããªã¯MySQL Casual Advent Calendar 2015ã®10æ¥ç®ã®ã¨ã³ããªã§ãã
å
æ¥ã®MySQL Casual Talks Vol8ã§@karupaneruraããããã©ã¡ã¼ã¿ã®æ¯ãè¿ãã®ãããªçºè¡¨ãããã¦ããã®ã§ãæ¨ä»ãã¾ãæ¸ãããªããªã£ãMySQLã«çµ¡ãè¨å®ãã©ã¡ã¼ã¿ã«ã¤ãã¦æ¸ãã¾ãããããªãã®ã¡ã¢ãª(32GBã¨ã)ãSSDã¨ã使ã£ã¦ãäºãåæã«ãããããªå
容ã¨ãªãã¾ãã
ä¾åãã¦å¤æ´ããæ¹ãè¯ããã©ã¡ã¼ã¿ãããã®ã§å
容ãåå¾ã«é£ã³ã¾ããã容赦ä¸ãããã¾ãã½ã¼ã¹ã³ã¼ãããã£ã¤ãèªãã ããã§ã¯ãªãããã³ããã¼ã¯ä¸ã®æåããæ¨æ¸¬ãã¦ããç®æãå¤åã«ããã¾ããMyISAMã®ãã¼ãã«ããµã¼ãã¹ç¨ãã¼ã¿ãã¼ã¹ã«åå±
ããäºãèæ
®ãã¦ãã¾ããã
çµæ§çªè²«ã§æ¸ãã¦ããã®ã§å¾ããå¾®å¦ã«ä¿®æ£ããå¯è½æ§ãããã¾ãã
InnoDBã®ãã©ã¡ã¼ã¿
- innodb_buffer_pool_size
- æããã¾ã§ãç¡ãæè¿ã®ãã¼ã¸ã§ã³ã®MySQLã使ããªãå¿ ãè¨å®ãã¹ããã©ã¡ã¼ã¿ããã¼ã¿ã¨INDEXãæ ¼ç´ããããããDBå°ç¨ãµã¼ãã¨ãã¦ä½¿ãå ´åã¯70%ç¨åº¦å²ãå½ã¦ãäºãæ¨å¥¨ããã¾ããå¾è¿°ãã innodb_log_file_size ããã¤ããªãã°ã¯ãã¡ã¤ã«ã·ã¹ãã ãã£ãã·ã¥ã«ããç¨åº¦è¼ã£ã¦ããæ¹ãè¯ãã®ã§ã®ãªã®ãªã¾ã§æ»ããå¿ è¦ã¯ãªããã¨æãã¾ãã
- innodb_page_sizeãããã©ã«ã(16k)ããå°ããããå ´åãä¾ãã°4kã«ããå ´åã¯16kã®æã¨æ¯è¼ãå¤ãã®ã¡ã¢ãªãæ¶è²»ãããããinnodb_page_sizeã16kã§éç¨ãã¦ãããµã¼ãã4kã«å¤æ´ããå ´åã¯è¨å®å¤ã«ããã¾ããGBåä½ã§ä¸ããªãã¨ã¹ã¯ãã使ãå§ãããã注æãå¿ è¦ã§ãã
- innodb_log_file_size
- redoãã°ãæ¸ããã¡ã¤ã«ã®ãµã¤ãºãæè¬ib_logfileã®ãã¡ã¤ã«ãµã¤ãºãæå®ãã¾ããredoãã°ã®åè¨ãµã¤ãºã¯innodb_log_file_size x innodb_log_files_in_group ã¨ãªãããæ°GBã¨ããå ´åã¯innodb_log_file_sizeã512MBã¨ã1GBã«è¨å®ããinnodb_log_files_in_groupã調æ´ããã®ãè¯ãã¨æãã¾ãã1ãã¡ã¤ã«ã大ããããããã¨ãã¡ã¤ã«ã·ã¹ãã ãã£ãã·ã¥ãã溢ãããããªãããã§ããæ´æ°ãå¤ãç°å¢ããã¤5.6以ä¸ãå©ç¨ãã¦ããå ´åã¯åè¨4GB以ä¸ã«è¨å®ããã®ãæãã§ãã5.5ã§ã¯åè¨ã®æ大å¤ã4GBã®ãã注æãã¦ä¸ããã
- ããã¨å°ãããµã¤ãºã«ããäºãããã¾ãããé©åã«ãã®ä»ã®ãã©ã¡ã¼ã¿ããã¡ã¤ã«ã·ã¹ãã ãé¸æåºæ¥ãã®ã§ããã°æ´æ°ããã¼ãªç°å¢ã§ã¯å¤§ããæ¹ãã»ã¨ãã©ã®å ´åãæ§è½åä¸ã»å®å®ããå¾åãããã¾ãã
- ä¸è¬ã«ib_logfileã®åè¨ãµã¤ãºã大ãããªãã¨crash recoveryã®æéãé·ããªãã¾ãã
- innodb_buffer_pool_instances
- innodb_buffer_pool_sizeãããã¤ã®ã¤ã³ã¹ã¿ã³ã¹ã«åãããæå®ãã¾ããããã©ã«ãã¯8ã§ãããªãinnodb_buffer_pool_sizeã1G以ä¸(æªæºï¼)ã®å ´åã¯1ãè¨å®ããã¾ã)40GBã¨ã大ããå¤ãinnodb_buffer_pool_sizeã«è¨å®ãã¦ããå ´åã¯20ã¨ãè¨å®ãããã¨ã§åæå®è¡æ§è½ãåä¸ãã¾ãã
- innodb_flush_method
- ãã¡ããè¨ãããããªããªãã©ã¡ã¼ã¿ãSANã¹ãã¬ã¼ã¸ãä»®æ³ç°å¢ã®å ´åã¯ããã©ã«ãã使ãäºãå¤ãããã§ãããBBUä»ãRAIDã使ã£ã¦ãããSSDã®å ´åã¯O_DIRECTãæ¨å¥¨ãã¾ãããã¡ã¤ã«ã·ã¹ãã ãXFSã®å ´åã¯ä¸¦åæ¸è¾¼ã¿ãå¯è½(ãã¶ãåä¸ãã¡ã¤ã«ã¸ã®)ã¨ãªãããã§ãã®ã§ext3ãext4ãããæ§è½ãä¸ããã±ã¼ã¹ãå¤ãã§ãã
- innodb_purge_threads
- ãã¾ãæèãã¦ãã人ã¯ããªãæ°ããããã©ã¡ã¼ã¿ã5.6ã§ã¯ããã©ã«ã1ã5.7ã§ã¯ããã©ã«ã4ã¨ãªã£ã¦ãã¾ããæ´æ°ãå¤ããshow engine innodb status\Gãè¦ãå ´åã« History list length [æ°å] ã®æ°åãè¥å¤§åãã¦ãã£ã¦ããå ´åã¯å¢ããäºãæ¨å¥¨ããã¾ããä½ãMySQLã®åèµ·åãå¿ è¦ã§ããHistory list lengthãå¢ãã¦ããã¨ãããã¨ã¯undoé å(ããã©ã«ãã§ã¯ibdata1ã¨åå± )ãè¥å¤§åããè¦å ã¨ãªãããæ°ãé ã£ãã»ããè¯ãã§ããã¾ãHistory list lengthã大ãããªã£ã¦ããã¨åºæ¬çã«ã¯æ§è½ãå£åããå¾åãããã¾ãã
- ä½ãæéã®è¶ 絶ãããSELECTã¨ããå®è¡ããã¦ããå ´åãHistory list lengthãå¢ããäºãããã¾ãããã®å ´åã¯å¢ããã¦ãããããå¹æã¯ãªãã¨èãããã¾ã(ã ã£ã¦purgeåºæ¥ãªãã)
- ãªããéå»ã®è¨æ¶ã§ãããç¡é§ã«å¢ãããããã¨ã³ã³ããã¹ãã¹ã¤ãããå¢å ãã¦å°ãªãè¨å®ã®æãããæ§è½ãä¸ããã®ã§ç¨ã»ã©ã«è¨å®ãã¦ä¸ããã5.7ãªãæ®ã©ã®ã±ã¼ã¹ã§ããã©ã«ãã§ååã ã¨æãã¾ãã
- innodb_io_capacity, innodb_io_capacity_max(maxå´ã¯5.6ãã追å )
- I/O ã¢ã¯ãã£ããã£ã¼ (ãããã¡ã¼ãã¼ã«ããã®ãã¼ã¸ã®ãã©ãã·ã¥ãæ¿å ¥ãããã¡ã¼ããã®ãã¼ã¿ã®ãã¼ã¸ãªã©) ã«ä¸éãè¨å®ãã¾ã(ããã©ãã®ã§ããã¥ã¢ã«ãã)ã
- å¢ãããæ¹ãè¯ãã±ã¼ã¹ã¨ãã¦ç¥ã£ã¦ãããã¿ã¼ã³ãè¨è¼ãã¾ããshow engine innodb status\Gãå®è¡ããINDIVIDUAL BUFFER POOL INFO以ä¸ã®åãããã¡ãã¼ã«ã¤ã³ã¹ã¿ã³ã¹ã®Free buffersã0ã§å¼µãä»ãã¦ããå ´åã¯å¢ããäºãæ¨å¥¨ããã¾ããå°ãªãã¨ãããã¡ããã®ãã©ãã·ã¥å¾ ã¡ãçºçãæ§è½å£åãã¾ãã
- ããã¯ioãªã¯ã¨ã¹ãæ°ã§ã¯ãªããinnodb_page_sizeã1ã¨ãã¦ããããã«è¦åãããã¾ããã§ãã®ã§innodb_page_sizeã16kã¨4kã§ã¯ç°ãªãè¨å®ã«ããäºãæ¨å¥¨ããã¾ãã1000è¨å®ãã¦ãã¦innodb_page_sizeã16kãªã 16k x 1000ã ãæ¸ããã¨ããã¯ãã§ãã
- ãã®ãã©ã¡ã¼ã¿ã®æåã¯5.5ã5.6ã5.7ã§å ¨é¨å¾®å¦ã«éãã¯ããªã®ã§ããããã®ç°å¢ã§èª¿æ´ããå¿ è¦ãããã¾ãã5.7ã§æ¹åããå¿ è¦ãªç¶æ³ã§ã¯ç¶ºéºã«page/secã§æ¸è¾¼ã¿ãè¡ãããã«è¦ããã¾ããã5.7ããåã¯è¨å®å¤ä»¥ä¸ã§æ¸è¾¼ã¿ãè¡ãã±ã¼ã¹ãè¦åãããã¾ããã
- ãªããå¢ããéãã¦ãæ§è½ãä¸ããã®ã§æ§åãè¦ãªããè¨å®å¤æ´ããã®ãè¯ãã§ãã
- innodb_lru_scan_depth
- ãã£ããæ¸ãã¨InnoDBãããã¡ãã¼ã«ã使ãåããããã®ãã¼ã¿ã»INDEXãåå¨ããç°å¢ã«ããã¦åãããã¡ãã¼ã«ã¤ã³ã¹ã¿ã³ã¹ã®Free buffersãã©ãã¾ã§ç©ºãã¦ããããæå®ãã¾ããããã©ã«ã1024ã§ãããã£ã¦innodb_buffer_pool_instancesãããã©ã«ãã®8ã®å ´åã¯1024 x 8(page)ã ãMySQLã¯ç©ºãã確ä¿ãããã¨ãã¾ãã
- innodb_lru_scan_depthã®è¨å®å¤ãä¸åãã¨MySQLã¯ç©ºãã確ä¿ããããã«ç©æ¥µçã«ãã©ãã·ã¥ãããã¨ãããããªã®ã§ãinnodb_io_capacityãå°ãªãã¨å¾ ã¡æéãé·ããªãæ§è½å£åããããã§ããã
- ä¸è¬ã«innodb_io_capacityãå¢ããããinnodb_lru_scan_depthãå¢ãããã¨è¨ããã¦ããããã§ããä½ããå ã«ãè¨è¼ããã¨ãããããã¡ãã¼ã«ã¤ã³ã¹ã¿ã³ã¹æ¯ã«ãªãããinnodb_buffer_pool_instancesã®è¨å®å¤ãæèãã¦èª¿æ´ããå¿ è¦ãããã¾ãã
- innodb_checksum_algorithm
- æè¿ã®CPUãªãCRC32ã®è¨ç®ãéãè¡ã£ã¦ãããã®ã§crc32ãè¨å®ãããã¨ãæ¨å¥¨ãã¾ããæ¸è¾¼ã¿æ§è½åä¸ãã¾ãããä½æã§ããã»ã©ã®ã¯ã¼ã¯ãã¼ãã¯æ» å¤ã«ãªãã¨æãã¾ãããæ°æã¡çã«crc32ã«è¨å®ããã®ãè¯ãã¨æãã¾ãã
- strict_crc32ã¯ä½¿ã£ããã¨ãç¡ãã§ãããããã¥ã¢ã«ãèªãéããµã¼ãã¹éå§æãããã®ãã©ã¡ã¼ã¿ãè¨å®åºæ¥ãå ´åã¯strict_crc32ã使ãã®ãè¯ãããã§ãã
- innodb_change_buffer_max_size
- innodb_change_bufferingãæå¹ã§ããã°ä½¿ããã¾ããããã©ã«ã25ã§ãã木下さんのスライドã«ããã¨SSDã®ããã«ioæ§è½é«ãå ´åã¯å°ãªããããã¨ãæ¨å¥¨ã®ããã§ããã»ã«ã³ããªã¤ã³ããã¯ã¹ã®æ´æ°ãå¤ããªããªãä¸ãã¦è¯ãããªã¼ãã¨ããæãã§ããç§ã¯ãã³ããã¼ã¯åãæã¯å¤å°ä¸ãã¦10ã«ãã¦ãã¾ãã
- innodb_numa_interleave
- 5.6.27, 5.7.9ããå©ç¨å¯è½ã«ãªã£ããã©ã¡ã¼ã¿ãNUMAç°å¢ã«ããã¦InnoDBãããã¡ãã¼ã«ãnumactl --interleave=allã®ããã«ãã¦æ±ãããã«ãªãã¾ããSwap Insanity対çã«ãæå¹ã«ãã¦ããã¨è¯ãã¨æãã¾ãã
- innodb_flush_neighbors, innodb_read_ahead_threshold
- innodb_read_io_threads, innodb_write_io_threads
- å¤æ´ãã¦å³å¯ã«æ¸¬å®ããäºã¯ç¡ãã®ã§ãããSSDã使ãå ´åã¯12ã¨ãè¨å®ããæ¹ãéããªãäºãå¤ãããã§ãã
- innodb_adaptive_hash_index
- KVSã®ãããªããã·ã¥ã¤ã³ããã¯ã¹ããããã¡å ã«æã¤ãã®æå®ãããã©ã«ãONã§æå¹ã§ãããã¼ã¿ã»INDEXãå ¨ã¦InnoDBãããã¡ãã¼ã«ã«åã¾ãç¯å²ã§ã¯æå¹ãªæ¹ãéããªãã±ã¼ã¹ãããã¾ãããåã¾ããªãå ´åã¯å½ç¶ãããããªããã¨ãããã®ã§ãªã¼ãã¼ããããçºçãæ§è½ãå£åããäºãããã¾ããä¸å¿5.7.8ããinnodb_adaptive_hash_index_parts(ããã©ã«ã8)ã¨ãããã©ã¡ã¼ã¿ãå¢ãã¦ãã¾ããããããã¡ã«åã¾ããªãç°å¢ã§ã¯ãã¯ãOFFã®æ¹ãæ§è½ãåºãå¾åã«ããã¾ãã
- æ©ãã±ã¼ã¹ã§ãåçã«ãã¨ããç¨ã§ã¯ãªãã®ã§OFFã§è¯ãããããªããã¨æè¿ã¯æã£ã¦ãã¾ãã
- å é¨ã®mutex競åã®åé¡ãããããã§ããä¸ã®äººãåé¡èªèã¯ããã¦ããããã§ãã®ã§5.8ã§ã¯æ¹åãããããããã¾ããã
ãã®ä»ã®ãã©ã¡ã¼ã¿
- thread_cache_size
- é½åº¦ã¢ããªã±ã¼ã·ã§ã³ããMySQLã¸æ¥ç¶/åæãè¡ããããªç°å¢ã§ã¯128ã¨ããã£ã¨å¤§ããå¤ãè¨å®ãã¦ãããæ¹ãæ¥ç¶ãéããªãã¾ããMySQLã¯æ¥ç¶ãã¨ã«ã¹ã¬ãããçæãã¾ãããããããã£ãã·ã¥ãã¦ãããã¨ã«ããã¹ã¬ããçæã®å¾ ã¡åãç縮ãããããã¨ãªãã¾ãã
- show global statusã®Threads_createdãå¢ãç¶ããå¾åã«ããå ´åã«ã¯é©å½ã«å¢ããã¦æ§åãè¦ãã®ãè¯ãã§ãããã®ãã©ã¡ã¼ã¿ã¯set globalã§åçå¤æ´å¯è½ã§ãã
- back_log
- å¢ããã¦ããã¨ä½ããã®ã¿ã¤ãã³ã°ã§ã¢ã¯ã»ã¹ãæ¥å¢ããMySQLã¸ã®æ¥ç¶ãä¸æ°ã«çºçããå ´åã®åããã¼ããæ¸ãã¾ããMySQLã®ã¹ã¬ããçæã追ãä»ããªããã¾ãã¯TCPæ¥ç¶ã®æ®µéã§ã¨ã©ã¼ã¨ãªã£ã¦ã¶ã£ãåãããå ´åã¯Lost Connectionãã¨ã©ã¼ã¨ãã¦ã¢ããªã±ã¼ã·ã§ã³ã®ãã°ã«è¨é²ãããäºãããã¾ãã
- ãã®å¤ãå¢ããå ´åã¯ä»¥éã«è¨è¼ããnet.core.somaxconnãnet.core.netdev_max_backlogãåããã¦å¢ããäºãæ¨å¥¨ããã¾ãã
OSå´è²ã
- vm.swappiness
- CentOS6ç³»(ããã6.3ãããã®Kernelãã)ãªãvm.swappiness=1(sysctl.confã«æ¸ãã¦OSåèµ·åãã¦ãæ°¸ç¶å)ã«ãã¦ããäºã§ã¹ã¯ããã使ãå¯è½æ§ãæå¶åºæ¥ã¾ããCentOS5ç³»ãªã0ã§ãããã ãã®è¨å®ãããã®ã¯æããã¨ãã人ãããããã§ããèªåã®çµé¨ã§ã¯innodb_buffer_pool_sizeã¨ããé©åã«è¨å®ãã¦ããã°åé¡ã«ãªã£ãäºã¯ããã¾ãããããã¡ãã絶対åé¡ç¡ãã¨ããä¿éã¯ãã¾ããã
- net.core.somaxconnãnet.core.netdev_max_backlogãå¤æ°ã®æ¥ç¶/åæãè¡ãããç°å¢ã§ã¯å¢ããäºãæ¨å¥¨ããã¾ãã
- ãã¡ã¤ã«ã·ã¹ãã
- æ´æ°ãå¤ãç°å¢ã§ããã°xfsã®æ¹ãéãå¾åã«ããã¾ããext4ã ã¨åä¸ãã¡ã¤ã«ã¸ã®è¤æ°ããã»ã¹(ã¹ã¬ãã)ããã®æ¸è¾¼ã¿ãè¡ããªãããã ã¨èãããã¾ãã
- ext4, xfsã§ã¯IOããªã¢ãããã©ã«ãæå¹ã®ãããæ§è½ãæ±ããã®ã§ããã°ãã¦ã³ããªãã·ã§ã³ã«barrier=0(ã¾ãã¯nobarrier)ã¨æ¸ãã¦ç¡å¹ã«ããäºãããã¾ããåãéãSSDã ã¨æå¹ã§ãç¡å¹ã§ããMySQLãã使ãåã«ã¯å·®ãå ¨ç¶åºãªãäºãããã¾ããã
ã¾ã¨ã
è²ã
ã¨æ¸ãã¦ã¿ã¾ãããã©ãã§ãããããä¸æ°ã«æ¸ãä¸ããã®ã§å¤ãªè¨è¼ãããããããã¾ãããåèã«ãªãã°å¹¸ãã§ãã
å人çã«ã¯ãã®ä»ãã©ã¡ã¼ã¿ã ã¨innodb_adaptive_flushing_lwmãåãããå ´åã®æåãããã¡ãã£ã¨ã¿ããã¨æã£ã¦ãã¾ãã
ä»ã«ããã®è¾ºè¨å®ããããã¨ãããã°MySQL Casualã®Slackãªããã§ãé£çµ¡ããã ããã¨åã³ã¾ãã