我想优化报表使用的查询。不幸的是,我无法修改报告,所以我必须提供一个特定格式的数据集。
所以,假设我有一个看起来像这样的表(实际上,它有 25 列和 20k 行):
Name Description Price MiscColumn1 MiscColumn2
Tea test description 10 misc1 misc2
Coffee test desc 20 misc3 misc4
Water test 20 misc1 misc2
因此,我需要将此数据集转换为如下所示:
Type Name Description Price MiscColumn1 MiscColumn2
1 Tea test description NULL NULL NULL
1 Coffee test desc NULL NULL NULL
1 Water test NULL NULL NULL
2 NULL NULL 10 NULL NULL
2 NULL NULL 20 NULL NULL
3 NULL NULL NULL misc1 misc2
3 NULL NULL NULL misc3 misc4
所以,基本上我需要做的是选择 3 组不同的记录回到数据集中。
我目前做的是:
Create #tempTable
然后像这样进行 3 次单独的不同选择:
insert into #tempTable (Name, Description)
select distinct Name, DEscription from myTable
insert into #tempTable (Price)
select distinct Price from myTable
但它真的很慢,最多可能需要 5 秒才能完成我的数据。
另外,我尝试使用 UNION,但没有获得任何性能改进。