网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
06月26日漏签0天
sql吧 关注:57,220贴子:144,727
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 0回复贴,共1页
<<返回sql吧
>0< 加载中...

帮忙看下这个存储过程怎么能优化一下速度

  • 只看楼主
  • 收藏

  • 回复
  • 该死先生
  • 高级粉丝
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
ALTER PROCEDURE [dbo].[GetEggData]
@BGName NVARCHAR(255),
@BUKey NVARCHAR(255),
@Category NVARCHAR(255),
@UserName NVARCHAR(255),
@Level INT,
@StartDate INT,
@EndDate INT
AS
BEGIN
SET NOCOUNT ON;
SET @BUKey = REPLACE(@BUKey,'''','')
SET @Category = REPLACE(@Category,',',''',''')
SET @UserName = REPLACE(@UserName,',',''',''')
SELECT * INTO #111 FROM (
select 'Revenue'Category,d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey,sum(a.[NetRev])egg
from [dbo].[FactRevenue] a
join [dbo].[DimBU] b on a.BUKey=b.BUKey
join [dbo].[DimMonth] d on a.MonthKey=d.MonthKey
join [dbo].[UserPolicy] e on (b.BGName=e.BUOrBG and e.PolicyLevel=1) or (b.BUName=e.BUOrBG and e.PolicyLevel=2)
where d.MonthKey>=@StartDate
and d.MonthKey<=@EndDate
group by d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey)T1
SELECT * INTO #222 FROM (
select 'Revenue'Category,d.[MonthName]+N'%' as [MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey,''Pegg
from [dbo].[FactRevenue] a
join [dbo].[DimBU] b on a.BUKey=b.BUKey
join [dbo].[DimMonth] d on a.MonthKey=d.MonthKey
join [dbo].[UserPolicy] e on (b.BGName=e.BUOrBG and e.PolicyLevel=1) or (b.BUName=e.BUOrBG and e.PolicyLevel=2)
where d.MonthKey>=@StartDate
and d.MonthKey<=@EndDate
group by d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey)T1
DECLARE @MainSqlM varchar(2000);
DECLARE @MainSqlM1 varchar(2000);
DECLARE @ColumnSqlM1 varchar(2000);
SET @ColumnSqlM1 =
STUFF(
(
SELECT ',' +'['+ #111.[MonthName] +']'
FROM #111
GROUP BY #111.[MonthName],#111.MonthKey
order by #111.MonthKey
FOR XML PATH('')
), 1, 1, '');
select a.Category,a.BGName,a.BUName,a.BUKey,a.[MonthName],a.egg into #m111 from #111 a
SET @MainSqlM1 =
'SELECT distinct Category,BGName,BUName,BUKey,'+@ColumnSqlM1 +' FROM #m111
join [dbo].[UserPolicy] e on (#m111.BGName=e.BUOrBG and e.PolicyLevel=1) or (#m111.BUName=e.BUOrBG and e.PolicyLevel=2)'
+ ' PIVOT( sum(egg) FOR [MonthName] IN ( '
+ @ColumnSqlM1
+ ' ) ) tmp where Category in ('''+@Category+''') and BUKey in ('+@BUKey+') and username in ('''+@UserName+''')'
DECLARE @MainSqlM2 varchar(2000);
DECLARE @ColumnSqlM2 varchar(2000);
SET @ColumnSqlM2 =
STUFF(
(
SELECT ',' +'['+ #222.[MonthName] +']'
FROM #222
GROUP BY #222.[MonthName],#222.MonthKey
order by #222.MonthKey
FOR XML PATH('')
), 1, 1, '');
select a.Category,a.BGName,a.BUName,a.BUKey,a.[MonthName],a.Pegg into #m222 from #222 a
SET @MainSqlM2 =
'SELECT distinct Category a,BGName b ,BUName c,BUKey,'+@ColumnSqlM2 +' FROM #m222
join [dbo].[UserPolicy] e on (#m222.BGName=e.BUOrBG and e.PolicyLevel=1) or (#m222.BUName=e.BUOrBG and e.PolicyLevel=2)'
+ ' PIVOT( sum(Pegg) FOR [MonthName] IN ('
+ @ColumnSqlM2
+ ' ) ) tmp where Category in ('''+@Category+''') and BUKey in ('+@BUKey+') and username in ('''+@UserName+''')';
SET @MainSqlM=
'select a.*,b.* from ('+@MainSqlM1+')a left join ('+@MainSqlM2+')b on a.Category=b.a and a.BGName=b.b and a.BUName=b.c'
EXECUTE ( @MainSqlM );
drop table #111;
drop table #222;
drop table #m111;
drop table #m222;
END


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 0回复贴,共1页
<<返回sql吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示