具有SQL查詢我的遺留代碼,現在我需要,因爲它花費的時間太長(14秒)在生產服務器上,以優化查詢。我仍然不確定從哪裏開始。特別是在rand()函數中,因爲它似乎有性能問題MySQL的rand()函數BY
SELECT
order_detail.product_id,
dtb_products.name,
dtb_products.main_list_image,
dtb_products_price.price01_min,
dtb_products_price.price01_max,
dtb_products_price.price02_min,
dtb_products_price.price02_max,
dtb_products.limited_flag,
dtb_products.limited_start_date,
dtb_products.limited_end_date
FROM
dtb_order
INNER JOIN
(SELECT
dtb_order_2.customer_id
FROM
dtb_order AS dtb_order_2
INNER JOIN
dtb_order_detail
ON
dtb_order_detail.order_id = dtb_order_2.order_id
WHERE
dtb_order_detail.product_id = 1256
GROUP BY
dtb_order_2.customer_id
) AS CUSTOMER
ON
dtb_order.customer_id = CUSTOMER.customer_id
INNER JOIN
(SELECT
dtb_order_detail.product_id,
dtb_order_detail.order_id
FROM
dtb_order_detail
INNER JOIN
dtb_products
ON
dtb_products.product_id = dtb_order_detail.product_id
WHERE
dtb_order_detail.product_id <> 1256 AND dtb_products.del_flg = 0 AND dtb_products.status = 1
) AS order_detail
ON
order_detail.order_id = dtb_order.order_id
LEFT JOIN
(
SELECT product_id as product_id_sub,
MIN(price01) AS price01_min,
MAX(price01) AS price01_max,
MIN(price02) AS price02_min,
MAX(price02) AS price02_max
FROM dtb_products_class
GROUP BY product_id
) AS dtb_products_price
ON
order_detail.product_id = dtb_products_price.product_id_sub
LEFT JOIN
dtb_products
ON
dtb_products.product_id = order_detail.product_id
GROUP BY
order_detail.product_id
ORDER BY
rand()
limit
8;
我應該從哪裏開始優化該查詢?
我已看過一些博客文章,其中之一是http://explainextended.com/2009/03/01/selecting-random-rows/但我還是搞清楚如何實現方法在上面的查詢。
我真的很感謝你的幫助。
謝謝
編輯
的要求是:
獲取這些產品的IDS有一個固定的產品ID,例如買了1256 ,然後從價格表中獲取這些產品的每個信息。
編輯
在查詢表
dtb_order
,dtb_order_detail
,dtb_products
,dtb_products_class
您可以發佈說明計劃爲您查詢 – Rahul
你應該從哪裏開始呢?通過查看解釋計劃 –
嗨@Rahul我在問題中添加了該信息。謝謝:) – Ardian