mysql的cpu使用率非常高

时间:2017-09-16 19:14:45

标签: mysql sqlperformance sql-optimization

我正在运行mysql来运行一个网站,我有一些平庸的复杂性查询,通常需要大约45%的cpu,有时它会占用80%的cpu。

我需要降低cpu使用率。任何方法,以便我可以将mysql切换到高性能模式或任何东西来优化查询,如下面的

示例 -

CREATE PROCEDURE `sp_sel_search_shout_new`(sid int, search_nm varchar(100), sh_id int, lt int)
BEGIN       
if(sid = 0) then        

    select s.*,l.auto_id as 'like' from vw_shout s 
    left join (select * from tbl_like where user_id=sid) l on s.shout_id = l.shout_id
        where s.shout_msg like  concat('%',search_nm,'%') 
        and s.shout_id > sh_id 
        and ( s.visibility is null or s.visibility < 2 )
        group by s.shout_id order by shout_id desc limit lt;


else 

    select t.* from (
    SELECT s.*, l.auto_id as 'like',f.friendid 
    from vw_shout s 
        left join (select * from tbl_like where user_id=sid) l on s.shout_id = l.shout_id       
    left join( select * from (select (case when userid=sid then friend_userid else userid end) 'FriendID' 
    from friends where userid=sid or friend_userid=sid) t union select sid) f on s.user_id = f.friendid   
    where s.shout_id > sh_id and s.shout_msg like concat('%',search_nm,'%')
    group by s.shout_id ) t
where (t.user_id is null) or ( t.visibility is null or t.visibility < 2 ) 
or (t.visibility<=2 and t.friendid is not null)
order by t.shout_id desc limit lt;
end if;
END

在上面的示例中,您可以看到我使用concat('%',search_nm,'%')进行搜索。我已在该列上编制索引全文。我应该像这样或通过自然语言方法进行搜索吗?

视图vw_shout正在使用查询 -

CREATE 
ALGORITHM = UNDEFINED 
SQL SECURITY DEFINER
VIEW `vw_shout` AS
SELECT 
    `s`.`shout_id` AS `shout_id`,
    `s`.`user_id` AS `user_id`,
    `s`.`shout_msg` AS `shout_msg`,
    `s`.`name` AS `name`,
    `s`.`time` AS `time`,
    `s`.`visibility` AS `visibility`,
    `s`.`ip_address` AS `ip_address`,
    `s`.`share_id` AS `share_id`,
    `r`.`FirstName` AS `firstname`,
    `r`.`LastName` AS `lastname`,
    `r`.`r_username` AS `r_username`,
    `r`.`profile_image_id` AS `profile_image_id`,
    COUNT(DISTINCT `t`.`auto_id`) AS `likes`,
    COUNT(DISTINCT `c`.`sh_comm_id`) AS `comments`,
    COUNT(DISTINCT `i`.`image_id`) AS `img_count`,
    COUNT(DISTINCT `p`.`shout_id`) AS `shares`
FROM
    (((((`shouts` `s`
    LEFT JOIN `tbl_like` `t` ON ((`s`.`shout_id` = `t`.`shout_id`)))
    LEFT JOIN `shout_comment` `c` ON ((`s`.`shout_id` = `c`.`shout_id`)))
    LEFT JOIN `image_shout` `i` ON ((`s`.`shout_id` = `i`.`shout_id`)))
    LEFT JOIN `roleuser` `r` ON ((`s`.`user_id` = `r`.`User_ID`)))
    LEFT JOIN `shouts` `p` ON ((`s`.`shout_id` = `p`.`share_id`)))
GROUP BY `s`.`shout_id`

0 个答案:

没有答案