具有数百万行的四个表之间的联接的 Sql 查询
我们有一个 transact sql 语句,用于查询 4 个表,每个表中有数百万行。
尽管根据 TuningAdvisor 的说法,它已经使用索引和统计信息进行了优化,但仍需要几分钟的时间。
查询的结构如下:
SELECT E.EmployeeName , SUM(M.Amount) AS TotalAmount , SUM(B.Amount) AS BudgetAmount , SUM(T.Hours) AS TotalHours , SUM(TB.Hours) AS BudgetHours , SUM(CASE WHEN T.Type = 'Waste' THEN T.Hours ELSE 0 END) AS WastedHours FROM Employees E LEFT JOIN MoneyTransactions M ON E.EmployeeID = M.EmployeeID LEFT JOIN BudgetTransactions B ON E.EmployeeID = B.EmployeeID LEFT JOIN TimeTransactions T ON E.EmployeeID = T.EmployeeID LEFT JOIN TimeBudgetTransactions TB ON E.EmployeeID = TB.EmployeeID GROUP BY E.EmployeeName
由于每个事务表包含数百万行,因此我考虑将其拆分为每个事务表一个查询,使用@real
、@budget
等表变量code> 和 @hours
,然后将它们加入到最终的
您将如何处理这个问题以加快速度?
We have a transact sql statement that queries 4 tables with millions of rows in each.
It takes several minutes, even though it has been optimized with indexes and statistics according to TuningAdvisor.
The structure of the query is like:
SELECT E.EmployeeName , SUM(M.Amount) AS TotalAmount , SUM(B.Amount) AS BudgetAmount , SUM(T.Hours) AS TotalHours , SUM(TB.Hours) AS BudgetHours , SUM(CASE WHEN T.Type = 'Waste' THEN T.Hours ELSE 0 END) AS WastedHours FROM Employees E LEFT JOIN MoneyTransactions M ON E.EmployeeID = M.EmployeeID LEFT JOIN BudgetTransactions B ON E.EmployeeID = B.EmployeeID LEFT JOIN TimeTransactions T ON E.EmployeeID = T.EmployeeID LEFT JOIN TimeBudgetTransactions TB ON E.EmployeeID = TB.EmployeeID GROUP BY E.EmployeeName
Since each transaction table contains millions of rows, I consider splitting it up into one query per transaction table, using table variables like @real
, @budget
, and @hours
, and then joining these in a final SELECT
. But in tests it seems to not speed up.
How would you deal with that in order to speed it up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您发布的查询是否会产生您期望的结果。
它将交叉连接所有维度表(MoneyTransactions 等)并将所有结果相乘。
尝试这个:
I'm not sure the query you posted will yield the results you're expecting.
It will cross join all the dimension tables (MoneyTransactions etc.) and multiply all the results.
Try this:
我不知道表上的所有索引是否会加快速度,但是拥有大表可能会对查询时间产生影响。
如果可能的话,我建议对表进行分区。 这是更多的工作,但是现在您为加快查询所做的一切在数百万条新记录之后都还不够。
I don't know if you have all the indexes on your tables that will speed up things, but having big tables could have this impact on a query time.
I would recommend partitioning the tables if possible. It is more work, but everything you do to speed up the query now it won't be enough after few millions new records.
试试这个:
Try this one: