I m running mysql to run a website and I have some mediocre complexity queries which usually take around 45% cpu and sometimes it shoots to 80% cpu usage.
I need to bring the cpu usage down. Any means so that I can switch mysql to high performance mode or anything to optimize the queries such as below
Example -
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
In the example above, you can see the i m searching using concat('%',search_nm,'%')
. I have indexed full text on that column. Should I be searching like this or by natural language method?
The view vw_shout is having the query -
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`