Sql 数据检索性能
使用SSMS或Azure data Studio,我可以在2毫秒内将50万行插入临时表,但从临时表或临时表中检索要筛选的行需要13到15秒。我不确定下一步该去哪里寻找性能损失的地方,因为这不是几个月前发布的问题 这是一台生产服务器,从SQL 2017开始已经运行了几个月。无论我是在客户机上还是直接在服务器上,都会发生这种情况,但是在一台带有标准磁盘的基本PC上&只有8gig的RAM,速度快了三倍Sql 数据检索性能,sql,sql-server,sql-server-2017,Sql,Sql Server,Sql Server 2017,使用SSMS或Azure data Studio,我可以在2毫秒内将50万行插入临时表,但从临时表或临时表中检索要筛选的行需要13到15秒。我不确定下一步该去哪里寻找性能损失的地方,因为这不是几个月前发布的问题 这是一台生产服务器,从SQL 2017开始已经运行了几个月。无论我是在客户机上还是直接在服务器上,都会发生这种情况,但是在一台带有标准磁盘的基本PC上&只有8gig的RAM,速度快了三倍 SELECT SML.CONTACT_Id INTO ##slr FROM dbo.Stage_Ma
SELECT SML.CONTACT_Id
INTO ##slr
FROM dbo.Stage_MailingLists AS SML;
SELECT *
FROM ##slr AS S;
DROP TABLE ##slr;
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 2 ms.
Table 'Stage_MailingLists'. Scan count 9, logical reads 13482, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(521001 rows affected)
(1 row affected)
SQL Server Execution Times:
CPU time = 1187 ms, elapsed time = 365 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
(521001 rows affected)
Table '##slr'. Scan count 1, logical reads 1493, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row affected)
SQL Server Execution Times:
CPU time = 329 ms, elapsed time = 8296 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
插入数据和显示数据是非常不同的。当您执行
插入时
不会将任何数据返回到客户机,所有内容都包含在实例中。应用程序也不需要解释返回的数据集并将其转换为显示
当您使用选择
以显示数据时,需要将该数据从实例发送到客户端;如果该客户机是远程主机,那么网络速度/流量等因素可能是大型数据集的一个重要因素(这里有50万行,这是一个相当可观的显示量)。应用程序还需要解释来自实例的数据,并将其转换为可显示的格式;在SSM中,可能正在将其转换为数据网格。如果您正在限制客户端,那么也可能会减慢查询速度,因为数据需要加载到应用程序的内存池中并进行处理以显示
选择
和插入
的速度,特别是对于大型数据集,可能无法与之相比,因为有着完全不同的操作。结果是,Sophos antivirus有一个更新“挂起重启”,导致服务器重启瓶颈,并全部返回到预期基线。因为您使用的是SQL 2017,您可以使用会话等待统计信息来测量引擎等待查询的时间。例如,在Master中安装一个程序,如:
use master
go
create or alter procedure sp_exec_with_time_and_wait_stats @sql nvarchar(max)
as
begin
set nocount on;
with q as
(
select *
from sys.dm_exec_session_wait_stats
where session_id = @@spid
union all
select @@spid session_id, 'CPU_TIME', 0,cpu wait_time_ms,0,0
from sysprocesses where spid = @@spid
)
select *
into #waits
from q
print ('-------begin batch--------')
print ( @sql )
print ('--------end batch---------')
set statistics io on;
set statistics time on;
exec ( @sql );
set statistics io off;
set statistics time off;
declare c cursor local for
with n as
(
select *
from sys.dm_exec_session_wait_stats
where session_id = @@spid
union all
select @@spid session_id, 'CPU_TIME', 0,cpu cpu_time,0,0
from sysprocesses where spid = @@spid
)
select n.wait_type,
n.waiting_tasks_count - coalesce(p.waiting_tasks_count,0) waiting_tasks_count,
n.wait_time_ms - coalesce(p.wait_time_ms,0) wait_time_ms,
case when n.max_wait_time_ms > coalesce(p.max_wait_time_ms,0) then n.max_wait_time_ms else null end max_wait_time,
n.signal_wait_time_ms - coalesce(p.signal_wait_time_ms,0) signal_wait_time
from n
left join #waits p
on p.wait_type = n.wait_type
where n.session_id = @@spid
and n.wait_time_ms > coalesce(p.wait_time_ms,0)
order by n.wait_time_ms - coalesce(p.wait_time_ms,0) desc;
declare @wait_type nvarchar(60),
@waiting_tasks_count bigint,
@wait_time_ms bigint,
@max_wait_time_ms bigint,
@signal_wait_time_ms bigint
print (
'
SQL Server Wait Times (with CPU):
wait_type waiting_tasks_count wait_time_ms max_wait_time signal_wait_time')
print (
' ------------------------------------------------------------ -------------------- --------------------- ------------------ --------------------')
open c
fetch next from c into @wait_type, @waiting_tasks_count, @wait_time_ms, @max_wait_time_ms, @signal_wait_time_ms
while @@FETCH_STATUS = 0
begin
declare @line nvarchar(2000) = N''
declare @val nvarchar(60) = @wait_type
declare @len int = 45
set @line = concat(@line,left(concat(@val, space(@len)),@len))
set @len = 20
set @val = str(@waiting_tasks_count)
set @line = concat(@line,right(concat(space(@len),@val),@len))
set @val = str(@wait_time_ms)
set @line = concat(@line,right(concat(space(@len),@val),@len))
set @val = str(coalesce(@max_wait_time_ms,''))
set @line = concat(@line,right(concat(space(@len),@val),@len))
set @val = str(@signal_wait_time_ms)
set @line = concat(@line,right(concat(space(@len),@val),@len))
print (' ' + @line)
fetch next from c into @wait_type, @waiting_tasks_count, @wait_time_ms, @max_wait_time_ms, @signal_wait_time_ms
end
close c
deallocate c
print ('')
end
然后从您的数据库
use AdventureWorksDW2017
go
declare @sql nvarchar(max) = 'select top 1000000 * into #x from factInternetSales order by 1,2,5;'
exec sp_exec_with_time_and_wait_stats @sql
它将打印您的IO、CPU和等待状态,如:
-------begin batch--------
select top 1000000 * into #x from factInternetSales order by 1,2,5;
--------end batch---------
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 12 ms.
Table 'FactInternetSales'. Scan count 9, logical reads 1260, physical reads 0, read-ahead reads 1260, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 1057, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 435 ms, elapsed time = 335 ms.
SQL Server Execution Times:
CPU time = 435 ms, elapsed time = 349 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Wait Times (with CPU):
wait_type waiting_tasks_count wait_time_ms max_wait_time signal_wait_time
------------------------------------------------------------ -------------------- --------------------- ------------------ --------------------
LATCH_EX 4879 572 0 69
CPU_TIME 0 435 0 0
CXPACKET 22 331 0 0
PAGEIOLATCH_SH 26 10 0 0
PAGELATCH_SH 17 6 0 1
PAGELATCH_UP 74 6 0 1
LCK_M_S 9 3 0 0
CXROWSET_SYNC 8 2 0 0
MEMORY_ALLOCATION_EXT 601 1 0 0
SESSION_WAIT_STATS_CHILDREN 11 1 0 1
LATCH_SH 3 1 0 0
你好感谢您的回复我理解,我试图证明服务器功能正常等,问题是检索方面出现了前所未有的大幅下降,我不知道要测试什么/基准点,即是否与SAN相关等,但如果数据是RAM,SAN为什么会成为问题,因此,网络,但为什么它直接在服务器上和在客户端上是一样的。“在基本pc上…”最有可能的意思是“在没有其他活动的pc上”。苹果与橙子的比较。它还省略了通过网络发送结果集所需的时间。组织外部的任何人都无法提供有用的信息。而等待数月的讨论只会增加查找原因的难度(假设它不是纯粹基于资源争用)。你的出发点应该是询问你的dba。将数据移出数据库是一个相当昂贵的操作——就像在某些设备上显示数据一样。嗨,我问过dba他和我一样困惑,我在工作时间之外做了测试,所以负载接近于零。我已经使用SQL多年了,我知道您提出的问题是,性能大幅下降,但没有升级/更改。这只是一个例子,看看人们是否可以给我任何选择来尝试,我有一个服务器上运行缓慢的查询负载,该服务器整天的空闲时间不到10%的CPU,使用该服务器运行速度很快,但突然大大降低,慢了3到5倍。