27,580
社区成员
发帖
与我相关
我的任务
分享
SELECT uid,name,lon,lat,(Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) as jl
FROM [users]
WHERE [online] = 1 and (Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000)<2 order by jl desc;
如果我同时把距离拉出来,像我这样计算好像不行,距离计算了两次,有什么办法可以避免吗?
我测试了一下,从10W行里检索到7W行,你的方法只需要1.4秒,而我这样增加一列需要2秒,消耗增加不少啊。
多谢大神指导。
[/quote]
CREATE PROC dbo.getDistance
@lon decimal(10,6),
@lat decimal(10,6)
AS
BEGIN
SELECT uid,name,lon,lat,c1 FROM (
SELECT uid,name,lon,lat,(Geography::Point(@lat, @lon, 4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) AS c1
FROM [User]
WHERE [online] = 1
) tt
WHERE tt.c1<2
END;
GO
你可以尝试再套一层,当然如果原始数据量比过滤后大很多的话,肯定还是你2#的计算2次更效率。这样做只是纯粹少计算一次罢了。[/quote]
套一层,我试了下,实际差距不大,都还是稳定在2秒左右[/quote]
这样我建议如果需要查询出来这个字段的话,采用你自己2#的计算2次语句。[/quote]
是的,我也是准备这么处理,实际生产数据WHERE过滤之后已经所剩无几了
SELECT uid,name,lon,lat,(Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) as jl
FROM [users]
WHERE [online] = 1 and (Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000)<2 order by jl desc;
如果我同时把距离拉出来,像我这样计算好像不行,距离计算了两次,有什么办法可以避免吗?
我测试了一下,从10W行里检索到7W行,你的方法只需要1.4秒,而我这样增加一列需要2秒,消耗增加不少啊。
多谢大神指导。
[/quote]
CREATE PROC dbo.getDistance
@lon decimal(10,6),
@lat decimal(10,6)
AS
BEGIN
SELECT uid,name,lon,lat,c1 FROM (
SELECT uid,name,lon,lat,(Geography::Point(@lat, @lon, 4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) AS c1
FROM [User]
WHERE [online] = 1
) tt
WHERE tt.c1<2
END;
GO
你可以尝试再套一层,当然如果原始数据量比过滤后大很多的话,肯定还是你2#的计算2次更效率。这样做只是纯粹少计算一次罢了。[/quote]
套一层,我试了下,实际差距不大,都还是稳定在2秒左右[/quote]
这样我建议如果需要查询出来这个字段的话,采用你自己2#的计算2次语句。
SELECT uid,name,lon,lat,(Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) as jl
FROM [users]
WHERE [online] = 1 and (Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000)<2 order by jl desc;
如果我同时把距离拉出来,像我这样计算好像不行,距离计算了两次,有什么办法可以避免吗?
我测试了一下,从10W行里检索到7W行,你的方法只需要1.4秒,而我这样增加一列需要2秒,消耗增加不少啊。
多谢大神指导。
[/quote]
CREATE PROC dbo.getDistance
@lon decimal(10,6),
@lat decimal(10,6)
AS
BEGIN
SELECT uid,name,lon,lat,c1 FROM (
SELECT uid,name,lon,lat,(Geography::Point(@lat, @lon, 4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) AS c1
FROM [User]
WHERE [online] = 1
) tt
WHERE tt.c1<2
END;
GO
你可以尝试再套一层,当然如果原始数据量比过滤后大很多的话,肯定还是你2#的计算2次更效率。这样做只是纯粹少计算一次罢了。[/quote]
套一层,我试了下,实际差距不大,都还是稳定在2秒左右
SELECT uid,name,lon,lat,(Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) as jl
FROM [users]
WHERE [online] = 1 and (Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000)<2 order by jl desc;
如果我同时把距离拉出来,像我这样计算好像不行,距离计算了两次,有什么办法可以避免吗?
我测试了一下,从10W行里检索到7W行,你的方法只需要1.4秒,而我这样增加一列需要2秒,消耗增加不少啊。
多谢大神指导。
[/quote]
没办法吧……sql会先执行where,再执行select,所以select的里边的别名计算列,在where中无法使用
SELECT uid,name,lon,lat,(Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) as jl
FROM [users]
WHERE [online] = 1 and (Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000)<2 order by jl desc;
如果我同时把距离拉出来,像我这样计算好像不行,距离计算了两次,有什么办法可以避免吗?
我测试了一下,从10W行里检索到7W行,你的方法只需要1.4秒,而我这样增加一列需要2秒,消耗增加不少啊。
多谢大神指导。
[/quote]
CREATE PROC dbo.getDistance
@lon decimal(10,6),
@lat decimal(10,6)
AS
BEGIN
SELECT uid,name,lon,lat,c1 FROM (
SELECT uid,name,lon,lat,(Geography::Point(@lat, @lon, 4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) AS c1
FROM [User]
WHERE [online] = 1
) tt
WHERE tt.c1<2
END;
GO
你可以尝试再套一层,当然如果原始数据量比过滤后大很多的话,肯定还是你2#的计算2次更效率。这样做只是纯粹少计算一次罢了。
SELECT uid,name,lon,lat,(Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000) as jl
FROM [users]
WHERE [online] = 1 and (Geography::Point(30.606311,114.253424,4326) .STDistance(( Geography::Point(lat,lon, 4326) ))/ 1000)<2 order by jl desc;
如果我同时把距离拉出来,像我这样计算好像不行,距离计算了两次,有什么办法可以避免吗?
我测试了一下,从10W行里检索到7W行,你的方法只需要1.4秒,而我这样增加一列需要2秒,消耗增加不少啊。
多谢大神指导。
CREATE PROC dbo.getDistance
@lon decimal(10,6),
@lat decimal(10,6)
AS
BEGIN
SELECT *
FROM [User]
WHERE (Geography::Point(@lat, @lon, 4326) .STDistance(( Geography::Point(lat,
lon, 4326) ))
/ 1000) < 2
AND [online] = 1;
END;
GO