ã€ãªã€ã»ã³ã¹ã¢ããã£ã³ã¹ããŒïŒ ãã€ãããã€ãªã³ã ïŒ
å§ãã«ãè°è«ãããããšã«ã€ããŠã®ã»ãã®å°ãã®èšèã ãŸããã¯ãšãªã®æé©åãšã¯äœã§ããïŒ äººã ãåŠæ¹ããããšã¯ãã£ãã«ãªãã圌ããããŠããããšã®ç解ããã°ãã°éå°è©äŸ¡ããããšããã°ãã°èµ·ãããŸãã ç¹å®ã®ãªã¯ãšã¹ããé«éåããããšãã§ããŸãããå¿ ãããæé©åãšã¯éããŸããã ãã®ãããã¯ã«ã€ããŠå°ãçè«åããŠããããã®åé¡ãžã®ã¢ãããŒãæ¹æ³ãæé©åãéå§ããã¿ã€ãã³ã°ãæ¹æ³ãã¯ãšãªãŸãã¯ã¯ãšãªã®ã»ãããäœããã®æ¹æ³ã§æé©åã§ããªãããšãç解ããæ¹æ³ã«ã€ããŠèª¬æããŸãããããããçŽãã ãã§ãã å¥åŠãªããšã«ãã¯ãšãªãæé©åããæ¹æ³ã®äŸã¯ã»ãšãã©ç€ºããŸããã100ã®äŸã§ããœãªã¥ãŒã·ã§ã³ã«è¿ã¥ããªãããã§ãã
éåžžãããã«ããã¯ã¯ãã©ã¡ãã®åŽã«ãã®åé¡ã«ã¢ãããŒããããã«ã€ããŠã®èª€è§£ã§ãã 誰ãããEXPLAINãèªãã§EXPLAINã®ã¯ãšãªããã§ãã¯ããŸãããšèšããŸãããEXPLAINãèŠãåŸãäœãèŠãŠãäœããã¹ããã«ã€ããŠãã質åãããŸããïŒ ããã«ã€ããŠããå°ãã話ããŸãããã
å ±åæžã®ãšãã°ã©ããšããŠãè¹ãé ã£ãŠããã¢ã«ããã·ã£ã³ã®ã¯ãªããããåŒçšããããšæããŸãããã®åŒçšã¯100幎以äžåã®ãã®ã§ãããäœãå€ãã£ãŠããŸããã
ããŒã¿ããŒã¹ïŒDBïŒã®äž»ãªåé¡ã¯ãããŒã¿ããŒã¹ã«äžèŠãªãã®ãããããäžèŠãªãã®ããããããã®æäœã«å¯Ÿããäœããã®èª€ã£ãã¢ãããŒããããããšã§ãã ååãšããŠã巚倧ãªãã£ãã·ã¥ãšè¶ é«äŸ¡ãªSSDãã©ã€ããåããããçš®ã®è¶ 倧åRAIDã³ã³ãããŒã©ãŒãæäŸã§ããŸããããããããªã³ã«ããªããšæ²æšãªçµæã«ãªããŸãã
ãã¯ãšãªã®æé©åããšã¯ã©ãããæå³ã§ããïŒ ååãšããŠããã®ãããªã¿ã¹ã¯ã¯ãããŸããã åé¡ã¯éåžžãããã¹ãŠãæªãïŒããšããããšã§ãã ç§ãã¡ã¯çŽ æŽããããããžã§ã¯ããæžããŠããŸãããã¹ãŠãæ©èœãã誰ããæºè¶³ããŠããŸãããããæç¹ã§ããå°ãåºåã泚æããããå°ããŠãŒã¶ãŒãæ¥ãŠããã¹ãŠãèœã¡ãŸããã ãããžã§ã¯ããè¿ éã«éçºããå Žåãéåžžã¯ãrailsãããdjangoããªã©ã䜿çšããããã§ãã ãé¡ã«ããšæžã-補åãããæ©ãæäŸããããšãéèŠã§ãã ããã¯å®éã«æ£ããã§ã åäœããªãå®å šã«ããªããããããããžã§ã¯ããå¿ èŠãšãã人ã¯ããŸããã 次ã«ãäœãèµ·ãã£ãŠããã®ããç解ããå¿ èŠããããŸãã æé©åãããããã®é ãã¯ãšãªã¯ããããé ããªã£ãŠããããšã瀺ãã€ã³ã¿ãŒãã§ã€ã¹ã«ãããŸãããããã®çç±ã¯ç°¡åã«å¥ã®ãã®ã«ãªãå¯èœæ§ããããŸãã æªãããŒããŠã§ã¢ããããããããŸãããæªæ§æã®ããŒã¹ããããããããŸããããããŠããã®æç¹ã§ãã¯ãšãªæé©åããå§ããŠãååãšããŠãããã¯äŸ¡å€ããããŸããã ãŸããããŒã¿ããŒã¹ã«äœãèµ·ãããã確èªããå¿ èŠããããŸãã
ããŒã¿ããŒã¹ã®ãã¥ãŒãã³ã°ã«ã¯åçŽã«å€§ããªãšã©ãŒããããä¿®æ£ããåã¯ãããã«ããã¯ãå¥ã®å Žæã«ãããããã¯ãšãªãæé©åããããšã¯åœ¹ã«ç«ã¡ãŸããã ããšãã°ãPostgresã«ã€ããŠè©±ããŠããå Žåãautovacuumã¯ç¡å¹ã«ãªã£ãŠããå¯èœæ§ããããŸãã äœããã®çç±ã§ã人ã ã¯ãããè¡ãããšããããŸãïŒãããè¡ãããšã¯æ±ºããŠã§ããŸããïŒïŒãããããç¡å¹ã«ãããšãããŒãã«ã®æçåãéåžžã«å€§ãããªããŸãã 10äžã¬ã³ãŒãã®ããŒãã«ã¯ã10åã¬ã³ãŒãã®ããŒãã«ã®ãµã€ãºã«ç°¡åã«ãªããŸãã åœç¶ãããã«å¯Ÿããã¯ãšãªã¯äºæ³ãããé ããªããŸãã ãããã£ãŠããŸãããŒã¿ããŒã¹ãæ§æãããã¹ãŠãæ£åžžã«æ©èœããããšã確èªããå¿ èŠããããŸãã
ãã1ã€ã®ããããééãã¯ãã¢ããªã±ãŒã·ã§ã³ããã®æ¥ç¶ãéåžžã«å€ããæ¥ç¶ãããŒã«ãŒããªãããã«1000人ã®Postgresã¯ãŒã«ãŒãåäœããããšã§ãã 500ã®æ¥ç¶ãããå Žåã¯ãå®è¡ããŠãããµãŒããŒã«500ã®ã³ã¢ãå¿ èŠã§ããããšãç解ããå¿ èŠããããŸãã ããã§ãªãå Žåããããã®æ¥ç¶ã¯äºãã«å¹²æžããåžžã«åŸ æ©ããŸãã ãããã®ãã³ã»ã³ã¹ãä¿®æ£ãããïŒéåžžã«å€ãã®å ŽåããããŸãããäž»ãªãã®-5ã10å-ã¡ã¢ãªããã£ã¹ã¯ãèªåããã¥ãŒã ã®æ£ããèšå®...ïŒãã¯ãšãªã®æé©åã«é²ãããšãã§ããŸãã ãããŠãã®æã ãã ä»ã«ããªããããŠããããšã ããæé©åããããšããªãã§ãã ããã
ã¢ã«ãŽãªãºã ãšã¯ãã€ãŸã ã¯ãšãªæé©åã®ã¢ãããŒãæ¹æ³ ãŸããèšå®ã確èªããå¿ èŠããããŸãã次ã«ãäœããã®æ¹æ³ã§ãæé©åããã¯ãšãªãéžæããŸãïŒããã¯éèŠãªãã€ã³ãã§ãïŒïŒããããŠå®éã«æé©åããŸãã æãé ãã¯ãšãªãããã¥ã¢ãããåŸããããã¯éããªããŸããå€ããããããããã«åã£ãŠä»£ãã£ãã®ã§ãããã«æ°ããé ãã¯ãšãªã«ãªããŸãã ãã®çµæããã®ã¢ã«ãŽãªãºã ãç¹°ãè¿ããªããã段éçã«åŸã ã«ãé ãã¯ãšãªãåãé€ããŸãã ãã¹ãŠãã·ã³ãã«ã§ãã
éåžžã«éèŠãªãã€ã³ãã¯ãæé©åããã¯ãšãªãç¥ãããšã§ãã è¡ã®ãã¹ãŠãæé©åãããšãã©ã®ã¯ãšãªãæãåé¡ãããããæšæž¬ã§ããªãå¯èœæ§ããããŸãã ããªãã¯å€ãã®æéãè²»ããã ãã§ãããé«ã確çã§ããªãã¯æ£ããä»äºã«çããªãã§ãããã ãããã£ãŠãåé¡ãçºçãããšãã«ã¯ãšãªãæé©åããå¿ èŠããããŸãã åäœãåæ¢ããå ŽæãèŠãŠãé ããŠæªããªããŸãã-ãã®éšåãæé©åããŸãã ãã£ãã«äœ¿çšãããªããã®ãããå Žåã¯ã觊ããªãã§ãã ãããæéãç¡é§ã«ããªãã§ãã ããã
éžæãããããã«å¯ŸããŠãªã¯ãšã¹ããè¡ããããããã§äœãã§ããããèŠããŸãã ãããè¡ãã«ã¯ãç¹ã«ããŒãžã§ã³9.4ã§ã¯ãEXTENSION pg_stat_statementsã䜿çšããã®ãæ£ããæ¹æ³ã§ããããã¯ãã¹ãŠãªã³ã©ã€ã³ã§ãªã³ã¶ãã©ã€ã§å®è¡ãããããããã¹ãŠèŠãããšãã§ããŸãã
ããã«ã€ããŠè©³ãã説æããŸãããã
ããŒãžã®äžçªäžã®ã¢ãã¬ã¹ã§-pg-utilsïŒç¡æã§å ¥æå¯èœïŒãããŠã³ããŒãããŠäœ¿çšã§ããŸãã ãã®ãã©ã«ããŒã«ã¯ãæšæºã®ã«ãŠã³ã¿ãŒããŒã¹pg_stat_statementsãå²ãç¹å®ã®ãã€ã³ãã£ã³ã°ããããŸããããã«ãããããŒã¿ããŒã¹ã§ã®1æ¥ã®äœæ¥ã«ã€ããŠãããã®ã¬ããŒããçæã§ããããã§äœãèµ·ãã£ãã®ãã確èªã§ããŸãã ãããã£ãŠãããã€ãã®äžäœã¯ãšãªãäœããã®åœ¢ã§ã©ã³ã¯ä»ããããŠããŸãã
æåã®äœçœ®ã2çªç®ïŒéåžžã¯ããã«10åïŒãããããšãç¥ãããšãéèŠã§ãããããã€ãã®ãã©ã¡ãŒã¿ãŒã«ã€ããŠã¯æåã«ç¹å®ã®ãªã¯ãšã¹ããåºãŠããããšãããããŸã-ããšãã°ãããŒã¹è² è·ã®24ïŒ ãããããŸãã ããã¯éåžžã«å€ããäœããã®åœ¢ã§æé©åããå¿ èŠããããŸãã ãªã¯ãšã¹ããèŠãŠãã圌ã¯ãããžã§ã¯ãã«ããããéãæã£ãŠããŸããïŒããšèããŸãã 圌ããããžã§ã¯ãã«å€é¡ã®ãéãæã¡èŸŒã¿ãããããååã®è² è·ãé£ã¹ããšããŠãããã圌ããéãæã¡èŸŒãŸããååã®ãªãœãŒã¹ãé£ã¹ãããããã¯æªãããšã§ããäœããããå¿ èŠããããŸãã ãããã£ãŠãåæ¥ã®äžäœã¯ãšãªãèŠãŠãããã«å¯ŸããŠäœããã¹ãããèããŸãã
ãããžã§ã¯ãã§äœããããéçºããŒã ãããŠãcronã§1æ¥ã«1åããã®ãããªã¬ããŒãããã¹ãŠã®DBAããã¹ãŠã®éçºè ããã¹ãŠã®ç®¡çè ã«å±ãã®ã¯è¯ãç¿æ £ãšèããããŠããŸãã
é ãã¯ãšãªãšã¯äœã§ããïŒ ãŸããããã¯æäžäœã«ããã¯ãšãªã§ãïŒãããã«ããŠããäœããã®æ¹æ³ã§æé©åããå¿ èŠããããŸãïŒã ããããçŽç²ã«æéã®åé¡ã¯åžžã«åé¡ã§ãã ããªç§ã®å°æ°éšã§æ©èœããã¯ãšãªã§ãããäŸç¶ãšããŠé ãå¯èœæ§ããããŸãã ããšãã°ããããã®èŠæ±ãå€æ°ããããã®çµæãšããŠå€ãã®å°ããªèŠæ±ãããŒã¿ããŒã¹ãªãœãŒã¹ã®éåžžã«å€§ããªå²åãæ¶è²»ããå Žåã§ãã
ãããã£ãŠããªã¯ãšã¹ãæéã¯çžå¯Ÿçãªãã®ã§ãããããã§ã¯ããã®ãªã¯ãšã¹ããæ©èœããé »åºŠã調ã¹ãå¿ èŠããããŸãã ãããããŒã ããŒãžã§äœããè¿ããããã®ãªã¯ãšã¹ãã«1ç§ãããå Žåããã®ããŒãžã«å ããŠãã®ããŒãžã圢æããããã«å¿ èŠãªãã¹ãŠã®ãªãŒããŒããããããããšãç解ããå¿ èŠããããŸãã ããã¯ããŠãŒã¶ãŒã«1ç§ãããé ãçµæãä¿èšŒãããŠè¡šç€ºãããããšãæå³ããŸãããªã³ã©ã€ã³ã®è² è·ã®é«ãWebã®å Žåããããã¯èš±å®¹ã§ããªãçµæã§ãã äœããã®åæã®ããã«ã¯ãšãªãå€éã«è¿œè·¡ãããå Žåãéåæã¬ããŒãã誰ãã«éä¿¡ããããããã圌ã¯ãã£ãããšäœæ¥ããäœè£ããããŸãã ã€ãŸããããŒã¿ãåžžã«ææ¡ãããã®ãªã¯ãšã¹ããæ©èœããããã«èš±å®¹ãããæéãåžžã«èæ ®ããå¿ èŠããããŸãã ç¹°ãè¿ããŸãããããŒã¹ã§ã®è² è·ã®æ§è³ªã¯éèŠã§ãã ããšãã°ãé·ããŠéãèŠæ±ããããããŒã¯æã«ãããæäœããŸããããã¯ãããçš®ã®åæããããŒãžã£ãŒãªã©ã«å¯ŸããèŠæ±ã§ãã
ããŒã¹ã®è² è·ãããã¡ã€ã«ã確èªããŸãã pg_stat_statementsãããããã®äžã«ãããšãã°åå2æããååŸ4æãŸã§ã®é ãã¯ãšãªã®ããããèŠãããšãã§ããçŸæç¹ã§ã¯é·ãåæã¯ãšãªãé§åããŸããã
ãã®åé¡ãã©ãã ãã®ãéããããããå€ãã®ããŒã¿ããŒã¹ãªãœãŒã¹ãå æããæš©å©ãããããå¿ããªãã§ãã ããã ãããžã§ã¯ãã§äœãåŸãããªãã¯ãŒã«ãªæ©èœãäœæãããã®ãªã¯ãšã¹ãããªãœãŒã¹ã®50ïŒ ã䜿ãæãããå Žåãæªããªã¯ãšã¹ããæžãããã®ã¢ã€ãã¢ãããçŽããæã«ã¯æè¡çã«éåžžã«é£ããããšã§ãªãœãŒã¹ã䜿ãæãããŠããŸãçç±ã説æããå¿ èŠãããããŸãã 誰ããããŽãŒã«ãã³ãã£ãã·ã¥ãç§ã®æ·å°å ã«çœ®ããŠã»ãããšèšããŸããããµãŒããŒã¯é補ã§ãããäžå®ã®å¶éãããããŽã ã䌞ã°ãããšã¯ã§ããŸããã ã¯ã©ãŠããäœæãã人ã ã¯äœãå¯èœããæããŠãããŸãããç§ã¯éå±ãªç®¡çè ãšããŠãããã¯äžå¯èœã ãšèšããŸãã
äžè¬ã«ã1ã€ã®ç¹å®ã®ãªã¯ãšã¹ãã®å®è¡ã«åé¡ãããã®ã¯ã©ãã§ããïŒ ãŸããã¯ã©ã€ã¢ã³ãããã®ããŒã¿è»¢éã§ããå¯èœæ§ããããèŠãç®ã»ã©é¢çœããã®ã§ã¯ãããŸããã 次ã®ã¹ã©ã€ãã¯ãç¬ãããã«åããããšãã§ããå Žæã瀺ããŠããŸãã
Rubyã§æžããŠãããããçš®é¡ã®cãªORMã䜿çšãã人ã¯èª°ã§ãããã®ãªã¯ãšã¹ãã«ãã£ãŠDjangoãèå¥ã§ããããšãç¥ã£ãŠããŸãã ããã¯äŒæ¥ã®ã¢ã€ãã³ãã£ãã£ã§ãããç¡ç·äºæ¥è ã®ææžãã¯äœãšãæ··åãããŸããã
ç§ã®äººçã§èŠããªã¹ãã®æ倧é·ã¯äœã ãšæããŸããïŒ ã®ã¬ãã€ãåäœã§èæ ®ããå¿ èŠããããŸãïŒ ORMã®æ©èœã確èªããªããšãæ°ã®ã¬ãã€ããç°¡åã«ååŸã§ãããã®ãªã¯ãšã¹ãã¯å®è¡ãããŸããã ããã¯æªãããšã§ãããããŒã¿ã¢ã¯ã»ã¹ãå®å šã«æé©åãããŠããªãããšãæå³ããŸãã ãã®èŠæ±ã¯ä»ã®å€ãã®çç±ã§æªãã§ãããäž»ãªçç±ã¯ãPostgresã人çã§ã¯ããŒã«ããªããããªé·ãã«ãªãå¯èœæ§ãããããšã§ãã
2çªç®ã®ãã€ã³ãã¯è§£æã§ãã åçŽã«é·æé解æãããéåžžã«è¯ãããªãªã¯ãšã¹ããäœæã§ããŸãã Postgresã®æ°ããããŒãžã§ã³ã§ã¯ãç§ãééããªããã°ãEXPLAINã«è§£ææéããããã©ããããæéããããããç解ã§ããããã«ãªããŸãã ããã§ãEXPLAINãäœæããŠãããããã¿ã€ãã³ã°ããªã¯ãšã¹ãã®å®è¡ã«ããã£ãéãããã³è§£æãŸã§ã®æéã確èªã§ããŸãã
次ã«ããªã¯ãšã¹ããæé©åããå¿ èŠããããŸãã ãªããã£ãã€ã¶ã¯éåžžã«è€éãªã¢ã«ãŽãªãºã ã§ãããããããã¯èŠããã»ã©ç°¡åãªã¿ã¹ã¯ã§ã¯ãããŸããã ããšãã°ã2ã€ã®ããŒãã«ãçµåããããšããŸãã 圌ã¯1ã€ã®ããŒãã«ãååŸããå¿ èŠãªããŒã¿ã«ã¢ã¯ã»ã¹ããæ¹æ³ãéžæããŠã次ã®ããŒãã«ãæ·»ä»ããŸãã ãã1ã€ã®ããŒãã«ãšã®å¥ã®çµåãããå Žåã¯ãæåã«2ã€ã®çµåãããã次ã«ResultSetããã1ã€çµåãããŸãã 512 Join'ovã§ãªã¯ãšã¹ããæžããå Žåãéåžžã«èå³æ·±ãããã»ãªãã¯ãã®ãã¹ãŠã®æé©åããå§ãŸããŸãã
Joinã®nïŒã®æ°ã«å¿ããŠãæé©ãªJoinã®ãã¹ãæŽçããŸãã èšç»ã®ãªãã·ã§ã³ãéžæãããŸãã ãããã£ãŠãå€ãã®Join'ovãããå Žåãæé©åããã»ã¹èªäœãéåžžã«é·ããªãå¯èœæ§ãããããšãããã«ç解ã§ããŸãã
次ã¯çŽæ¥ããã©ãŒãã³ã¹ãããããŸããã ãªã¯ãšã¹ãã10 GBã®ããŒã¿ãè¿ãå Žåããããããªç§åäœã§æ©èœãããšããäºå®ãåœãŠã«ããããšã¯å°é£ã§ãã éæ³ã¯åœŒãäœãããšãã§ããŸããã ãããã£ãŠã倧éã®ããŒã¿ãæäŸããå¿ èŠãããå Žåã¯ãéæ³ããªãããšã«æ³šæããŠãã ããã NoSQLã®äžçã§èµ·ãããŸãããããã«ã¯ãããŸããã
ããŠãçµæãè¿ããŸãã ç¹°ãè¿ããŸããããããã¯ãŒã¯äžã§æ°ã®ã¬ãã€ãã®ããŒã¿ãé§åããŠããå Žåã¯ããããã¯ãŒã¯ãééã§ããéã«äžå®ã®å¶éããããããé床ãé ããªãããšã«åããŠãã ããã ãã®ãããªå Žåãããããã¹ãŠã®çµæãå¿ èŠãã©ãããæã èããã®ã¯çã«ããªã£ãŠããŸããïŒ ããã¯éåžžã«äžè¬çãªåé¡ã§ãã
ãã®ãã¬ãŒã³ããŒã·ã§ã³ã®æãéèŠãªã¹ã©ã€ãïŒ
ããã¯èª¬æã§ãã
äžäœã®ã¯ãšãªã匷調衚瀺ããåŸãäœããã®æ¹æ³ã§ãããã®ã¯ãšãªãé ãããšã確èªããã¯ãšãªã䜿çšããŠäœããå®è¡ããã«ã¯ãEXPLAINãå®è¡ããå¿ èŠããããŸãã
å€ãã®äººããã®æ®µéã«éããããããã²ã£ããã£ãã ãããŠãç§ãã¡ã¯EXPLAINãèŠãŠã次ã«ãããã©ããããïŒããšäžæºãèšãã ããã¯ç§ãããªãã«ä»è©±ãããšã§ãã
EXPLAINã®ã¹ã©ã€ã2ã§ã¯ãæ§æãå°ãç°ãªããŸãã
ããã¯æ¬¡ã®ããã«è¡ãããšãã§ããŸãïŒExplainïŒåæãªã³ããããã¡ãŒãªã³ïŒãããšãã°ãåã«Explainåæãšæžãããšãã§ããŸãã
EXPLAINã¯ãããªãããããã©ãããã¹ããã«ã€ããŠã®èšç»ãåã«ããªãã«äžããããšãç解ããããšãéèŠã§ãã ãããã£ãŠãANALYZEãæå®ããå Žåããã®ãªã¯ãšã¹ãã¯å®éã«å®è¡ãããå®è¡æ¹æ³ã«é¢ããããŒã¿ã衚瀺ãããŸãã åã«èª¬æããã ãã§ãªããå®éã«ã¯äœãèµ·ãã£ãŠããã®ããäœããã®çè·¡ããããŸãã
é ãã¯ãšãªã®ããããéžæããå ŽåãEXPLAIN ANALYZEã䜿çšããã®ãé©åã§ããæé©ãªãã©ã³ãéžæããå¯èœæ§ããããçµ±èšãåéãããªããªã©ãããããã§ãã
Postgresã¯ã¹ãŒããŒãã©ã³ã¶ã¯ã·ã§ã³ã§ããã¯ãšãªãèšè¿°ããŠããå Žåãäœããæé©åãããšãã«ãããã®çµæãæžãçããããªããããšãã°ãéå§ããã¯ãšãªãéå§ããŸãïŒãã ããäžè¬çã«äœãèµ·ãããã確èªããããšããå§ãããŸã-éãã¯ãšãªããŒã¯æã«ã¯ãæŠéåºå°ã§åžžã«è¯ããšã¯éããŸããïŒããrallbackããšçºå£°ããŠããã®ããŒã¿ãèšé²ãããªãããã«ããŸãã
ããã€ãã®æ°å€ãããã«è¡šç€ºããããã®ããã€ãã¯EXPLAINã«å±ããããã€ãã¯ANALYZEã«å±ããŸãã ãããã¯éèŠãªæ°åã§ãã EXPLAINã«ã¯ããã³ã¹ãããšåŒã°ããæ¡ä»¶ä»ãã®ããªãŠã ãããããŸãã ããã©ã«ãã§ã¯ãPostgresã®1ã€ã®ã³ã¹ãã¯ãé 次é 次ã¹ãã£ã³ã§8 KBã®åäžãããã¯ãæœåºããã®ã«ãããæéã§ãã ååãšããŠããã®å€ã¯ãã·ã³ã«äŸåãããããæ¡ä»¶ä»ãã§ãããã䟿å©ã§ãã é«éãã©ã€ãã䜿çšããŠããå Žåã¯ãäœé-äœéã®å Žåã¯é«éã«ãªããŸãã ã³ã¹ã= 9.54ã§ããããšãç解ããããšãéèŠã§ã-ããã¯ããµã€ãºã8 KBã®1ãããã¯ãååŸããããã9.54åé ãããšãæå³ããŸãã
2ã€ã®æ°åããããŸãã1ã€ç®ã¯ãæåã®çµæãè¿ããããŸã§ã«ã©ãã ãæéãçµéãããã2ã€ç®ã¯ãçµæå šäœãè¿ããããŸã§ã«ã©ãã ãæéãçµéãããã瀺ããŸãã 倧éã®ããŒã¿ãæœåºããå Žåãæåã®æ°åã¯æ¯èŒçå°ããã2çªç®ã®æ°åã¯éåžžã«å€§ãããªããŸãã ããã¯å®éã«ããã£ãå®éã®æéã§ãã äœããã®çç±ã§ã³ã¹ããéåžžã«å°ããããã®æéãéåžžã«å€§ããå Žåãçµ±èšã®åéã«ããã€ãã®åé¡ããããŸããautovacuumããªã³ã«ãªã£ãŠãããã©ããã確èªããå¿ èŠããããŸãããªããã£ãã€ã¶ãŒã
EXPLAINã¯ãã®ãããªããªãŒã§ãã倧ãŸãã«èšã£ãŠããã£ã¹ã¯ããããŒã¿ãååŸããæ¹æ³ã¯ãã£ãšäœããªãã·ã§ã³ããããŸããããã¯ãã¬ãŒãã®ã¹ãã£ã³ãã€ã³ããã¯ã¹ã®ã¹ãã£ã³ãªã©ã§ãã äžäœã®ãªãã·ã§ã³ã¯ãããçš®ã®éçŽãçµåãªã©ãäžã«éããããŠããå Žåã§ãã ãã®ãããªEXPLAINãèŠããšãã¿ã¹ã¯ã¯éåžžã«ç°¡åã§ãã
- ãããã©ã®ãããéãåäœããããç解ããã©ã³ã¿ã€ã ãèŠãŠãããã§äœãèµ·ãã£ãããèŠãã
- ãã®ããªãŒã®ã©ã®ããŒããæãé«äŸ¡ãã確èªããŠãã ããã äžçªäžã®æ®µéã§ã³ã¹ããååã«å€§ããå Žåãå®éã®æéãé·ãå Žåããã®éšåãæé©åããå¿ èŠããããŸãã ããšãã°ãããŒãã«å šäœãã¹ãã£ã³ããå Žåãããã«ã€ã³ããã¯ã¹ãäœæããå¿ èŠãããå ŽåããããŸãã ããã¯ãããªããæ¬åœã«æé©åããå¿ èŠãããå Žæã§ãããããªãã¯ãããèŠãå¿ èŠãããã
ããšãã°ãã¹ã©ã€ãã®å Žåã®ããã«éçŽãããããå Žåã¯ãéçŽãããéãã®ã§ããããåãé€ãæ¹æ³ãèããå¿ èŠããããŸãã
ã ããããªãã¯EXPLAINãèŠãŠãæãé«äŸ¡ãªå ŽæãèŠã€ããŸãã EXPLAINãå幎éââèŠãåŸããããã®å ŽæãèçŒã§èŠãããšãåŠã³ããã§ã«é ã®äžã«ã¬ã·ãã®ã»ããããããŸãã 詳现ã«ã€ããŠã¯ãŸã æ€èšããŸããã
ã©ã®ãããªãã¯ããã¯ã䜿çšã§ããŸããïŒ ã€ã³ããã¯ã¹ãäœæã§ããŸãã ã€ã³ããã¯ã¹ã®ã¢ã€ãã¢ã¯ã倧ããªããŒãã«ãã¯ããŒã«ããã®ã§ã¯ãªããã¹ãã£ã³ã䟿å©ãªå°ããªããŒã¿é åã§ãããšããããšã§ãã ãããã£ãŠããã¹ãŠã®ããã°ã©ããŒã¯ã€ã³ããã¯ã¹ã倧奜ãã§ãããããå Žåã«ã€ã³ããã¯ã¹ãäœæããããã圹ç«ã€ãšèããŠããŸãã ã€ã³ããã¯ã¹ãç¡æã§ã¯ãªããããããã¯ééã£ãŠããŸãã ã€ã³ããã¯ã¹ã¯ã¹ããŒã¹ãå æããŸããããŒãã«å ã®åãšã³ããªã¯ã€ã³ããã¯ã¹ãåæ§ç¯ããããã©ã³ã¹ãåããŠããããã¹ãŠã空ããŠããããã§ã¯ãããŸããã
䜿çšãããŠããªãã€ã³ããã¯ã¹ã§ããŒãã«å šäœããã³ã°ããŠããå Žåãé«ã確çã§ãããã®äžéšãç Žæ£ã§ããé«éã«ãªããŸãã ããã§ããããšãã°ããŒãã«ããããŒã¿ã®ååãæœåºããããã«ã¯ãšãªãå¿ èŠãªå Žåãããªãæ£ç¢ºãªå Žæã«ã€ã³ããã¯ã¹ãé 眮ããŠãã®ããŒã¿ãååŸããã®ãçã«ããªã£ãŠãããããã€ã³ããã¯ã¹ã¯äœ¿çšãããªãå¯èœæ§ãéåžžã«é«ããªããŸãã ãµã€ãºãããŒãã«ã«å¹æµãã倧ããªãã·ãŒãããå¿ èŠãªå Žåãæåã«ã€ã³ããã¯ã¹ã¹ãã£ã³ãå®è¡ãã次ã«å¥ã®æäœãå®è¡ããå¿ èŠããããããããŒãã«èªäœã®é 次ã¹ãã£ã³ã¯åžžã«ã€ã³ããã¯ã¹ã¹ãã£ã³ãããé«éã«ãªããŸãã
ã»ãšãã©ã®å Žåããªããã£ãã€ã¶ãŒã¯ãã®ãããªããšãééããŠããŸããã ã€ã³ããã¯ã¹ãäœæãããªãããã䜿çšãããªãã®ãçåã«æã£ãŠããå Žåãã€ã³ããã¯ã¹ããªããšåçŽã«é«éã«ãªãããã§ãã
Postgresã«ã¯ãã®ãããªãã©ã¡ãŒã¿ããããŸã-ã»ãã·ã§ã³å€æ°ãã€ã³ããã¯ã¹ã¹ãã£ã³ããªãã«èšå®ããŸãã¯éã«ã·ãŒã±ã³ã·ã£ã«ã¹ãã£ã³ããªãã«èšå®ãããšãã€ã³ããã¯ã¹ã®æç¡ã«ãããããé«é/äœéã«ãªããŸãã ãã®æ¹æ³ã§ã¯ãšãªããæŠéäžãã«æé©åããããšã¯ãå§ãããŸãããããã¯éåžžã«é£ãããæŸèæãã§ããããªããã£ãã€ã¶ã®æ©èœã®éåžžã«æ·±å»ãªå¶éã§ãããå®éšããŠç¢ºèªããã®ã«åœ¹ç«ã¡ãŸã ãªã¯ãšã¹ããäœæããã€ã³ããã¯ã¹ãäœæããæ©èœãããšæããã·ãŒã±ã³ã·ã£ã«ã¹ãã£ã³ããªãã«ãããªããã£ãã€ã¶ãŒã¯ã€ã³ããã¯ã¹ä»ãã®ãã©ã³ãéžæããããã«åŒ·å¶ãããPostgresãæäŸãããã®ãããé ããªã£ããã©ããã確èªããŸãã ã»ãšãã©ã®å Žåãããã¯ãŸãã«ããã§ãã
次ã«ããªã¯ãšã¹ãã®äœææ¹æ³ãéèŠã§ãã ãã®ãããªãã®ãããå Žå-ïŒcounter + 1 = 46ïŒ-ã€ã³ããã¯ã¹ã¯ååŸããããPostgresã¯ãã®æäœãèªåçã«å®è¡ã§ããŸããã åçŽãªè¿œå ã®ããã«èŠããŸãããåãæåãåããããšã§ããªããã£ãã€ã¶ãŒã«å·®åã解決ãããããšãã§ããŸãã Postgresã«ã¯å€æ°ã®ããŒã¿åããããæŒç®åãå®çŸ©ãããã代æ°ãªã©ã®ã¢ã¯ã·ã§ã³ãå®çŸ©ãããããªããã£ãã€ã¶ãŒã¯ããããã¹ãŠã®ã¿ã€ãã«ã€ããŠãã®ã¢ã¯ã·ã§ã³ãå®è¡ããæ¹æ³ãç¥ã£ãŠããå¿ èŠããããŸããã圌ã«ãšã£ãŠã¯é£ããã¿ã¹ã¯ã§ããåäœããŸããã
以äžã¯ãããšãã°ãçµåãããŸãæ©èœããªãçç±ã§ãã ããã¯éèŠãªããŒãã®1ã€ã§ã誰ãã䜿çšããŸãã
Joinã«ã¯ããŸããŸãªã¿ã€ãããããLEFTãRIGHTãINNERãªã©ã«ã€ããŠã§ã¯ãªããJoinãå®è¡ãããã¢ã«ãŽãªãºã ã«ã€ããŠèª¬æããŠããŸãã Postgresã«ã¯3ã€ã®äž»ãªçµåã¢ã«ãŽãªãºã ããããŸããã€ãŸãããã¹ãã«ãŒãïŒååãããããããã«ã1ã€ã®ããŒãã«ããããŒã¿ãååŸããŠãµã€ã¯ã«ã§çµåããŸãïŒãããã·ã¥ã€ã³ããã¯ã¹ïŒ1ã€ãå€ãã®å Žåå°ããããã·ã¥ããŒãã«ãããã³ãã®ããã·ã¥å¥ã®ããŒãã«ãšã®çµåïŒãšçµåã®çµåïŒãããã©ã®ããã«æ©èœãããã¯æããã§ãïŒã
ãããã®çµåã¯åžžã«åãããã«åœ¹ç«ã€ããã§ã¯ãªãããªããã£ãã€ã¶ãŒã¯ããããéžæã§ããŸãã ããšãã°ã2ã€ã®ããŒãã«Joinãããããªããã£ãã€ã¶ãHash JoinãéžæããŸãããåäœãé ãããšãç解ããŠãããããããªãã«ã¯åããŠããŸããã Join'iteã®ãã£ãŒã«ãã«ã€ã³ããã¯ã¹ãä»ããŠãããã©ããã確èªããã®ã¯çã«ããªã£ãŠããŸãã , Nested Loop, . , Nested Loop, .
â - Nested Loop, , , â Hash Join , . , work mem'. ã€ãŸã worker Postgres'. , , 100 , work mem' 30 , worker . work mem' , Hash Join .
, , , .
« » ( workload ), .
:
. , , - Postgres , â , - ( , Postgres', , , ). WHERE , ORM' . , Postgres - . , , .
EXPLAIN :
, , , Index Scan , - , , .
, . , Postgres Hash Join, . «» , -Join'. , .
VALUES, ResultSet:
Index Scan Hash Join. , .
, , , , , :
.
, , EXPLAIN, . , , .
â , . âcount(*).
, -, â , , . , count , ? , , , . , - , , , , . - , , , count .
count â , Postgres, , , .
â count'. , . â count. PG-, -select', analyze', . , PG- â select value . - PG-, , «security definer» , - security.
- â Join 300 . , 300! , Join . , Join 300 , , , - , . Join â , . , , . Join' , .
â 1 000 000 . Google? ? , , , - 1 . , â - ? 10, 20 , , 100, 1 . . , , - , , , - , .
, ORM' - , , , , 10%. limit offset - , 1 . â , , , - .
é£çµ¡å
hydrobiont
â HighLoad++ .
ãŸãããããã®è³æã®äžéšã¯ãé«è² è·ã·ã¹ãã HighLoadã®éçºã«é¢ãããªã³ã©ã€ã³ãã¬ãŒãã³ã°ã³ãŒã¹ã§äœ¿çšãããŸãã¬ã€ãã¯ãç¹å¥ã«éžæãããæåãèšäºãè³æããããªã®ãã§ãŒã³ã§ãã ç§ãã¡ã®æç§æžã«ã¯ãã§ã«30以äžã®ãŠããŒã¯ãªè³æããããŸãã æ¥ç¶ããŠãã ããïŒ
â " - ", , HighLoad++ Junior .