1

私はこれが一般的な質問であることを知っています。

以下のクエリは非常に遅いです。すべての where ステートメントにインデックスを付けています。私はそれをスピードアップするためのいくつかのアドバイスで行うことができます:

  1. 渡すパラメーターを少なくして、これらをクエリに直接含めると、柔軟性が失われます。これにより、クエリが高速化されますか。
  2. 持っていることを選択するより迅速な方法はありますか。必ずしも列を選択する必要はありません。タグ、セットの場合のように、列が選択されていない場所を簡単にクエリできますか
  3. 選択ステートメントのサブクエリは、結合を実行するよりも遅くなります。最初の選択でアドレス数を最適化して、物事を改善できますか。

最も役立つアドバイス

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
4

0 に答える 0