有人可以告诉我如何减少使用此查询的时间?
这是SQL查询:
SELECT
`i`.`id`,
`i`.`local_file`,
`i`.`remote_file`,
`i`.`remote_file_big`,
`i`.`image_name`,
`i`.`description`,
IF(`i`.`prevent_sync`='1', '5', `i`.`status`) `status`,
GROUP_CONCAT(`il`.`user_id` SEPARATOR ',') AS `likes`,
COUNT(`il`.`user_id`) AS `likes_count`
FROM `images` `i`
LEFT JOIN `image_likes` `il` ON (`il`.`image_id`=`i`.`id`)
WHERE 1 AND `i`.`created` < DATE_SUB(CURDATE(), INTERVAL 48 HOUR)
GROUP BY `i`.`id`
ORDER BY `likes_count` DESC LIMIT 3 OFFSET 0;
在检查查询时间时,结果如下:
# Query_time: 9.948511 Lock_time: 0.000181 Rows_sent: 3 Rows_examined: 4730490
# Rows_affected: 0
表image_likes
:
id (Primary) int(11)
local_file varchar(100)
orig_name varchar(100)
remote_file varchar(1000)
remote_file_big varchar(1000)
remote_page varchar(1000)
image_name varchar(50)
image_name_eng varchar(50)
user_idIndex int(11)
author varchar(50)
credit varchar(250)
credit_eng varchar(250)
location varchar(50)
description varchar(500)
description_eng varchar(275)
notes varchar(550)
categoryIndex int(11)
date_range varchar(50)
createdIndex datetime
license enum('1', '2', '3')
status enum('0', '1', '2', '3', '4')
locked enum('0', '1')
watch_list enum('0', '1', '2')
url_title varchar(100)
url_data varchar(8192)
rem_date datetime
rem_notes varchar(500)
original_url varchar(1000)
prevent_sync enum('0', '1')
checked_by int(11)
system_recommended enum('0', '1')
请建议。
答案 0 :(得分:0)
这可能会受到{inflate-deflate'综合症的影响,这种综合症经常发生在$locations=(get-azurermlocation).Location
foreach ($location in $locations)
{
$usage=Get-AzureRmVmUsage -Location $location
"Now, location is $location" |out-file D:\test1.csv -Append
$usage | out-file d:\test1.csv -Append
}
+ JOIN
。此外,它通常会导致错误的聚合值。
GROUP BY
此变体将排除likes_count = 0的行,但这似乎是合理的。
它假定SELECT `id`, `local_file`, `remote_file`,
`remote_file_big`, `image_name`, `description`,
IF(`prevent_sync`='1', '5', `status`) `status`,
s.likes, s.likes_count
FROM `images` AS `i`
JOIN
( SELECT GROUP_CONCAT(user_id SEPARATOR ',') AS likes,
COUNT(*) AS likes_count
FROM `image_likes`
GROUP BY image_id
ORDER BY `likes_count` DESC
LIMIT 3 OFFSET 0;
) AS s ON s.`image_id`=`i`.`id`
WHERE `created` < CURDATE() - INTERVAL 2 DAY
ORDER BY `likes_count` DESC;
的{{1}}为PRIMARY KEY
。
images
需要id
,并会对该表进行一次扫描。然后只有3次查找到image_likes
。
原始查询必须扫描INDEX(user_id)
的所有行,并重复扫描所有images
。