优化mysql,分组扫描太多行

时间:2016-12-26 18:14:56

标签: mysql performance optimization

表示状态的表。用户可以在FB上重新共享状态,因此可以重新分享原始数据。

| user_status | CREATE TABLE `user_status` (
  `status_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `destination_user_id` int(11) NOT NULL,
  `original_id` int(11) DEFAULT NULL,
  `type` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`status_id`),
  KEY `IDX_1E527E21A76ED395` (`user_id`),
  KEY `IDX_1E527E21C957ECED` (`destination_user_id`),
  KEY `core_index` (`destination_user_id`,`original_id`),
  CONSTRAINT `FK_1E527E21A76ED395` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
  CONSTRAINT `FK_1E527E21C957ECED` FOREIGN KEY (`destination_user_id`)REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=161362 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

我正在尝试优化新闻源的查询(为了能够优化查询的核心,我删除了所有不必要的内容,但它仍然很慢)。

查询:

EXPLAIN SELECT MAX(us.status_id)
    FROM user_status us 
    WHERE us.destination_user_id IN (25,30,31,32,33,34,35,36,37,38,39,40,42,43,44,46,49,50,51,52,53,55,56,57,58,59,60,62,64,66,68,74,78,79,81,88,91,92,94,96,98,99,100,101,102,106,110,112,113,114,117,124,128,129,133,138,140,144,149,150,151,154,155,156,158,159,160,164,170,174,175,180,184,186,187,210,211,222,225,227,228,231,234,235,236,237,240,264,269,271,276,282,287,289,295,297,298,301,302,311,315,318,322,326,328,345,350,379,396,398,403,404,418,426,428,431,449,460,471,476,477,495,496,506,538,539,540,542,546,551,554,557,559,561,564,571,572,575,585,586,588,590,616,617,624,629,630,641,645,649,654,655,656,657,658,659,660,662,663,673,685,690,693,696,698,724,728,734,737,746,757,760,762,769,791,797,808,829,833,841,857,858,865,878,879,881,888,889,898,919,921,932,937,944,949,950,958,961,965,966,974,980,986,994,996,1005,1012,1013,1019,1020,1027,1044,1062,1079,1081,1097,1121,1122,1131,1140,1174,1178,1199,1214,1219,1221,1259,1261,1262,1268,1277,1282,1294,1300,1307,1320,1330,1331,1333,1336,1350,1361,1371,1388,1393,1440,1464,1482,1497,1507,1509,1511,1513,1514,1525,1537,1558,1569,1572,1573,1577,1584,1588,1591,1593,1627,1644,1645,1666,1688,1716,1729,1735,1751,1756,1803,1818,1828,1867,1871,1876,1914,1935,2038,2047,2058,2072,2074,2085,2106,2153,2168,2197,2232,2279,2355,2359,2511,2560,2651,2773,2803,2812,2818,2829,2835,2841,2865,2891,3032,3051,3095,3100,3148,3412,3476,3578,3623,3808,3853,3968,3976,3992,4045,4047,4069,4077,4119,4156,4237,4271,4280,4285,4337,4348,4644,4711,4872,4898,5084,5108,5110,5248,5254,5266,5268,5315,5318,5553,5716,5744,5768,5782,5784,5794,5815,5883,5920,5921,5985,5987,6016,6070,6364,7067,7522,7571,7733,7800,8259,8421,8640,9743,10039,11900,12344,12794,13419,13468,13548,13778,13829,13892,13902,13910,13976,13977,14042,14056,14171,14175,14176,14210,14255,14258,14279,14301,14343,14394,14465,14501,14538,14650,14656,14657,14805,14807,14813,14970,14975,15110,15174,15277,15284,15306,15354,15404,15649,15710,15776,16084,16099,14752,16516,1130,9770,1127,14200,13950,15842,16406,15614,16566,16209,16672,13887,16122,14857,16877,10093,15752,16131,17618,17767,5783,17867,16081,18224,6972,14273,18471,15403,16261,6641,18669,15153,18708,18534,17447,18843,18840,27,61,18656,18336,18006,15337,17197,18999,14360,19023,19002,16856,2885,17237,16560,15575,16297,11199,17836,14313,759,18403,19421,19514,2828,14562,1792,18131,19703,1280,18314,15944,17078,18316,19695,20017,16493,19566,17028,19104,17518,2045,16312,15508,20092,5060,18207,1773,17129,17154,18786,17077,15155,17640,2845,19480,20943,107,2775,21247,3989,20292,19077,20046,18230,18241,18102,19225,
         14230,21011,5765,15344,21732,11249,15532,14105,4136,17373,14612,17944,17040,15505,17528,20461,22200,14059,11701,19410,3085,12180,22730,22631,17673,2820,20826,21895,23992,24080,24249,25144,25146,25171,25177,25181,25222,25223,25232,25245,25248,25250,25252,25255,25264,25267,25276,25279,25280,25284,25294,25298,25300,25312,25324,25332,25359,25373,25374,25381,25402,25412,25430,25434,25437,25442,25444,25446,25454,25465,25474,25486,25490,25491,25494,25535,25540,25549,25555,25568,25671,25711,25713,25714,25722,25737,25755,25768,25774,
         25783,25784,25839,25854,25886,25889,25891,25913,25926,25956,25967,26026,26043) 
    GROUP BY us.original_id
    ORDER BY us.status_id DESC
    LIMIT 0,10;

解释查询:

enter image description here

执行时间:10行(0,41秒),MySQL 5.7(严格模式关闭)

伊莫。一个小到100k行的表应该表现得更好。我试图上下改变索引,但它们似乎已正确设置。

任何想法如何将此查询优化为0.0x或0.1x?

更新

链接的副本与我的问题无关,不应与imo链接。

删除不必要的额外连接解决了该问题。

enter image description here

现在它可以通过使用紧密索引扫描http://dev.mysql.com/doc/refman/5.7/en/group-by-optimization.html

来实现

我无法摆脱“使用临时;使用filesort”,即使我将ORDER BY更改为“us.original_id”,但执行时间现在正如预期的那样: 0.08

1 个答案:

答案 0 :(得分:1)

删除两个额外的表,因为us似乎是唯一相关的表。

它对ORDER BY status_id无效,因为它不在GROUP BY中,也不在SELECT中(技术上)..