简体   繁体   English

ORDER BY中的MySQL rand()函数

[英]MySQL rand() function in ORDER BY

I have legacy code that has sql query, now i need to optimize that query because it is taking too long (14 sec) in production server. 我有具有sql查询的旧代码,现在我需要优化该查询,因为它在生产服务器中花费的时间太长(14秒)。 I'm still unsure where to begin. 我仍然不确定从哪里开始。 Especially in rand() function because it seems has performance issue 特别是在rand()函数中,因为它似乎存在性能问题

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;

Where should I begin to optimize that query? 我应该从哪里开始优化该查询?

I have read several blog posts, one of them is http://explainextended.com/2009/03/01/selecting-random-rows/ but i'm still figuring out how to implement that method in query above. 我已经阅读了几篇博客文章,其中一篇是http://explainextended.com/2009/03/01/selecting-random-rows/,但我仍在弄清楚如何在上面的查询中实现该方法。

I really appreciate your help. 非常感谢您的帮助。

Thank you 谢谢

EDIT 编辑

The requirement is : 要求是:

fetch products ids of those were bought with a fixed product id, eg 1256 and then fetch each of these products info, including from the price table. 获取具有固定产品ID(例如1256)的产品ID,然后获取每个产品信息,包括从价格表中获取的信息。

EDIT 编辑

tables in query 查询表

dtb_order , dtb_order_detail , dtb_products , dtb_products_class dtb_orderdtb_order_detaildtb_productsdtb_products_class

I does not read your question body, but as usual, I do limit 80, save sql result set into some cached storage (you can use json encode, or serialize, and put content in file), and when you need, get it from your storage, and get 8 random items from this. 我没有阅读您的问题正文,但通常,我会限制80,将sql结果集保存到某些缓存的存储中(您可以使用json编码或序列化并将内容放入文件中),并在需要时从您的存储空间,并从中获得8个随机物品。 Also, think about cache storage reset function, when you will add some data to your table. 另外,在将一些数据添加到表中时,请考虑缓存存储重置功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM