关于关联表的问题
假设有两张表,表1存放将某个区域分为小格的信息,字段x1,y1,x2,y2分别表示小格的最小和最大坐标;
表2存放很多经纬度信息;
先要求,将表2中的的数据与表1关联,即查询每个经纬度所属的小格ID(每个小格覆盖的区域不重复,因此每个经纬度信息对应一个小格ID)
详细的请看以下具体数据
--测试数据
create table t1(id int identity(1,1),name varchar(100),x1 numeric(9,3),y1 numeric(9,3),x2 numeric(9,3),y2 numeric(9,3))
go
declare @cntx int
declare @cnty int
set @cntx=0
set @cnty=0
--插入小格信息
while(@cntx<1000)
begin
set @cnty=0
while(@cnty<1000)
begin
insert into t1(name,x1,y1,x2,y2) values(left(newid(),5),@cntx*3,@cnty*3,@cntx*3+2.999,@cnty*3+2.999)
set @cnty=@cnty+1
end
set @cntx=@cntx+1
end
go
create table t2(area_id int identity(1,1),logi numeric(9,3),lat numeric(9,3))
go
--随机插入经纬度信息
declare @cntx int
set @cntx=0
while(@cntx<5000)
begin
insert into t2(logi,lat) values(abs(checksum(newid()))%3000,abs(checksum(newid()))%3000)
set @cntx=@cntx+1
end
go
--删除测试表
drop table t1
drop table t2
--我进行关联的方法
select * from t1 inner join t2 on logi between x1 and x2 and lat between y1 and y2
有没有更好更快的方法可以使用?
在相关字段建立索引可以提高查询的速度
现求更好的方法,或好的索引建立方案
请将方法和实现的效果、效率一并给出,谢谢