简体   繁体   English

SQL Server:在左联接查询的执行计划中插入了隐藏的“排序”

[英]SQL Server: hidden “Sort” inserted in execution plan in left join query

SQL 2017 Standard i have a database in a star schema model (data warehouse) to fill in a fact table, i have a stored procedure with a temporary table having around 470,0000 rows. SQL 2017 Standard我在星型模式模型(数据仓库)中有一个数据库来填充事实表,我有一个存储过程,其中的临时表约有470,0000行。 to fill in the dimension ids, i have a left join operation between the temporary table and the dimensions tables. 填写维度ID,我在临时表和维度表之间进行了左联接操作。 for ex: 例如:

insert into factTable (...fields list...)
select t.Quantity1,t.Quantity2,d1.ID,d.ID,...,19.id from
MyTemp t
left outer join dim1 d1 on t.F1=d1.F1 and t.CompanyID=d1.CompanyID and t.DataSourceID=d1.DataSourceID
left outer join dim2 d2 on t.F2=d2.F2 and t.CompanyID=d2.CompanyID and t.DataSourceID=d2.DataSourceID
left outer join dim3 d3 on t.F3=d3.F3 and t.CompanyID=d2.CompanyID and t.DataSourceID=d2.DataSourceID
.......
left outer join dim19 d19 on t.F19=3.F19 and t.CompanyID=d19.CompanyID and t.DataSourceID=d19.DataSourceID

the problem is when using a smaller number of joins, for ex just for the first 5 or 6 dimensions, the query is very fast. 问题是当使用较少数量的联接时,例如仅对于前5或6个维,查询速度非常快。

with 19 joins, it is taking more than 4 hours. 进行19次加入后,耗时超过4个小时。

the execution plan shows that the bottleneck comes from a hidden "Sort" operation inserted by the query optimizer!!!! 执行计划显示瓶颈来自查询优化器插入的隐藏“排序”操作!!!!

when reading data from each dimension, sql server is sorting the dimension data before joining with the temporary table. 从每个维度读取数据时,SQL Server在加入临时表之前对维度数据进行排序。 introducing indexes on the temporary table did not solve the problem. 在临时表上引入索引并不能解决问题。 even limiting the query to the first field from an index on the temp table did not help 即使将查询从临时表上的索引限制为第一个字段也无济于事

隐藏在执行计划中

隐藏排序-第二张图片

Using 使用

CREATE CLUSTERED COLUMNSTORE INDEX

on the temporary table solved the problem. 在临时表上解决了问题。 Thank you all. 谢谢你们。 You can check CLUSTERED COLUMNSTORE INDEX for more details (if you are working on SQL Server 2016 Standard edition you will need SP2) 您可以检查CLUSTERED COLUMNSTORE INDEX以获取更多详细信息(如果您使用的是SQL Server 2016 Standard Edition,则需要SP2)。

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

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