# 如何优化查询，使其首先查找一个索引，然后查找另一个索引

12

``````select top 100000 h.Time, m.Time, h.GeoPoint.STDistance(m.GeoPoint)/1000.0
on h.mjd between m.mjd-.125 and m.mjd+.125
option( table hint ( h, index(ix_MJD) ), table hint( m, index(ix_MJD) ) )
``````

``````select top 10 h.Time, m.Time, h.GeoPoint.STDistance(m.GeoPoint)/1000.0
on h.mjd between m.mjd-.125 and m.mjd+.125
and h.GeoPoint.STDistance(m.GeoPoint)<200000
option( table hint ( h, index(ix_MJD) ), table hint( m, index(ix_MJD) ) )
``````

``````select top 5 h.Time, m.Time, h.GeoPoint.STDistance(m.GeoPoint)/1000.0
on h.GeoPoint.STDistance(m.GeoPoint)<200000
and h.mjd between m.mjd-.125 and m.mjd+.125
option( table hint ( h, index(ix_MJD), index(ix_GeoPoint) ), table hint( m, index(ix_MJD) ) )
``````

@Zane，我编辑了帖子并添加了无提示查询计划。它用扫描代替了寻道，而且时间太糟糕了。
user261963

6

``````select top 100000 h.Time, m.Time, h.GeoPoint.STDistance(m.GeoPoint)/1000.0
on h.mjd between m.mjd-.125 and m.mjd+.125
where h.GeoPoint.STDistance(m.GeoPoint)/1000.0 < 200
option( table hint ( h, index(ix_MJD) ) );
``````

user261963

...因此该计划看起来像您的原始计划，但是有一个额外的谓词或过滤器。

user261963