程序的CALL继续加载,占用高CPU,并且失败

时间:2018-06-11 12:19:01

标签: mysql sql call explain

我正在尝试创建一个过程,该过程将采用由数千条记录组成的视图表,并将根据各种条件删除多条记录来过滤它。

当我尝试使用CALL运行它时,它会继续加载,并且根据WHM Panel,进程(phpMyAdmin / import.php)占用大约90%的CPU功率。 最终,由于超时,它可能在10分钟后失败:

  

静态分析:

     

在分析过程中发现了1个错误。

     

缺少表情。 (靠近" ON"在第25位)SQL查询:编辑编辑

     

SET FOREIGN_KEY_CHECKS = ON;

     

MySQL说:文档

     

2006 - MySQL服务器已经消失

我使用以下代码,新视图表使用3个DB表:

vw_r2_items_tr正在使用tr数据库核心表和vw_items - 使用itemsvisits核心表的视图表

DELIMITER $$

CREATE VIEW `vw_r2_items_tr` AS 
SELECT
  IF((`i`.`id` = `t`.`item_ord`),1,0) AS `has_tr`,
  `i`.`calculated_score`   AS `calculated_score`,
  `i`.`visited_at`         AS `visited_at`,
  `i`.`ip_address`         AS `ip_address`,
  `i`.`user_id`            AS `user_id`,
  `i`.`visit_id`           AS `visit_id`,
  `i`.`product_id`         AS `product_id`,
  `i`.`name`               AS `name`,
  `i`.`id`                 AS `id`,
  `i`.`item_no`            AS `item_no`,
FROM (`vw_items` `i`
   LEFT JOIN `trs` `t`
     ON (((`t`.`visit_id` = `i`.`visit_id`)
          AND (`t`.`item_ord` = `i`.`id`))))$$

DELIMITER ;


DELIMITER $$

CREATE PROCEDURE `vw_unique_trs2`()
BEGIN
SET @vat=0;
SET @ht=0;
SET @tdiff=0;
-- EXPLAIN  
SELECT 
COUNT(*) AS ct, 
tb1.* FROM (SELECT it.*, @tdiff:=UNIX_TIMESTAMP(CONVERT_TZ(it.visited_at,'+00:00',@@global.time_zone)) AS tdiffs,
IF(@vat > 0 AND ((@vat-@tdiff) < 28800 AND (@vat-@tdiff) > -28800),@vat DIV 28800, @tdiff DIV 28800) AS diff,
@ht:=(IF(@vat > 0 AND ((@vat-@tdiff) < 28800  AND (@vat-@tdiff) > -28800),@ht+has_tr,has_tr)) hts,
@vat:=(IF(@vat > 0 AND ((@vat-@tdiff) < 28800 AND (@vat-@tdiff) > -28800),@vat,@tdiff)) curr_vat
FROM (SELECT * FROM (SELECT * FROM `vw_r2_items_tr` ORDER BY has_tr DESC, item_no, visited_at) AS tb
GROUP BY `tb`.`visit_id`,`tb`.`product_id`) AS it 
ORDER BY has_tr DESC, it.visited_at DESC
) AS tb1
GROUP BY user_id, calculated_score, diff, hts;
END$$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我认为你不能只使用一个“ON”和两个连接,尝试做2个连接查询

    `SELECT *
FROM Table1
LEFT JOIN Table2 ON Table1.key = Table2.key` and 
LEFT JOIN Table3 ON Table1.key = Table2.key`