簡體   English   中英

MySQL SELECT與眾不同非常慢

[英]MySQL SELECT distinct very slow

對於我的移動應用程序,我正在使用Web服務從MySQL數據庫(如Instagram)中獲取圖像和所有記錄。

但是SELECT DISTINCT花費了7到8秒以上的時間。請問有什么我可以做的以減少時間嗎?

SELECT distinct count(*) as totalrecord ,p.description,p.photo_id ,p.user_id,p.imagepath1 ,p.friends, p.is_report_photo,
    p.imagepath2,p.imagepath3,p.imagepath4,p.imagepath5,p.count_comment,p.count_like,p.created as photo_created ,

    (select username from users where id = p.user_id) as username , 
    (select country from users where id = p.user_id) as country, 
    (select email from users where id = p.user_id) as email , 
    (select image_path from users where id = p.user_id) as image_path ,
    (select gender from users where id = p.user_id) as gender, 
    (select privacy from users where id = p.user_id) as privacy,
    (select audio_privacy from users where id = p.user_id) as audio_privacy,
    (select video_privacy from users where id = p.user_id) as video_privacy,
    (select photo_privacy from users where id = p.user_id) as photo_privacy,

    If(pl.user_id && pl.photo_id !='',like_status,0) as islike ,
    If(pr.user_id && pr.photo_id !='',1,0) as isreport,
    p.user_visibility

    from friends as fr
    inner join users as u on u.id = (select distinct if (fr.user_id != $user_id,fr.user_id,(fr.to_user_id)) as rd
    from friends) && fr.read_status = '1'
    inner join photos as p on (p.user_visibility = 'p')
    LEFT JOIN photolike as pl ON pl.photo_id = p.photo_id && pl.user_id = $user_id
    LEFT JOIN photo_report as pr on pl. photo_id = pr.photo_id && pr.user_id = $user_id     
    ORDER BY p.photo_id DESC;
Try using group-by clause instead of distinct.

使用mysql索引將減少時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM