简体   繁体   English

最佳性能 SQL 查询 SSRS

[英]Best Performance SQL Query SSRS

I'm building a report and I'm in a dilemma which path to follow to reach better performance.我正在构建一份报告,但我正处于两难境地,要遵循哪条路径才能达到更好的性能。

I got a table where我有一张桌子

COD | CTYPID | MNT

The above contains COD as the primary key, type and amount以上包含COD为主键,类型和金额

The report will contain each type as a column.该报告将包含每个类型作为一列。 Meaning one COD will have 3 columns for each CTYPID .这意味着一个COD将为每个CTYPID包含 3 列。

Which one is the best:哪一个是最好的:

1- 1-

   Insert into #temp
   Select sum(amount),CTYPID From table
   Group by COD,CTYPIDz

Then I left join this table with my main table where CTYPID=1 Then another left join where CTYPID=2 and so one然后我将这张表与我的主表连接起来,其中 CTYPID=1 然后另一个左连接,其中 CTYPID=2 等等

2- 2-

 --insert in a temp table on multiple columns
   insert into #temp
   (Select sum(amount) column1,CTYPID From table where CTYPID=1
   Group by COD)
   Left join (Select sum(amount)column2 ,CTYPID From table where CTYPID=2
   Group by COD)

Then join the above with my main table然后将上面的与我的主表连接起来

One alternative option would be to do conditional aggregation:另一种选择是进行条件聚合:

insert into #temp
select
    cod,
    sum(case when ctypid = 1 then mnt else 0 end) mnt_ctypid1,
    sum(case when ctypid = 2 then mnt else 0 end) mnt_ctypid2,
    sum(case when ctypid = 3 then mnt else 0 end) mnt_ctypid3
from mytable 
group by cod

For this query, you want an index on (cod, ctypid) (which might be already there since this seems to - or should - be the primary key of your table).对于此查询,您需要(cod, ctypid)上的索引(cod, ctypid)该索引可能已经存在,因为这似乎 - 或应该 - 是您的表的主键)。

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

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