Re: MySQLæé©åã®ããtips
å ãã¿: http://labs.unoh.net/2007/07/mysqltips.html
ãã¾ãå
·ä½çãããªãã®ã§ãåã®èãã¨ãã
æ£ãããã©ããã¯åèªã®ç¶æ³ã ã¨ãå®é試ãã¹ããªãã ãã©ãåèã«ãªãã°ã
MyISAMãInnoDBãªã©ãã¼ãã«ã¿ã¤ã
åã¯æç¶InnoDBæ´¾ã§ãã
ãã ä»°ãã¨ããããã°ãã ãã®ãã¼ãã«ã¨ããªãMyISAMã§ãããã¨ã¯æããã©ã
ãã©ã³ã¶ã¯ã·ã§ã³ãããã¯å¦çãªã©ãå¿ è¦ãªãå ´åãªã©ãMyISAMå½¢å¼ã«ãè¯ãã¨ããã¯ããã®ã§æ¤è¨ãã¦ã¿ã価å¤ã¯ããããããã¾ããã
ããã ãã®ææã ã¨ã¡ãã£ã¨å¾®å¦ãªæ°ãããã§ãã
MyISAMã®ä½¿ãã©ããã£ã¦ã®ã¯ã
- ãã³ã§ä»ã¨ãªã¬ã¼ã·ã§ã³ãç¡ãåç´è¿½è¨ç³»ã®ãã¼ãã«
- ãªã¬ã¼ã·ã§ã³ããããåä¸ãã©ã³ã¶ã¯ã·ã§ã³å ã§ã®æ´æ°ç³»ã¯ã¨ãªãåå¨ããå ´åã¯ããã©ã³ã¶ã¯ã·ã§ã³ãæå¾ éãã«åããªãã®ã§ãåºæ¬çã«ã¯InnoDBã¨æ··å¨ãããã¹ãã§ã¯ãªã
- å ¨é¨MyISAMã«ããããªããããã·ã§ã³ã¯ãªãã£ã«ã«ãããªãæ¡ä»¶ã®ãªããã¤ãªã¬ã¼ã·ã§ã³ãå°ãªããã¼ã¿ãã¼ã¹ã«é©ç¨ãã¹ã
ã£ã¦æãã§åã¯èãã¦ãã¾ãã
ã§ãçµæã¨ãã¦æ··å¨ãå®ãµã¼ãã¹ã«æå
¥ããäºã¯ç¡ãã§ãã*1
ãã¨åä¸ãã¼ã¿ãã¼ã¹å
ã«ç°ãªããã¼ãã«ã¿ã¤ããããã¨ããã¯ã¢ãããæå¾
ã©ããåããã©ããåãããã*2
InnoDBã®å ´åã¯mysql_dump使ãäºã«ãªãã¨æããã©ãMyISAMãªãã¼ãã«ã¯é¤å¤ãã¹ãã ã¨æããããã®å ´åã¯éç¨ãã¾ãã©ããªãããã
ã»ã¨ãã©ãããã¨ç¡ãã¨æããã ãã©ãªãã
ããã©ã¼ãã³ã¹ãInnoDBããã¡ãã¨è¨è¨ããã°ãããã¾ã§éè²ãªãã¨æãã¾ããã©ã
æ´æ°(update)ã»åé¤(delete)ã®ãã©ã°å
ä¸æ¦ã«ã¯è¨ãã¾ããããã¬ã³ã¼ãæ°ã大ãããªãã¨ã³ã¹ããä¸ãã£ã¦ãã¾ããæ´åæ§ãªã©ã®æ³¨æç¹ãããã®ã§æ éã«è¡ãã¾ãããã
ãæ´åæ§ãªã©ã®æ³¨æç¹ããããã£ã¦myisamchkã®ãã¨ï¼
æ´åæ§ãæ°ã«ããªãããããªããã¼ãã«ã«MyISAMãªãã使ã£ã¡ããã¡ã§ãã
æ´æ°ããåé¤ããããªãã«ãããã¼ãã«ãªãInnoDB使ã£ãæ¹ãããã
ãã¨ãã¹ã¬ã¼ãã«INDEXå¼µã£ã¦ãå ´åãªãä¸æçã«ã¬ããªã±ã¼ã·ã§ã³ããªãããã«ãã¦ã
ALTER TABLEã§æ¹ãã¦InnoDBã«storage engineãæå®ããã ãã§INDEXã®æé©åã¯è¡ã£ã¦ãããã
å¤ãã®å ´åããã¼ã¿éã¯å¤ããªã£ã¦ããããæ´æ°ãåé¤ãããã§ãããã
ã§ããããã³ã¹ããé«ããªãã§ãããäºã¯å½ããåã§ããã¥ã¼ãã³ã°ã£ã¦ã®ã¯ããã§ãããã©ã¼ãã³ã¹ãæ
ä¿ãããããããããã¹ãäºãªã®ã§ã¯ï¼
ãã¼ã¿éãã©ãã«ããªããä½å¢ããããããç©ççã«ãã¼ãã£ã·ã§ãã³ã°ãã¦ã¢ããªã§ãã¼ã¸ãããããªè¨è¨ã«å¤æ´ããªããã ã¨æãã¾ãã
ã¬ã³ã¼ãæ°ãªã©ã®åç §ã¯éæ£è¦åãã¦ã¿ã
é½åº¦ã¬ã³ã¼ãæ°ãéè¨ããã®ã§ã¯ãªããä¸é¨éæ£è¦åãã¦æã¤ãªã©ãåãåããçºè¡æ°ãæ¸ããã®ãæ¹åæ¡ã®ã²ã¨ã¤ã§ãã
ãªã©ã¤ãªã¼ã®ãå®è·µãã¤ããã©ã¼ãã³ã¹MySQLãã«ç¢ºãæ¸ãã¦ãã£ãã¨æããã©ãåç´ãªå
¨ã¬ã³ã¼ãã«ã¦ã³ãã¯MyISAMã®å ´åã¯è¨ç®æ¸ã¿ã ã£ããããã®ã§æ°ã«ããå®è¡ãã¦ããã¯ãã
å®é試ãã¦ã¿ãã°åããã¨æããã©ç°å¸¸ã«é«éã§ãã
InnoDBãå¥ã«é
ãã¯ç¡ãã§ãã
éè¨é¢æ°ã¯éå®ããã人ã使ã管çç³»æ©è½ã§ã¡ããã£ã¨åºã¦ããã®ã¯ãããã©ãããµã¼ãã¹å´ã§ä¹±ç¨ããã¨ç¢ºãã«é ãã¨æãã¾ãã
ã«ã¦ã³ãç³»ã®å¤ã¯æ£è¦åãå´©ã*3ã£ã¦ããããæ¢ã«äºåã«è¨ç®æ¸ã¿ã®ãã¼ãã«ãããã®ããã¿ã¼ããªã¼ã¨æãã¾ãã
ãã¡ãããã®ã«ã¦ã³ãæ¸ã¿ã®ãã¼ãã«ãæ´æ°ããéã«ã¢ããªå´ã§ä½è¨ãªå¦çã¯å¢ãã¾ããã©ãã
æ´æ°ç³»ã®å¦çã¯ã¢ããªå´ã®å·¥å¤«ã§æéãæãã£ã¦ãªããããªã¤ã¡ã¼ã¸ãæããäºã¯åºæ¥ããã©ãåç §å½¢ã®å¦çã¯å¾ã ã«ãã¦ã¬ãã³ã³ãªãã§ãå¾ãããã¡ãªããã®ãå¤ãããªã¼ã¨ã
ã§ãæ¬æ¥ã®ãªã¬ã¼ã·ã§ã³ä¸ã®æ£è¦åãå´©ãã£ã¦ã®ã¯ã
- çå±ä¸ãã®å¤ããªã¬ã¼ã·ã§ã³ãé§ä½¿ãã¦åãããã©ãã©ãè¦ã¦ãJOINãã¾ãããªãªã¬ã¼ã·ã§ã³
- ãªã¬ã¼ã·ã§ã³ä¸ãé£ã®é£ã«ãããã¼ãã«ã¨ã®çµåã容æã«ããããã®ä½è¨ãªãã¼
ã¨ãã¯æ¤è¨ããä½å°ãããããå®éã«åããã£ãããã¾ãã
InnoDBã§å¤é¨ãã¼ä½¿ãã°ãå¿
ç¶çã«INDEXãå¼µããããå¾ãªããã
indexãé©åãã©ãã
ç¡é§ãªindexã¯ãªããã©ããã調æ»ãã¦ã¿ã¾ããããããã£ã¦é ããªã£ãããã¦ããããããã¾ããã
ã¤ããããããç¡é§ãªINDEXãã®å®ç¾©ãåãããªãã®ã§ãããããæãã£ã±ãªããªæ°ãw
ã¡ãªã¿ã«INDEXã«ã¤ãã¦ã¯ã
- ãµã¼ãã¹éå§æã¯ãªã¬ã¼ã·ã§ã³ã«åºã¥ã(è¤å)å¤é¨ãã¼ã«ããINDEXã®ã¿
- é »åº¦ãé«ãã¯ã¨ãªããEXPLAINãã¦ãæ¤ç´¢æ¡ä»¶ã«ãªããã¼ã«å¯¾ãã¦(ãã¼ã®å 容ã«å¿ãã¦è¤å)INDEXãå¼µã
ã§OKã ã¨æãã
ãªã¬ã¼ã·ã§ã³ã«ã¯ç´æ¥é¢ä¿ç¡ãINDEXãæ¤è¨¼ç¡ãã«å¼µãã®ãç¡é§ãªINDEXã ã¨åã¯æã訳ã§ãã
MySQLã®å ´åãslow query logã¯ãã®æé
ãã£ãã¯ã¨ãªã®ãã°ã§ãã´ããæ··ãã£ã¦ãã®ã§
- slow query logã«é »ç¹ã«æãã£ã¦ã¦
- EXPlAINã®çµæãè³ãããªã
ã£ã¦ã®ããé ã«INDEXã®æ¤è¨ããã®ãçéãã¨ã
ãã®è¾ºãã«INDEXã¨ãFOREIGN KEYã«ã¤ãã¦ã¯æ¸ããã
å©ç¨ç¯å²ãèæ ®ãã
ãã¼ã¿ãå©ç¨ãããå ´é¢ãæ³å®ãããªã¢ã«ã¿ã¤ã æ§ãæ±ããããªãã£ãããåé·æ§ããªããã©ããã調ã¹ã¦ã¿ã¾ããããå ´åã«ãã£ã¦ã¯ãã°ãã¼ãã«ãªã©ãå©ç¨ããã®ãããããããã¾ããã
ãªã¢ã«ã¿ã¤ã æ§ãæ±ããããªãå ´åã¯ãDBãã¥ã¼ãã³ã°ã£ã¦ããããDBã«è³ããªã工夫ããã¹ããmemcachedã¨ã使ã£ã¦ã¢ããªå´ã§ãã£ãã·ã¥ã§ããã
ãã¼ã¿ã®åé·æ§ã£ã¦å
·ä½çã«ãªãã®äºã ãããã
ãã¼ãã«è¨è¨ï¼ããã¨ãç¡æå³ã«ä½è¨ãªã«ã©ã ãããã¯ã¬ã³ã¼ãã¾ã§åç
§ããäºï¼
ã¯ã¨ãªãã£ãã·ã¥ãèããããçºè¡ããã¯ã¨ãªãæããæ¹ãããã®ã§ã
ãããç¹ã«åç
§ã¯ã¨ãªã®ãã¿ã¼ã³ãããããã«ãã®DBã«å¯¾ãã¦å¤ããªããªãããã«ããæ¹ãè¯ãã¨æãã
ãã°ãã¼ãã«ã£ã¦5.1ããã®ã
http://dev.mysql.com/doc/refman/5.1/ja/log-tables.html
辺ãã®äºããªï¼ã ã¨ãããã¯ã¨ãªãã°ã£ã¦è¨ã£ãæ¹ãéããè¯ãã¨æããã§ããã©ã
ãµããªãã¼ãã«ã®å°å ¥ãæ¤è¨ãã
ã©ã³ãã³ã°ãªã©éè¨å¦çã¯ã³ã¹ããé«ãã¤ããã¡ãªã®ã§ããªã¯ã¨ã¹ãæ¯ã«çæããå¿ è¦ãããããæ¤è¨ããã
ããã¯ãã®éããã§ããã®åã«ã»ã¼åçã®äºãè¨ã£ã¦ããã¼ãªã
explain ã§åç §å¦çãæ¤è¨¼ãã
é©åãªindexãå©ç¨ããã¦ããããfilesortãçºçãã¦ããªããããªã¼ãã¼ãããã大ãããªãããªã©ã®æ å ±ãè¦ããã¨ãåºæ¥ã¾ãã
ã¾ããããã¯ãã®éãããªãã
ã¨ã¯ãããéç¨æ®µéã§é©åã§ãªãåç
§ã¯ã¨ãªãè¦æ¥µããç¹å®ãã¦ããã®ã¯ãã¼ãã«æ°ãå¤ãã¨ç¸å½ã«éª¨ãªãã§ããã以åã«ãã¼ã¿ãã¼ã¹ãµã¼ãã¼ã®è¨å®ãè¦ç´ãããªãSHOWã³ãã³ãã§è¦ããæ
å ±ãåèã«ãã©ã¡ã¼ã¿ãå度æ¤è¨ãã¦ã¿ãã¨ããã¨æããInnoDBå°ç¨ã®
SHOW InnoDB STATUS\G
ã®çµæãç¸å½åèã«ãªãã¯ãã
ã¯ã¨ãªã®ãã¥ã¼ãã³ã°ã¯ãã®å¾ï¼
ããããå¿ è¦ãªå¦çã»åãåãããªã®ããè¦æ¥µãã
å¥ãã¼ãã«ãéçãã¡ã¤ã«ã§ä»£ç¨å¯è½ãã©ãããªã©ãæå¤ãªç²ç¹ãããããããã¾ããã
wwww
ãããä»°ãéãï¼
ã¤ããéç¨æ®µéãªãçã£å
ã«ããã試ãããªã¼ã
ãã¼ãã«è¨è¨ã¨ãã¯ã¨ãªã®è¨è¨ã£ã¦ã®ã¯ãç¹ã«äºåã«æ¤è¨ãã¦ããã¹ãäºã§ã
INDEXãããã£ãã·ã¥ã£ã¦ã®ã¯å¾ããå¦ä½æ§ã«ãå¤æ´åºæ¥ããããã¬ãã·ãã«ã«å¯¾å¿ã§ãkï¼
ãã¨DBã«é¢ãã¦ã¯ãåã®å
ååã®æãã¤ã人ã®Tæ°æ°ãã
ãæã
ã¯å¯åæ³ãªãã¼ã¿ãã¼ã¹ãå´ã£ã¦ãããªããã°ãªããªãã
ã¨è¨ã£ã¦ããã©æ¬å½ã«ãã®éãã§ã極åãã¼ã¿ãã¼ã¹ã«åãåãããªããããªã¢ããªå´ã®åªåãç¸å½å¤§äºã ã¨æãã¾ãã
ã¾ã¨ã
- MySQL :: MySQL 5.6 リファレンスマニュアル
- MySQL :: MySQL 5.6 リファレンスマニュアル
- O'Reilly Japan - 実践ハイパフォーマンスMySQL
- Database Performance Blog – Percona
ãã®è¾ºãèªãã§ããã°ãã»ã¨ãã©ã®ã±ã¼ã¹ã§å¤§ä½å¯¾å¿åºæ¥ãã¨æãã§ãã
ã©ããæ¬å½ã«ã½ã¼ã¹ã¬ãã«ã®ç¶æ
ããã®ãã¥ã¼ãã³ã°ãæ¸ãã¦ããã
ç¹ã«å ¬å¼ã®ããã¥ã¡ã³ãã¯è¯ãæ¸ããã¦ããã®ã§ãæ°ã«ãªã£ããã¾ãå ¬å¼ã®ããã¥ã¡ã³ãå«ã¨ã