求一条语句的优化

戈壁上的月光 2009-08-21 10:00:12
语句如下:

select V.V_No,S.Typetxt,D.Deviceid from
(select DeviceID from DynData where Time>@StartTime and
Time<@EndTime and Longitude<@MaxX and Longitude> @MinX
and latitude<@MaxY and latitude>@MinY GROUP BY DeviceID) D inner join Vehicle V On D.DeviceID=V.V_DeviceID
inner join SysType S on V.V_TeamNo=S.TypeID where s.fieldname='V_teamNo'


其中DynData 表中有大量数据,导致查询缓慢,请帮忙优化下,谢谢!
...全文
96 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
oflying907 2009-08-21
  • 打赏
  • 举报
回复

select V.V_No,S.Typetxt,D.Deviceid from
(select distinct DeviceID from DynData where Time>@StartTime and
Time<@EndTime and Longitude<@MaxX and Longitude> @MinX
and latitude<@MaxY and latitude>@MinY) D inner join Vehicle V On D.DeviceID=V.V_DeviceID
inner join SysType S on V.V_TeamNo=S.TypeID where s.fieldname='V_teamNo'

Q315054403 2009-08-21
  • 打赏
  • 举报
回复
若你们公司有预算有偿,欢迎联系ME
戈壁上的月光 2009-08-21
  • 打赏
  • 举报
回复
一天800万吧
feixianxxx 2009-08-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 harvest2006 的回复:]
Time字段加主键索引了
查询条件必须要这么多
表结构的话
DeviceID  Time  联合主键

Longitude
latitude
查询条件  时间段 >@StartTime  <@EndTime

经纬度范围在 MaxX MinX MaxY MinY  内的

DeviceID 
再连接查询其他表字段,主要是DynData  表数据量大 。


[/Quote]

Longitude 加了吗?

数据量多大。。
如果很大的 话 稍微慢点正常阿
戈壁上的月光 2009-08-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wufeng4552 的回复:]
SQL code--tryselectdistinct V.V_No,S.Typetxt,D.Deviceidfrom DynData dinnerjoin Vehicle VOn D.DeviceID=V.V_DeviceIDand d.Time>@StartTimeand
d.Time<@EndTimeand d.Longitude<@MaxXand d.Longitude>@Mi¡­
[/Quote]

测试发现 还比以前的慢一点!
戈壁上的月光 2009-08-21
  • 打赏
  • 举报
回复
Time字段加主键索引了
查询条件必须要这么多
表结构的话
DeviceID Time 联合主键

Longitude
latitude
查询条件 时间段 >@StartTime <@EndTime

经纬度范围在 MaxX MinX MaxY MinY 内的

DeviceID
再连接查询其他表字段,主要是DynData 表数据量大 。

水族杰纶 2009-08-21
  • 打赏
  • 举报
回复
--try
select distinct V.V_No,S.Typetxt,D.Deviceid from DynData d
inner join Vehicle V On D.DeviceID=V.V_DeviceID
and d.Time>@StartTime and
d.Time<@EndTime and d.Longitude<@MaxX and d.Longitude> @MinX
and d.latitude<@MaxY and d.latitude>@MinY
inner join SysType S on V.V_TeamNo=S.TypeID where s.fieldname='V_teamNo'
--小F-- 2009-08-21
  • 打赏
  • 举报
回复
连接字段加索引,升级硬件

语句不需要嵌套的尽量不要嵌套
SQL77 2009-08-21
  • 打赏
  • 举报
回复
select DeviceID from DynData where Time>@StartTime and
Time<@EndTime and Longitude<@MaxX and Longitude> @MinX
and latitude<@MaxY and latitude>@MinY GROUP BY DeviceID

你这个表的查询条件那么多,看有没有其它方法简化一下
feixianxxx 2009-08-21
  • 打赏
  • 举报
回复
Time
Longitude
字段都加索引了么?
SQL77 2009-08-21
  • 打赏
  • 举报
回复
条件上加索引试试
feixianxxx 2009-08-21
  • 打赏
  • 举报
回复
暂时看不 出来怎么优化。。
xiequan2 2009-08-21
  • 打赏
  • 举报
回复
连接字段加索引,升级硬件,贴出表结构以及想要的结果,就会有人帮你写了
zzz1975 2009-08-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yang_ 的回复:]
两个改法,应该都有效
1、DeviceID  Time  联合主键  改成 Time DeviceID 联合主键,即换下主键的字段排列。
2、在Longitude 字段建立一个索引

[/Quote]

支持

另外看看子查询建个视图,这样查询的时候jion 链接一下。估计快点
Yang_ 2009-08-21
  • 打赏
  • 举报
回复
两个改法,应该都有效
1、DeviceID Time 联合主键 改成 Time DeviceID 联合主键,即换下主键的字段排列。
2、在Longitude 字段建立一个索引
yinchuanwang 2009-08-21
  • 打赏
  • 举报
回复
查询语句问题不大,问题应该在 索引缺少上面。

能不能贴出 执行计划来看看,这样看语句是没什么意义的?

首先Time,Longitude, Latitude上最好要有 聚簇索引, 如果还不可以的话,连接键
DeviceID上面也建个 索引。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧