https://offers.connpass.com/event/304025/ çºè¡¨è³æã§ã
MySQLã®DATETIMEåã¸ã¤ã³ããã¯ã¹ãã¤ããå ´å注æãå¿ è¦ã ä¾ãã°ãããªSQLã§ã¯ã¤ã³ããã¯ã¹ã使ç¨ãããªãã SELECT * FROM table WHERE datetime LIKE '2009-05-17%' æ¤ç´¢çµæã¯2009-05-17åå ¨ã¦ãæ½åºããããããã«ã¹ãã£ã³ããããã ãã®å ´åã以ä¸ã®ãããªSQLãè¯ãã SELECT * FROM table WHERE date_time BETWEEN '2009-05-17' AND '2009-05-17' + INTERVAL 1 DAY å®è¡æéè¨æ¸¬ã®ä¾ è¡æ°ï¼13,776è¡ ãã¼ã¿ï¼2,935KB ã¤ã³ããã¯ã¹ï¼datetime SELECT SUM( col ) FROM table WHERE datetime LIKE '2009-05-17%' ãã«ã¹ãã£ã³ å®è¡æéï¼0.0235 ç§ SELEC
æåååã«ã©ã ï¼varcharåãcharåãªã©ï¼ã«å¯¾ãã¦ã¤ã³ããã¯ã¹ãä½æããå ´åã«æ大ãã¼é·ããããããã¯ãã¤ãæ°ã§ç®¡çããã¦ãã¾ããä»åã¯ããã¤ãã®ãªãã·ã§ã³ããã©ã¡ã¼ã¿ããInnoDBã®ã¤ã³ããã¯ã¹ã®æ大ãã¼é·ã«å¯¾ãã¦ã©ã®ããã«å½±é¿ããããç´¹ä»ãã¾ãã InnoDBã®ãã¡ã¤ã«ãã©ã¼ãããã«ããã¤ã³ããã¯ã¹ã®æ大ãã¼é·ã®éã åºæ¬çã«ã¯åä¸ã«ã©ã ã¤ã³ããã¯ã¹ã®æ大ãã¼é·ã¯767ãã¤ãã¾ã§ä½æã§ãã¾ããç¹å®ã®æ¡ä»¶ã§ã¯ã¤ã³ããã¯ã¹ã®æ大ãã¼é·ã3072ãã¤ãã¾ã§æ¡å¼µãããã¨ãã§ãã¾ãããã®æ¡ä»¶ã¯ä»¥ä¸ã®ã¨ããã§ãã ãã¼ãã«ä½ææã«è¡ãã©ã¼ããããDYNAMICã¾ãã¯COMPRESSEDã«æå®ããã innodb_file_per_tableãã©ã¡ã¼ã¿ãONã«è¨å®ãã¦ããã¼ãã«ãã¼ã¿ãåå¥ã®ibdãã¡ã¤ã«ã«æ ¼ç´ããããã«ããã innodb_large_prefixãã©ã¡ã¼ã¿ãæå¹ã«ããã
ã¤ã³ããã¯ã¹ã使ãã¨ä½æ æ©ããªãã®ã æ¬ãã¼ã¸ã§ã¯ORACLEã«ããã¦ã¤ã³ããã¯ã¹ã使ãã¨ä½æ ã¯ã¨ãªãæ©ããªãã®ããã¾ãã¯ãªãéããªããªãã®ãã説æãã¾ãã ãªããæ¬ãã¼ã¸ã§ã¯ç´¢å¼æ§æ表ãããããããã¤ã³ããã¯ã¹çã®ç¹æ®ãªãã¼ã¿æ§é ãæã¤ãªãã¸ã§ã¯ãã¯èæ ®ãã¦ãããä¸è¬çãªãã¼ãã«ã¨ã¤ã³ããã¯ã¹ãæ³å®ãã¦ãã¾ãã åæç¥è ã¤ã³ããã¯ã¹ã使ãã¨ãªãéããªãã®ããç解ããããã«ã¯ä»¥ä¸ã®åæãç解ãã¦ããå¿ è¦ãããã¾ãã ã»ãã£ã¹ã¯ã¢ã¯ã»ã¹ã¯CPUã¢ã¯ã»ã¹ã¨æ¯è¼ããã¨é常ã«é ã ä¸è¬çã«ã¯CPUå¦çæéãããç§åä½ã§ããã®ã«å¯¾ãã¦ãã£ã¹ã¯ã®ã¢ã¯ã»ã¹æéã¯ããªç§åä½ã§ããããã£ã¹ã¯ã¯CPUã«æ¯ã¹ã¦100ä¸åç¨åº¦å¦çãé ãã¨ããã¦ãã¾ãã ãããã£ã¦ãã¯ã¨ãªã®å¦çæéã¯ãã£ã¹ã¯ã¢ã¯ã»ã¹ãæãå°ãªãå®è¡è¨ç»ãæãé«éã¨ãªãå¯è½æ§ãé«ãã¨èãããã¾ãã ã»I/Oã®åä½ã¯ãããã¯ã§ãã ORACLEã®I
ãã®è¨äºã¯ããBASE Advent Calendar 2018ãã®9æ¥ç®ã®è¨äºã§ãã devblog.thebase.in åæ¥ã¯ id:match_1 ã§ãããããã«ã¡ã¯ã10æ¥ã»ã©åã«ãã¼ã¿ãã¼ã¹ç§»è¡ã«ã¤ãã¦ã®è¨äºãæ¸ããã¦ããã ããæ¤æ¨ã§ãã ä»åã¯ã¤ã³ããã¯ã¹ã¨Btreeã®ããã©ã¼ãã³ã¹ãã¥ã¼ãã³ã°ç³»ã®ã話ããããã¨æãã¾ãã æ¦è¦ Btreeã®æ§é ãªã©ã説æãã¦ããè³æã¯ä»ã«ãããããSQLã®ã¢ã³ããã¿ã¼ã³ãããç¨åº¦ãããã©ãä½æ ã¢ã³ããã¿ã¼ã³ã«ãªãã®ããªã©æ§é ãç解ããªããã®èª¬æãç¡ãããã«æã£ã¦ãã¾ãããä»åã¯Btreeã®æ§é ã説æããªãããã©ã®ããã«åãã¦ããã©ã¼ãã³ã¹ã«å½±é¿ãã¦ããã®ãã説æããããã¨æãã¾ããSQLã®ããã©ã¼ãã³ã¹ãã¥ã¼ãã³ã°ã¯Btreeã®æ§é ãç解ãã¦ãããã©ããã§å ¨ãéããã®ã«ãªãã¾ããæ§é ãæèãã¤ã¤ã誤解ããããã¨ãããä½æ ãã®å¦çã§ã¤ã³ããã¯
InnoDBã¯ã¯ã©ã¹ã¿ã¤ã³ããã¯ã¹ã¨ããæ§é ã«ãªã£ã¦ãããä»æ¥ã¯ã¯ã©ã¹ã¿ã¤ã³ããã¯ã¹ãã©ããããã¨ãã¨ãããã¨ããçããã«ç解ãã¦é ãããããã£ã¨ãç解ãã¦é ããããã¤ã³ãã¯ãã»ã«ã³ããªã¤ã³ããã¯ã¹ã®ãªã¼ããã¼ãã«ã¯ä¸»ãã¼ã®å¤ãå«ã¾ãã¦ãããã¨ãããã¨ã ã 主ãã¼ã®æ§é InnoDBã®ä¸»ãã¼ã¯æ¬¡ã®å³ã®ããã«ããã¼ã¿ã主ãã¼ã®ãªã¼ããã¼ãã«å«ã¾ãããã¨ããæ§é ã«ãªã£ã¦ããããã®ãããªæ§é ãã¯ã©ã¹ã¿ã¤ã³ããã¯ã¹ã¨ããã ãã®ãããªæ§é ã«ãªã£ã¦ãããã¨ã«ã¯å©ç¹ã¨æ¬ ç¹ããããã大ããªå©ç¹ã¯ä¸»ãã¼ã®å¤ã§æ¤ç´¢ãããã¨é常ã«é«éã ã¨ãããã¨ã ã主ãã¼ã®ãªã¼ããã¼ãã«ãã©ãçããã¨ãã«ã¯ãæ¢ã«ãã¼ã¿ã®ãã§ãããå®äºãã¦ããããã¼ã¿ã¨ã¤ã³ããã¯ã¹ãå¥ã ã«æ ¼ç´ããã¦ããã¿ã¤ãã®ã¹ãã¬ã¼ã¸ã¨ã³ã¸ã³ã§ã¯ãã¤ã³ããã¯ã¹ãããã¼ã¿ã®ä½ç½®ãèªã¿åã£ã¦ããã®å¾ãã¼ã¿ãã¡ã¤ã«ãããã¼ã¿ããã§ããããããã®ããã«äºæ®µéã®æä½ã
å»å¹´ã½ã¼ãã«é¢ããè¨äºãæ¸ããããä»æ¥ã¯ãã®ç¶ãã§ããã MySQLã§EXPLAIN SELECT...ãå®è¡ããã¨Extraãã£ã¼ã«ãã§ããè¦ããããUsing filesortãã¨ããæååãFilesortã£ã¦ä¸ä½ãªãã ããï¼ã¨æã£ããã¨ã¯ãªãã ããããååç´å ¥ã«è¨ã£ã¦Filesortã®æ£ä½ã¯ã¯ã¤ãã¯ã½ã¼ãã§ããã ã¯ã¨ãªã«ORDER BYãå«ã¾ããå ´åãMySQLã¯ããç¨åº¦ã®å¤§ããã¾ã§ã¯å ¨ã¦ã¡ã¢ãªå ã§ã¯ã¤ãã¯ã½ã¼ããå¦çãããããç¨åº¦ã®å¤§ããã¨ã¯sort_buffer_sizeã§ãããããã¯ã»ãã·ã§ã³ãã¨ã«å¤æ´å¯è½ã§ãããã½ã¼ãã«å¿ è¦ãªã¡ã¢ãªãsort_buffer_sizeãã大ãããªãã¨ããã³ãã©ãªãã¡ã¤ã«ï¼ãã³ãã©ãªãã¼ãã«ã§ã¯ãªãï¼ãä½æãããã¡ã¢ãªã¨ãã¡ã¤ã«ãä½µç¨ãã¦ã¯ã¤ãã¯ã½ã¼ããå®è¡ãããã Filesortã¯å ¨ã¦ã®ã½ã¼ãå¦çã«ããã¦å®è¡ãããããã§ã¯ãªããååã®è¨äº
ããã«ã¡ã¯ããµã¼ãã¹éçºé¨ã®èå¼ (@a_bicky) ã§ãã çªç¶ã§ãããRDBMS ã®æ¢åã®ãã¼ãã«ãè¦ã¦ã¿ãããä½ã§ãããªã«ã¤ã³ããã¯ã¹ã ãããªã®ï¼ãã¿ãããªçµé¨ã¯ããã¾ãããï¼ä¸è¦ãªã¤ã³ããã¯ã¹ã¯å®¹éãå§è¿«ããããæ¿å ¥ãé ããªã£ããã¨è¯ããã¨ãããã¾ããã ãããªããã§ãä»åã¯ã¬ã³ã¼ããæ¤ç´¢ããããã«å¿ è¦ãªã¤ã³ããã¯ã¹ã®åºç¤ç¥èã¨ãããè¦ãããä¸é©åãªã¤ã³ããã¯ã¹ã«ã¤ãã¦è§£èª¬ãã¾ããã¯ãã¯ãããã§ã¯ Rails ã®ãã¼ã¿ãã¼ã¹ã¨ãã¦ä¸»ã« MySQL 5.6ãMySQL ã®ã¹ãã¬ã¼ã¸ã¨ã³ã¸ã³ã¨ãã¦ä¸»ã« InnoDB ã使ã£ã¦ããã®ã§ãMySQL 5.6 ã® InnoDB ã«ã¤ãã¦è§£èª¬ãã¾ãã InnoDB ã®ã¤ã³ããã¯ã¹ã«é¢ããåºç¤ç¥è ã¤ã³ããã¯ã¹ã®æ§é (B+ æ¨) InnoDB ã§ã¯ B+ æ¨ã使ããã¦ãã¾ããB+ æ¨ã¯æ¬¡ã®ãããªç¹å¾´ãæã£ãæ¨æ§é ã§ãã 次æ°ã b ã¨ããã¨ã
"Nested Loop Joinããåãä¸ãã¦ç¡ãã®ã«ã¿ã¤ãã«ã大ããããªãã" ã¨ææãé ããã®ã§ãã¿ã¤ãã«ãä¿®æ£ãã¾ãããMerge Joinã¨Hash Joinã®ãã¨ã¯ã¾ãä»åº¦æ¸ããã¨æãã¾ãã ãJOINã¯é ããã¨ããè¨ããã¾ããç¹ã«RDBã使ãå§ãã¦éããªãå ã«ããããè¨èª¬ã«è§¦ããçµæãJOIN=æªãã¨ããèªèã§åºå®åããã¦ãã¾ã£ã¦ãã人ãå¤ãããã«æãã¦ãã¾ãã ãããã«ãJOINãå«ããããªSELECTæã¯ãå«ã¾ãªããã®ã«æ¯ã¹ã¦éãããªãå¾åããããã¨ã¯äºå®ã§ããã¾ããæ¬è³ªçã«åãåããããå 容ãè¤éã§ã対å¦ãããã¨ãé£ãããã®ãåå¨ãã¾ããããããRDBã®ä¸ã§ä¸ä½ã©ããããã¨ãèµ·ãã¦ããã®ããç¥ããããã«åºãã¦å¯¾å¦ããã°é«éåã§ãããã¨ãå°ãªããªãã¨èãã¦ãã¾ãã æ¬ç¨¿ã§ã¯ãJOINã®å é¨åä½ã解説ããä¸ã§ãWebãµã¼ãã¹ãä½ã£ã¦ããã¨ããåºã¦ããJOIN SQLãä¾é¡ã«
â»ãã®å 容ã¯å人çãªèå¯ãªã®ã§ãééã£ã¦ããç®æãããã¨æãã¾ããããããé¨åãè¦ã¤ããéã¯ãã²æãã¦ä¸ããã RDBMSã®æ¤ç´¢ãæ©ãããããã«Indexã£ã¦ä½¿ãã¾ããããä¾ãã°ãããªãã¼ãã« CREATE TABLE user ( id INT UNSIGNED NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX (id) ); idã«ã©ã ã«Indexãå¼µã£ã¦ãã¾ããããã¯idã§ã®æ¤ç´¢ãé«éã«ããããã§ããããã§idã«ã©ã ã«Indexãè²¼ã£ã¦ããªãå ´åã¨æ¯ã¹ãã¨æ¤ç´¢æéãå¤§å¹ ã«å¤ãã£ã¦ãã¦ãã¾ãã¾ãï¼ç¹ã«ã¬ã³ã¼ããå¤ããªã£ãæ) ã§ã¯ãªãIndexãè²¼ãã¨æ¤ç´¢ãæ©ããªããã§ãããï¼ï¼ Indexã¨ã¯ãã®åã®éãç´¢å¼ãæå³ãã¾ããç¹å®ã®ã«ã©ã ã®ç´¢å¼ãä½æãã¦ãããã¨ã§æ¤ç´¢ãé«éåãã¾ãã (æ¬ã®æå¾ã«ãã¿ããªé ã§åèªã並ã¹ãããããã¦ãã¾ã
B-treeãMySQLã§ä½¿ç¨ããã¦ããèæ¯ãããB-treeã¤ã³ããã¯ã¹ã®æ§é ãããã¦ããã«åºã¥ããã¤ã³ããã¯ã¹ã®ä½¿ç¨æ¹æ³ã®å ¥éç·¨ã§ãã以ä¸ã®æµãã«æ²¿ã£ã¦ã¾ã¨ãã¦ããã¾ãã ã¤ã³ããã¯ã¹ã£ã¦ãªã«ï¼ B-treeã£ã¦ãªãã§ã¤ã³ããã¯ã¹ã«ä½¿ããã¦ããã®ï¼ B-treeã¤ã³ããã¯ã¹ã®æ§é ã¤ã³ããã¯ã¹ã®ä½¿ç¨æ¹æ³ â» åå¼·ãããã¦ã¾ã¨ãã¦ãããã¨ããããééã£ã¦ããç®æããããã¾ãããæãã¦ããã ããã¨å¬ããã§ãã ã¤ã³ããã¯ã¹ã£ã¦ãªã«ï¼ å ¨ä½ã®å 容ã®ä¸ããç¹å®é¨åãæ¢ãããã«ä½¿ç¨ãããæ¬ã®ç´¢å¼ã®ãããªæ¦å¿µã®ãã¨ã§ãããããç¨ãããã¨ã§ãæ¤ç´¢ãé«éåãããã¨ãã§ãã¾ãã ç¹å®ã®é ç®ãæ¬ã®ã©ãã«è¼ã£ã¦ãããã確èªããããã«ç´¢å¼ã調ã¹ããã¨ã§ãå ¨ãã¼ã¸ãé ã«èª¿ã¹ãªãã¦ãããã®é ç®ãç»å ´ãããã¼ã¸çªå·ãããã MySQLã®ã¹ãã¬ã¼ã¸ã¨ã³ã¸ã³ã§ããã¤ã³ããã¯ã¹ãåæ§ã®æ¹æ³ã§å©ç¨ããã¦ãããã¤ã³ããã¯ã¹ã®
â ã¤ã³ããã¯ã¹ã¨ã¯ ãã¼ã¿ãã¼ã¹ã®ä¸çã§ãã¤ã³ããã¯ã¹(ç´¢å¼)ã¨ã¯ãã¼ãã«ã«æ ¼ç´ããã¦ãããã¼ã¿ã é«éã«åãåºãçºã®ä»çµã¿ãæå³ãã¾ãã ã¤ã³ããã¯ã¹ãé©åã«ä½¿ç¨ãããã¨ã«ãã£ã¦SQLæã®å¿çæéãåçã«æ¹å ãããå¯è½æ§ãããã¾ãã ã¤ã³ããã¯ã¹ã«ã¯B-Treeã¤ã³ããã¯ã¹ãã¯ãããããããããã¤ã³ããã¯ã¹ã é¢æ°ã¤ã³ããã¯ã¹ãªã©ã®ç¨®é¡ãããã¾ãããããã§ã¯æãä¸è¬çã«ä½¿ãããã㤠ã»ã¨ãã©ã®DBMSã§ãµãã¼ãããã¦ããB-Treeã¤ã³ããã¯ã¹ã«ã¤ãã¦è§£èª¬ãã¾ãã â» CREATE INDEXæã§ãªãã·ã§ã³ãæå®ããªãå ´åã¯é常B-Treeã¤ã³ããã¯ã¹ã ä½æããã¾ãã â B-Treeã¤ã³ããã¯ã¹ã®ããã¿ B-Tree(Balanced Tree)ã¤ã³ããã¯ã¹ã¯æ¬¡ã®ãããªããªã¼ç¶ã®æ§é ã«ãªã£ã¦ãã¾ãã ããªã¼ã®å é ã¯ããããããã¯ã¨å¼ã°ãã¦ãã¾ããããããããã¯ã§ã¯ããã¼å¤ã® ç¯å²
CREATE TABLE ãã¼ãã«å(ã«ã©ã å å,ã«ã©ã å å, ... INDEX(ã«ã©ã å); ã¾ã㯠CREATE INDEX ã¤ã³ããã¯ã¹å ON ãã¼ãã«å(ã«ã©ã å); ã¾ã㯠ALTER TABLE ãã¼ãã«å ADD INDEX ã¤ã³ããã¯ã¹å(ã«ã©ã å); æ¤ç´¢ãããã¼ã¿éãå¤ãå ´åãªã©ãæ¤ç´¢å¯¾è±¡ã®ã«ã©ã ã«ã¤ã³ããã¯ã¹ãä½æãã¦ããã¾ãããã ã¤ã³ããã¯ã¹ãä½æããªãã¨ãæ¤ç´¢å¯¾è±¡ã®ã«ã©ã ã®ãã¼ã¿ãå ¨é¨æ¤ç´¢ããããããã¼ã¿ãã¼ã¹ã«è² æ ããããããè½ã¡ããã¨ãããã¾ãã ã¤ã³ããã¯ã¹ãä½æãã¦ããã°ãã«ã©ã ã«è¨å®ãããã¤ã³ããã¯ã¹ãæ¤ç´¢ããã®ã§ããã¼ã¿ãã¼ã¹ã®è² æ ã軽æ¸ãããªããã¤æ¤ç´¢å¹çãã¢ãããã¾ãã 主ãã¼ãè¨å®ããã«ã©ã åã«ã¯ãèªåçã«ã¤ã³ããã¯ã¹ãä½æããã¾ãã®ã§ãããã¦ä½æããå¿ è¦ã¯ããã¾ããã ãã¼ã¿ãã¼ã¹ä½ææã®CREATE TABLEæã§ã¤ã³ããã¯ã¹ä½æ
ã©ã³ãã³ã°
ã©ã³ãã³ã°
ã©ã³ãã³ã°
ãªãªã¼ã¹ãé害æ å ±ãªã©ã®ãµã¼ãã¹ã®ãç¥ãã
ææ°ã®äººæ°ã¨ã³ããªã¼ã®é ä¿¡
å¦çãå®è¡ä¸ã§ã
j次ã®ããã¯ãã¼ã¯
kåã®ããã¯ãã¼ã¯
lãã¨ã§èªã
eã³ã¡ã³ãä¸è¦§ãéã
oãã¼ã¸ãéã
{{#tags}}- {{label}}
{{/tags}}