使用SSMS或Azure Data Studio,我可以在2毫秒内将50万行插入到临时表中,但是从表或临时表中检索要显示的行需要13到15秒。我不确定在哪里丢失了性能,下一步该怎么办,几个月前就发布了。
这是一台已经运行了几个月的SQL Server生产服务器,现在已经运行SQL2017。无论我是在客户端上还是直接在服务器上,但在具有标准磁盘和仅8 GB RAM的基本PC上,都会发生这种情况快三倍
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.
答案 0 :(得分:1)
插入数据和显示数据非常不同。当您执行INSERT
时,没有数据返回给客户端,所有内容都包含在实例中。该应用程序也不需要解释返回的数据集并将其转换为显示。
使用SELECT
来显示数据时,该数据需要从实例发送到客户端;如果该客户端是远程主机,则网络速度/流量之类的因素可能是大型数据集的重要因素(此处有50万行,这是一个不错的显示量)。应用程序还需要从实例中解释该数据并将其转换为可显示的格式。在SSMS中,可能会将其转换为数据网格。如果您正在限制客户端,那么您还可能会减慢查询速度,因为需要将数据加载到应用程序的内存池中并进行处理才能显示。
SELECT
和INSERT
的速度(尤其是对于大型数据集)的速度可能不具有可比性,因为操作方法大不相同。
答案 1 :(得分:0)
结果是,Sophos防病毒软件有一个“待重启”更新,导致瓶颈重新启动了服务器,并全部恢复了预期的基准。
答案 2 :(得分:0)
由于您使用的是SQL 2017,因此可以使用会话等待统计信息来衡量引擎等待查询的时间。 EG在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