我有一个非常有效的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 :(得分:0)
我没有看解释脚本,但我遇到了类似的问题。
您在哪个列中选择与选择相同的索引?如果您有这样的查询:
select *
from exampletable
where foreignkey = somevalue
order by column1, column2
其中一个索引应该是
foreignkey, column1, column2