2014-05-14 58 views
2

我有一个非常高效的SQL查询,但我有一种感觉可以改进。优化一个已经在做索引查询的SQL查询

这是索引寻找后使用IX_thing_time_location之后的排序的48%,我希望可以改进。我不想认为这是用这个查询可以做的最好的。除了更新查询,更改索引,分区(我知道这些并不总是意味着性能提升),我还可以做其他什么来提高性能?

下面是执行计划:http://pastebin.com/G4Zi2tnw

我试图将其粘贴在这里,但它太大。

指数的定义:

CREATE NONCLUSTERED INDEX [IX_thing_time_location] ON [dbo].[tippy] 
(
    [time_start] ASC, 
    [location] ASC 
) 
INCLUDE ( [id], 
    [name], 
    [time_end], 
    [is_meetup], 
    [utc_offset], 
    [type], 
    [all_day]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
GO 

存储过程:

ALTER PROCEDURE [dbo].[GetthingsByLatLong] 
@minLat FLOAT, 
@maxLat FLOAT, 
@minLong FLOAT, 
@maxLong FLOAT, 
@startTime BIGINT, 
@endTime BIGINT 
AS 

SELECT id, name, latitude, longitude 
INTO #templocations 
FROM locations 
WHERE latitude BETWEEN @minLat AND @maxLat 
AND longitude BETWEEN @minLong AND @maxLong 
ORDER BY id; 

-- This is a container 
-- Get all "routes" (containers) within a given lat/long combo 
SELECT thing_routes.* 
INTO #tempRoutes 
FROM thing_routes 
WHERE latitude BETWEEN @minLat AND @maxLat 
AND longitude BETWEEN @minLong AND @maxLong; 


-- Get all things which are in the above containers 
SELECT tip.id, tip.name, tip.location, tip.time_start, tip.time_end, tip.is_meetup, 
tip.utc_offset, tip.[type], tip.all_day, 
#tempRoutes.id AS route_id, locations.name AS location_name, 
locations.latitude AS latitude, locations.longitude AS longitude 
INTO #tempRoute_things 
FROM #tempRoutes 
INNER JOIN link_thing_routes 
ON link_thing_routes.route_id = #tempRoutes.id 
INNER JOIN locations 
ON locations.id = #tempRoutes.location 
INNER JOIN thing AS tip 
ON link_thing_routes.thing_id = tip.id; 


-- Return the data 
SELECT * FROM #tempRoutes 


-- Return the data - Add in the things from external_thing_routes 
-- Join the two tables from earlier, filtering on time 
SELECT tip.id, tip.name, tip.location, tip.time_start, tip.time_end, tip.is_meetup, 
tip.utc_offset, tip.[type], tip.all_day, NULL as route_id, #templocations.name AS location_name, 
#templocations.latitude AS latitude, #templocations.longitude AS longitude 
FROM #templocations 
INNER MERGE JOIN thing AS tip 
ON #templocations.id = tip.location 
WHERE time_start BETWEEN @startTime AND @endTime 



SELECT external_thing_routes.thing_id, external_thing_routes.route_id 
FROM external_thing_routes 
+0

包含在[IX_thing_time_location]索引哪些列。 –

+0

@AnthonyHorne - 我将它添加到问题中。 – Faraday

+1

也许看看有多少I/O发生和筛选索引。 –

回答

0

我没有看剧本解释,但我已经碰到了类似的问题。

是否在与选择相同的索引中排序的列?如果你有这样的查询:

select * 
from exampletable 
where foreignkey = somevalue 
order by column1, column2 

一个指标应该是

foreignkey, column1, column2 
+0

概念这背后其实是正确的 –