如何优化MySQL查询

时间:2014-11-11 02:35:39

标签: php mysql mysqli

我的VPS CPU数量:2 内存:2048 MB 存储:虚拟40 GB

我的查询使CPU总是很高(150% - 190%)。 这个查询使用了很多,每个成员平均1000次/天

SELECT turn_member.idmem,turn_member.idturn,turn_member.id,now()
    FROM turn_member JOIN members ON members.id=turn_member.idmem JOIN upgrade ON members.level=upgrade.level AND turn_member.idturn=upgrade.idturn
    WHERE 
            active=1    
        AND 
            turn_member.idmem<>$idmem
        AND 
            (SELECT point FROM members WHERE id=turn_member.idmem)
            >
            (SELECT count(1) FROM cache_members WHERE idmemview=turn_member.idmem AND cache_members.done=0 AND del_temp=0 AND UNIX_TIMESTAMP()-UNIX_TIMESTAMP(date_start)<60*20)*10+20
        AND (
                turn_member.cpd=0 
            OR
                (SELECT count(1) FROM cache_members WHERE id_turn_member=turn_member.id AND extract(day from cache_members.date_start)=extract(day from NOW())) 
                < 
                turn_member.cpd
        )
        AND (
                turn_member.cph=0 
            OR
                (SELECT count(1) FROM cache_members WHERE id_turn_member=turn_member.id AND extract(hour from cache_members.date_start)=extract(hour from NOW())) 
                < 
                turn_member.cph
        )
        AND 
            (SELECT count(1) FROM cache_members WHERE cache_members.idmemview=turn_member.idmem AND cache_members.id_turn=turn_member.idturn AND cache_members.done=1 AND extract(day from cache_members.date_start)=extract(day from NOW())) 
            < 
            upgrade.view_per_day
        AND 
            turn_member.id NOT IN (SELECT id_turn_member FROM cache_members WHERE idmem=$idmem AND del_temp=0 )
        AND 
            turn_member.id NOT IN (SELECT idlink FROM `report_link` WHERE idmem=$idmem)
    ORDER BY rand() DESC
    LIMIT 0,1

请帮我优化一下, 抱歉,我不得不使用谷歌翻译来写这篇文章 .my英语不好

0 个答案:

没有答案