更快的 CROSS JOIN 替代方案 - PostgreSQL
我正在尝试交叉联接两个表(客户和项目),这样我就可以创建按客户按项目的销售报告。我有 2000 个客户和 2000 个商品。
从客户中选择客户名称; --需要 100 毫秒
从项目中选择 item_number; --需要 50 毫秒
从客户中选择 customer_name、item_number CROSS JOIN 项目;需要 200000ms
我知道这是 400 万行,但是有可能让它运行得更快吗?我想最终将其与这样的销售表连接起来:
SELECT customer_name, item_number, sales_total FROMcustomers CROSS JOIN items LEFT JOIN sales ON (customer.customer_name = sales.customer_name, item.item_number=sales.item_number);< /code>
销售表显然不会包含所有客户或所有商品,因此这里的目标是有一个报告,显示所有客户和所有商品以及已售出和未售出的商品。
我正在使用 PostgreSQL 8.4
I am trying to CROSS JOIN two tables, customers and items, so I can then create a sales by customer by item report. I have 2000 customer and 2000 items.
SELECT customer_name FROM customers; --Takes 100ms
SELECT item_number FROM items; --Takes 50ms
SELECT customer_name, item_number FROM customers CROSS JOIN items; Takes 200000ms
I know this is 4 million rows, but is it possible to get this to run any faster? I want to eventually join this with a sales table like this:
SELECT customer_name, item_number, sales_total FROM customers CROSS JOIN items LEFT JOIN sales ON (customer.customer_name = sales.customer_name, item.item_number=sales.item_number);
The sales table will obviously not have all customers or all items, so the goal here is to have a report that shows all customers and all items along with what was sold and not sold.
I'm using PostgreSQL 8.4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
回答你的问题:不,你不能比这更快地进行交叉连接 - 如果你可以,那么这就是交叉连接的实现方式。
但实际上您并不想要交叉连接。您可能需要两个单独的查询,一个列出所有客户,另一个列出所有商品以及它们是否已售出。
To answer your question: No, you can't do a cross join faster than that - if you could then that would be how CROSS JOIN would be implemented.
But really you don't want a cross join. You probably want two separate queries, one which lists all customers, and another which lists all items and whether or not they were sold.
这确实需要多份报告。我能立即想到几个可以产生更有效的信息包装的方法:
这里的要点是,人们不应该坚持要求该工具立即处理所有可能的结果并生成更多数据,任何人都可以手动消化。人们应该让数据的最终用户和消费者了解他们的需求,并定制输出以满足这些需求。从长远来看,这将使双方的生活变得更加轻松。
This really needs to be multiple reports. I can think of several off the top of my head that will yield more efficient packaging of information:
The point here is that one should not insist that the tool process every possible outcome at once and generate more data and anyone could possibly digest manually. One should engage the end-users and consumers of the data as to what their needs are and tailor the output to meet those needs. It will make both sides' lives much easier in the long run.
如果您希望查看给定客户的所有商品(即使该客户没有商品),我宁愿尝试
这应该为您提供所有客户的列表,以及由销售加入的所有商品。
If you wish to see all items for a given client (even if the cient has no items), i would rather try
This should give you a list of all clients, and all items joined by sales.
也许你想要这样的东西?
Perhaps you want something like this?