为什么这些查询需要这么长时间?怎么解决?

时间:2016-08-06 03:25:31

标签: mysql sql

我的mysql慢查询日志中显示以下查询:

SELECT Count(*) AS aggregate 
FROM   `posts` 
       INNER JOIN `post_stats` 
               ON `posts`.`id` = `post_stats`.`post_id` 
WHERE  `posts`.`group_id` IN ( '1', '2', '20', '21', 
                                  '22', '25', '23', '53', 
                                  '24', '3', '16', '64', 
                                  '10', '4', '5', '7', 
                                  '26', '28', '27', '32', 
                                  '29', '30', '31', '36', 
                                  '68', '65', '54', '37', 
                                  '38', '67', '45', '33', 
                                  '34', '39', '35', '69', 
                                  '46', '55', '66', '44', 
                                  '47', '6', '49', '50', 
                                  '59', '57', '56', '58', '9' );

查询需要将近3秒钟才能完成。

同样,此查询需要将近5秒钟才能运行:

SELECT * 
FROM   `post_shares` 
WHERE  `post_shares`.`post_id` IN ( '127867', '127784', '127454', '127473' 
                                          , 
                                          '127465', '127870', '127871', '127444' 
                                          , 
                                          '127866', '127872', '127865', '127468' 
                                          , 
                                          '127463', '127451', '127464', '127442' 
                                          , 
                                          '127489', '127479', '127863', '127487' 
                                          , 
                                          '127697', '127457', '127859', '127472' 
                                          , 
                                          '127579', '127856', '127452', '127862' 
                                          , 
                                          '127429', '127422', '127475', '127854' 
                                          , 
                                          '127518', '127431', '127852', '127405' 
                                          , 
                                          '127853', '127850', '127402', '127846' 
                                          , 
                                          '127520', '127838', '127437', '127839' 
                                          , 
                                          '127421', '127390', '127842', '127388' 
                                          , 
                                          '127837', '127396', '127851', '127822' 
                                          , 
                                          '127364', '127363', '127362', '127361' 
                                          , 
                                          '127828', '127825', '127820', '127387' 
                                          , 
                                          '127408', '127354', '127380', '127458' 
                                          , 
                                          '127353', '127453', '127344', '127910' 
                                          , 
                                          '127395', '127372', '127832', '127346' 
                                          , 
                                          '127805', '127812', '127342', '127352' 
                                          , 
                                          '127334', '127332', '127804', '127330' 
                                          , 
                                          '127407', '127373', '127333', '127830' 
                                          , 
                                          '127798', '127350', '127300', '127801' 
                                          , 
                                          '127283', '127308', '127351', '127292' 
                                          , 
                                          '127322', '127303', '127459', '127793' 
                                          , 
                                          '127280', '127274', '127312', '127271' 
                                          , 
                                          '127278', '127313', '127796', '127277' 
                                          , 
                                          '127302', '127262', '127284', '127969' 
                                          , 
                                          '127964', '127323', '127790', '127782' 
                                          , 
                                          '127252', '127282', '127844', '127777' 
                                          , 
                                          '127249', '127275', '127776', '127238' 
                                          , 
                                          '127772', '127263', '127254', '127233' 
                                          , 
                                          '127795', '127773', '127251', '127217' 
                                          , 
                                          '127216', '127215', '127774', '127768' 
                                          , 
                                          '127205', '127218', '127270', '127241' 
                                          , 
                                          '127197', '127210', '127767', '127450' 
                                          , 
                                          '127759', '127946', '127219', '127189' 
                                          , 
                                          '127755', '127220', '127756', '127221' 
                                          , 
                                          '127231', '127195', '127309', '127204' 
                                          , 
                                          '127196', '127172', '127255', '127714' 
                                          , 
                                          '127183', '127174', '127184', '127928' 
                                          , 
                                          '127927', '127754', '127160', '127168' 
                                          , 
                                          '127147', '127171', '127747', '127750' 
                                          , 
                                          '127132', '127131', '127130', '127746' 
                                          , 
                                          '127122', '127161', '127153', '127741' 
                                          , 
                                          '127749', '127169', '127110', '127108' 
                                          , 
                                          '127762', '127106', '127102', '127109' 
                                          , 
                                          '127737', '127096', '127185', '127094' 
                                          , 
                                          '127734', '127116', '127133', '127100' 
                                          , 
                                          '127150', '127087', '127222', '127158' 
                                          , 
                                          '127085', '127117', '127720', '127717' 
                                          , 
                                          '127206', '127912', '127619', '127726' 
                                          , 
                                          '127709', '127710', '127430', '127708' 
                                          , 
                                          '127056', '127295', '127250', '127223' 
                                          , 
                                          '127055', '127066', '127057', '127048' 
                                          , 
                                          '127068', '127054', '127712', '127107' 
                                          , 
                                          '127701', '127043', '127340', '127591' 
                                          , 
                                          '127692', '127065', '127038', '127698' 
                                          , 
                                          '127693', '127078', '127691', '127029' 
                                          , 
                                          '127028', '127694', '127075', '127051' 
                                          , 
                                          '127695', '127134', '127592', '127301' 
                                          , 
                                          '127040', '127763', '127683', '127730' 
                                          , 
                                          '127020', '127019', '127632', '127684' 
                                          , 
                                          '127725', '127679', '127093', '127568' 
                                          , 
                                          '127685', '127675', '127013', '127031' 
                                          , 
                                          '127010', '127681', '127009', '127032' 
                                          , 
                                          '127006', '127194', '127002', '127005' 
                                          , 
                                          '127001', '127016', '127668', '127033' 
                                          , 
                                          '126996', '127324', '127686', '127662' 
                                          , 
                                          '126989', '127669', '127670', '127672' 
                                          , 
                                          '127674', '127030', '126987', '127656' 
                                          , 
                                          '126985', '126981', '127643', '127657' 
                                          , 
                                          '127014', '126991', '127764', '127671' 
                                          , 
                                          '127493', '127637', '127638', '127076' 
                                          , 
                                          '126968', '126961', '127651', '127658' 
                                          , 
                                          '127649', '127646', '127214', '126967' 
                                          , 
                                          '126962', '127624', '127620', '127008' 
                                          , 
                                          '127650', '126963', '126955', '126954' 
                                          , 
                                          '127041', '126976', '127256', '127621' 
                                          , 
                                          '126933', '126995', '126931', '127612' 
                                          , 
                                          '126948', '127613', '126940', '127622' 
                                          , 
                                          '126935', '126928', '126993', '127602' 
                                          , 
                                          '127601', '127604', '127607', '126964' 
                                          , 
                                          '127126', '126980', '126950', '126986' 
                                          , 
                                          '126918', '126917', '127595', '126915' 
                                          , 
                                          '126924', '127593', '127052', '127460' 
                                          , 
                                          '127061', '127611', '127118', '127608' 
                                          , 
                                          '127585', '126974', '127571', '127569' 
                                          , 
                                          '127597', '126904', '127598', '127596' 
                                          , 
                                          '127890', '126949', '126903', '127594' 
                                          , 
                                          '126938', '126890', '127583', '126892' 
                                          , 
                                          '127580', '127600', '126888', '127551' 
                                          , 
                                          '127552', '127572', '127558', '127573' 
                                          , 
                                          '127829', '127821', '126916', '127537' 
                                          , 
                                          '126934', '127553', '126881', '127538' 
                                          , 
                                          '127296', '127127', '127539', '127561' 
                                          , 
                                          '127567', '126870', '127581', '126979' 
                                          , 
                                          '126871', '126887', '126878', '127574' 
                                          , 
                                          '127584', '126893', '126864', '126977' 
                                          , 
                                          '126896', '126880', '126879', '127532' 
                                          , 
                                          '126911', '126858', '126856', '126994' 
                                          , 
                                          '127528', '126978', '127625', '127521' 
                                          , 
                                          '127128', '127529', '126872', '127073' 
                                          , 
                                          '127819', '127818', '127817', '126850' 
                                          , 
                                          '127542', '127543', '127509', '127544' 
                                          , 
                                          '127524', '127515', '126845', '127504' 
                                          , 
                                          '126883', '127526', '127512', '127517' 
                                          , 
                                          '127510', '127564', '127554', '126877' 
                                          , 
                                          '127506', '127531', '126836', '126848' 
                                          , 
                                          '127497', '127511', '127502', '127522' 
                                          , 
                                          '127507', '127505', '126843', '127498' 
                                          , 
                                          '126969', '127503', '127499', '126897' 
                                          , 
                                          '126842', '126829', '126827', '127486' 
                                          , 
                                          '126898', '127500', '126852', '126822' 
                                          , 
                                          '127477', '127495', '127470', '126837' 
                                          , 
                                          '127476', '127491', '127480', '126814' 
                                          , 
                                          '126925', '126810', '126809', '126804' 
                                          , 
                                          '126803', '127095', '127516', '127496' 
                                          , 
                                          '127481', '127461', '126835', '126788' 
                                          , 
                                          '126851', '126899', '126900', '127570' 
                                          , 
                                          '127513', '127485', '127007', '126777' 
                                          , 
                                          '126794', '127478', '127471', '126841' 
                                          , 
                                          '126771', '127456', '127462', '127482' 
                                          , 
                                          '127440', '127492', '126768', '126767' 
                                          , 
                                          '126819', '126975', '126901', '126823' 
                                          , 
                                          '126790', '126849', '126761', '127455' 
                                          , 
                                          '126785', '126772', '126765', '126825' 
                                          , 
                                          '126749', '126748', '127433', '126753' 
                                          , 
                                          '126780', '126752', '126781', '126801' 
                                          , 
                                          '127434', '126779', '127443', '126745' 
                                          , 
                                          '126815', '127447', '127441', '126737' 
                                          , 
                                          '127426', '127427', '126744', '126926' 
                                          , 
                                          '127417', '126769', '126927', '127438' 
                                          , 
                                          '126770', '126715', '126714', '127419' 
                                          , 
                                          '126718', '126735', '127425', '126958' 
                                          , 
                                          '126724', '127063', '127062', '126758' 
                                          , 
                                          '127406', '127420', '127428', '127416' 
                                          , 
                                          '127423', '127432', '126736', '127414' 
                                          , 
                                          '127404', '127415', '127410', '127424' 
                                          , 
                                          '127775', '126951', '127397', '126687' 
                                          , 
                                          '127615', '127413', '126677', '126747' 
                                          , 
                                          '126672', '126671', '126668', '126679' 
                                          , 
                                          '127401', '127399', '126816', '127369' 
                                          , 
                                          '126662', '126701', '127398', '126784' 
                                          , 
                                          '127411', '127394', '126694', '126711' 
                                          , 
                                          '127382', '126824', '127374', '126678' 
                                          , 
                                          '126654', '126653', '126695', '126650' 
                                          , 
                                          '126676', '127392', '127631', '126696' 
                                          , 
                                          '127384', '126643', '126642', '127359' 
                                          , 
                                          '127370', '126708', '127371', '127348' 
                                          , 
                                          '127129', '127379', '127385', '127349' 
                                          , 
                                          '127365', '127375', '127366', '127357' 
                                          , 
                                          '127345', '127367', '127368', '127376' 
                                          , 
                                          '127338', '127360', '127358', '126644' 
                                          , 
                                          '127339', '127386', '127343', '126670' 
                                          , 
                                          '126634', '127337', '126630', '127377' 
                                          , 
                                          '127347', '126667', '127335', '126638' 
                                          , 
                                          '126604', '127341', '126623', '126697' 
                                          , 
                                          '127336', '127326', '126639', '126592' 
                                          , 
                                          '127327', '126640', '126581', '126675' 
                                          , 
                                          '127391', '127466', '126610', '126631' 
                                          , 
                                          '126586', '127314', '126568', '127315' 
                                          , 
                                          '126607', '127740', '126562', '127318' 
                                          , 
                                          '127317', '127115', '126618', '127307' 
                                          , 
                                          '127328', '126589', '126641', '127329' 
                                          , 
                                          '127320', '126569', '126577', '127304' 
                                          , 
                                          '126603', '127319', '126755', '127325' 
                                          , 
                                          '127321', '126549', '127293', '126567' 
                                          , 
                                          '127310', '126632', '127316', '126561' 
                                          , 
                                          '126560', '126698', '127299', '126578' 
                                          , 
                                          '126595', '127291', '127305', '127286' 
                                          , 
                                          '127285', '126699', '127306', '127279' 
                                          , 
                                          '127288', '127276', '127735', '127724' 
                                          , 
                                          '127289', '126530', '127381', '127267' 
                                          , 
                                          '126538', '126546', '126521', '127287' 
                                          , 
                                          '127261', '126518', '127259', '127290' 
                                          , 
                                          '126520', '126511', '127268', '126523' 
                                          , 
                                          '127266', '127265', '126689', '127273' 
                                          , 
                                          '127269', '127281', '127258', '126700' 
                                          , 
                                          '126496', '127253', '126517', '126628' 
                                          , 
                                          '127272', '126476', '126596', '126587' 
                                          , 
                                          '127248', '126828', '126472', '126738' 
                                          , 
                                          '126656', '126516', '127245', '127264' 
                                          , 
                                          '126524', '127239', '126466', '127247' 
                                          , 
                                          '127225', '126537', '127240', '127689' 
                                          , 
                                          '127232', '127235', '127234', '126457' 
                                          , 
                                          '127244', '127213', '127230', '127237' 
                                          , 
                                          '127297', '126448', '126525', '126462' 
                                          , 
                                          '127226', '126444', '126526', '127227' 
                                          , 
                                          '126458', '127246', '126471', '126438' 
                                          , 
                                          '126691', '126501', '127193', '127483' 
                                          , 
                                          '126436', '126445', '126442', '126441' 
                                          , 
                                          '126455', '126470', '127229', '127207' 
                                          , 
                                          '126488', '126443', '126454', '127224' 
                                          , 
                                          '127191', '126430', '127203', '126720' 
                                          , 
                                          '126719', '126504', '127209', '126423' 
                                          , 
                                          '126419', '126721', '127187', '126414' 
                                          , 
                                          '126739', '127211', '127186', '126439' 
                                          , 
                                          '127190', '126605', '126704', '127546' 
                                          , 
                                          '127228', '127173', '127200', '126398' 
                                          , 
                                          '126397', '126396', '126440', '126487' 
                                          , 
                                          '127163', '127167', '127198', '127175' 
                                          , 
                                          '127178', '126408', '126390', '127170' 
                                          , 
                                          '127182', '127177', '127188', '126424' 
                                          , 
                                          '126384', '127180', '126399', '127467' 
                                          , 
                                          '127547', '127164', '127199', '126372' 
                                          , 
                                          '126467', '126468' ) 
ORDER  BY `created_at` DESC; 

他们为什么要这么久?我怎样才能加速它们?

4 个答案:

答案 0 :(得分:2)

可能会产生问题的一些事情。

看起来您正在尝试过滤整数,但是您将它们作为字符串传递。不应该让它陷入困境,但在过去将整个过滤器作为字符串过滤后,我得到了奇怪的结果。

您只获取记录数,因此将其限制为一列,以便通过计数没有点传递整个数据集。

尝试类似以下内容

SELECT Count([UniqueColumn]) AS aggregate 
FROM   `posts` 
       INNER JOIN `post_stats` 
               ON `posts`.`id` = `post_stats`.`post_id` 
WHERE  `posts`.`group_id` IN (    1,  2,      22, 25,
                                  24, 3,      10, 4, 
                                  26, 28,     29, 30,
                                  68, 65,     38, 67,
                                  34, 39,     46, 55,
                                  47, 6,      59, 57)

答案 1 :(得分:1)

检查您是否正确定义了索引。特别是对于第一个请求,您需要一个索引,如:

CREATE INDEX posts_id_group_id_index ON `posts` (`id`, `group_id`)

和第二个:

CREATE INDEX post_shares_post_id_index ON `post_shares`(`post_id`, `created_at`)

答案 2 :(得分:1)

使用IN时,必须对每一行测试一次条件。这不是一个"大" IN条件很小的问题,但是当你处理这么大的一套时,它会让事情变得很慢。

我认为你应该做两件事:

  1. 确保您要过滤的列已编入索引。
  2. 如果可能,请将要用作过滤条件的值提供给临时表,对其编制索引,然后使用INNER JOIN执行查询。

答案 3 :(得分:1)

function type(object){ var stringConstructor = "test".constructor; var arrayConstructor = [].constructor; var objectConstructor = {}.constructor; if (object === null) { return "null"; } else if (object === undefined) { return "undefined"; } else if (object.constructor === stringConstructor) { return "String"; } else if (object.constructor === arrayConstructor) { return "Array"; } else if (object.constructor === objectConstructor) { return "Object"; } else { return "null"; } } 个列表非常慢。尝试将其描述为一系列范围:

IN (...)
相关问题