通过消除冗余的SELECT'来优化存储过程。声明/查询

时间:2016-02-11 09:53:59

标签: sql sql-server stored-procedures optimization query-optimization

我在名为Stored Procedure的{​​{1}}下面,为GetFilteredArticles 7 seperate tables day of a week返回i.e. Monday to Sunday

CREATE Procedure [dbo].[GetFilteredArticles]                                                                                                
@UserName NVARCHAR(100), 
AS                            
BEGIN                                                                  
SET NOCOUNT ON;  

[**Returns table for Monday**]
SELECT PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                               
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                 
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction,ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost ,Sources, HeadLine,                                   
   ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                                     
   FROM  PromoteToArticleNLPrep                                      
where day='Monday' and UserName = @UserName <--Condition [where day='Monday']                                                                                                                             
order by ISNULL(SortOrder,0) asc

[**Returns table for Tuesday**]                                                                        
SELECT PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                                                                                     
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                      
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction  , ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost,Sources, HeadLine ,                                   
   ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                                       
   FROM PromoteToArticleNLPrep                                                                                                                                                                                                                     
where day='Tuesday' and UserName = @UserName <--Condition [where day='Tuesday']                                                                                                                            
order by ISNULL(SortOrder,0) asc    

[**Returns table for Wednesday**]                                                                                                                                                                            
SELECT PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                                                                                     
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                      
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction  , ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost,Sources, HeadLine ,          
    ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                       
 FROM  PromoteToArticleNLPrep                                                                                                                                                             
where day='Wednesday' and UserName = @UserName <--Condition [where day='Wednesday']                                                                                                                               
order by ISNULL(SortOrder,0) asc
.....
.....
.....
..... and so on for rest of the days...
END

注意 - day is a column name

总而言之,每天都会有7 tables回复。 为此,我想在最低级别使用SELECT Query,我该怎么做..

此外,2 -

还有remove redundancy个选项
  1. 使用While loop 7次day will be dyanamic
  2. Static SQL Query N'&#39;只有一天将是dyanmic
  3. 预计 - single Select query return 7 tables each day instead 7使用select statementssuggestions or thoughts js:true

    此处还有其他describe "Some feature", js: true do ... end ..?

1 个答案:

答案 0 :(得分:0)

存储过程是方法,意味着。功能旨在阅读和传递 ......

这是我的建议:

我没有在您的查询中更改任何内容,只是添加了日期列...还有一些改进的机会...

CREATE FUNCTION dbo.GetFilteredArticles
(
    @UserName VARCHAR(150)=NULL
   ,@WeekDay VARCHAR(30)=NULL
)
RETURNS TABLE
AS
RETURN
SELECT day, PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                               
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                 
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction,ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost ,Sources, HeadLine,                                   
   ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                                     
   FROM  PromoteToArticleNLPrep                                      
where @WeekDay IS NULL OR day=@WeekDay 
  and UserName = @UserName;                                                                                                                  

GO

--You might call this like this: You'll get all rows sorted 
SELECT * FROM dbo.GetFilteredArticles(NULL,'Pranav')
ORDER BY day, CASE WHEN SortOrder IS NULL THEN 0 ELSE SortOrder END asc

--or for one day
SELECT * FROM dbo.GetFilteredArticles('Tuesday','Pranav')
ORDER BY day, CASE WHEN SortOrder IS NULL THEN 0 ELSE SortOrder END asc