我有一个用 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