MySQL SELECT明显很慢

时间:2015-03-16 06:14:09

标签: php mysql

对于我的移动应用程序,我正在使用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;

2 个答案:

答案 0 :(得分:1)

Try using group-by clause instead of distinct.

答案 1 :(得分:1)

使用可缩短时间的mysql indexing