{"id":898,"date":"2021-02-10T06:28:28","date_gmt":"2021-02-09T21:28:28","guid":{"rendered":"http:\/\/conchan.akita.jp\/blog\/?p=898"},"modified":"2021-02-09T18:22:01","modified_gmt":"2021-02-09T09:22:01","slug":"mysql-index","status":"publish","type":"post","link":"https:\/\/conchan.akita.jp\/blog\/mysql-index\/","title":{"rendered":"\u5b9f\u8df5\uff01MySQL\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b"},"content":{"rendered":"\n<p class=\"has-black-color has-text-color\">\u3053\u3093\u306b\u3061\u306f\u3002<br>\u30b3\u30f3\u3061\u3083\u3093\u3053\u3068\u4f50\u3005\u6728\u3067\u3059\u3002<br>MySQL\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u77e5\u898b\u3092\u5f97\u305f\u306e\u3067\u66f8\u304d\u4e0b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">DB\u9ad8\u901f\u5316\u306e\u305f\u3081\u306b\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u306e\u304c\u6709\u52b9\u3067\u3059\u3002<br>\u7d19\u306e\u8f9e\u66f8\u306b\u306f\u300c\u3042\u3067\u59cb\u307e\u308b\u5358\u8a9e\u306f\u3053\u3053\u304b\u3089\u300d\u300c\u3044\u3067\u59cb\u307e\u308b\u5358\u8a9e\u306f\u3053\u3053\u304b\u3089\u300d\u300c\u3046\u3067\u59cb\u307e\u308b\uff5e\u300d\u3068\u3044\u3046\u3088\u3046\u306a\u7d22\u5f15\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u3092\u7528\u610f\u3059\u308b\u306e\u3068\u540c\u3058\u611f\u3058\u3002<br>\u691c\u7d22(SELECT\u6587)\u304c\u65e9\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u30c6\u30fc\u30d6\u30eb\u3054\u3068\u306b\u7528\u610f\u3057\u307e\u3059\u3002<br>User\u30c6\u30fc\u30d6\u30eb\u306fuser_id\u30ab\u30e9\u30e0\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u3001\u3068\u3044\u3063\u305f\u3088\u3046\u306b\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u6ce8\u610f\u70b9\u3067\u3059\u304c\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u60c5\u5831\u3092\u6301\u3064\u305f\u3081\u306bDB\u306e\u5bb9\u91cf\u3092\u4f7f\u3046\u306f\u305a\u306a\u306e\u3067\u3001\u5fc5\u8981\u306a\u3082\u306e\u3060\u3051\u306b\u5f35\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u5b9f\u884c\u74b0\u5883\u306f\u4ee5\u4e0b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"has-inline-color has-black-color\">MySQL8.0\u7cfb<\/span><\/li><li><span class=\"has-inline-color has-black-color\">Python3.7\u7cfb<\/span><\/li><li><span class=\"has-inline-color has-black-color\">SQLAlchemy1.3\u7cfb<\/span><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"has-medium-font-size wp-block-heading\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u304b\u3069\u3046\u304b\u306e\u691c\u8a0e\u624b\u9806<\/h2>\n\n\n\n<p class=\"has-black-color has-text-color\">\u9045\u3044\u30af\u30a8\u30ea\u3092\u898b\u3064\u3051\u3001\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u3067\u6539\u5584\u691c\u8a3c\u3057\u3001\u672c\u756a\u5b9f\u65bd\u306e\u6d41\u308c\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"has-normal-font-size wp-block-heading\">1. <span class=\"has-inline-color has-black-color\">DB\u306b\u98db\u3093\u3067\u3044\u308bSELECT\u30af\u30a8\u30ea\u6587\u306e\u628a\u63e1<\/span><\/h3>\n\n\n\n<p class=\"has-black-color has-text-color\">\u4ee5\u4e0b\u306e\u624b\u6bb5\u3067\u628a\u63e1\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"has-inline-color has-black-color\">DB\u306b\u30af\u30a8\u30ea\u3092\u98db\u3070\u3057\u3066\u3044\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30b3\u30fc\u30c9\u3092\u8aad\u3080<\/span><\/li><li><span class=\"has-inline-color has-black-color\">ORMapper(\u4ee5\u4e0bORM)\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306fORM\u306e\u30c7\u30d0\u30c3\u30b0\u51fa\u529b\u7b49\u3092ON\u306b\u3057\u3066ORM\u304c\u4f5c\u308b\u30af\u30a8\u30ea\u6587\u3092\u51fa\u529b\u3055\u305b\u308b<\/span><ul><li><span class=\"has-inline-color has-black-color\">Python\u3067ORM\u3067\u3042\u308bSQLAlchemy\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u30ed\u30b0\u30ec\u30d9\u30eb\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u306eWARN\u304b\u3089INFO\u306b\u8a2d\u5b9a\u5909\u66f4\u3059\u308b\u3068\u51fa\u529b\u3055\u308c\u308b<\/span><\/li><li><span class=\"has-inline-color has-black-color\"><a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/engines.html#configuring-logging\">https:\/\/docs.sqlalchemy.org\/en\/14\/core\/engines.html#configuring-logging<\/a><\/span><\/li><\/ul><\/li><li><span class=\"has-inline-color has-black-color\">\u30b9\u30ed\u30fc\u30af\u30a8\u30ea\u30ed\u30b0\u3092\u51fa\u529b\u3055\u305b\u78ba\u8a8d\u3059\u308b<\/span><ul><li><span class=\"has-inline-color has-black-color\">MySQL\u81ea\u4f53\u304c\u6301\u3063\u3066\u3044\u308b\u6a5f\u80fd\u3067\u3001DB\u306b\u5bfe\u3057\u3066\u8a2d\u5b9a\u7528\u30af\u30a8\u30ea\u3092\u98db\u3070\u3059\u304b\u3001AWS\u306eRDS\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u306a\u3089\u30de\u30cd\u30b8\u30e1\u30f3\u30c8\u30b3\u30f3\u30bd\u30fc\u30eb\u304b\u3089(\u307b\u307c)GUI\u3067\u8a2d\u5b9a\u3067\u304d\u308b<\/span><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"has-black-color has-text-color\">\u3069\u306e\u30af\u30a8\u30ea\u306e\u901f\u5ea6\u3092\u6539\u5584\u3057\u305f\u3044\u304b\u3092\u6c7a\u3081\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"has-normal-font-size wp-block-heading\">2. \u5b9f\u884c\u8a08\u753b\u3092\u898b\u308b<\/h3>\n\n\n\n<p class=\"has-black-color has-text-color\">\u5148\u306e\u30af\u30a8\u30ea\u306e\u5148\u982d\u306b <code>EXPLAIN<\/code> \u3092\u4ed8\u4e0e\u3059\u308b\u3068\u5b9f\u884c\u8a08\u753b\u306a\u308b\u3082\u306e\u3092\u898b\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u4f8b\u3048\u3070\u3001<br><code>SELECT * FROM user WHERE user_id=1;<\/code><br>\u3068\u3044\u3046\u30af\u30a8\u30ea\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u898b\u308b\u306a\u3089\u3001<br><code>EXPLAIN SELECT * FROM user WHERE user_id=1;<\/code><br>\u3068\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u4ee5\u4e0b\u306f\u3001\u79c1\u304c\u5b9f\u969b\u306b\u898b\u305fEXPLAIN\u7d50\u679c\u3092\u3061\u3087\u3044\u3068\u307c\u304b\u3057\u305f\u3082\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"scroll\">\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>id<\/th><th>select_type<\/th><th>table<\/th><th>partitions<\/th><th>type<\/th><th>possible_keys<\/th><th>key<\/th><th>key_len<\/th><th>ref<\/th><th>rows<\/th><th>filtered<\/th><th>Extra<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>PRIMARY<\/td><td>a<\/td><td>NULL<\/td><td>ALL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>1383610<\/td><td>100.00<\/td><td>Using where<\/td><\/tr><tr><td>1<\/td><td>PRIMARY<\/td><td>t<\/td><td>NULL<\/td><td>eq_ref<\/td><td>PRIMARY<\/td><td>PRIMARY<\/td><td>8<\/td><td>hogedb.a.huga_id<\/td><td>1<\/td><td>10.00<\/td><td>Using where<\/td><\/tr><tr><td>1<\/td><td>PRIMARY<\/td><td><\/td><td>NULL<\/td><td>ref<\/td><td><\/td><td><\/td><td>15<\/td><td>hogedb.a.huga_id2,hogedb.a.fuga_id3<\/td><td>10<\/td><td>100.00<\/td><td>Using index<\/td><\/tr><tr><td>2<\/td><td>DERIVED<\/td><td>\u30c6\u30fc\u30d6\u30eb\u540d<\/td><td>NULL<\/td><td>ALL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>1383610<\/td><td>11.11<\/td><td>Using where; Using temporary<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u3056\u3063\u304f\u308a\u78ba\u8a8d\u3059\u308b\u306a\u3089\u4ee5\u4e0b3\u3064\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"has-inline-color has-black-color\">keys: \u30af\u30a8\u30ea\u5b9f\u884c\u6642\u306b\u9078\u629e\u3055\u308c\u305f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d\u304c\u5165\u308b\u306e\u3067\u3053\u3053\u304cNULL\u3060\u3068\u6539\u5584\u4f59\u5730\u3042\u308a<\/span><\/li><li><span class=\"has-inline-color has-black-color\">rows: \u30af\u30a8\u30ea\u5b9f\u884c\u6642\u306b\u691c\u7d22\u3059\u308b\u884c\u6570(\u304a\u304a\u307e\u304b\u306a\u3082\u306e)\u306a\u306e\u3067\u3053\u3053\u304c\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u884c\u6570\u306b\u8fd1\u304b\u3063\u305f\u308a\u3059\u308b\u3068\u6539\u5584\u4f59\u5730\u3042\u308a<\/span><\/li><li><span class=\"has-inline-color has-black-color\">Extra: Using index\u304c\u306a\u3044\u5834\u5408\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4f7f\u308f\u308c\u3066\u3044\u306a\u3044(\u3042\u308b\u3044\u306f\u5f35\u3089\u308c\u3066\u3044\u306a\u3044)\u306e\u3067\u6539\u5584\u4f59\u5730\u3042\u308a<\/span><\/li><\/ul>\n\n\n\n<p class=\"has-black-color has-text-color\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5f35\u3089\u308c\u3066\u3044\u308b\/\u3044\u306a\u3044 \u3060\u3051\u78ba\u8a8d\u3057\u305f\u3044\u5834\u5408\u306f<br><code>SHOW INDEX FROM \u30c6\u30fc\u30d6\u30eb\u540d<\/code> \u3067OK\u3067\u3059\u3002<br>\u3053\u308c\u3082\u7d50\u679c\u4f8b\u3092\u8f09\u305b\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"scroll\">\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Table<\/th><th>Non_unique<\/th><th>Key_name<\/th><th>Seq_in_index<\/th><th>Column_name<\/th><th>Collation<\/th><th>Cardinality<\/th><th>Sub_part<\/th><th>Packed<\/th><th>Null<\/th><th>Index_type<\/th><th>Comment<\/th><th>Index_comment<\/th><th>Visible<\/th><th>Expression<\/th><\/tr><\/thead><tbody><tr><td>\u30c6\u30fc\u30d6\u30eb\u540d<\/td><td>0<\/td><td>PRIMARY<\/td><td>1<\/td><td>id<\/td><td>A<\/td><td>1383610<\/td><td>NULL<\/td><td>NULL<\/td><td><\/td><td>BTREE<\/td><td><\/td><td><\/td><td>YES<\/td><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"has-normal-font-size wp-block-heading\"><span class=\"has-inline-color has-black-color\">3. \u73fe\u72b6\u306e\u6642\u9593\u8a08\u6e2c<\/span><\/h3>\n\n\n\n<p class=\"has-black-color has-text-color\">Workbench\u3084Sequel Ace\u7b49\u306eDB\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u30af\u30a8\u30ea\u3092\u98db\u3070\u3059\u3068\u5b9f\u884c\u6642\u9593\u304c\u51fa\u308b\u306e\u3067\u305d\u308c\u3092\u30e1\u30e2\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br>\u66f8\u304d\u306a\u304c\u3089\u601d\u3063\u305f\u3051\u3069\u3001\u5225\u306b\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u4f7f\u308f\u305a\u76f4\u306b\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u30af\u30a8\u30ea\u98db\u3070\u3057\u3066\u3082\u5b9f\u884c\u6642\u9593\u51fa\u307e\u3059\u306d\u2026\u3002<br>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u524d\u5f8c\u3067\u30af\u30a8\u30ea\u5b9f\u884c\u7d50\u679c\u306b\u5dee\u7570\u304c\u306a\u3044\u3053\u3068\u3082\u78ba\u8a8d\u3057\u305f\u3044\u5834\u5408\u306f\u3053\u3053\u3067\u5b9f\u884c\u7d50\u679c\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"has-normal-font-size wp-block-heading\"><span class=\"has-inline-color has-black-color\">4. \u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b<\/span><\/h3>\n\n\n\n<p class=\"has-black-color has-text-color\">\u3044\u304d\u306a\u308a\u672c\u756a\u74b0\u5883\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u3089\u305a\u306b\u3001\u307e\u305a\u306f\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u3067\u691c\u8a3c\u3057\u307e\u3057\u3087\u3046\u3002<br>\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u304c\u672c\u756a\u74b0\u5883\u3068\u30c7\u30fc\u30bf\u91cf\u304c\u540c\u3058\u3067\u3042\u308c\u3070\u3042\u308b\u307b\u3069\u691c\u8a3c\u304c\u6b63\u78ba\u3067\u5b09\u3057\u3044\u3067\u3059\u306d\u3002<br>\u672c\u756a\u74b0\u5883\u306e\u5f53\u8a72\u30c6\u30fc\u30d6\u30eb\u306b1\u5104\u30ec\u30b3\u30fc\u30c9\u3042\u308b\u306e\u306b\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u3060\u30681000\u30ec\u30b3\u30fc\u30c9\u3001\u3060\u3068\u305d\u3082\u305d\u3082\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u5f35\u3089\u305a\u3068\u3082\u65e9\u305d\u3046\u3067\u3059\u3057\u2026\u3002<br>\u30af\u30a8\u30ea\u6587\u306b\u672c\u756a\u56fa\u6709\u306e\u60c5\u5831\u304c\u5165\u3063\u3066\u3044\u308b\u5834\u5408(WHERE\u53e5\u3067user_id=1000000\u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u304c\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u306b\u305d\u3093\u306a\u30c7\u30fc\u30bf\u304c\u306a\u3044\u3068\u304b)\u306f\u3001\u305d\u3053\u3060\u3051\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u7528\u306b\u66f8\u304d\u63db\u3048\u308b\u306a\u3069\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u306b\u306f\u3001\u307e\u305a\u3069\u306e\u30ab\u30e9\u30e0\u306b\u5f35\u308b\u304b\u3092\u6c7a\u3081\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\u5f53\u8a72\u30af\u30a8\u30ea\u306eWHERE\u53e5\u3067\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u30ab\u30e9\u30e0\u306b\u5f35\u308b\u306e\u304c\u826f\u3044\u307f\u305f\u3044\u3067\u3059\u3002<br><code>SELECT * FROM user WHERE user_id=1;<\/code> \u3060\u3068user_id\u306b\u5f35\u308b\u3001\u3068\u3044\u3063\u305f\u611f\u3058\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">CREATE INDEX\u6587\u3001\u307e\u305f\u306fALTER TABLE\u6587\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u5148\u306e\u4f8b\u3067\u3044\u304f\u3068\u3001<br><code>ALTER TABLE user ADD INDEX user_id_index(index_id)<\/code> \u3068\u3044\u3063\u305f\u611f\u3058\u3002<br>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d\u3092\u6c7a\u3081\u3066\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u3042\u3068\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u3060\u3051\u3067\u3059\u304c\u3001\u30df\u30b9\u3063\u305f\u6642\u3084\u52b9\u679c\u306a\u304b\u3063\u305f\u6642\u306b\u3059\u3050\u623b\u305b\u308b\u3088\u3046\u306b\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u5f35\u3063\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<br><code>begin;<\/code><br>\u3055\u3089\u306b\u3001\u81ea\u52d5\u30b3\u30df\u30c3\u30c8\u6a5f\u80fd\u3082\u5207\u3063\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<br><code>set autocommit = 0;<\/code><br>\u5207\u308c\u305f\u304b\u3069\u3046\u304b\u306e\u78ba\u8a8d\u306f<br><code>show variables like 'autocommit';<\/code><br>\u3067\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u3053\u3053\u307e\u3067\u304d\u305f\u3089\u3001\u5148\u307b\u3069\u306eALTER TABLE\u6587\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308a\u307e\u3057\u3087\u3046\uff01<br>SHOW INDEX\u6587\u3067\u306e\u78ba\u8a8d\u3082\u5fd8\u308c\u305a\u306b\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u5f35\u308c\u305f\u3089\u3001EXPLAIN\u3067\u5b9f\u884c\u8a08\u753b\u3092\u78ba\u8a8d\u3002<\/p>\n\n\n\n<div class=\"scroll\">\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>id<\/th><th>select_type<\/th><th>table<\/th><th>partitions<\/th><th>type<\/th><th>possible_keys<\/th><th>key<\/th><th>key_len<\/th><th>ref<\/th><th>rows<\/th><th>filtered<\/th><th>Extra<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>PRIMARY<\/td><td><\/td><td>NULL<\/td><td>ALL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>NULL<\/td><td>7632<\/td><td>100.00<\/td><td>Using where<\/td><\/tr><tr><td>1<\/td><td>PRIMARY<\/td><td>a<\/td><td>NULL<\/td><td>ref<\/td><td>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d<\/td><td>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d<\/td><td>6<\/td><td>b.hoge_id1<\/td><td>4<\/td><td>10.00<\/td><td>Using where<\/td><\/tr><tr><td>1<\/td><td>PRIMARY<\/td><td>t<\/td><td>NULL<\/td><td>eq_ref<\/td><td>PRIMARY<\/td><td>PRIMARY<\/td><td>8<\/td><td>hogedb.a.hoge_id2<\/td><td>1<\/td><td>10.00<\/td><td>Using where<\/td><\/tr><tr><td>2<\/td><td>DERIVED<\/td><td>\u30c6\u30fc\u30d6\u30eb\u540d<\/td><td>NULL<\/td><td>range<\/td><td>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d<\/td><td>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d<\/td><td>6<\/td><td>NULL<\/td><td>7632<\/td><td>100.00<\/td><td>Using index condition; Using MRR; Using temporary<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">rows\u304c\u6e1b\u3063\u3066\u3001key\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d\u304c\u3042\u3063\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4f7f\u308f\u308c\u305d\u3046\u3067\u3059\u306d\uff01<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u3067\u306f\u554f\u984c\u306e\u9045\u304b\u3063\u305f\u30af\u30a8\u30ea\u306e\u901f\u5ea6\u78ba\u8a8d\u3002<br>3. \u3068\u540c\u3058\u3053\u3068\u3092\u3057\u307e\u3059\u3002<br>\u6539\u5584\u3057\u307e\u3057\u305f\u304b\u2026\uff1f<br>\u79c1\u306e\u5b9f\u4f8b\u3067\u306f\u30015.8s\u306e\u30af\u30a8\u30ea\u304c0.04s\u306b\u306a\u308a\u307e\u3057\u305f\u3002<br>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u524d\u5f8c\u3067\u30af\u30a8\u30ea\u5b9f\u884c\u7d50\u679c\u306b\u5dee\u7570\u304c\u306a\u3044\u3053\u3068\u3082\u78ba\u8a8d\u3057\u305f\u3044\u5834\u5408\u306f\u3053\u3053\u3067\u5b9f\u884c\u7d50\u679c\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u3066\u3001\u524d\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u305f\u3082\u306e\u3068\u5dee\u5206\u3092\u53d6\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">DB\u306e\u30b5\u30a4\u30ba\u3084\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u3063\u305f\u30c6\u30fc\u30d6\u30eb\u306e\u30b5\u30a4\u30ba\u3082\u898b\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<br>\u4ee5\u4e0b\u30af\u30a8\u30ea\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- DBsize\nSELECT\n    table_schema, sum(data_length+index_length) \/1024 \/1024 AS MB FROM information_schema.tables\nWHERE\n    table_schema = \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d;\n-- table size\nSELECT\n    table_name, engine, table_rows AS tbl_rows, avg_row_length AS rlen, floor((data_length+index_length)\/1024\/1024) AS all_mb, floor((data_length)\/1024\/1024) AS data_mb, floor((index_length)\/1024\/1024) AS index_mb\nFROM\n    information_schema.tables\nWHERE\n    table_schema=database() AND table_name=\u30c6\u30fc\u30d6\u30eb\u540d ORDER BY (data_length+index_length) DESC;<\/pre>\n\n\n\n<p class=\"has-black-color has-text-color\">\u6700\u5f8c\u306b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u7de0\u3081\u304f\u304f\u308a\u51e6\u7406\u3002<br>\u623b\u3055\u306a\u304f\u3066\u3088\u3044\u6642\u306f <code>commit;<\/code> \u3092\u6700\u5f8c\u306b\u3001\u623b\u3059\u6642\u306f <code>rollback;<\/code> \u3092\u6700\u5f8c\u306b\u6253\u3066\u3070OK\u3067\u3059\u3002<br>\u307e\u3042\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u524a\u9664\u6587\u3082\u3042\u308b(<code>ALTER TABLE \u30c6\u30fc\u30d6\u30eb\u540d DROP INDEX \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d;<\/code>)\u306e\u3067\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u5fc5\u9808\u304b\u3068\u3044\u308f\u308c\u308b\u3068\u305d\u3046\u3067\u3082\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u7df4\u7fd2\u3063\u3066\u3053\u3068\u3067\u3002<br>(\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u4f7f\u7528\u4f8b\u3092\u5168\u7136\u898b\u306a\u3044\u306e\u3067\u3001\u4e00\u822c\u7684\u306b\u306f\u3084\u3089\u306a\u3044\u306e\u304b\u306a\u2026)<\/p>\n\n\n\n<h3 class=\"has-normal-font-size wp-block-heading\"><span class=\"has-inline-color has-black-color\">5. \u672c\u756a\u5b9f\u65bd<\/span><\/h3>\n\n\n\n<p class=\"has-black-color has-text-color\">\u6539\u5584\u304c\u78ba\u8a8d\u3055\u308c\u305f\u3089\u3001\u540c\u3058\u3053\u3068\u3092\u672c\u756a\u74b0\u5883\u306eDB\u5b9b\u306b\u5b9f\u65bd\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"has-black-color has-text-color has-medium-font-size wp-block-heading\">\u305d\u306e\u4ed6<\/h2>\n\n\n\n<p class=\"has-black-color has-text-color\">\u305d\u3082\u305d\u3082\u9045\u3044\u30af\u30a8\u30ea\u304c1\u3064\u3042\u308b\u30b1\u30fc\u30b9\u3088\u308a\u3082\u3001\u30df\u30ea\u79d2\u3067\u7d42\u308f\u308b\u30af\u30a8\u30ea\u3092\u5927\u91cf\u306b(\u6570\u767e\uff5e\u6570\u5343\u4ee5\u4e0a)\u98db\u3070\u3057\u305f\u7d50\u679c\u9045\u304f\u306a\u308b\u30b1\u30fc\u30b9\u306e\u65b9\u304c\u591a\u3044\u304b\u3082\u3001\u305d\u3046\u306a\u308b\u3068\u6539\u5584\u3059\u3079\u304d\u306fDB\u3067\u306f\u306a\u304fDB\u306b\u63a5\u7d9a\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u5074\u3068\u306a\u308a\u307e\u3059\u3002<br>\u3044\u308f\u3086\u308bN+1\u554f\u984c\u3092\u30a4\u30e1\u30fc\u30b8\u3057\u3066\u3044\u307e\u3059(\u3053\u308c\u306f\u3053\u308c\u3067\u307e\u305f\u8a18\u4e8b\u66f8\u304d\u305f\u3044)\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u307e\u305f\u3001Python\u3067SQLAlchemy+\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30c4\u30fc\u30eb\u306balembic\u3092\u7528\u3044\u3066\u3044\u308b\u5834\u5408\u3001\u4ee5\u4e0b1\u884c\u3092\u5f53\u8a72\u30c6\u30fc\u30d6\u30eb\u306e\u30e2\u30c7\u30ebclass\u306b\u66f8\u3044\u3066\u304a\u304d\u307e\u3059\u3002<br><code>Index(\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d, \u30ab\u30e9\u30e0\u540d)<\/code><br>\u3059\u308b\u3068\u3001alembic\u306e\u901a\u5e38\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f5c\u3063\u3066\u9069\u7528\u3059\u308b\u3068\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5f35\u3089\u308c\u307e\u3059\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u6700\u5f8c\u306b\u767b\u5834\u3057\u305f\u30af\u30a8\u30ea\u3092\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u624b\u9806\nbegin;\nset autocommit = 0;\nshow variables like 'autocommit';\n\n-- add index\nALTER TABLE \u30c6\u30fc\u30d6\u30eb\u540d ADD INDEX \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u540d(\u30ab\u30e9\u30e0\u540d);\nshow indexes from \u30c6\u30fc\u30d6\u30eb\u540d;\n\n-- DBsize\nSELECT\n    table_schema, sum(data_length+index_length) \/1024 \/1024 AS MB FROM information_schema.tables\nWHERE\n    table_schema = \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d;\n-- table size\nSELECT\n    table_name, engine, table_rows AS tbl_rows, avg_row_length AS rlen, floor((data_length+index_length)\/1024\/1024) AS all_mb, floor((data_length)\/1024\/1024) AS data_mb, floor((index_length)\/1024\/1024) AS index_mb\nFROM\n    information_schema.tables\nWHERE\n    table_schema=database() AND table_name=\u30c6\u30fc\u30d6\u30eb\u540d ORDER BY (data_length+index_length) DESC;\n\n-- explain, check speed\n-- \u5b9f\u884c\u8a08\u753b\u3092\u898b\u305f\u308a\u901f\u5ea6\u3092\u78ba\u8a8d\u3057\u305f\u308a\u3057\u307e\u3057\u3087\u3046\n-- commit\nset autocommit = 1;\nshow variables like 'autocommit';\n--rollback;\n--commit;<\/pre>\n\n\n\n<p class=\"has-black-color has-text-color\">\u304a\u75b2\u308c\u3055\u307e\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">\u53c2\u8003\u30ea\u30f3\u30af<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"has-inline-color has-black-color\"><a rel=\"noreferrer noopener\" href=\"https:\/\/qiita.com\/C058\/items\/1c9c57f634ebf54d99bb\" target=\"_blank\">https:\/\/qiita.com\/C058\/items\/1c9c57f634ebf54d99bb<\/a><\/span><\/li><li><span class=\"has-inline-color has-black-color\"><a href=\"https:\/\/qiita.com\/katsukii\/items\/3409e3c3c96580d37c2b\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/qiita.com\/katsukii\/items\/3409e3c3c96580d37c2b<\/a><\/span><\/li><li><span class=\"has-inline-color has-black-color\"><a href=\"https:\/\/qiita.com\/SuguruOoki\/items\/5c4c04a120d9fa5b4267\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/qiita.com\/SuguruOoki\/items\/5c4c04a120d9fa5b4267<\/a><\/span><\/li><li><span class=\"has-inline-color has-black-color\"><a href=\"https:\/\/techlife.cookpad.com\/entry\/2017\/04\/18\/092524\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/techlife.cookpad.com\/entry\/2017\/04\/18\/092524<\/a><\/span><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u30b3\u30f3\u3061\u3083\u3093\u3053\u3068\u4f50\u3005\u6728\u3067\u3059\u3002MySQL\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u77e5\u898b\u3092\u5f97\u305f\u306e\u3067\u66f8\u304d\u4e0b\u3057\u3066\u304a\u304d\u307e\u3059\u3002 DB\u9ad8\u901f\u5316\u306e\u305f\u3081\u306b\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5f35\u308b\u306e\u304c\u6709\u52b9\u3067\u3059\u3002\u7d19\u306e\u8f9e\u66f8\u306b\u306f\u300c\u3042\u3067\u59cb\u307e\u308b\u5358\u8a9e\u306f\u3053\u3053\u304b\u3089\u300d\u300c\u3044\u3067\u59cb\u307e\u308b\u5358\u8a9e\u306f\u3053\u3053\u304b\u3089 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-6","entry"],"_links":{"self":[{"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":45,"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"predecessor-version":[{"id":951,"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions\/951"}],"wp:attachment":[{"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/conchan.akita.jp\/blog\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}