优化SQL查询以减少执行时间

时间:2013-05-30 13:19:17

标签: sql-server database sql-server-2008-r2 query-optimization

我有一个用MS SQL Server R2编写的查询。请帮助优化它。它返回超过6个Lakhs的数据的字符串结构。返回结果大约需要5-6分钟。有什么方法可以改善这个吗?

以下是我的询问:

CREATE proc [dbo].[Test]
(    
@UserTypeID int,    
@UserID int,    
@CityID int,    
@OperatorID int,    
@ParameterID int    
)                 
as    
begin    
declare @temp table    
(    
 range decimal(18,2),    
 range2 decimal(18,2),    
 image varchar(50),    
 symbol  varchar(20)    
)    
If(@UserID>0)          
Begin    
--print 'hii'    
 Insert into @temp(range,range2,image,symbol)          
 SELECT     tbl_Legend_ViewNetwork_Dtls.range, tbl_Legend_ViewNetwork_Dtls.range2, tbl_Legend_ViewNetwork_Dtls.image,tbl_Legend_ViewNetwork_Dtls.symbol          
 FROM         tbl_Legend_ViewNetwork_Dtls INNER JOIN          
                      tbl_Legend_ViewNetwork ON tbl_Legend_ViewNetwork_Dtls.tbl_legend_view_network_id = tbl_Legend_ViewNetwork.id          
 WHERE tbl_Legend_ViewNetwork.parameter_id = @ParameterID and tbl_Legend_ViewNetwork.user_type_id = @UserTypeID and tbl_Legend_ViewNetwork.is_default = 1 and tbl_Legend_ViewNetwork.user_id = @UserID          

update @temp set       
range = range2,      
range2 = range      
where symbol = '<'      
End          
Else          
Begin          
 Insert into @temp(range,range2,image,symbol)          
 SELECT     tbl_Legend_ViewNetwork_Dtls.range, tbl_Legend_ViewNetwork_Dtls.range2, tbl_Legend_ViewNetwork_Dtls.image,tbl_Legend_ViewNetwork_Dtls.symbol         
 FROM         tbl_Legend_ViewNetwork_Dtls INNER JOIN          
                      tbl_Legend_ViewNetwork ON tbl_Legend_ViewNetwork_Dtls.tbl_legend_view_network_id = tbl_Legend_ViewNetwork.id          
 WHERE tbl_Legend_ViewNetwork.parameter_id = @ParameterID and tbl_Legend_ViewNetwork.user_type_id = @UserTypeID and tbl_Legend_ViewNetwork.is_default = 1           

update @temp set       
range = range2,      
range2 = range      
where symbol = '<'      
End          
--select * from @temp          

Select '[' + STUFF(          
 (SELECT    ',{"latitude":"'+ a.lat+ '","longitude":"' + a.long+ '","value":"' +convert(varchar(20),a.value)+ '","image":"' +temp.image +'"}'    
 FROM         (SELECT     tbl_Survey_Details.lat, tbl_Survey_Details.long, tbl_Survey_Details.value          
         FROM          tbl_Survey_Details INNER JOIN          
              tbl_Survey ON tbl_Survey_Details.tbl_survey_id = tbl_Survey.id INNER JOIN          
              tbl_Location ON tbl_Survey.tbl_location_id = tbl_Location.id INNER JOIN          
              tbl_Area ON tbl_Location.tbl_area_id = tbl_Area.id INNER JOIN    
              tbl_City ON tbl_Area.tbl_city_id = tbl_City.id    
WHERE tbl_Survey_Details.tbl_parameter_id = @ParameterID and tbl_Survey.tbl_mobile_operator_id = @OperatorID and tbl_Area.tbl_city_id = @CityID) AS a    
 INNER JOIN @temp temp on a.value between temp.range and temp.range2    
For XML Path ('')),1,1,'') + ']' as data    
End

2 个答案:

答案 0 :(得分:1)

试试这个 -

CREATE PROC [dbo].[Test] 
(
    @UserTypeID INT,
    @UserID INT,
    @CityID INT,
    @OperatorID INT,
    @ParameterID INT
)
AS BEGIN

    SELECT data = '[' + STUFF((
        SELECT
            ',{"latitude":"' + A.lat + '","longitude":"' + A.long + '","value":"' + CONVERT(VARCHAR(20), A.value) + '","image":"' + temp.IMAGE + '"}'
        FROM (
            SELECT
                  sd.lat
                , sd.long
                , sd.value
            FROM dbo.tbl_Survey_Details sd
            JOIN dbo.tbl_Survey s ON sd.tbl_survey_id = s.id
            JOIN dbo.tbl_Location l ON s.tbl_location_id = l.id
            JOIN dbo.tbl_Area a ON l.tbl_area_id = a.id
            JOIN dbo.tbl_City c ON a.tbl_city_id = c.id
            WHERE sd.tbl_parameter_id = @ParameterID
                AND s.tbl_mobile_operator_id = @OperatorID
                AND a.tbl_city_id = @CityID
        ) a
        JOIN (
            SELECT  
                  [range] = CASE WHEN symbol = '<' THEN t.range2 ELSE t.[range] END
                , range2 = CASE WHEN symbol = '<' THEN t.[range] ELSE t.range2 END
                , [image] = t.[image]
            FROM dbo.tbl_Legend_ViewNetwork_Dtls t
            JOIN dbo.tbl_Legend_ViewNetwork n ON t.tbl_legend_view_network_id = n.id
            WHERE n.parameter_id = @ParameterID
                AND n.user_type_id = @UserTypeID
                AND n.is_default = 1
                AND (
                        (
                              n.[user_id] = @UserID 
                            AND 
                              @UserID > 0
                        ) 
                        OR 
                          @UserID <= 0
                )
        ) temp ON A.value BETWEEN temp.[range] AND temp.range2
        FOR XML PATH ('')
    ), 1, 1, '') + ']'

END

答案 1 :(得分:0)

如果您在MS SQL Management Studio查询分析器中运行此查询并单击菜单查询,在数据库调优顾问程序中分析此查询,它将告诉您需要添加的索引以优化查询...

对于我们这些在南亚以外的人来说,Lakhs的PS是100,000