phpMyAdmin错误代码:504使用MySQL查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了phpMyAdmin错误代码:504使用MySQL查询相关的知识,希望对你有一定的参考价值。

我对SQL一般还是比较新的。我在phpMyAdmin中尝试以下查询。

SELECT i.`ITEM_NO` as SKU, 
   i.`DESCR` as Description, 
   (SELECT SUM(`QTY_SOLD`) 
           FROM `ps_tkt_hist_lin` 
           WHERE `ITEM_NO` = i.`ITEM_NO` 
              AND `BUS_DAT` >= MAKEDATE(YEAR(CURDATE()),1)
              ) as 'QTY Sold YTD',
   (SELECT SUM(`QTY_SOLD`) 
           FROM `ps_tkt_hist_lin` 
           WHERE `ITEM_NO` = i.`ITEM_NO` 
              AND `BUS_DAT` >= DATE_SUB(DATE_SUB(CURDATE(),INTERVAL (DAY(CURDATE())-1) DAY), INTERVAL 1 MONTH)
              ) as 'QTY Sold Last Month',
   (SELECT SUM(`QTY_ON_HND`) 
           FROM `IM_INV` 
           WHERE `ITEM_NO` = i.`ITEM_NO` 
              AND `LOC_ID` IN ('SL','S','LO','AF')
              ) as 'QTY on Hand'
FROM `ps_tkt_hist_lin` as i
WHERE i.`ITEM_VEND_NO` = 'MD'
GROUP BY i.`ITEM_NO`, i.`DESCR`
ORDER BY i.`ITEM_NO`

运行时出现以下错误。

Error in processing request
Error code: 504
Error text: Gateway Time-out (rejected)
It seems that the connection to server has been lost. Please check your network connectivity and server status.

如果有帮助,这是服务器信息:

服务器:通过UNIX套接字的Localhost 服务器类型:MariaDB 服务器版本:10.1.31-MariaDB - MariaDB Server cpsrvd 11.68.0.29 数据库客户端版本:libmysql - 5.1.73 PHP版本:5.6.30

感谢您提前耐心地帮助一个刚刚开始使用SQL和MySQL的人。

答案

您看到的错误消息可能是由少数不同的事情引起的。一种可能性是涉及的表非常大,而查询只需要太长时间。大型表可能成为性能杀手的原因是您在select子句中有几个相关的子查询。

一种解决方法是使用适当的连接替换相关子查询到非相关子查询:

SELECT
    i.ITEM_NO AS SKU,
    i.DESCR AS Description,
    COALESCE(t1.qty_sold_ytd, 0) AS qty_sold_ytd,
    COALESCE(t1.qty_sold_last_month, 0) AS qty_sold_last_month,
    COALESCE(t2.qty_on_hand, 0) AS qty_on_hand
FROM ps_tkt_hist_lin i
LEFT JOIN
(
    SELECT
        ITEM_NO,
        SUM(CASE WHEN BUS_DAT >= MAKEDATE(YEAR(CURDATE()), 1)
                 THEN QTY_SOLD ELSE 0 END) AS qty_sold_ytd,
        SUM(CASE WHEN BUS_DAT >=
                      DATE_SUB(DATE_SUB(CURDATE(), INTERVAL (DAY(CURDATE())-1) DAY),
                               INTERVAL 1 MONTH)
                 THEN QTY_SOLD ELSE 0 END) AS qty_sold_last_month
    FROM ps_tkt_hist_lin
    GROUP BY ITEM_NO
) t1
    ON i.ITEM_NO = t1.ITEM_NO
LEFT JOIN
(
    SELECT ITEM_NO, SUM(QTY_ON_HND) AS qty_on_hand
    FROM IM_INV
    WHERE LOC_ID IN ('SL','S','LO','AF')
    GROUP BY ITEM_NO
) t2
    ON i.ITEM_NO = t2.ITEM_NO
WHERE
    i.ITEM_VEND_NO = 'MD'
ORDER BY
    i.ITEM_NO;

我试图坚持原始查询的逻辑,这完全没有意义。例如,不清楚为什么要在子查询和外部查询中进行聚合。我的建议会给你ps_tkt_hist_lin表中的每一行。

以上是关于phpMyAdmin错误代码:504使用MySQL查询的主要内容,如果未能解决你的问题,请参考以下文章

在 NGINX 服务器上全新安装时出现错误 504 和 413

mysql 引用键问题,phpmyadmin 错误代码 150 [重复]

如何修复此 MYSQL//Phpmyadmin 错误? [复制]

phpmyadmin mysql 触发语法错误

PHPMYADMIN ,,Error in Processing Request 错误代码:200 错误文本:OK

如何解决 Xampp phpMyAdmin 和 MySQL 的空白错误