我可以在GROUP BY之前优化这个ORDER BY,然后再进一步优化GROUP BY,查询MySQL吗?

发布于 2024-12-26 10:47:40 字数 1379 浏览 2 评论 0原文

我的应用程序用于每天比较多个商家销售的产品。运行一个进程来加载每个商店的当前数据值,并将其与任何先前的数据一起输入数据库。后来我计划根据数据历史创建图表等。

SELECT `a`.`part_number`, GROUP_CONCAT(`a`.`seller_id` ORDER BY `price` ASC), GROUP_CONCAT(`b`.`part_id` ORDER BY `price` ASC), GROUP_CONCAT(`b`.`price` ORDER BY `price` ASC)
FROM `parts`
LEFT JOIN (
           SELECT * 
           FROM (
                 SELECT * 
                 FROM `parts_data` AS `tmp_a`
                 ORDER BY `date_added` DESC
                ) AS `tmp_b`
           GROUP BY `part_id`
          ) AS `b`
ON `a`.`part_id` = `b`.`part_id`
GROUP BY `a`.`part_number`

为了稍微解释一下查询,我需要选择多个零件 ID 的最新价格信息,并将它们与选定的零件编号分组在一起进行比较。此外,在串联后,分组的项目按价格从最高到最低排序。

每个零件号还可以有任意数量的与其关联的项目 ID,因此并不意味着有多少家公司正在受到监视。

part_number |  column1  |  column2  |  column3
++++++++++++++++++++++++++++++++++++++++++++++++
            | seller:A1 | seller:A3 | seller:A6
  153231    | id:x423   | id:x534   | id:x902
            | cost:23   | cost:34   | cost:57
++++++++++++++++++++++++++++++++++++++++++++++++
            | seller:A3 | seller:A1 | seller:A6
  345123    | id:x313   | id:x631   | id:x652
            | cost:78   | cost:86   | cost:99
++++++++++++++++++++++++++++++++++++++++++++++++
            | seller:A5 | seller:A1 |
  231756    | id:x663   | id:x291   |
            | cost:35   | cost:52   |

My application is for the use of comparing a product sold by several merchants on a daily basis. A process runs to load the current values of data for each store and enters it into a database along with any previous data. Later I plan to create graphs and such off of the history of data.

SELECT `a`.`part_number`, GROUP_CONCAT(`a`.`seller_id` ORDER BY `price` ASC), GROUP_CONCAT(`b`.`part_id` ORDER BY `price` ASC), GROUP_CONCAT(`b`.`price` ORDER BY `price` ASC)
FROM `parts`
LEFT JOIN (
           SELECT * 
           FROM (
                 SELECT * 
                 FROM `parts_data` AS `tmp_a`
                 ORDER BY `date_added` DESC
                ) AS `tmp_b`
           GROUP BY `part_id`
          ) AS `b`
ON `a`.`part_id` = `b`.`part_id`
GROUP BY `a`.`part_number`

To explain the query a little, I need to select the most recent price information for several part IDs and group them together with a selected part number for comparison. Also, upon concatenation the grouped items are sorted by price to be highest to lowest.

Each part number also can have any number of item ids associated with it so there is no implication of how many companies are being watched.

part_number |  column1  |  column2  |  column3
++++++++++++++++++++++++++++++++++++++++++++++++
            | seller:A1 | seller:A3 | seller:A6
  153231    | id:x423   | id:x534   | id:x902
            | cost:23   | cost:34   | cost:57
++++++++++++++++++++++++++++++++++++++++++++++++
            | seller:A3 | seller:A1 | seller:A6
  345123    | id:x313   | id:x631   | id:x652
            | cost:78   | cost:86   | cost:99
++++++++++++++++++++++++++++++++++++++++++++++++
            | seller:A5 | seller:A1 |
  231756    | id:x663   | id:x291   |
            | cost:35   | cost:52   |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文