我正在绞尽脑汁试图弄清楚为什么这个查询需要这么长时间。它可以正常工作,但是当我稍后将此查询与另一个查询连接时,我收到了500内部服务器错误“。任何明显的想法是什么导致此查询超时?链接的所有表都运行得很快。
SELECT
`ORDERS`.`Order ID` AS `Order ID`,
`ORDERS`.`Item ID` AS `Item ID`,
`qb_group`.`product` AS `product`,
`qb_group`.`qb_product` AS `qb_product`,
`qb_group`.`description` AS `description`,
`ORDERS`.`Ordered` AS `Ordered`,
`qb_group`.`price` AS `price`,
(
`ORDERS`.`Ordered` * `qb_group`.`price`
) AS `Total`,
IF (
(
`wp_postmeta`.`meta_value` = NULL
),
`ORDERS`.`Shipped`,
`ORDERS`.`Ordered`
) AS `Shipped`,
`qb_group`.`group_name` AS `group_name`,
`wp_postmeta`.`meta_value` AS `ship_date`,
`qb_suborder_shipments`.`suborder` AS `suborder`,
`qb_suborder_shipments`.`sub_qty` AS `sub_qty`,
`qb_suborder_shipments`.`sub_shipdate` AS `sub_shipdate`
FROM
(
(
(
`ORDERS`
JOIN `qb_group` ON (
(
`ORDERS`.`Item ID` = `qb_group`.`order_item_id`
)
)
)
LEFT JOIN `wp_postmeta` ON (
(
`ORDERS`.`Order ID` = `wp_postmeta`.`post_id`
)
)
)
LEFT JOIN `qb_suborder_shipments` ON (
(
`ORDERS`.`Order ID` = `qb_suborder_shipments`.`order_id`
)
)
)
WHERE
(
`wp_postmeta`.`meta_key` = _utf8 '_completed_date'
)
答案 0 :(得分:0)
首先,我会重写您的查询,以便它清晰可读:
SELECT o.`Order ID`, /* Avoid spaces in column names */
o.`Item ID`, /* Avoid aliasing columns to the same thing */
qbg.product,
qbg.qb_product,
qbg.description,
o.Ordered, /* Avoid capitals in column names */
qbg.price,
o.Ordered*qbg.price Total,
IF(wppm.meta_value IS NULL, o.Shipped, o.Ordered) Shipped,
qbg.group_name, /* x = NULL will always return false use IS NULL */
wppm.metavalue ship_date,
qbsos.suborder,
qbsos.sub_qty,
qbsos.sub_shipdate
FROM ORDERS o /* Alias your table names */
JOIN qb_group qbg /* Avoid unnecessary parentheses */
ON qbg.order_item_id=o.`Item ID`
JOIN wp_postmeta wppm
ON wppm.post_id = o.`Order ID`
AND wppm.meta_key = _utf8 '_completed_date'
LEFT JOIN qb_suborder_shipments qbsos
ON qbsos.order_id = o.`Order ID`
您的WHERE wppm.meta_key = _utf8 '_completed_date'
拒绝了您的LEFT JOIN
,因此我将其更改为JOIN
并将其添加到ON
条款中。
你是对的,这应该相当快..运行EXPLAIN ...
找到瓶颈。如果您想进一步输入,请在此处发布。