ãã¨ãã°ãMySQL を使ったお手軽メッセージキュー実装 - ドワンゴ 研究開発ブログã«åºã¦ãããããªInnoDBãã¡ãã»ã¼ã¸ãã¥ã¼ã®ããã«ä½¿ã£ã¦ããã¨ãã«ããã¥ã¼ã«ã©ãã ãã¡ãã»ã¼ã¸ãæºã¾ã£ã¦ããã確èªãããã¨ããæ®éã«èããã¨COUNTããã¨æãã
SELECT COUNT(*) AS count FROM test_queue;
ãã®è»½ãæ°æã¡ã§ããCOUNTãããããã£ãããã¥ã¼ã«100ä¸ã¬ã³ã¼ãããããã£ããããã¨InnoDBã ã¨PRIMARYãã¼ç·ãªãã¨ããã¡ãã£ã¦ã¬ã¹ãã³ã¹ã«ããã0.1ç§ãããã®ããã 罪æªæã«èã¾ãããã¨ã§ãããã
ãã®ã¨ãå·éã«èããã¨ããããã¥ã¼ã1件ãå¦çããã¦ããªããã°ãidã¯auto_incrementãªã®ã§ç¹ã«ç´°å·¥ãã¦ããªããã°
SELECT MAX(id) AS count FROM test_queue;
ãããå ¨ä½ã®ã¬ã³ã¼ãæ°ã«çããã§ãããã
ãã¥ã¼ãæ¢ã«ä½ä»¶ãå¦çããã¦ããå ´åã§ããidãå°ããã»ãããé çªã«å¦çããã¦ããã®ã§
SELECT MAX(id) - MIN(id) + 1 AS count FROM test_queue;
ããã§ãå ¨ä½ã®ã¬ã³ã¼ãæ°ã«çãããªãã¯ãã§ãã
EXPLAINãè¦ã¦ã
EXPLAIN SELECT MAX(id) - MIN(id) + 1 AS count FROM test_queue\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: NULL type: NULL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: Select tables optimized away 1 row in set (0.01 sec)
Select tables optimized awayã¨åºã¦ãã ããã£ã¦ã¬ã³ã¼ãæ°ãå¢ãã¦ãä¸ç¬ã§ã¬ã¹ãã³ã¹ãè¿ã£ã¦ãã¾ãã
ããããã¦ã¼ã¹ã±ã¼ã¹ã¯å®éãªããããããªããã©ãã¨ããããã¡ã¢ã