é
ããæ©ãããå€ãã®å€§èŠæš¡ãããžã§ã¯ãã§ã¯ãæçš¿ãããŒãžã³ã°ãããšãã«ããã©ãŒãã³ã¹ã®åé¡ãçºçããŸãã ãããã®ããã€ãã¯ã衚瀺å¯èœãªã¬ã³ãŒãã®æ°ãå¶éããããšã§ãã®åé¡ã解決ããŸãïŒããšãã°ã1000以äžïŒã ããã¯èš±å®¹ã§ãã解決çã§ãã ãã ãããã®å ŽåããµãŒãããŒãã£ã®æ€çŽ¢ãšã³ãžã³ã«ãããµã€ãã®ã€ã³ããã¯ã¹äœæã«åé¡ãçºçããå¯èœæ§ãããããããæ倧ã®è
åšãšãªããŸãã ãã®èšäºã§ã¯ãåçŽãªãåæ¹...åŸæ¹ãïŒèª¬æã¯ç°¡åã«ãªããŸãïŒãåªå
ããŠãéåžžã®ã1..2..3..4 ..ãããã²ãŒã·ã§ã³ããŒããã¹ãŠæŸæ£ããŸããããããå®è£
ããŠãåé¡ã¯ãããŸãããæåã®ãªãã·ã§ã³ã
ãã®æ°åã¯èª°ã«ãšã£ãŠãç°ãªããããŒããã©ã€ãã®é床ãã¡ã¢ãªã®éãããŒã¿ã®ãã£ãã·ã¥éã«å€§ããäŸåããããããã¬ãŒãã衚瀺ãããã®ã«ååãªå€§ããã®ã¬ã³ãŒãã®æ°ãèšãããšã§ãããã¯ã決å®ããããšã¯ããæ£ç¢ºã«ã¯ãªããŸãããã®äžãªã©ã«ã ããããããªããšããªãã®ãµãŒããŒãåºåäžã®nçªç®ã®ããŒãžãæåã®ããŒãžãããéããšæãããããã©ããããããããªãå Žå-èšäºã¯ããªãã®ããã§ãã ããããæåã«ãITãé
ãçç±ãæã§èª¬æããããšæããŸãã
ã¡ãªã¿ã«ããã¹ãã¯ä»®æ³ãã·ã³ã§è¡ãããã«ãŒãã®äžã§DBMSãæäœããŸããMySQLã®ããŒãžã§ã³ã¯5.0.32ã§ãã
1ããŒã¿ããå§ããŸããã
ãã¹ãã®ããã«ãå°ããªãã¬ãŒããäœæããäœãã§æºãããŸãã
CREATE TABLE items (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
height INT UNSIGNED NOT NULL DEFAULT 0,
width INT UNSIGNED NOT NULL DEFAULT 0,
price DECIMAL(10,2) NOT NULL DEFAULT 0.0,
title VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
å°ããªPHPã¹ã¯ãªãããINSERT 100,000ã¬ã³ãŒããçæããŸããã ãã®çš®ã®ããŒã¿ïŒ
-INSERTã®ãã£ãŒã«ãã®é åºïŒ
é«ããå¹
ãäŸ¡æ Œãã¿ã€ãã«
-ãã£ãŒã«ãã®ãã³ãã¬ãŒãïŒ
$ val_tmpl = "\ tïŒïŒ
dãïŒ
dãïŒ
fã 'ItemïŒ
d'ïŒ";
-ãã¹ãå€ïŒ$ i = 1..100000ïŒïŒ
sprintfïŒ$ val_tmplãrandïŒ0ã120ïŒãrandïŒ0ã220ïŒã10 * randïŒ0ã$ iïŒ/ $ iã$ iïŒ;
ãã¹ãŠãããŒã¿ããŒã¹ã«å
¥ããŸãã ãããŠãããªãã¯å§ããããšãã§ããŸã...
2åŸæ¥ã®ããŒãžããŒã·ã§ã³æ¹æ³
COUNTïŒ*ïŒãšLIMIT ... OFFSETãæªãçç±ããã§ã«ç¥ã£ãŠãã人ã¯ããã®éšåãã¹ãããã§ããŸãã
ããã²ãŒã¿ãŒãæç»ããåã«ãSELECT COUNTïŒ*ïŒ... WHEREïŒselection_conditionsïŒãå®è¡ããŸãã å€ãã®å Žåãäœããã®çç±ã§ãæ°çŸäžã®ã¬ã³ãŒããããå Žåã§ããéžæã®æ¡ä»¶ã«ããã€ã³ããã¯ã¹ã䜿çšã§ããããã«ãªããšããã®ãããªã¯ãšãªã¯éåžžã«è¿
éã«æ©èœããŸãã å®éšããŠã¿ãŸãããã é«ãã100ãè¶
ããã¬ã³ãŒãã®æ°ãéžæããŸãããŸããé«ããã£ãŒã«ãã«ã€ã³ããã¯ã¹ããªãå Žåã«äœãèµ·ããããèŠãŠã¿ãŸãããã
ãã©ãã·ã¥ã¹ããŒã¿ã¹;
SELECT SQL_NO_CACHE COUNTïŒ*ïŒFROM items WHERE height> 100;
+ ---------- +
| ã«ãŠã³ãïŒ*ïŒ|
+ ---------- +
| 16405 |
+ ---------- +
ã»ããå
ã®1è¡ïŒ0.09ç§ïŒ
ãhandlerïŒ
ãã®ãããªã¹ããŒã¿ã¹ã衚瀺ããŸãã
æåŸã®ã³ãã³ãã¯ãèŠæ±ãæºããããã«DBMSãå®è¡ããå¿
èŠã®ããæ°ãšã¢ã¯ã·ã§ã³ã瀺ããŸãã ã€ã³ããã¯ã¹ããªããããMySQLã¯ããŒãã«ããçŽæ¥ããŒã¿ãèªã¿åãå¿
èŠããã£ãããã次ã®è¡ã«èå³ããããŸãã
...
| Handler_read_rnd_next | 100001 |
...
ã€ãŸããMySQLã¯ã¯ãšãªã«äžèŽãããã¹ãŠãèŠã€ããããã«ã100001ã®ç§»åæäœã次ã®ã¬ã³ãŒãã«å®è¡ããå¿
èŠããããŸããã
以äžã®ã©ãã§ããåSELECTã®åã¯FLUSH STATUSã®å®è¡ãæå³ããåŸã¯SHOW STATUS LIKE 'handlerïŒ
'ãæå³ããŸãã
ã€ã³ããã¯ã¹ã¯ã©ã®ããã«åœ¹ç«ã¡ãŸããïŒ
ALTER TABLEã¢ã€ãã ADD INDEX height_idxïŒé«ãïŒ;
SELECT SQL_NO_CACHE COUNTïŒ*ïŒFROM items FORCE INDEXïŒheight_idxïŒWHERE height> 100;
+ ---------- +
| ã«ãŠã³ãïŒ*ïŒ|
+ ---------- +
| 16405 |
+ ---------- +
ã»ããå
ã®1è¡ïŒ0.04ç§ïŒ
ãã®å Žåãã€ã³ããã¯ã¹ã䜿çšããããããHandler_read_rnd_nextã¯0ã«ãªããŸããã
...
| Handler_read_next | 16405 |
...
ã€ãŸããã€ã³ããã¯ã¹ã䜿çšãããšãæåã«å¿
èŠãªã¬ã³ãŒãã®ã¿ãèšç®ã§ããŸãããããã§ããã¹ãŠã調ã¹ãå¿
èŠããããŸãã éæ³ã¯ãããŸãã; MySQLã¯ã¯ãšãªã«äžèŽããã¬ã³ãŒãã®æ°ãã©ãã«ãä¿åããŸããã ãããã£ãŠãã¯ãšãªæ¡ä»¶ã«äžèŽããæ°çŸäžã®ã¬ã³ãŒããããå ŽåãCOUNTã®åäœã¯éåžžã«é
ããªããŸãã
第äºã®ç¬éã LIMIT ... OFFSETã åãå®éšã 5ã€ã®ã¬ã³ãŒããèŠæ±ããŸãã
SELECT SQL_NO_CACHE * FROMã¢ã€ãã FORCE INDEXïŒheight_idxïŒWHERE height> 100 LIMIT 5;
...
ã»ããå
ã®5è¡ïŒ0.00ç§ïŒ
...
| Handler_read_next | 4 |
...
ãã¹ãŠãè«ççãªããã§ãã ãããŠã16401ããå§ãŸãä»ã®5ã€ã®ã¬ã³ãŒããè¿ãããã«ãé¡ãããŸãã
SELECT SQL_NO_CACHE * FROM items FORCE INDEXïŒheight_idxïŒWHERE height> 100 LIMIT 16400ã5;
...
ã»ããå
ã®5è¡ïŒ0.13ç§ïŒ
ãµã³ããªã³ã°æéã倧å¹
ã«å¢å ããŠããããšãããããŸãã ã¹ããŒã¿ã¹ã確èªããŸãã
...
| Handler_read_next | 16404 |
...
ã€ãŸããMySQLã¯16405ããã¹ãŠèªã¿åãããã®åŸã®ã¿äžèŠãªãã®ããã¹ãŠç Žæ£ããŸããã
ã«ãªãæ¹æ³
3ãªãæ¹æ³
ã ããã 10åã®ãšã³ããªã衚瀺ããããã²ãŒã·ã§ã³ã¡ãã¥ãŒãæç»ããå¿
èŠããããŸãã çµæãåºãããã«å¿
èŠãªèšé²ãåŸãããã«ãMySQLã¯å€ãã®äœåãªäœæ¥ãè²»ããããšã«æ°ä»ããŸããã ãããåé¿ããå¯äžã®æ¹æ³ã¯ãéžææ¡ä»¶ãå€æŽããŠãæ£ãããã®ã«ãŸã£ããé²ãããšã§ãã
ãã¹ãŠãåçŽãªäŸã§èããŠã¿ãŸããããã¬ã³ãŒããIDã§ãœãŒãããŸãã ãã®å Žåãåæ¢ããã¬ã³ãŒãã®IDããªã³ã¯ãšå
±ã«æž¡ãå¿
èŠããããŸãã ãããŠãid = 10ã®ã¬ã³ãŒãã§åæ¢ããŸãã ã€ãŸãã次ã®ããŒãžãžã®ãªã³ã¯ã®ãã©ã¡ãŒã¿ãŒã§ã¯ã10ãæž¡ãå¿
èŠããããŸãããããã£ãŠã2ããŒãžç®ã§ã¯ãèŠæ±ã¯æ¬¡ã®ããã«ãªããŸãã
SELECT SQL_NO_CACHE id from items WHERE id> 10 ORDER BY id LIMIT 10;
ã¡ãªã¿ã«ãã©ã¡ãã®å ŽåããHandler_read_nextã¯9ã«ãªããŸããã€ãŸãããªã¯ãšã¹ãã«äžèŽããæåã®ã¬ã³ãŒãã«ãžã£ã³ããïŒã€ã³ããã¯ã¹ã®ãããã§ïŒã次ã®ã¬ã³ãŒãã«9å移è¡ããŸããã æãéèŠãªããšã¯ã10ã®æ¡ä»¶ã§äœã代å
¥ããŠããSHOW STATUSã®çµæãšããŠåžžã«åããã®ã衚瀺ããããã®ãããªãªã¯ãšã¹ãã®å®è¡æéã¯çŸåšã®å Žæã«äŸåãããäŸåããã®ã¯äŸåããããšã§ãã©ã®ããããäœãéžã¶ãã
æå³ãç解ããŠãã ããã 次ã«ãããã²ãŒã·ã§ã³ã¡ãã¥ãŒãã©ããããã決ããŠãããããã«è€éãªç¶æ³ã«é²ã¿ãŸãããã urlã§nextãpreviousãlastããŒã¯ãŒãã䜿çšããŸãã ã©ã®ãããªå Žåã«ãªã³ã¯ããé²ããããæ»ããããæåŸããšè¡šç€ºãããŸããïŒ
次ã®ããŒãžïŒæ¬¡ã®ããŒãžã®ãªã¯ãšã¹ãïŒãæ¥ããã³ã«ã10åã®ã¬ã³ãŒãã§ã¯ãªãããªã¯ãšã¹ããã©ã¡ãŒã¿ã§æž¡ãããidããå§ãŸã11åã®ã¬ã³ãŒããéžæããŸãã 11åã®ã¬ã³ãŒããè¿ããå Žåã10çªç®ã®ã¬ã³ãŒãã®IDã§åæ¹ãžã®ãªã³ã¯ã衚瀺ãã11çªç®ãæããããå¿
èŠããããŸãã è¿ããããšã³ããªã11æªæºã®å Žåããã©ã¯ãŒããªã³ã¯ã¯è¡šç€ºãããŸããã åæã«ãåžžã«ïŒæ¬¡ã®å Žåã¯åžžã«ïŒéžæããã®æåã®ã¬ã³ãŒãã®IDã§ïŒåã®ïŒãªã³ã¯ã衚瀺ããŸãã ãå
é ãžãããã³ãæåŸããžã®ãªã³ã¯ã¯ããããããæ»ããããã³ãé²ãããšãšãã«åžžã«è¡šç€ºãããŸãã ã€ãŸãããæ»ããã衚瀺ããããšã«ããå Žåããæåã«ã衚瀺ããå¿
èŠããããŸãã
åã®ã¬ã³ãŒããå±ããã³ã«ïŒåã®ããŒãžãžã®ãªã¯ãšã¹ãïŒãIDããªã¯ãšã¹ãã§æå®ããããããå°ãã11ã®ã¬ã³ãŒããéžæããéé ã«ãœãŒãããŸãã åãïŒ11åã®ã¬ã³ãŒããè¿ãããå Žåããæ»ãããªã³ã¯ã衚瀺ãããŸãã åžžã«åæ¹ãªã³ã¯ã衚瀺ããŸãã
ç§ãã¯ã£ãããšæžããŠãããããšæããŸã...
ãlastããšããã¯ãšãªãåãåã£ãå Žåã¯ã©ããªããŸããïŒ ææ°ã®ãã®ããå§ããŠã10åã®ãšã³ããªã衚瀺ããã ãã§ãã ããã¯ïŒ
ã¢ã€ãã ããIDãéžæIDã§äžŠã¹æ¿ãDESC LIMIT 10;
ãŠãŒã¶ãŒã®1人ããæ°çŸããŸãã¯æ°åã®ããŒãžãç¡é§ã«ããŠãæçµçã«10ãšã³ããªã§ã¯ãªãæåã®ããŒãžãèŠã€ãããšéé£ããåããããšæããŸããïŒ ããšããããååã§ãã£ããšããŠãã圌ã¯ããŸãã«ãé·ãéæºãããšçããããšãã§ããŸã...
åã®äŸã¯åçŽã§ãIDã¯äžæã§ãã ãããããã£ãŒã«ãã§ãœãŒãããå¿
èŠãããããã®å€ãç¹°ãè¿ãããšãã§ããå Žåã¯ã©ãã§ããããïŒ ããšãã°ããã®å Žåã®é«ãã ç°¡åãªã¯ãšãªã«ãããããŒãã«ã§ã¯åé«ãã®å€ãçŽ800åçºçããããšãããããŸããã ç
§äŒãã©ã¡ãŒã¿ãŒã§æåŸã«è¡šç€ºãããé«ããæž¡ãã ãã§ã¯äžååã§ãã åãIDã圹ç«ã¡ãŸãã ã¬ã³ãŒããé«ãã§ãœãŒãããããã«é ŒãŸããŸãããããã¯åŸã§IDã§ã¬ã³ãŒãããœãŒãããã®ãæ¢ããŸãããïŒ
ãããè¡ãã«ã¯ãæ°ããã€ã³ããã¯ã¹ãå¿
èŠã§ãã
ALTER TABLE items ADD KEY height_id_idxïŒheightãidïŒ;
æåã®ããŒãžã®ãªã¯ãšã¹ãã¯æ¬¡ã®ããã«ãªããŸãã
SELECT SQL_NO_CACHE idãheight FROM items ORDER BY heightãid LIMIT 10;
ç§ã®çµæã§ã¯ãæåŸã®ãšã³ããªã®é«ãã¯0ãIDã¯1174ã§ãã ãã®ããã次ã®ããŒãžãæž¡ãå¿
èŠããããŸãã ããšãã°ãnext_0_1174ãŸãã¯next / 0/1074-å¿
èŠã«å¿ããŠã
ããã§ãé«ãã0ãã倧ããããé«ãã0ã§ãid> 1174ã®ã¬ã³ãŒããéžæããå¿
èŠããããŸãïŒãã®ããã«ãè¿œå ã®äžŠã¹æ¿ããè¡ããŸããïŒã
ããã¯ïŒ
SELECT SQL_NO_CACHE * FROMã¢ã€ãã WHEREïŒheight> 0ïŒORïŒheight = 0 AND id> 1174ïŒORDER BY heightãid LIMIT 10;
ããããªããããªã®ãã説æããå¿
èŠããªãããšãé¡ã£ãŠããŸãã ã¹ããŒã¿ã¹ã¯ãŸã 9ã¹ãããé²ãã ãã§ãã
ãããã£ãŠãä»ã®ãœãŒããè¿œå ã§ããŸãã ããšãã°ãäŸ¡æ Œãšé«ãã§ãœãŒãããããã¹ãŠã®ã¬ã³ãŒãã衚瀺ããå Žåãã¯ãšãªã¯æ¬¡ã®ããã«ãªããŸãã
SELECT SQL_NO_CACHE * FROM WHERE WHERE WHEREïŒprice> 5ïŒORïŒprice = 5 AND height> 0ïŒANDïŒprice = 5 AND height = 0 AND id> 1174ïŒORDER BYäŸ¡æ Œãé«ããid LIMIT 10;
ãã¹ãŠã®å¿
èŠãªããŒã¿ã転éãããããæ£ããåŠçãããªã¯ãšã¹ãã«ä»£å
¥ããã ãã§ãã ãããŠãã€ã³ããã¯ã¹ãå¿ããªãã§ãã ããã
4çµæã®æ°ãã©ãããã
èŠã€ãã£ãçµæã®æ°ããŠãŒã¶ãŒã«è¡šç€ºããå Žåã¯ã©ããªããŸããïŒ ç§ãã¡ã¯å€æ°ã«ã€ããŠè©±ããŠããã®ã§ã誰ããç§ãã¡ããã§ãã¯ããããšã¯ãŸããããŸããã åãGoogleã¯ãã¯ãšãªã«äžèŽãã1,000,000ããŒãžãæ€åºããããšãäŒããããšãã§ããŸããã1,000ãè¶
ããããŒãžã¯è¡šç€ºãããŸããã ç§ãã¡ããçµæã®æ°ããããã®ã¿ã«æž¡ãããšãã§ããŸãã ã©ãã§å
¥æã§ããã©ã®ããã«è©äŸ¡ããã®ã§ããïŒ ãªã¯ãšã¹ããå®è¡ããããšãå¿ããªãã§ãã ããïŒ
SELECT SQL_NO_CACHE COUNTïŒ*ïŒFROM items FORCE INDEXïŒheight_idxïŒWHERE height> 100;
ãããŠããããããŸãããïŒ
EXPLAIN SELECT SQL_NO_CACHE * FROM items FORCE INDEXïŒheight_idxïŒWHERE height> 100;
ãã®çµæã次ã®ãããªçµæãåŸãããŸãã
+ ---- + ------------- + ------- + ------- + -------------- -+ ------------ + --------- + ------ + ------- + ---------- --- +
| id | select_type | ããŒãã«| ã¿ã€ã| possible_keys | ããŒ| key_len | ref | è¡| ãšã¯ã¹ãã©|
+ ---- + ------------- + ------- + ------- + -------------- -+ ------------ + --------- + ------ + ------- + ---------- --- +
| 1 | ã·ã³ãã«| ã¢ã€ãã | ç¯å²| height_idx | height_idx | 4 | NULL | 22616 | whereã䜿çšãã|
+ ---- + ------------- + ------- + ------- + -------------- -+ ------------ + --------- + ------ + ------- + ---------- --- +
è¡ã®åã«ã¯ã衚瀺ããå¿
èŠãããæšå®ã¬ã³ãŒãæ°ã衚瀺ãããŸãã 22616ããã³16405-éãã¯ãŸã£ãã倧ãããããŸããã ã20,000ã«äžžããããšãã§ããŸãã ããŸãã ããšãã°ããµãã¯ãšãªãçµåã䜿çšãããšãEXPLAINã¯è€æ°ã®è¡ãè¿ãããšã«æ³šæããŠãã ããã ãããã¯ãã¹ãŠèªã¿åãããè¡ã®å€ã§ä¹ç®ãããå¿
èŠããããŸãã
ãããã«
ãã®åé¡ã¯ãã§ã«ããã§ç°¡åã«ã«ããŒãããŠããŸããããã®ãããªè©³çŽ°ã§ã¯ãããŸããã
èšäºã¯äºæ³ä»¥äžã«å€æããŸããããããã¹ãã¯ã¯ãšãªãšçµæã®æ¿å
¥ã§åžéãããŠããŸãã çŸæç¹ã§ã¯ãããŒã¿ã®çæã«äœ¿çšãããã¹ã¯ãªãã+ sqlãã¡ã€ã«ãé
眮ããå Žæã¯ãããŸããã äžè¬çã«...çµè«ãåºãåã¯ãããŸããã§ãã:)