SqlServer 多表连接查询优化方案

2,895 阅读2分钟

对表进行分类拆分

因为业务需求,一次涉及到近20张表的连接查询耗时近3秒,及其影响前端页面的使用体验,经过不懈的努力找到了以下的解决方案。 先简单的描述下优化方案: 把表分类,比如父-子订单表、游戏-道具-游戏服务器表等等,先把类别下的几张表做表关联查询插入到临时表中:

--游戏表、道具表、游戏服务器表
select game.gameid,game.gameName,item.itemid,item.itemName,server.serverName
into #gameInfo --插入临时表
from Game game with(nolock)
inner join GameItem item with(nolock)
on game.gameid = item.gameid
inner join GameServer server with(nolock)
on game.gameid = server.gameid
where 1 = 1
and --条件句;

--父订单表、子订单表(表示用户一次购买多个道具,每个道具算作一个子订单)
select order.orderNo,order.gameid,suborder.itemid,suborder.subOrderNo
into #orderInfo
from Order order with(nolock)
inner join SubOrder suborder with(nolock)
on order.orderNo = suborder.orderNo
where 1 = 1
and --条件句;

用生成的两个临时表再做表关联查询:

select *
from #gameInfo gameInfo with(nolock)
inner join #orderInfo orderInfo with(nolock)
on gameInfo.gameid = orderInfo.gameid and gameInfo.itemid = orderInfo.itemid
where 1 = 1;

为临时表创建索引进一步提升查询速率

以上这种方式可以极大的提高查询速率,但查询耗时仍然有近1秒,体验依然不好,能否降低到300ms内?之后又在网上各种搜,终于找到了解决办法,也挺简单,就是创建临时表索引:

CREATE INDEX GameInfo_ItemID_Index ON #gameInfo(itemid);
CREATE INDEX OrderInfo_ItemID_Index ON #orderInfo(itemid);

尽量把索引建立在关键字段上

最后一定不要忘了把临时表删除:

DROP TABLE #gameInfo
DROP TABLE #orderInfo

为临时表创建的索引不需要单独删除,删掉表即可。

查询SQL语句的耗时、I/O等数据

可以用以下语句查询SQL语句的耗时情况:

SELECT creation_time N'语句编译时间'
,last_execution_time N'上次执行时间'
,total_physical_reads N'物理读取总次数'
,total_logical_reads/execution_count N'每次逻辑读次数'
,total_logical_reads N'逻辑读取总次数'
,total_logical_writes N'逻辑写入总次数'
,execution_count N'执行次数'
,total_worker_time/1000 N'所用的CPU总时间ms'
,total_elapsed_time/1000 N'总花费时间ms'
,(total_elapsed_time / execution_count)/1000 N'平均时间ms'
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) N'执行语句'
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
where SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) like '%into #gameInfo%' --sql语句的关键字
and last_execution_time > '2020-09-28 15:19:25.750' --sql语句执行时间
ORDER BY total_elapsed_time / execution_count DESC;

也可以用以下方式查看每次查询时各张表的耗时、I/O等数据:

SET STATISTICS IO ON 
SET STATISTICS TIME ON 

--你的SQL语句

SET STATISTICS IO OFF
SET STATISTICS TIME OFF