我有一个SQL Server 2017 Standard Edition数据库,该数据库在命令上使用多个线程。
例如,我运行简单的select并且数据库打开了更多线程。这是不可取的,因为我为有限数量的用户购买了许可证,并且如果some命令花费的时间更长,则会阻止对数据的访问。如何禁用它?
感谢答案
DECLARE @loginname NCHAR(128)
DECLARE @spid SMALLINT
DECLARE @EventType NCHAR(30)
DECLARE @Parameters SMALLINT
DECLARE @EventInfo NVARCHAR(4000)
SELECT
loginame, nt_domain, nt_username, hostname, login_time, program_name,
CASE
WHEN dbid = 0 THEN N''
ELSE DB_NAME(dbid)
END,
spid, open_tran, net_library, cpu, physical_io,
memusage, blocked, status, last_batch, cmd, context_info
FROM
master.dbo.sysprocesses
WHERE
net_library <> N''
AND program_name <> N'SQLAgent - Generic Refresher'
AND program_name <> N'SQLAgent - Alert Engine'
AND program_name <> N'SQLAgent - Job invocation engine'
DECLARE c1 CURSOR LOCAL FAST_FORWARD FOR
SELECT loginame, spid
FROM master.dbo.sysprocesses
WHERE net_library <> N''
AND program_name <> N'SQLAgent - Generic Refresher'
AND program_name <> N'SQLAgent - Alert Engine'
AND program_name <> N'SQLAgent - Job invocation engine'
OPEN c1
WHILE 1 = 1
BEGIN
FETCH NEXT FROM c1 INTO @loginname, @spid
IF @@FETCH_STATUS <> 0
BREAK
DBCC INPUTBUFFER(@spid)
END
CLOSE c1
DEALLOCATE c1
例如,使用相同选择命令的7个线程的输出:
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 405 | 0 | 4 | 0 | runnable | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 218 | 77 | 0 | 0 | suspended | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 265 | 93 | 0 | 0 | suspended | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 219 | 30 | 0 | 0 | suspended | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 219 | 65 | 0 | 0 | suspended | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 141 | 39 | 0 | 0 | runnable | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 203 | 75 | 0 | 0 | suspended | 2018-07-28 19:47:17.207 | SELECT |
| WS2012 | 2018-07-28 19:47:17.217 | Helios Orange - HeliosMain 2.0.2018.0600 HEIQ0100-22970|2 | Helios001 | 62 | 0 | TCP/IP | | 125 | 20 | 0 | 0 | runnable | 2018-07-28 19:47:17.207 | SELECT |
答案 0 :(得分:1)
您可以使用以下方式禁用查询并行性:
EXEC sp_configure 'show',1;
RECONFIGURE;
EXEC sp_configure 'max degree of parallelism',1;
RECONFIGURE;
也就是说,解决阻塞问题的方法不是关闭并行性。解决方案是查询和索引调整以及适当的隔离级别和/或打开READ_COMMITTED_SNAPSHOT
数据库选项。