1

我有一个用 MS SQL Server R2 编写的查询。请帮助优化它。它返回超过 60 万的数据字符串结构。返回结果大约需要 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
4

2 回答 2

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
于 2013-05-31T05:19:24.210 回答
0

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

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

于 2013-05-30T13:30:10.270 回答