有用户反馈show processlist列表出现一堆查询,导致CPU直接跑满24核.....
后来查看ip来源于网页端的phpadmin,但是开发人员均反馈没有执行该操作,只是有同事点击打开了这个库,仔细看了下这个出问题的sql的表机构,发现原来它是一个写得超级烂的VIEW
mysql> show create table t_yqrx_contact_user_redit\G
*************************** 1. row ***************************
View: t_yqrx_contact_user_redit
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`webapp`@`%` SQL SECURITY DEFINER VIEW `t_yqrx_contact_user_redit` AS select `uc`.`id` AS `id`,`uc`.`user_id` AS `user_id`,`uc`.`contact_user_id` AS `contact_user_id`,`u`.`mobilephone` AS `mobilephone`,ifnull(`uc`.`c_comments`,`u`.`mobilephone`) AS `c_comments`,(select cast(ifnull(max(`pl`.`serial_number`),(select min(`t_yqrx_preauth_level`.`serial_number`) from `t_yqrx_preauth_level`)) as decimal(10,0)) from (`t_yqrx_preauth_level` `pl` join `t_yqrx_user_preauth` `uath2`) where ((`uath2`.`user_id` = `uc`.`contact_user_id`) and (`uath2`.`contact_user_id` = `uc`.`user_id`) and (`uath2`.`amount` >= ifnull(`pl`.`level_amount`,0)))) AS `credit_level`,(select ifnull(sum(`uath2`.`amount`),0) from `t_yqrx_user_preauth` `uath2` where ((`uath2`.`user_id` = `uc`.`contact_user_id`) and (`uath2`.`contact_user_id` = `uc`.`user_id`))) AS `credit_level_amount`,ifnull(sum(`uath`.`amount`),0) AS `worth_amount`,count((case when (`uath`.`amount` > 0) then 1 else NULL end)) AS `preauthed_users`,(select ifnull(sum(`up`.`amount`),0) from `t_yqrx_user_preauth` `up` where (`up`.`contact_user_id` = `u`.`id`)) AS `preauth_amount` from ((`t_yqrx_user_contact` `uc` join `t_yqrx_user` `u` on((`uc`.`contact_user_id` = `u`.`id`))) left join `t_yqrx_user_preauth` `uath` on((`u`.`id` = `uath`.`user_id`))) group by `uc`.`id`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
*************************** 1. row ***************************
View: t_yqrx_contact_user_redit
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`webapp`@`%` SQL SECURITY DEFINER VIEW `t_yqrx_contact_user_redit` AS select `uc`.`id` AS `id`,`uc`.`user_id` AS `user_id`,`uc`.`contact_user_id` AS `contact_user_id`,`u`.`mobilephone` AS `mobilephone`,ifnull(`uc`.`c_comments`,`u`.`mobilephone`) AS `c_comments`,(select cast(ifnull(max(`pl`.`serial_number`),(select min(`t_yqrx_preauth_level`.`serial_number`) from `t_yqrx_preauth_level`)) as decimal(10,0)) from (`t_yqrx_preauth_level` `pl` join `t_yqrx_user_preauth` `uath2`) where ((`uath2`.`user_id` = `uc`.`contact_user_id`) and (`uath2`.`contact_user_id` = `uc`.`user_id`) and (`uath2`.`amount` >= ifnull(`pl`.`level_amount`,0)))) AS `credit_level`,(select ifnull(sum(`uath2`.`amount`),0) from `t_yqrx_user_preauth` `uath2` where ((`uath2`.`user_id` = `uc`.`contact_user_id`) and (`uath2`.`contact_user_id` = `uc`.`user_id`))) AS `credit_level_amount`,ifnull(sum(`uath`.`amount`),0) AS `worth_amount`,count((case when (`uath`.`amount` > 0) then 1 else NULL end)) AS `preauthed_users`,(select ifnull(sum(`up`.`amount`),0) from `t_yqrx_user_preauth` `up` where (`up`.`contact_user_id` = `u`.`id`)) AS `preauth_amount` from ((`t_yqrx_user_contact` `uc` join `t_yqrx_user` `u` on((`uc`.`contact_user_id` = `u`.`id`))) left join `t_yqrx_user_preauth` `uath` on((`u`.`id` = `uath`.`user_id`))) group by `uc`.`id`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
但是为什么phpadmin打开db时会有这种查询呢,我复现了一下,点击这个按钮
打开general_log查看它到底做了哪些事
好吧,问题就在这了,phpadmin打开一个db时做了这些莫名其妙的工作,如果只是正常的use database命令,它做的事是这样的
从截图可以看到,use一个db时会查看下该库下所有表的filed list,这也能解释如果一个db下库多达3000以上时,use非常慢