优化已在执行索引搜索的SQL查询
我有一个非常有效的SQL查询,但我觉得它可以改进 这是使用优化已在执行索引搜索的SQL查询,sql,sql-server,sql-server-2012,database-performance,Sql,Sql Server,Sql Server 2012,Database Performance,我有一个非常有效的SQL查询,但我觉得它可以改进 这是使用IX_thing\u time\u location进行索引查找后排序的48%成本,我希望可以改进。我不想认为这是用这个查询可以做到的最好的方法。在更新查询、更改索引、分区(我知道这些并不总是意味着性能提高)方面,我还可以做些什么来提高性能 以下是执行计划: 我试着把它贴在这里,但它太大了 索引定义: CREATE NONCLUSTERED INDEX [IX_thing_time_location] ON [dbo].[tippy] (
IX_thing\u time\u location
进行索引查找后排序的48%成本,我希望可以改进。我不想认为这是用这个查询可以做到的最好的方法。在更新查询、更改索引、分区(我知道这些并不总是意味着性能提高)方面,我还可以做些什么来提高性能
以下是执行计划:
我试着把它贴在这里,但它太大了
索引定义:
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
我没有看解释脚本,但我遇到了类似的问题 对其排序的列是否与所选内容在同一索引中?如果您得到这样的查询:
select *
from exampletable
where foreignkey = somevalue
order by column1, column2
其中一个索引应该是
foreignkey, column1, column2
[IX_thing_time_location]索引中包含哪些列。@AnthonyHorne-我将其添加到了问题中。也许可以查看发生了多少I/O并过滤了索引。也许您的查询,不仅执行计划会有用…@Alfons-缓慢来自注释下方的查询<代码>连接前面的两个表,按时过滤-我在整个SP中添加了一个以供参考。