我知道这是一个普遍的问题。
下面的查询很慢。我对所有 where 语句都有索引。我可以提出一些加快速度的建议:
- 如果我传入较少的参数并直接在查询中使用这些参数,我会失去灵活性——这是否会加快查询速度。
- 有没有更快的选择方法。我不一定需要选择列。我可以简单查询一下列不在选择中的位置,就像标签、集合的情况一样
- 选择语句中的子查询比执行连接慢 - 我可以优化第一个选择中的地址计数以改进事情。
任何最有帮助的建议
BEGIN
DECLARE user_id int;
SET user_id =(SELECT im.user_id FROM image im WHERE im.id = in_image_id);
/*Image details*/
SELECT STRAIGHT_JOIN im.id, im.alias_title, im.title,im.guid_id, im.description, im.hits, im.show_comment, im.can_print, im.can_download, im.can_share, im.created_on, im.date_taken, im.approved, im.visible,
(SELECT count(*) from image where image.address_id = im.address_id) as address_count,
ad.id as image_address_id, ad.address_line_1, ad.address_line_2, ad.town_village_city, ad.state_province_county, ad.postal_code, ad.other_address_detail, co.country,
geo.latitude, geo.longitude, geo.zoom, geo.yaw, geo.pitch,
c.make, c.model,
us.first_name, us.surname, uf.user_id, uf.real_name, uf.user_name, uf.gender, uf.description as user_description, uf.buddy_icon_url, uf.first_taken_date, uf.first_date,
uf.time_zone_label, uf.time_zone_offset,
adf.id as user_address_id,
adf.address_line_1 as user_address_line_1, adf.address_line_2 as user_address_line_2, adf.town_village_city as user_town_village_city, adf.state_province_county as user_state_province_county,
adf.postal_code as user_postal_code, adf.other_address_detail as user_other_address_detail, cof.country as user_country,
geof.latitude as user_geolocation_latitude, geof.longitude as user_geolocation_longitude, geof.zoom as user_geolocation_zoom, geof.yaw as user_geolocation_yaw, geof.pitch as user_geolocation_pitch,
im.id = in_image_id AS image_selected -- image selected
FROM image im
LEFT JOIN address ad ON im.address_id = ad.id
LEFT JOIN country co ON ad.country_id = co.id
LEFT JOIN geolocation geo ON im.geolocation_id = geo.id
LEFT JOIN camera c ON im.camera_id = c.id
INNER JOIN user us ON im.user_id = us.id
LEFT JOIN user_flickr uf ON us.id = uf.id
LEFT JOIN address adf ON uf.address_id =adf.id
LEFT JOIN country cof ON ad.country_id = cof.id
LEFT JOIN geolocation geof ON uf.geolocation_id = geof.id
WHERE im.user_id = user_id
AND im.approved = in_image_approved
AND im.visible = in_image_visible
AND (im.advertise_to <= NOW() OR im.advertise_to IS NULL)
ORDER BY image_selected DESC, date_taken DESC;
/* Select albums for a user and albums the image belongs to */
SELECT a.alias_title, a.title, a.hits, a.created_on, a.updated_on, a.updated_by,
im.id = in_image_id AS image_albums -- album belongs to
FROM album a
JOIN album_image ai ON a.id = ai.album_id
JOIN image im ON ai.image_id = im.id
WHERE a.user_id = user_id
AND a.approved = in_album_approved
AND a.visible = in_album_visible
GROUP BY alias_title
ORDER BY image_albums DESC;
/*Tags*/
SELECT ta.id, ta.raw,ta.title, ta.hits as tag_hits, it.hits as image_tag_hits,
(SELECT count(*) from image_tag where image_tag.tag_title_id = ta.id) as tag_count
FROM tag_title ta
INNER JOIN image_tag it ON ta.id = it.tag_title_id
INNER JOIN image im ON it.image_id = im.id
WHERE im.id = in_image_id HAVING tag_count >1;
/*Comments*/
SELECT fc.comment_html, fc.author_user_id, fc.author_user_name,
fc.permalink, fc.author_buddy_icon
FROM flickr_comment fc
INNER JOIN image_flickr_comment ifc ON fc.id = ifc.comment_id
INNER JOIN image im ON ifc.image_id = im.id;
/*Sets*/
SELECT ms.alias_title, ms.title, ms.photo_set_small_url,
(SELECT count(*) from image_media_set where image_media_set.media_set_id = ms.id) as media_set_count
FROM media_set ms
INNER JOIN image_media_set ims ON ms.id = ims.media_set_id
INNER JOIN image im ON ims.image_id = im.id
WHERE im.id = in_image_id HAVING media_set_count >1;
/*Media Galleries*/
SELECT mg.alias_title, mg.title, mg.primary_photo_medium_url,
(SELECT count(*) from image_media_gallery where image_media_gallery.media_gallery_id = mg.id) as media_gallery_count
FROM media_gallery mg
INNER JOIN image_media_gallery img ON mg.id = img.media_gallery_id
INNER JOIN image im ON img.image_id = im.id
WHERE im.id = in_image_id HAVING media_gallery_count >1;
/*Media Groups*/
/*SELECT mg.alias_title, mg.name, mg.icon_url FROM media_group mg
INNER JOIN image_media_group img ON mg.id = img.media_group_id
INNER JOIN image im ON img.image_id = im.id
WHERE im.id = in_image_id;*/
SELECT mg.alias_title, mg.name, mg.icon_url,
(SELECT count(*) from image_media_group where image_media_group.media_group_id = mg.id) as media_group_count
FROM media_group mg
INNER JOIN image_media_group img ON mg.id = img.media_group_id
INNER JOIN image im ON img.image_id = im.id
WHERE im.id = in_image_id HAVING media_group_count >1;
END