我可以在GROUP BY之前优化这个ORDER BY,然后再进一步优化GROUP BY,查询MySQL吗?
我的应用程序用于每天比较多个商家销售的产品。运行一个进程来加载每个商店的当前数据值,并将其与任何先前的数据一起输入数据库。后来我计划根据数据历史创建图表等。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论