MySQL ã¨ã¡ã¢ãªã«é¢ããã¾ã¨ã
ååã®ã¨ã³ããªã¼データベースを用いたセッションデータ管理についてã§ãMySQL ã¨ã¡ã¢ãªã®é¢ä¿ã«ã¤ãã¦è¯ãåãããªãé¨åãããã¨æ¸ãã¾ããã
å®ã¯ããã«é¢ããç解ã¯ããªãææ§ãªé¨åããã£ã¦ã調æ»ãã¦è¿½è¨ãã¾ããã¨ãã«ã¡ã¢ãªå©ç¨éã«ã¤ãã¦ãmysqld ã®ããã»ã¹ãå©ç¨ã§ããã¡ã¢ãªã®ä¸éãã32bit OS ã®å ´åã¯3G ç¨åº¦ã¨ãããã¨ã¯ãinnodb_buffer_pool_size ããã®å¶éãåããããã«ã¤ãã¦ã®è¦åããå ã«ç´¹ä»ãããªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã®ãã®ã¨ããç解ã ãã©ããã®ã ãããã¨ããã®ã1ã¤ã
2 ã¤ç®ã¯ããã®ç解ããã£ã¦ããã¨ããã¨ã4G 以ä¸ã®ã¯ã©ã¹ã®ã¡ã¢ãªãã¤ãã ãµã¼ããDB ãµã¼ãã¨ãã¦å©ç¨ããå ´åã64 bit OS ã§ãªãã¨ãªã½ã¼ã¹ã®æå¹æ´»ç¨ãã§ããªãããããã¨ããå ã«æ¸ããã¨ãããOS ã¬ãã«ã®ãã£ãã·ã¥ã¨ãã¦å©ç¨ã§ãããããçµæã¨ãã¦ãã¼ã¿ãã¡ã¤ã«ãèªã¿è¾¼ãéã®I/O è² è·è»½æ¸ã«ãªãã
ãããå«ãMySQL ã¨ã¡ã¢ãªã®é¢ä¿ã«ã¤ãã¦èª¿æ»ãã¾ãããæåã«ã¡ã¢ãªã®åºæ¬çãªé¨åãè¿°ã¹ã¦ã次ã«MySQL ã¨é¢ä¿ããé¨åãè¿°ã¹ã¾ããä¸ã®2ã¤ã®çåã«ã¤ãã¦ã®èªåãªãã®è§£ã¯MySQL ã®é¨åã§ä¸ç·ã«æ¸ãããã¨æãã¾ãã
OS ã¯Linux ãåæã¨ãã¾ãã
OS ã¨ã¡ã¢ãª
MySQL äºã
ã®åã«ãã¾ãOS ã¨ã¡ã¢ãªã«ã¤ãã¦ã§ãã
32bit OS ãèªèã§ããã¡ã¢ãªå®¹é
ä¸è¬çãªWindows ã¯ã©ã¤ã¢ã³ããæ±ããã¡ã¢ãªã¯3GB ç¨åº¦ã¨ããã®è¾ºãã®è©±ã§ãã
- ç©çã¢ãã¬ã¹ç©ºé
ã¾ãå§ãã«ãOS ãã¡ã¢ãªãèªèããã«ã¯ãç©çã¢ãã¬ã¹ç©ºéããå¿
è¦ã«ãªãã¾ãã32bit OS ã®å ´åã4G(= 2^32)Bã®ç©çã¢ãã¬ã¹ç©ºéãæã£ã¦ãã¾ãããã£ã¦ã32bit OS ãèªèã§ããã¡ã¢ãªå®¹éã¯æ大4GB ã¨ãããã¨ã«ãªãã¾ãã
- MMIO(Memory Mapped IO)
å ã«æ¸ããéãã32bit OS ã¯4GB ã®ç©çã¢ãã¬ã¹ç©ºéãæã¡ã¾ãããå®éã«èªèã§ããã¡ã¢ãªå®¹éã¯4GB ããå°ãããªãã¾ãããã®çç±ã¯ãMemory Mapped IO ã¨ããä»çµã¿ã«ãããã®ã§ãã
MMIOã¯æéã§ããã¢ãã¬ã¹ç©ºéã«ãã¡ã¢ãªã¨æ§ã ãªããã¤ã¹å¶å¾¡ç¨ã®ç©ºéãå ±æãããä»çµã¿ã§ããæ¥ç¶ãã¦ããããã¤ã¹ã®å¶å¾¡(使ç¨)ãããã«ã¯ãã¢ãã¬ã¹ç©ºéãå¿ è¦ã«ãªãã¨ãããã¨ã§ãã
PCã¯ã¾ãããã¤ã¹å¶å¾¡ç¨ã«å¿ è¦ãªç©ºéãã¢ãã¬ã¹ç©ºéã«äºç´ããæ®ããã¡ã¤ã³ã¡ã¢ãªã«å²ãå½ã¦ã¾ããé åã被ã£ã¦ä½¿ç¨ãããã¨ã¯ã§ããªãããã§ããããã¤ã¹å¶å¾¡ç¨ã«å²ãå½ã¦ãããé åãå¤ããã°ããã®å使ç¨ã§ããã¡ã¤ã³ã¡ã¢ãªã¯å°ãªããªãã¾ãã
ã¢ãã¬ã¹ç©ºé(4GB)âããã¤ã¹å¶å¾¡ç¨äºç´å(1GBåå¾)ï¼ä½¿ç¨ã§ããã¡ã¢ãªé(3GBåå¾)
ããããWindows ã¯ã©ã¤ã¢ã³ããèªèã§ããã¡ã¢ãªã®éçã3GB ç¨åº¦ãªçç±ã§ããx86 ã¢ã¼ããã¯ãã£ã®PC ãªãLinux ã§ãåæ§ã®ä»çµã¿ã使ã£ã¦ãã¦ãåæ§ã®å¶éãåãã¾ãã
- PAE(ç©çã¢ãã¬ã¹æ¡å¼µ)
ããã¾ã§ã§ã32bit OS ã§ã¯3GB ç¨åº¦ã®ã¡ã¢ãªããèªèã§ããªããã¨ãåããã¾ãããã§ãããLinux ã§ã¯32bit OS ã使ã£ã¦ããå ´åã§ã3GB ãè¶
ããã¡ã¢ãªãèªèã§ããå ´åãããã¾ãããã®æ©è½ãPAE ã¨è¨ããç©çã¢ãã¬ã¹ç©ºéã64GB ã¾ã§æ¡å¼µãã¦ããã¾ãã
PAE ãæå¹ã«ãªã£ã¦ãããã¯ããuname ã³ãã³ããã§èª¿ã¹ããã¨ãã§ããæè¿ã¯æåããæå¹ã«ãªã£ã¦ãããã¨ãå¤ãã¿ããã§ããåã®å¨ãã®ãã·ã³ã§ã¯å
¨ã¦æå¹ã«ãªã£ã¦ãã¾ããã
# ã«ã¼ãã«ãã¼ã¸ã§ã³ã®æå¾ã«ãPAEããã¤ãã¦ããã°æå¹ã«ãªã£ã¦ãã $ uname -r 2.6.18-92.el5PAE
ããã§ã®ãã¤ã³ãã¯ãæè¿ã®Linux ã使ã£ã¦ããã°32bit OS ã§ã16GB çã®å¤§å®¹éã¡ã¢ãªãæ±ããã¨ãã§ããã¨ãããã¨ã§ãã
MySQL ã¨ã¡ã¢ãª
ç¶ãã¦ä»åã®æ¬é¡ã®MySQL ã¨ã¡ã¢ãªã®è©±ã§ãã
Linux ã32bit OS ã§ã大容éã¡ã¢ãªãæ±ããããªããã¡ã¢ãªã«é¢ãã¦64bit OS ã使ãã¡ãªããããã¾ããªãããã«æãã¾ãã8GB ãããã®ã¡ã¢ãªãç©ãã§ãinnodb_buffer_pool_size=4G ã¨ãã«ããã°ããããããªãã§ããããã
å®ã¯ããã¯ç¡çã§ããOS ãæ±ããã¡ã¢ãªå®¹éã¨ãmysqld ã®ãããªåã
ã®ããã»ã¹ãæ±ããã¡ã¢ãªå®¹éã¯åãã§ã¯ãªãã®ã§ãã
- ä»®æ³ã¡ã¢ãªã¨ä»®æ³ã¢ãã¬ã¹ç©ºé
ä»®æ³ã¡ã¢ãªã¨ã¯ãåããã»ã¹å°ç¨ã«ä¸ããããã¡ã¢ãªã®ãããªãã®ã§ãã¡ã¢ãªã¨ãããªã½ã¼ã¹ãå¹çè¯ãå©ç¨ããããã®ä»çµã¿ã§ãã1ã¤ã®ããã»ã¹ãå©ç¨ã§ããã¡ã¢ãªã®æ大容éã¯ããã®ä»®æ³ã¡ã¢ãªã®æ大容éã¨åãã§ãããã®å®¹éã¯ãä»®æ³ã¢ãã¬ã¹ç©ºéãã®ãµã¤ãºã§ã32bit çLinux ãªã4GB ã¨ãªãã¾ããå
ã«è¿°ã¹ãPAE ã使ãã¨ç©çã¢ãã¬ã¹ç©ºéã®å®¹éã¯å¤§ãããªãã®ã§ããããã®ä»®æ³ã¢ãã¬ã¹ç©ºéã®ãµã¤ãºã¯å¤§ãããªãã¾ããã
- ã«ã¼ãã«ç©ºéã¨ã¦ã¼ã¶ç©ºé
å ã®ãä»®æ³ã¢ãã¬ã¹ç©ºéãã¯ããã«ããã«ã¼ãã«ç©ºéãã¨ãã¦ã¼ã¶ç©ºéãã«åããã¦ãã¾ãã容éã¯ãããã1GBã3GB ã¨ãªãã¾ããmysqld ã®ãããªã¦ã¼ã¶ããã»ã¹ã¯ã¦ã¼ã¶ç©ºéã®ã¿ãã使ç¨ãããã¨ãã§ãã¾ããã
ãããã«ããã32bit OS ã®å ´åã1ããã»ã¹ã§ä½¿ç¨ã§ããã¡ã¢ãªã®æ大容éã¯3GB ã¨ãããã¨ãåããã¾ãããããã§ã1ã¤ç®ã®çåã«ã¤ãã¦èãã¦ã¿ããã¨æãã¾ãã
- mysqld ãæ¶è²»ããã¡ã¢ãªå®¹éã¨glibc ã®å¶ç´
mysqld ã®ããã»ã¹ãå©ç¨ã§ããã¡ã¢ãªã®ä¸éãã32bit OS ã®å ´åã¯3G ç¨åº¦ã¨ãããã¨ã¯ãinnodb_buffer_pool_size ããã®å¶éãåããããã«ã¤ãã¦ã®è¦åããå ã«ç´¹ä»ãããªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã®ãã®ã¨ããç解ã ãã©ããã®ã ãããã¨ããã®ã1ã¤ã
ååã¯yes ã§ããmysqld ã¯1ããã»ã¹/ãã«ãã¹ã¬ãããªã®ã§ãinnodb_buffer_pool, key_buffer ã®ãããªã°ãã¼ãã«ãããã¡ãã¹ã¬ããæ¯ã«ç¢ºä¿ãããsort_buffer, read_buffer ã®ãããªã¹ã¬ãããããã¡å
¨ã¦ã®åè¨ã3GB ã«åã¾ã£ã¦ããªãã¨ããã¾ããã
å½ç¶ãinnodb_buffer_pool_size ãããªãã©ã¡ã¼ã¿ã¯ãã®å¶éãæ°ã«ãã¦è¨å®ãã¦ãããªããã°ãªãããã¾ãinnodb_buffer_pool_size=3G ããã«ãã¦ãã¾ãã¨ãæ®ãã®ãããã¡ã«ãã¦ãã¡ã¢ãªãã¾ãã£ãããªããªã£ã¦ãã¾ãã¾ãã
mysqld ãæ¶è²»ããã¡ã¢ãªå®¹éã®ç®åºå¼ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
innodb_buffer_pool_size + key_buffer_size + max_connections * (sort_buffer_size + read_buffer_size) + max_connections * 2 MB
ããã3GB 以å
ã«ãªãã°ããã®ãã¨ããã¨ããã§ã¯ããã¾ããã(ãªããªãã !!!)
ãããçåã®å¾åã§è¨ã£ã¦ããããããã«ã¤ãã¦ã®è¦åããå
ã«ç´¹ä»ãããªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã®ãã®ã¨ããç解ã ãã©ããã®ã ããããã¨ããé¨åã§ããå
ç¨ã®ãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã¨åæ§ã®ãã¼ã¸ã«ä»¥ä¸ã®ããã«ããã¾ãã
è¦å: Linux x86 ã§ã¯ãã¡ã¢ãªä½¿ç¨çã®è¨å®ãé«ããéããªãããã«æ³¨æãã¦ãã ãããglibc ã¯ããã»ã¹ãã¼ããã¹ã¬ããã¹ã¿ãã¯ããã大ãããªããã¨ã許å¯ãã¦ããããã®å ´åã«ãµã¼ããã¯ã©ãã·ã¥ãã¾ãã
ãã®å¤(ã¡ã¢ãªå®¹éã®è¨ç®å¼ã®å¤)ãã2 GB ã«è¿ããã2 GB ãè¶ ãã¦ããã¨å±éºã§ããåã¹ã¬ããã¯ã¹ã¿ãã¯ã使ç¨ãï¼é常㯠2 MBããã ã MySQL AB ãã¤ããªã§ã¯ 256 KB ã®ã¿ï¼ãææªã®å ´åãsort_buffer_size + read_buffer_size ã®å¤§ããã®è¿½å ã¡ã¢ãªã使ç¨ãã¾ãã
ãã®å¶éã«ãããçµå±mysqld ã¯2GB ã¾ã§ããã¡ã¢ãªãæ±ããã¨ãã§ãã¾ãããä»®æ³ã¢ãã¬ã¹ç©ºéã®å¶éã¨ãã§ã¯ãªãglibc ã®å®è£
ä¸ã®å¶éã§ãããã¡ãªã¿ã«ããã®å¶éã«ã²ã£ããããªãããã§ãã¯ããã¹ã¯ãªãããDSAS開発者の部屋:5分でできる、MySQLのメモリ関係のチューニング!ã§ç´¹ä»ããã¦ãã¾ãã
ç¶ãã¦2ã¤ãã®çåã«ã¤ãã¦èãã¾ãã
ãã®ç解(mysqld ã使ç¨ã§ããã¡ã¢ãªå®¹éã2GB ã¨ãããã¨)ããã£ã¦ããã¨ããã¨ã4G 以ä¸ã®ã¯ã©ã¹ã®ã¡ã¢ãªãã¤ãã ãµã¼ããDB ãµã¼ãã¨ãã¦å©ç¨ããå ´åã64 bit OS ã§ãªãã¨ãªã½ã¼ã¹ã®æå¹æ´»ç¨ãã§ããªãããããã¨ããå ã«æ¸ããã¨ãããOS ã¬ãã«ã®ãã£ãã·ã¥ã¨ãã¦å©ç¨ã§ãããããçµæã¨ãã¦ãã¼ã¿ãã¡ã¤ã«ãèªã¿è¾¼ãéã®I/O è² è·è»½æ¸ã«ãªãã
- OS ã®ãã£ãã·ã¥æ©æ§(ãã¼ã¸ãã£ãã·ã¥)
å
ã®çåã«ã¤ãã¦èããã«ããã£ã¦ããã¼ã¸ãã£ãã·ã¥ã«ã¤ãã¦ç¥ã£ã¦ããå¿
è¦ãããã¾ãã
ãã¼ã¸ãã£ãã·ã¥ã¯ãã£ã¹ã¯IO è² è·ãæå°éã«æããæ©è½ã§Linux ãæ®éã«ä½¿ã£ã¦ããã°æèãããã¨ãªãå©ç¨ãã¦ãã¾ããç°¡åã«è¨ãã°ãããã£ã¹ã¯ããåãã¦èªãã ãã¼ã¿ãã¡ã¢ãªä¸ã«ãã£ãã·ã¥ãã¦ããã次ã«åããã¼ã¿ã«ã¢ã¯ã»ã¹ããéã«ã¯ã¡ã¢ãªã«ã ãã¢ã¯ã»ã¹ããã°è¯ããã¨ãããã¨ã§ããããã«ãã£ã¦ããã£ã¹ã¯ã¢ã¯ã»ã¹ã®åæ°ãæ¸ããIO ããã©ã¼ãã³ã¹ãåä¸ããããã¨ãã§ãã¾ãã
詳ããã¯ãnaoyaのはてなダイアリー - Linux のページキャッシュ, Linux I/O のお話 write 編 - naoyaのはてなダイアリーããããèªãã§ã¿ã¦ä¸ããã
ãã¼ã¸ãã£ãã·ã¥ã®ãã¤ã³ãã¯ãã«ã¼ãã«ã¯ä½¿ã£ã¦ããªãã¡ã¢ãªãæ大éãã¼ã¸ãã£ãã·ã¥ã«ä½¿ãããã«è¨è¨ããã¦ãããã¨ã¨ããã¼ã¸ãã£ãã·ã¥ã§å©ç¨ã§ããã¡ã¢ãªå®¹éã¯OS ãèªèãã¦ããã¡ã¢ãªå®¹éã§ããã¨ãããã¨ã§ãã
ããã§å ã®çåã«çãããã¾ããä¾ãã°4GB ã®ã¡ã¢ãªãç©ãã ãµã¼ãã§MySQL ã使ã£ã¦ãã¦ãmysqld ã2GB ã®ã¡ã¢ãªã確ä¿ãã¦ããã¨ãã¾ãããããã®å ´åãæ®ãã®2GBå«ãmysqld ãå®éã«ä½¿ã£ã¦ããªãå®ã¡ã¢ãªã¯å ¨ã¦ãã£ãã·ã¥ã¨ãã¦å©ç¨ããã¾ãããªã®ã§ãå®éã«ã¯32bit OS ã使ã£ã¦ãã¦ããã¡ã¢ãªãå¤ãã¤ãã°ãããã ãIO è² è·ã軽æ¸ããããã¨ãã§ãã¾ãã
ããã§çåã¯è§£æ±ºããã®ã§ãããããå°ãMySQL ã¨ã¡ã¢ãªã«ã¤ãã¦æ¸ãã¾ãã
å®ã¯ãã¡ã¢ãªã®ä½¿ãæ¹ã¯ã¹ãã¬ã¼ã¸ã¨ã³ã¸ã³ã¬ãã«ã§ãç°ãªãã¾ãã
MyISAM ã¯MySQL ç¬èªã®ãããã¡(key_buffer)ä¸ã«ãã¼ãã«ã®ã¤ã³ããã¯ã¹æ
å ±ããã£ãã·ã¥ããããã«è¨è¨ããã¦ãã¾ããä¸æ¹ãInnoDB ã¯ãã¤ã³ããã¯ã¹ã¨ãã¼ã¿ã両æ¹ã¨ãç¬èªãããã¡(innodb_buffer_pool)ä¸ã«ãã£ãã·ã¥ããããã«è¨è¨ããã¦ãã¾ãã
漢(オトコ)のコンピュータ道: MySQLを高速化する10の方法çãå§ãMySQL ã®ãã¥ã¼ãã³ã°ã§ããè¨ããããkey_buffer_size ã«ã¯ã¡ã¢ãªã®25%, innodb_buffer_pool_size ã«ã¯ã¡ã¢ãªã®70-80% ããããã¦ãã¨ããã®ã¯ãMyISAM ã®å ´åã¯ãã¼ã¿ãkey_buffer ä¸ã«ãã£ãã·ã¥ããªãããããã¼ã¸ãã£ãã·ã¥ç¨ã®ã¡ã¢ãªãæ®ãã¦ããå¿
è¦ãããããã§ãã
- MySQL ç¬èªã®ãã£ãã·ã¥æ©æ§
MySQL ã¯ç¬èªã®ãããã¡ãã¡ã¢ãªä¸ã«æã¡ããã®ä¸ã«ãã¼ã¿ããã£ãã·ã¥ãããã¨ã¯æ¢ã«æ¸ãã¾ããããã®æ©æ§ã¯innoDB ã§ã¯ç¹ã«ãèªã¿/æ¸ãåä½ã¨ãã«ãã¼ã¸ãã£ãã·ã¥æ©æ§ã¨ä¼¼ã¦ãã¾ããããªãã¡ãä¸åèªã¿åºãããã¼ã¿ãã¡ã¢ãªä¸ã«ãã£ãã·ã¥ãããã¨ãããã¦ãæ¸ãè¾¼ã¿ã¯éåæã«å®è¡ãã¾ãã詳ããã¯漢(オトコ)のコンピュータ道: InnoDBのログとテーブルスペースの関係ãåç
§ä¸ããã
ããã¯ç¡é§ããããã¨ã示ãã¦ãã¾ããå®éã«MySQL ã«ã¯ãOS ã®ãã¼ã¸ãã£ãã·ã¥ã®å©ç¨ãæå¶ããè¨å®ãããã以ä¸ã®ããã«è¨å®ãã¾ãã
innodb_flush_method = O_DIRECT
ããã使ç¨ããå ´åãMySQL ã使ãããããã¡ã¯ç¬èªãããã¡ã®ã¿ã«ãªãã®ã§innodb_buffer_pool_size ã大ããã«ã¨ããã¨ã«ãªãã¾ããå®éãã®æ¹æ³ã¯InnoDB ã®ãã¥ã¼ãã³ã°ã«ä½¿ããã¦ããæ¹æ³ã§ãshow innodb status でチェックする値 - id:kazuhookuのメモ置き場ãMySQL Conference & Expo 2007 - とあるはてな社員の日記ãèªãã¨Pathtraq ãYoutube ã§ã使ããã¦ããã¨æ³åã§ãã¾ãã
- 64bit OS
32bit OS ã§ãããã¡ã¢ãªç©ãã+ãPAEã+ãmysqld ã§2GB 使ã(æ®ããã¼ã¸ãã£ãã·ã¥)ãã«ããã°è¯ãããã©ã¼ãã³ã¹ãåºããã¨ã¯ã§ããã¨æãã¾ãããããã大容éã®ãã¼ã¿ãæ±ãå ´åã§ã¯ãã¯ãmysqld ã«ã¡ã¢ãªããã£ã¨ãã¦ããå ´åãããããã§ããç¹ã«InnoDB ã®å ´åã¯MySQL ã«æé©åããã¦ããç¬èªã®ãããã¡ã使ãæ¹ãããããã§ã
ã¹ã±ã¼ã©ããªãã£ãèããã¨DB ãµã¼ãã§ã¯64bit OS ã使ãã®ãè¯ãã¨ãããã¨ã«ãªãã¨æãã¾ãã
ã¾ã¨ã
- 32bit çLinux ã§ã¯PAE ãæå¹ã«ãªã£ã¦ããã°64GB ã¾ã§ã¡ã¢ãªãæ±ãã
- 32bit çLinux ã§ã¯ã¦ã¼ã¶ããã»ã¹ã使ç¨ã§ããã¡ã¢ãªã¯3GB ã¾ã§
- 32bit çLinux ã§ã¯mysqld ã使ç¨ã§ããã¡ã¢ãªã¯2GB ã¾ã§
- 使ç¨ããã¦ããªãã¡ã¢ãªã¯ãã¼ã¸ãã£ãã·ã¥ã¨ãã¦IO è² è·è»½æ¸ã«å©ç¨ããã
- 32bit çLinux ã§MySQL ãµã¼ããã¥ã¼ãã³ã°ãé å¼µããªããã¡ã¢ãªç©ãã+ãPAEã+ãmysqld ã§2GB 使ã(æ®ããã¼ã¸ãã£ãã·ã¥)ã
- 64bit çLinux ã§å¶éããè±å´ãã
- 64bit çLinux ã§ã¹ã±ã¼ã©ããªãã£ã確ä¿ãã