{"id":21562,"date":"2022-01-11T13:30:04","date_gmt":"2022-01-11T04:30:04","guid":{"rendered":"https:\/\/labs.gree.jp\/blog\/?p=21562"},"modified":"2022-01-11T13:32:39","modified_gmt":"2022-01-11T04:32:39","slug":"post21562","status":"publish","type":"post","link":"https:\/\/labs.gree.jp\/blog\/2022\/01\/21562\/","title":{"rendered":"\u53d7\u53d6\u671f\u9650\u306e\u904e\u304e\u305f\u30c7\u30fc\u30bf\u3092MySQL\u4e0a\u304b\u3089\u524a\u9664\u3059\u308b\u8a71"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u308f\u3002\u305b\u3058\u307e\u3067\u3059\u3002\u4eca\u56de\u306f\u5730\u5473\u3067\u6ce5\u81ed\u3044\u8a71\u3092\u3057\u307e\u3059\u3002\u305f\u3060\u3001\u5272\u3068\u5e73\u6613\u306a\u5185\u5bb9\u304b\u3068\u601d\u3044\u307e\u3059\u306e\u3067\u3001\u521d\u5b66\u8005\u306e\u65b9\u306b\u3082\u30aa\u30b9\u30b9\u30e1\u3067\u3059\u3002<\/p>\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u30b2\u30fc\u30e0\u3067\u306f\u3001\u53d7\u53d6\u671f\u9650\u306e\u3064\u3044\u305f\u30ed\u30b0\u30a4\u30f3\u30dc\u30fc\u30ca\u30b9\u7684\u306a\u3082\u306e\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30e6\u30fc\u30b6\u304c\u671f\u9650\u307e\u3067\u306b\u53d7\u3051\u53d6\u3089\u306a\u3044\u3068\u3001\u30e6\u30fc\u30b6\u304b\u3089\u305d\u306e\u30c7\u30fc\u30bf\u306f\u4e0d\u53ef\u8996\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u5fc5\u305a\u3057\u3082\u3001\u4e0d\u53ef\u8996\u306b\u306a\u3063\u305f\u77ac\u9593\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u76f4\u3061\u306b\u524a\u9664\u3055\u308c\u308b\u3001\u3068\u3044\u3046\u308f\u3051\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u304b\u4f55\u304b\u3067\u3001\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u3088\u3046\u306b\u524a\u9664\u3059\u308b\u30b1\u30fc\u30b9\u304c\u591a\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u307e\u305f\u3001\u8ad6\u7406\u524a\u9664\u3068\u3044\u3046\u6982\u5ff5\u3082\u3042\u308a\u307e\u3059\u3002\u8ad6\u7406\u524a\u9664\u306b\u3064\u3044\u3066\u306f\u3044\u308d\u3044\u308d\u610f\u898b\u3084\u8003\u3048\u65b9\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u306e\u3067\u3001\u3053\u3053\u3067\u305d\u308c\u306b\u3064\u3044\u3066\u306f\u8ad6\u3058\u307e\u305b\u3093\u304c\u3001\u300c\u524a\u9664\u30d5\u30e9\u30b0\u304c\u7acb\u3063\u3066\u30e6\u30fc\u30b6\u304b\u3089\u4e0d\u53ef\u8996\u306b\u306a\u3063\u305f\u5f8c\u3001\u4e09\u30f6\u6708\u4ee5\u4e0a\u7d4c\u904e\u3057\u305f\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3057\u305f\u3044\u300d\u307f\u305f\u3044\u306a\u3053\u3068\u306f\u3001\u30b2\u30fc\u30e0\u306b\u9650\u3089\u305a\u3001\u3057\u3070\u3057\u3070\u3042\u308b\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u3046\u3044\u3063\u305f\u3001\u30e6\u30fc\u30b6\u304b\u3089\u4e0d\u53ef\u8996\u306b\u306a\u3063\u3066\u3057\u3070\u3089\u304f\u7d4c\u904e\u3057\u305f\u30c7\u30fc\u30bf\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u84c4\u7a4d\u3057\u7d9a\u3051\u308b\u3068\u3001\u30b9\u30c8\u30ec\u30fc\u30b8\u306e\u4f7f\u7528\u91cf\u304c\u5897\u3048\u3066\u3044\u3063\u305f\u308a\u3001MySQL\u7684\u306b\u306fRows_examined\u304c\u5897\u3048\u305f\u308a\u3001 buffer pool \u306b\u30c6\u30fc\u30d6\u30eb\u304c\u53ce\u307e\u3089\u306a\u304f\u306a\u3063\u305f\u308a\u3068\u3001\u69d8\u3005\u306a\u30c7\u30e1\u30ea\u30c3\u30c8\u304c\u767a\u751f\u3057\u305f\u308a\u3082\u3057\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u53e4\u3044\u6d88\u305b\u308b\u30c7\u30fc\u30bf\u306f\u524a\u9664\u3057\u305f\u65b9\u304c\u826f\u3044\u3067\u3059\u3088\u306d\u3001\u3068\u3044\u3063\u305f\u610f\u3067\u3001<a href=\"https:\/\/www.slideshare.net\/takanorisejima\/innodb-178140575\/37\">\u6b21\u306e\u30b9\u30e9\u30a4\u30c9<\/a>\u3067\u3082\u66f8\u304b\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002<\/p>\n<div class=\"blog-card\">\n<div class=\"blog-card-body-outer\">\n<div class=\"blog-card-body\">\n<h5 class=\"blog-card-title\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.slideshare.net\/takanorisejima\/innodb-178140575\/37\">Client Challenge<\/a><br \/>\n\t\t\t\t\t\t\t<\/h5>\n<div class=\"blog-card-site-title\">\n\t\t\t\t\t\t\t<a href=\"http:\/\/www.slideshare.net\"><br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\thttp:\/\/www.slideshare.net\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>\u3067\u306f\u3053\u3046\u3044\u3063\u305f\u3068\u304d\u3001\u3069\u306e\u3088\u3046\u306a\u3053\u3068\u3092\u8003\u3048\u306a\u304c\u3089\u6d88\u3057\u3066\u884c\u3051\u3070\u3088\u3044\u306e\u304b\u3001\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3092\u5165\u308c\u305a\u3001\u30e6\u30fc\u30b6\u306b\u3082\u5f71\u97ff\u3092\u4e0e\u3048\u306a\u3044\u3088\u3046\u306b\u3057\u306a\u304c\u3089\u524a\u9664\u3057\u3066\u3044\u304f\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u306e\u304b\u3001\u793e\u5185\u5411\u3051\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u66f8\u3053\u3046\u304b\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u793e\u5185\u306b\u7559\u307e\u3089\u305a\u793e\u5916\u306b\u5411\u3051\u3066\u516c\u958b\u3057\u3066\u3082\u826f\u3044\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u601d\u3063\u305f\u306e\u3067\u3001blog\u306b\u66f8\u304f\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u57fa\u672c\u7684\u306b\u306fMySQL\u3084Aurora MySQL\u306a\u3069\u3092\u524d\u63d0\u306b\u66f8\u304d\u307e\u3059\u304c\u3001\u4ed6\u306eRDBMS\u306a\u3069\u3067\u3082\u3001\u53c2\u8003\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<h2>\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9<\/h2>\n<p>\u6b21\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; show create table sejima\\G\n*************************** 1. row ***************************\n       Table: sejima\nCreate Table: CREATE TABLE `sejima` (\n  `id` bigint NOT NULL AUTO_INCREMENT,\n  `value` varchar(255) DEFAULT NULL,\n  `created_at` datetime NOT NULL,\n  `updated_at` timestamp NOT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=458728 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci\n1 row in set (0.00 sec)\n\nmysql&gt; select min(id),max(id),count(*) from sejima;\n+---------+---------+----------+\n| min(id) | max(id) | count(*) |\n+---------+---------+----------+\n|       1 |  409578 |   294912 |\n+---------+---------+----------+\n1 row in set (0.01 sec)\n\nmysql&gt;\n<\/pre>\n<p>\u3042\u308b\u3044\u306f\u3001\u6b21\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; show create table sejima2\\G\n*************************** 1. row ***************************\n       Table: sejima2\nCreate Table: CREATE TABLE `sejima2` (\n  `user_id` bigint NOT NULL,\n  `item_id` int NOT NULL,\n  `value` varchar(255) DEFAULT NULL,\n  `created_at` datetime NOT NULL,\n  `updated_at` timestamp NOT NULL,\n  PRIMARY KEY (`user_id`,`item_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci\n1 row in set (0.00 sec)\n\nmysql&gt; select min(user_id),max(user_id),count(*) from sejima2;\n+--------------+--------------+----------+\n| min(user_id) | max(user_id) | count(*) |\n+--------------+--------------+----------+\n|            1 |       409578 |  1179648 |\n+--------------+--------------+----------+\n1 row in set (0.04 sec)\n\nmysql&gt;\n<\/pre>\n<p>\u3053\u308c\u3089\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u3001 <code>updated_at<\/code> \u304c\u3042\u308b\u7a0b\u5ea6\u53e4\u3044\u3082\u306e\u3092\u6d88\u3057\u3066\u3044\u304f\u30b1\u30fc\u30b9\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n<h2>\u5148\u305a\u306f\u307e\u3068\u3081<\/h2>\n<p>\u9577\u3005\u3068\u8aad\u3080\u306e\u306f\u624b\u9593\u3067\u3057\u3087\u3046\u304b\u3089\u3001\u6700\u521d\u306b\u307e\u3068\u3081\u3092\u66f8\u3044\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<h3>PRIMARY KEY \u304c AUTO_INCREMENT \u306e\u5834\u5408<\/h3>\n<ol>\n<li><code>SELECT MIN(id), MAX(id) FROM sejima;<\/code> \u3067id\u306e\u4e0b\u9650\u3068\u4e0a\u9650\u3092\u8abf\u3079\u308b\u3002<\/li>\n<li>${min_id}=MIN(id), ${max_id}=MAX(id), ${delta}=MAX(id)-MIN(id); \u3068\u3059\u308b<\/li>\n<li>\u6b21\u306e\u3088\u3046\u306aSELECT\u3092replica\u306b\u5bfe\u3057\u3066\u6295\u3052\u308b\u3002\u30e6\u30fc\u30b6\u304b\u3089\u53c2\u7167\u3055\u308c\u306a\u3044\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u7528replica\u304c\u3042\u308c\u3070\u306a\u304a\u826f\u3044\n<ul>\n<li><code>SELECT id FROM sejima WHERE id BETWEEN ${min_id} AND ${min_id}+${delta}\/10 AND updated_at &lt; ? LIMIT ?;<\/code>\n<ul>\n<li>\u8981\u3059\u308b\u306b\u3001full table scan \u306b\u306a\u3089\u306a\u3044\u3088\u3046\u3001\u3056\u3063\u304f\u308a 10%\u4ee5\u4e0b\u306e\u884c\u307e\u3067\u3057\u304b\u63a2\u7d22\u3057\u306a\u3044\u3088\u3046\u306a\u30d2\u30f3\u30c8\u3068\u3057\u3066 BETWEEN \u3067\u6307\u5b9a\u3059\u308b\u3002LIMIT \u3067\u5236\u9650\u3059\u308b\u5024\u306f\u3001\u4f8b\u3048\u30701000\u304f\u3089\u3044<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>SELECT\u3067\u53d6\u5f97\u3055\u308c\u305fid\u3092\u4f7f\u3063\u3066source\u306bDELETE\u3092\u6295\u3052\u308b\n<ul>\n<li><code>DELETE FROM sejima WHERE id IN (...,) AND updated_at &lt; ?;<\/code>\n<ul>\n<li>\u66f4\u65b0\u3055\u308c\u3066\u306a\u3044\u30c7\u30fc\u30bf\u3060\u3051\u78ba\u5b9f\u306b\u6d88\u3057\u305f\u3044\u306e\u3067\u3042\u308c\u3070\u3001updated_at &lt; ? \u3082\u6307\u5b9a\u3057\u3066\u304b\u307e\u308f\u306a\u3044<\/li>\n<li>\u4e00\u5ea6\u306b\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6d88\u3059\u3068\u30ed\u30c3\u30af\u306e\u7af6\u5408\u306a\u3069\u304c\u554f\u984c\u306b\u306a\u308b\u53ef\u80fd\u6027\u3082\u3042\u308b\u306e\u3067\u3001\u3067\u304d\u308c\u3070\u30d4\u30fc\u30af\u30bf\u30a4\u30e0\u3092\u3055\u3051\u3066\u524a\u9664\u3057\u305f\u65b9\u304c\u826f\u3044\u3002\u307e\u305f\u3001\u524a\u9664\u6642\u306e\u8ca0\u8377\u306b\u3064\u3044\u3066\u306f\u3001 IN (...,) \u3067\u6e21\u3059 id \u306e\u91cf\u3067\u8abf\u6574\u3059\u308b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>1\uff5e3\u79d2\u7a0b\u5ea6 sleep \u3059\u308b\u3002\uff08\u307e\u3068\u307e\u3063\u305fDELETE\u3092\u4f11\u307f\u306a\u304f source \u306b\u6295\u3052\u7d9a\u3051\u308b\u306e\u306f\u91cd\u3044\u306e\u3067\u3002\uff09<\/li>\n<li>3.\u306eSELECT\u3067 id \u304c\u8fd4\u3063\u3066\u6765\u306a\u304f\u306a\u308b\u307e\u3067\u3001 SELECT\u3068DELETE&amp;sleep\u3092\u7e70\u308a\u8fd4\u3059\u3002SELECT\u3067id\u304c\u8fd4\u3063\u3066\u6765\u306a\u304f\u306a\u3063\u305f\u3089\u3001BETWEEN \u3067\u6307\u5b9a\u3059\u308b\u7bc4\u56f2\u3092\u305a\u3089\u3057\u3066\u518d\u5ea6 id \u3092\u53d6\u5f97\u3059\u308b\n<ul>\n<li>\u4f8b\uff1a\n<ul>\n<li><code>SELECT id FROM sejima WHERE id BETWEEN ${min_id}+${delta}\/10)+1 AND ${min_id}+(${delta}\/10)*2 AND updated_at &lt; ? LIMIT ?;<\/code> \u306a\u3069<\/li>\n<li><code>SELECT id FROM sejima WHERE id BETWEEN ${min_id}+(${delta}\/10)*9)+1 AND ${max_id} AND updated_at &lt; ? LIMIT ?;<\/code> \u307e\u3067\u7e70\u308a\u8fd4\u3059<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>PRIMARY KEY \u304c\u8907\u5408\u30ad\u30fc\u306e\u5834\u5408<\/h3>\n<ol>\n<li><code>SELECT MIN(user_id), MAX(user_id) FROM sejima2;<\/code>  \u3067 user_id\uff08\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u5de6\u7aef\u306e\u30d7\u30ea\u30d5\u30a3\u30af\u30b9\uff09\u306e\u4e0b\u9650\u3068\u4e0a\u9650\u3092\u8abf\u3079\u308b\u3002\n<ul>\n<li>${min_id}=MIN(user_id), ${max_id}=MAX(user_id), ${delta}=MAX(user_id)-MIN(user_id); \u3068\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n<li>\u6b21\u306e\u3088\u3046\u306aSELECT\u3092replica\u306b\u5bfe\u3057\u3066\u6295\u3052\u308b\u3002\u30e6\u30fc\u30b6\u304b\u3089\u53c2\u7167\u3055\u308c\u306a\u3044\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u7528replica\u304c\u3042\u308c\u3070\u306a\u304a\u826f\u3044\n<ul>\n<li><code>SELECT user_id, item_id FROM sejima2 WHERE user_id BETWEEN ${min_id} AND ${min_id}+${delta}\/10 AND updated_at &lt; ? LIMIT ?;<\/code>\n<ul>\n<li>\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u4f7f\u3063\u305f\u3068\u304d\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u4f7f\u3048\u306a\u304b\u3063\u305f\u3068\u3057\u3066\u3082\u3001SELECT\u3057\u3066\u8abf\u3079\u3066\u304a\u3051\u3070\u3001\u524a\u9664\u5bfe\u8c61\u3068\u306a\u308b user_id, item_id \u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u30ed\u30b0\u306a\u3069\u306b\u6b8b\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>DELETE&amp;sleep\u3059\u308b\n<ul>\n<li>\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u4f7f\u3063\u3066\u3082\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u4f7f\u3048\u308b\u5834\u5408\n<ul>\n<li><code>DELETE FROM sejima2 WHERE (user_id,item_id) IN ((?,?),(?,?),(?,?),(?,?)...,) AND updated_at &lt; ?;<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u4f7f\u3063\u305f\u3068\u304d\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u4f7f\u3048\u306a\u3044\u5834\u5408\uff08 IN \u3067\u6e21\u3059 user_id \u306e\u500b\u6570\u3068LIMIT\u3067\u8ca0\u8377\u3092\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u3059\u308b\uff09\n<ul>\n<li><code>DELETE FROM sejima2 WHERE user_id IN (...,) AND updated_at &lt; ? LIMIT ?;<\/code>\n<ul>\n<li>LIMIT\u3064\u3051\u3066\u6d88\u3059\u5834\u5408\u306f\u3001<a href=\"https:\/\/dev.mysql.com\/doc\/c-api\/8.0\/en\/mysql-affected-rows.html\">mysql_affected_rows()<\/a> \u76f8\u5f53\u306eAPI\u3067\u5b9f\u969b\u306bDELETE\u3055\u308c\u3066\u308b\u304b\u78ba\u8a8d\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n<li>\u3042\u308b\u3044\u306f\u3001 binary log \u306b\u66f4\u65b0\u5c65\u6b74\u3092\u7d30\u304b\u304f\u6b8b\u3057\u305f\u3044\u306a\u3069\u3042\u308c\u3070\u3001\u6b21\u306e\u3088\u3046\u306aDELETE\u3082\u8003\u3048\u3089\u308c\u308b\n<ul>\n<li><code>DELETE FROM sejima2 WHERE user_id = ? AND item_id IN (...,) AND updated_at &lt; ? LIMIT ?;<\/code><\/li>\n<li>\u3053\u308c\u306f\u8907\u6570\u306e user_id \u3092IN\u3067\u6307\u5b9a\u3059\u308b\u3068\u304d\u3088\u308a\u3001DELETE\u306e\u767a\u884c\u56de\u6570\u304c\u5897\u3048\u308b\u3060\u308d\u3046\u304b\u3089\u3001\u30c6\u30fc\u30d6\u30eb\u5185\u306e\u30c7\u30fc\u30bf\u306e\u50be\u5411\u3084DELETE\u306b\u8981\u3059\u308b\u30c8\u30fc\u30bf\u30eb\u306e\u6642\u9593\u306a\u3069\u8003\u616e\u3057\u3066\u691c\u8a0e\u3059\u308c\u3070\u826f\u3044<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>PRIMARY KEY \u304c AUTO_INCREMENT \u306e\u5834\u5408\u306e\u3068\u304d\u3068\u540c\u3058\u3088\u3046\u306b\u3001SELECT\u3068DELETE&amp;sleep\u3092\u7e70\u308a\u8fd4\u3059<\/li>\n<\/ol>\n<p>\u304b\u3064\u3066MySQL\u3067\u306f\u3001\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3092\u4f7f\u3063\u305f\u3068\u304d\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u9069\u5207\u306b\u4f7f\u3048\u306a\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u53c2\u8003\uff1a <a href=\"https:\/\/bugs.mysql.com\/bug.php?id=70705\">Performance impact of row constructors is not properly documented<\/a><\/p>\n<p>8.0.27\u3067\u8a66\u3059\u3068\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u4f7f\u3048\u3066\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u4f7f\u3048\u3066\u308b\u304b\u3069\u3046\u304b\u306f\u3001MySQL\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u4f9d\u5b58\u3059\u308b\uff08MySQL\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u3042\u3093\u307e\u308a\u53e4\u3044\u3068\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u4f7f\u3048\u306a\u3044\uff09\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; explain select user_id,item_id from sejima2 where (user_id,item_id) IN ((1411,1412),(1413,1414),(1421,1422));\n+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+\n| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                    |\n+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+\n|  1 | SIMPLE      | sejima2 | NULL       | range | PRIMARY       | PRIMARY | 12      | NULL |    3 |   100.00 | Using where; Using index |\n+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+\n1 row in set, 1 warning (0.00 sec)\n\nmysql&gt; select user_id,item_id,updated_at from sejima2 where (user_id,item_id) IN ((1411,1412),(1413,1414),(1421,1422));\n+---------+---------+---------------------+\n| user_id | item_id | updated_at          |\n+---------+---------+---------------------+\n|    1411 |    1412 | 2021-12-29 11:39:28 |\n|    1413 |    1414 | 2021-12-29 11:39:28 |\n|    1421 |    1422 | 2021-12-29 11:39:28 |\n+---------+---------+---------------------+\n3 rows in set (0.00 sec)\n\nmysql&gt;\n<\/pre>\n<p>\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u4f7f\u3046\u5834\u5408\u306f\u3001\u304a\u624b\u5143\u306e\u74b0\u5883\u3067 explain \u3057\u3066\u898b\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u4f7f\u3063\u3066PRIMARY KEY\u6307\u5b9a\u3057\u3066DELETE\u3067\u304d\u308b\u306e\u304c\u671b\u307e\u3057\u3044\u3067\u3059\u304c\u3001\u305d\u308c\u304c\u3067\u304d\u306a\u3044\u5834\u5408\u306f\u3001 <code>WHERE user_id IN (...,) AND updated_at &lt; ? LIMIT ?;<\/code> \u306e\u3088\u3046\u306b\u3001user_id \u3092\u7d5e\u308a\u3064\u3064 LIMIT \u3067\u8ca0\u8377\u3092\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u3059\u308b\u306e\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<br \/>\n\u307e\u305f\u3001MySQL\u306e\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u95a2\u9023\u3067\u3001\u904e\u53bb\u306b\u3044\u304f\u3064\u304b\u30d0\u30b0\u30ec\u30dd\u30fc\u30c8\u304c\u4e0a\u304c\u3063\u3066\u3044\u305f\u3088\u3046\u306b\u898b\u53d7\u3051\u3089\u308c\u307e\u3059\u306e\u3067\u3001 IN\u3067\u884c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u4f7f\u3063\u3066DELETE\u3059\u308b\u306a\u3089\u3070\u3001<code>AND updated_at &lt; ?<\/code> \u3068\u3044\u3046\u3088\u3046\u306a\u6307\u5b9a\u3092\u3057\u3066\u304a\u304f\u306e\u306f\u3001\u3088\u308a\u5b89\u5168\u3068\u3044\u3046\u304b\u7121\u96e3\u3067\u60aa\u304f\u306a\u3044\u6c17\u3082\u3057\u307e\u3059\u3002<\/p>\n<p>\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3068\u3057\u3066\u306f<\/p>\n<ul>\n<li>replica \u5074\u3067\u524a\u9664\u5bfe\u8c61\u3068\u306a\u308bPRIMARY KEY\u306e\u5024\u3092\u53ce\u96c6\u3059\u308b\n<ul>\n<li>\u30bb\u30ab\u30f3\u30c0\u30ea\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u3063\u305f\u308a\u3001BETWEEN\u3084LIMIT\u3092\u3064\u3051\u3066\u7d5e\u308a\u8fbc\u3093\u3067\u3001\u3042\u308b\u7a0b\u5ea6\u8ca0\u8377\u3092\u4e0b\u3052\u308b\u3088\u3046\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n<li>source\u306bDELETE\u3092\u6295\u3052\u308b\u3068\u304d\u306f\u3001PRIMARY KEY\u6307\u5b9a\u3067\u6295\u3052\u308b<\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u30ed\u30c3\u30af\u306e\u7af6\u5408\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3001DELETE\u306f\u30bb\u30ab\u30f3\u30c0\u30ea\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u306f\u306a\u304f\u3001PRIMARY KEY\u4f7f\u3063\u3066\u5b9f\u884c\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3067\u306f\u3001\u88dc\u8db3\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2>\u5bfe\u8c61\u3068\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u3092\u7d5e\u308a\u8fbc\u3080<\/h2>\n<p>\u6d88\u305b\u308b\u30c7\u30fc\u30bf\u306f\u3069\u3093\u3069\u3093\u6d88\u3057\u3066\u3044\u3063\u305f\u65b9\u304c\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u305d\u3046\u306f\u3044\u3063\u3066\u3082\u3001\u3044\u307e\u307e\u3067\u6d88\u3057\u3066\u3044\u306a\u304b\u3063\u305f\u3082\u306e\u3092\u6d88\u3059\u3088\u3046\u306b\u3059\u308b\u306e\u306f\u3001\u305d\u308c\u306a\u308a\u306b\u4f5c\u696d\u3092\u8981\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3088\u3063\u3066\u3001\u524a\u9664\u3059\u308b\u305f\u3081\u306e\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u3092\u4ed5\u8fbc\u3080\u306e\u306f\u3001\u8cbb\u7528\u5bfe\u52b9\u679c\u306e\u5927\u304d\u3044\u3068\u3053\u308d\u3060\u3051\u306b\u3057\u305f\u3044\u3068\u8003\u3048\u308b\u306e\u304c\u81ea\u7136\u3067\u3059\u3002<\/p>\n<p>MySQL\u306e\u9762\u5012\u3092\u898b\u3066\u3044\u308b\u4eba\u306f\u3001 slow query log \u306a\u3069\u306f\u3057\u3070\u3057\u3070\u898b\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4e00\u3064\u306e\u76ee\u5b89\u3068\u3057\u3066<\/p>\n<ul>\n<li>Rows_sent \u3068 Rows_examined \u306e\u5dee\u5206\u304c\u5927\u304d\u3044 slow query \u306f\u3001\u6700\u9069\u5316\u306e\u4f59\u5730\u304c\u5927\u304d\u3044<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u306e\u3092\u3001\u5927\u524d\u63d0\u306b\u3059\u308c\u3070\u826f\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u3046\u307e\u304f\u4f7f\u3048\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001USE INDEX \u3084 FORCE INDEX\u3001 IGNORE INDEX \u306a\u3069\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30d2\u30f3\u30c8\u3092\u898b\u76f4\u3057\u305f\u308a\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8ffd\u52a0\u3059\u308b\u306a\u3069\u3057\u3066\u3082\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u305f\u3060\u3001 WHERE \u53e5\u3067 created_at \u3084 ctime \u306a\u3069\u3001\u53e4\u3044\u30c7\u30fc\u30bf\u3092\u53c2\u7167\u3057\u306a\u3044\u3088\u3046\u306a\u6761\u4ef6\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u305f\u308a\u3001\u524a\u9664\u6e08\u307f\u304b\u3069\u3046\u304b\u3001\u30d5\u30e9\u30b0\u7ba1\u7406\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u3067\u3042\u308c\u3070<\/p>\n<p><strong>\u305d\u308c\u3001\u305d\u3082\u305d\u3082\u6d88\u305b\u3070\u826f\u3044\u3093\u3058\u3083\u306d\uff1f<\/strong><\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u3067\u53e4\u3044\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u3082\u826f\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>replica \u304b\u3089 id \u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u3055\u3044\u304d\u3093\u306eMySQL\u7684\u306a\u8a00\u3044\u65b9\u3060\u3068 source\/replica\uff08\u304b\u3064\u3066\u306e\u8a00\u3044\u65b9\u3060\u3068 master\/slave\u3001Aurora MySQL \u3060\u3068 writer\/reader \u3067\u3059\u304c\uff09\u3001\u66f4\u65b0\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u308b source \u306b\u5bfe\u3057\u3066\u91cd\u3044SELECT\u3092\u6295\u3052\u308b\u306e\u306f\u5f97\u7b56\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u304b\u3089\u306e\u91cd\u3044SELECT\u306f\u3001\u305b\u3081\u3066replica\u306b\u6295\u3052\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u524d\u8ff0\u3057\u305f\u30b9\u30e9\u30a4\u30c9\u3067\u3042\u3063\u305f\u3088\u3046\u306b\u3001\u30b5\u30fc\u30d3\u30b9\u304b\u3089\u53c2\u7167\u3055\u308c\u306a\u3044replica\u304c\u3042\u308c\u3070\u3001\u305d\u3053\u306bSELECT\u3092\u6295\u3052\u308c\u3070\u826f\u3044\u3093\u3067\u3059\u304c\u3001\u305d\u3046\u3082\u3044\u304b\u306a\u3044\u30b1\u30fc\u30b9\u3082\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002\u305d\u3046\u3044\u3046\u3068\u304d\u306f\u3001\u3067\u304d\u308b\u7bc4\u56f2\u3067SELECT\u304c\u8efd\u304f\u306a\u308b\u3088\u3046\u914d\u616e\u3059\u308b\u306e\u304c\u826f\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u66f4\u65b0\u65e5\u6642\u304c\u4e00\u5b9a\u4ee5\u4e0a\u53e4\u3044\u30ec\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066SELECT\u3092\u6295\u3052\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u4f8b\u3048\u3070\u6b21\u306e\u3088\u3046\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u304c<\/p>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; explain select id from sejima where updated_at &lt; '2021-12-29 11:39';\n+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+-------------+\n| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |\n+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+-------------+\n|  1 | SIMPLE      | sejima | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 294712 |    33.33 | Using where |\n+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+-------------+\n1 row in set, 1 warning (0.00 sec)\n\nmysql> select id from sejima where updated_at < '2021-12-29 11:39';\n+----+\n| id |\n+----+\n|  1 |\n|  2 |\n|  3 |\n|  4 |\n|  5 |\n|  6 |\n|  7 |\n|  8 |\n|  9 |\n+----+\n9 rows in set (0.10 sec)\n\nmysql&gt;\n<\/pre>\n<p>\u3053\u308c\u306f\u4e8c\u91cd\u306e\u610f\u5473\u3067\u671b\u307e\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<ul>\n<li><code>updated_at<\/code> \u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u3089\u308c\u3066\u306a\u3044\u306e\u3067 full table scan \u306b\u306a\u308b<\/li>\n<li>WHERE\u53e5\u3067\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u6761\u4ef6\u6b21\u7b2c\u3067\u3001ResultSet\u304c\u5de8\u5927\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002\u5de8\u5927\u306aResultSet\u304c\u8fd4\u3063\u3066\u304f\u308b\u3068\u3001\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u3092\u5b9f\u884c\u3059\u308b\u30b5\u30fc\u30d0\u3067\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u304c Out Of Memory \u306b\u306a\u308b\u30ea\u30b9\u30af\u304c\u6709\u308b\n<ul>\n<li>\u4e00\u3064\u4e00\u3064\u306eResultSet\u304c\u305d\u3053\u307e\u3067\u5927\u304d\u304f\u306a\u304b\u3063\u305f\u3068\u3057\u3066\u3082\u3001\u30d0\u30c3\u30c1\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u8907\u6570\u8d77\u52d5\u3057\u3066\u3044\u305f\u308a\u3001ResultSet\u304b\u3089PHP\u306a\u3069\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u914d\u5217\u3092\u751f\u6210\u3057\u305f\u3068\u304d\u3001\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u304c\u5927\u304d\u304f\u5897\u52a0\u3059\u308b\u30b1\u30fc\u30b9\u3082\u3042\u308b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4eca\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u305f\u304b\u3060\u304b294712\u4ef6\u3068\u5c0f\u3055\u3044\u3067\u3059\u304c\u3001\u3053\u308c\u304c\u6570\u5104\uff5e\u6570\u5341\u5104\u4ef6\u4ee5\u4e0a\u3068\u3044\u3063\u305f\u5de8\u5927\u306a\u30c6\u30fc\u30d6\u30eb\u306b\u306a\u308b\u3068\u3001\u3053\u3046\u3044\u3063\u305fSELECT\u306f\u554f\u984c\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002<\/p>\n<p>\u5148\u305a\u306f\u305b\u3081\u3066 LIMIT \u3092\u3064\u3051\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u307e\u3059\u3002 LIMIT \u3092\u4f7f\u3046\u3053\u3068\u3067\u4ee5\u4e0b\u306e\u6700\u9069\u5316\u304c\u671f\u5f85\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/ja\/limit-optimization.html\">MySQL 8.0 \u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u30de\u30cb\u30e5\u30a2\u30eb | 8.2.1.19 LIMIT \u30af\u30a8\u30ea\u30fc\u306e\u6700\u9069\u5316<\/a><\/p>\n<blockquote><p>\n  MySQL \u306f\u5fc5\u8981\u306a\u6570\u306e\u884c\u3092\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u9001\u4fe1\u3059\u308b\u3068\u305f\u3060\u3061\u306b\u3001SQL_CALC_FOUND_ROWS \u304c\u4f7f\u7528\u3055\u308c\u3066\u3044\u306a\u3044\u304b\u304e\u308a\u3001\u30af\u30a8\u30ea\u30fc\u3092\u4e2d\u6b62\u3057\u307e\u3059\u3002\n<\/p><\/blockquote>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; select id from sejima where updated_at &lt; '2021-12-29 11:39' limit 1000;\n+----+\n| id |\n+----+\n|  1 |\n|  2 |\n|  3 |\n|  4 |\n|  5 |\n|  6 |\n|  7 |\n|  8 |\n|  9 |\n+----+\n9 rows in set (0.11 sec)\n<\/pre>\n<p>\u4f8b\u3048\u3070\u3001<\/p>\n<ul>\n<li>\u5bfe\u8c61\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u9069\u5207\u306b sharding \u3055\u308c\u3066\u3044\u3066\u3001\u4e00\u3064\u4e00\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u5145\u5206\u306b\u5c0f\u3055\u3044<\/li>\n<li>\u30e6\u30fc\u30b6\u304b\u3089\u53c2\u7167\u3055\u308c\u306a\u3044\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u7528replica\u306bSELECT\u3092\u6295\u3052\u308b<\/li>\n<\/ul>\n<p>\u306e\u3067\u3042\u308c\u3070\u3001LIMIT\u3064\u3051\u308b\u3060\u3051\u3067\u5145\u5206\u306b\u8efd\u304f\u3067\u304d\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u305f\u3060\u3001 sharding \u3055\u308c\u3066\u3044\u306a\u304b\u3063\u305f\u308a\u3001\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u5c02\u7528\u306ereplica\u304c\u5b58\u5728\u3057\u306a\u3044\u306e\u3067\u3042\u308c\u3070\u3001\u3082\u3046\u3061\u3087\u3063\u3068\u8efd\u304f\u3057\u305f\u3044\u3068\u3053\u308d\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u66f4\u65b0\u983b\u5ea6\u304c\u4f4e\u3044\u30c6\u30fc\u30d6\u30eb\u306a\u3089\u3001 updated_at \u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u3063\u3066\u3082\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3082\u3057\u3001 updated_at \u304c\u983b\u7e41\u306b\u66f4\u65b0\u3055\u308c\u308b\u30c6\u30fc\u30d6\u30eb\u3067\u3042\u308c\u3070\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u308b\u306e\u306f\u8e8a\u8e87\u308f\u308c\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u306e\u5834\u5408\u3001 <code>WHERE id BETWEEN ? AND ?<\/code> \u3067\u66f4\u306b\u7d5e\u308a\u8fbc\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; explain select id from sejima where id between 1 and 10000 and updated_at &lt; '2021-12-29 11:39' limit 1000;\n+----+-------------+--------+------------+-------+---------------+---------+---------+------+-------+----------+-------------+\n| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows  | filtered | Extra       |\n+----+-------------+--------+------------+-------+---------------+---------+---------+------+-------+----------+-------------+\n|  1 | SIMPLE      | sejima | NULL       | range | PRIMARY       | PRIMARY | 8       | NULL | 12182 |    33.33 | Using where |\n+----+-------------+--------+------------+-------+---------------+---------+---------+------+-------+----------+-------------+\n1 row in set, 1 warning (0.00 sec)\n\nmysql&gt;\n<\/pre>\n<p><code>where id between 1 and 10000<\/code> \u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3051\u308c\u3070\u3001 <code>updated_at &lt; '2021-12-29 11:39'<\/code> \u3092\u6e80\u305f\u3059\u884c\u304c1000\u4ef6\u898b\u3064\u304b\u308b\u307e\u3067\u3001\u30c6\u30fc\u30d6\u30eb\u304c\u30b9\u30ad\u30e3\u30f3\u3055\u308c\u7d9a\u3051\u307e\u3059\u3002\u3082\u30571000\u4ef6\u898b\u3064\u304b\u3089\u306a\u3051\u308c\u3070\u3001 full table scan \u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u3057\u304b\u3057\u3001 <code>where id between 1 and 10000<\/code> \u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u306b\u3088\u308a\u3001id\u304c10000\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u3067 <code>updated_at &lt; '2021-12-29 11:39'<\/code> \u3092\u6e80\u305f\u3059\u3082\u306e\u304c1000\u4ef6\u306a\u304b\u3063\u305f\u3068\u3057\u3066\u3082\u3001\u305d\u3053\u3067\u30b9\u30ad\u30e3\u30f3\u306f\u6253\u3061\u5207\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>PRIMARY KEY \u306e\u4e0a\u9650\u3068\u4e0b\u9650\u3092\u3001\u6b21\u306e\u3088\u3046\u306aSELECT\u3067\u6c42\u3081\u308b\u3068\u66f8\u304d\u307e\u3057\u305f\u304c<\/p>\n<pre><code>SELECT MIN(id), MAX(id) FROM sejima; \n<\/code><\/pre>\n<p>MySQL\u3060\u3068\u3053\u308c\u306f\u6700\u9069\u5316\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >\nmysql&gt; explain select min(id),max(id) from sejima;\n+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+\n| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                        |\n+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+\n|  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Select tables optimized away |\n+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+\n1 row in set, 1 warning (0.00 sec)\n\nmysql&gt; explain analyze format=tree select min(id),max(id) from sejima\\G\n*************************** 1. row ***************************\nEXPLAIN: -> Rows fetched before execution  (cost=0.00..0.00 rows=1) (actual time=0.000..0.000 rows=1 loops=1)\n\n1 row in set (0.00 sec)\n\nmysql&gt;\n<\/pre>\n<p>MySQL\u4ee5\u5916\u306eRDBMS\u3067\u3082\u3001PRIMARY KEY\u304cB+Tree\u3068\u304b\u3067\u3042\u308c\u3070\u3001MIN\u3084MAX\u3092\u6c42\u3081\u308b\u306e\u306f\u9ad8\u901f\u306b\u5b9f\u884c\u3067\u304d\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u3082\u3057MySQL\u4ee5\u5916\u3067\u3084\u308b\u5834\u5408\u306f\u3001\u304a\u624b\u5143\u306e\u74b0\u5883\u3067\u8a66\u3057\u3066\u3082\u3089\u3046\u306e\u304c\u826f\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u3001 PRIMARY KEY \u304c\u3001UUID\u306a\u3069\u3053\u3046\u3044\u3063\u305f\u7528\u9014\u306b\u5411\u3044\u3066\u306a\u3044\u3082\u306e\u3067\u3042\u308b\u306a\u3089\u3070\u3001 created_at \u306a\u3044\u3057 ctime \u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u308c\u306a\u3044\u304b\u3001\u691c\u8a0e\u3059\u308b\u306e\u3082\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002 \u521d\u56de\u306bINSERT\u3055\u308c\u305f\u3068\u304d\u306e\u307f\u66f4\u65b0\u3055\u308c\u308b\u30ab\u30e9\u30e0\u306f\u3001\u30bb\u30ab\u30f3\u30c0\u30ea\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u308b\u306e\u306b\u306f\u60aa\u304f\u306a\u3044\u5bfe\u8c61\u3060\u3068\u601d\u3044\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u3001 <code>SELECT id FROM table_name WHERE ctime BETWEEN ? AND ? LIMIT 1000;<\/code> \u306a\u3069\u306e\u3088\u3046\u306b ctime \u306e\u7bc4\u56f2\u3092\u5236\u9650\u3059\u308b\u3053\u3068\u3067\u3001\u30b9\u30ad\u30e3\u30f3\u3092\u9ad8\u901f\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u524d\u8ff0\u3057\u305f\u4f8b\u3067\u306f\u3001 MAX(id) - MIN(id) \u306e\u5dee\u5206\u3092\u6c42\u3081\u300110%\u7a0b\u5ea6\u306e id \u304c\u8a72\u5f53\u3059\u308b\u3088\u3046\u306b BETWEEN \u3067\u6307\u5b9a\u3057\u307e\u3057\u305f\u304c\u3001\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u300110%\u672a\u6e80\u3001\u3082\u3063\u3068\u5c11\u306a\u304f\u3066\u3082\u69cb\u3044\u307e\u305b\u3093\u3002 <code>LIMIT 1000<\/code> \u3068\u6307\u5b9a\u3059\u308b\u306e\u3067\u3042\u308c\u3070\u3001 LIMIT \u306b\u5bfe\u3057\u3066\u5145\u5206\u306a\u7bc4\u56f2\u306e BETWEEN \u3067\u3042\u308c\u3070\u3001\u3088\u308a\u8efd\u3044SELECT\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\uff08MySQL\u7684\u306b\u8003\u3048\u308b\u3068\u3001 Innodb_rows_read \u3060\u3051\u3067\u306a\u304f\u3001\u30ed\u30c3\u30af\u306e\u7af6\u5408\u3092\u6e1b\u3089\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\uff09\u3002<\/p>\n<h3>sleep\u306e\u6642\u9593\u3068\u524a\u9664\u3057\u305f\u3044\u884c\u6570\u306b\u3064\u3044\u3066<\/h3>\n<p>\u3053\u3046\u3044\u3063\u305f\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u7684\u306a\u3053\u3068\u3092\u8003\u3048\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u4e00\u4ef6\u4e00\u4ef6DELETE\u3057\u3066\u305f\u3089\u524a\u9664\u304c\u8ffd\u3044\u3064\u304b\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3089\u300150\uff5e1000\u4ef6\u304f\u3089\u3044\u306f\u307e\u3068\u3081\u3066DELETE\u3057\u305f\u3044\u3068\u3053\u308d\u3067\u3057\u3087\u3046\u3002\u307e\u3068\u3081\u3066DELETE\u3059\u308b\u3068\u3001\u30e6\u30fc\u30b6\u304b\u3089\u306e\u66f4\u65b0\u51e6\u7406\u3068\u30ed\u30c3\u30af\u304c\u7af6\u5408\u3057\u3084\u3059\u304f\u306a\u308b\u3067\u3057\u3087\u3046\u304b\u3089\u3001sleep\u3057\u3064\u3064DELETE\u3057\u305f\u65b9\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<br \/>\nsleep\u3057\u3064\u3064DELETE\u3057\u3066\u3082\u8ca0\u8377\u304c\u9ad8\u3044\u3088\u3046\u306a\u3089\u3001\u30d4\u30fc\u30af\u30bf\u30a4\u30e0\u3092\u3055\u3051\u3066\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u3092\u5b9f\u884c\u3057\u305f\u65b9\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002\u4eee\u306b\u3001\u591c\u4e2d\u306e3\u6642\u304b\u30895\u6642\u307e\u3067\u306e\u4e8c\u6642\u9593\u30011000\u4ef6DELETE\u3057\u30663\u79d2sleep\u3068\u3044\u3046\u306e\u3092\u7e70\u308a\u8fd4\u3057\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<p><code>1000*((60*60*2)\/3) = 2,400,000<\/code><\/p>\n<p>2\u6642\u9593\u3067\u6700\u5927240\u4e07\u4ef6\u524a\u9664\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3092\u4e00\u5e74\u9593\u5b9f\u884c\u3057\u305f\u5834\u5408<\/p>\n<p><code>1000*((60*60*2)\/3)*365 = 876,000,000<\/code><\/p>\n<p>\u6700\u59278\u51047600\u4e07\u4ef6\u524a\u9664\u3067\u304d\u307e\u3059\u3002MySQL \u306e\u7b26\u53f7\u4ed8\u304dINT\u306e\u6700\u5927\u5024\u306f2147483647\u306a\u306e\u3067\u3001PRIMARY KEY\u304c<\/p>\n<p><code>id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,<\/code><\/p>\n<p>\u3067\u3042\u308c\u3070\u3001\u4e00\u5e74\u3067\u6700\u5927\u7d044\u5272\u304f\u3089\u3044\u306e\u30c7\u30fc\u30bf\u3092\u6d88\u305b\u308b\u306e\u3067\u3001\u5145\u5206\u306a\u901f\u5ea6\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u4e00\u5e74\u9593\u30678\u5104\u4ef6\u306e\u30da\u30fc\u30b9\u3067\u524a\u9664\u3057\u3066\u3082\u8ffd\u3044\u3064\u304b\u306a\u3044\u5834\u5408\u3001\u4f8b\u3048\u3070<\/p>\n<ul>\n<li>PRIMARY KEY \u304c BIGINT AUTO_INCREMENT \u3067\u3001\u3082\u3063\u3068\u6fc0\u3057\u304fINSERT\u3055\u308c\u3066\u3044\u308b<\/li>\n<li>PRIMARY KEY \u306f INT \u3060\u3051\u3069\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u30b5\u30a4\u30ba\u306e\u5897\u5927\u306e\u901f\u5ea6\u304c\u6fc0\u3057\u3044<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u5834\u5408\u3084<\/p>\n<ul>\n<li>\u304b\u306a\u308a\u8ca0\u8377\u306e\u9ad8\u3044\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306a\u306e\u3067\u3001\u4e00\u5ea6\u306b1000\u4ef6\u3082\u6d88\u305b\u306a\u3044\u3001\u3042\u308b\u3044\u306f\u3001 sleep \u304c3\u79d2\u7a0b\u5ea6\u3067\u306f\u8db3\u308a\u306a\u3044<\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u5834\u5408\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u304c\u3001\u305d\u3046\u3044\u3063\u305f\u5834\u5408\u306f<\/p>\n<p><strong>\u7d20\u76f4\u306b sharding \u3057\u305f\u3089\u826f\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<\/strong><\/p>\n<p>sharding \u3059\u308b\u3053\u3068\u3067\u3001\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u30d0\u30c3\u30c1\u30b8\u30e7\u30d6\u3092\u3001\u5206\u5272\u3057\u305f\u30af\u30e9\u30b9\u30bf\u3054\u3068\u306b\u540c\u6642\u306b\u5b9f\u884c\u3067\u304d\u307e\u3059\u3057\u3001\u305d\u308c\u305e\u308c\u306e source \u306b\u5bfe\u3057\u3066\u4e00\u5ea6\u306b1000\u4ef6\u306eDELETE\u3092\u6295\u3052\u306a\u304f\u3066\u3082\u3001\u5145\u5206\u306a\u901f\u5ea6\u3067\u6d88\u305b\u308b\u3068\u601d\u308f\u308c\u307e\u3059\u3002\u307e\u305f\u3001\u305d\u3053\u307e\u3067\u9ad8\u8ca0\u8377\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u3042\u308c\u3070\u3001\u8ca0\u8377\u5bfe\u7b56\u306e\u89b3\u70b9\u304b\u3089\u3082\u3001sharding\u3059\u308b\u30e1\u30ea\u30c3\u30c8\u306f\u5927\u304d\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u3053\u3046\u3044\u3063\u305f\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u7684\u306a\u3053\u3068\u3092\u8003\u3048\u308b\u5834\u5408\u306f<\/p>\n<ul>\n<li>\u524a\u9664\u3059\u308b\u5bfe\u8c61\u306e\u4ef6\u6570\u306f\u3069\u306e\u7a0b\u5ea6\u304b<\/li>\n<li>\u3069\u306e\u7a0b\u5ea6\u306e\u901f\u5ea6\u3067\u6d88\u305b\u3070\u3088\u3044\u306e\u304b<\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u3053\u3068\u3092\u3001PRIMARY KEY \u306e\u6700\u5927\u5024\u306a\u3069\u304b\u3089\u8003\u3048\u308c\u3070\u826f\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u304a\u308f\u308a\u306b<\/h2>\n<p>\u3060\u3044\u3076\u5730\u5473\u306a\u8a71\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4e00\u3064\u306e\u30b2\u30fc\u30e0\u3084\u30b5\u30fc\u30d3\u30b9\u30925\uff5e10\u5e74\u3068\u7d99\u7d9a\u3057\u3066\u3044\u304f\u3068\u3001\u3042\u307e\u308a\u8efd\u8996\u3067\u304d\u306a\u3044\u3053\u3068\u3067\u3082\u3042\u308b\u304b\u306a\u3068\u601d\u3044\u3001\u3055\u3063\u304f\u308a\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u4eca\u56de\u306f\u5272\u3068\u3086\u308b\u3075\u308f\u306a\u8a71\u3060\u3063\u305f\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002\u305f\u3060\u3001\u3053\u3046\u3044\u3063\u305f\u3053\u3068\u3082\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3068\u3057\u3066\u6b8b\u3057\u3066\u3044\u3063\u305f\u65b9\u304c\u6709\u76ca\u304b\u3082\u306a\u3041\u3068\u601d\u3063\u305f\u306e\u3067\u3001\u4eca\u5f8c\u3082\u305f\u307e\u306b\u306f\u3053\u3046\u3044\u3046\u8a71\u3092\u66f8\u3044\u3066\u3044\u304f\u3064\u3082\u308a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u308f\u3002\u305b\u3058\u307e\u3067\u3059\u3002\u4eca\u56de\u306f\u5730\u5473\u3067\u6ce5\u81ed\u3044\u8a71\u3092\u3057\u307e\u3059\u3002\u305f\u3060\u3001\u5272\u3068\u5e73\u6613\u306a\u5185\u5bb9\u304b\u3068\u601d\u3044\u307e\u3059\u306e\u3067\u3001\u521d\u5b66\u8005\u306e\u65b9\u306b\u3082\u30aa\u30b9\u30b9\u30e1\u3067\u3059\u3002 \u306f\u3058\u3081\u306b \u30b2\u30fc\u30e0\u3067\u306f\u3001\u53d7\u53d6\u671f\u9650\u306e\u3064\u3044\u305f\u30ed\u30b0\u30a4\u30f3\u30dc\u30fc\u30ca\u30b9\u7684\u306a\u3082\u306e\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u30e6\u30fc\u30b6\u304c\u671f\u9650\u307e\u3067\u306b\u53d7 [&hellip;]<\/p>\n","protected":false},"author":137,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[17],"class_list":["post-21562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-info","tag-mysql"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts\/21562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/users\/137"}],"replies":[{"embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/comments?post=21562"}],"version-history":[{"count":3,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts\/21562\/revisions"}],"predecessor-version":[{"id":21564,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts\/21562\/revisions\/21564"}],"wp:attachment":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/media?parent=21562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/categories?post=21562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/tags?post=21562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}