简体   繁体   English

如何减慢 SQL 查询?

[英]How To Slow Down A SQL Query?

As strange as it sounds I need to slow down a SQL query.听起来很奇怪,但我需要减慢 SQL 查询的速度。 Currently I'm using Microsoft SQL Server 2008 R2 on an in-house development server with the AdventureWorks database.目前,我在带有 AdventureWorks 数据库的内部开发服务器上使用 Microsoft SQL Server 2008 R2。 I'm in the process of testing some code and the queries that I'm running are too fast no matter what I try!我正在测试一些代码,无论我尝试什么,我正在运行的查询都太快了!

Basically I'm testing a cut-off feature and need a sufficiently long query to be able to cut it off before it completes.基本上我正在测试一个截止功能并且需要足够长的查询才能在它完成之前将其切断。

Unfortunately as it is a local installation there isn't a single query or large enough table in the AdventureWorks database to actually give me good data to work with.不幸的是,由于它是本地安装,因此 AdventureWorks 数据库中没有单个查询或足够大的表来实际为我提供有用的数据。 I've tried我试过了

WAITFOR DELAY '01:00'

Which worked great to just test to make sure it was working, but now I need to test to see if I can cut the data stream off mid-read.这对测试以确保它正常工作非常有用,但现在我需要测试看看我是否可以在读取中途切断数据 stream。 The WAITFOR statement doesn't do me justice in that respect because I need it to actively be retrieving data back from the server. WAITFOR 语句在这方面并不公平,因为我需要它主动从服务器检索数据。 My first intuition was to use convoluted calculations to slow it down, however even having SQL server multiply all the numerical values in the query by themselves 37 times only slowed down the query by milliseconds.我的第一直觉是使用复杂的计算来减慢它的速度,但是即使让 SQL 服务器将查询中的所有数值乘以 37 次,也只会减慢查询速度几毫秒。 The second thing I tried was embedding the WAITFOR statement in a sub-query but it appears you can't do that.我尝试的第二件事是将WAITFOR语句嵌入到子查询中,但您似乎不能这样做。 Finally, the only thing I haven't tried is to execute multiple stored procedures and WAITFOR in between them, but I don't think that would work for what I need.最后,我唯一没有尝试过的是执行多个存储过程和它们之间的WAITFOR ,但我认为这不会满足我的需要。

I have to say, I'm impressed at how hard it is to make an absolutely terrible query when you're this close to the server.我不得不说,当您离服务器这么近时,进行一个绝对糟糕的查询是多么困难,这给我留下了深刻的印象。

Is there any way I can slow down a query easily?有什么办法可以轻松地减慢查询速度吗?

Thank you!谢谢!

Just do a load of cross joins. 只需加载一些交叉连接。

SELECT T1.*
FROM SomeTable T1,  
     SomeTable T2,  
     SomeTable T3,  
     SomeTable T4

For a 1,000 row table that will generate 1,000 billion rows which should be plenty slow enough. 对于1000行表,将产生1,000亿行,这应该足够慢。

DECLARE @EndTime DATETIME;
SET @EndTime = DATEADD(s, 5, GETDATE()); -- Set your delay here

WHILE @EndTime > GETDATE()
    SELECT 'Test Result'; -- Add your desired query here

EDIT 编辑

Another option using recursion: 使用递归的另一个选项:

Create a UDF wrapper for GETDATE() so that a new date value will be calculated for each row in the result: GETDATE()创建一个UDF包装器,以便为结果中的每一行计算新的日期值:

CREATE FUNCTION dbo.GetExactDate()
RETURNS DATETIME    
AS
BEGIN
    RETURN GETDATE();
END

and then use a cte 然后使用cte

DECLARE @EndTime DATETIME;
SET @EndTime = DATEADD(s, 5, GETDATE()); -- Set your delay here

WITH cte AS (
    SELECT dbo.GetExactDate() Value
    UNION ALL
    SELECT dbo.GetExactDate()
    FROM cte
    WHERE Value < @EndTime
)
SELECT Value
FROM cte
OPTION (MAXRECURSION 0);

This has the advantage of returning the results in one query, not many (like my first solution) while still being able to set the amount of time for which you would like the query to keep returning results. 这样做的好处是可以在一个查询中返回结果,而不是很多(比如我的第一个解决方案),同时仍然能够设置您希望查询保持返回结果的时间量。

Tested on SQL Server 2016: (SQL View query takes always exactly two seconds to reply) 在SQL Server 2016上测试:( SQL View查询总是需要两秒钟才能回复)

/* Call WAITFOR DELAY inside SQL View */
/* Usefull for example for async testing */
CREATE FUNCTION WaitForDelay()
RETURNS INT
AS
BEGIN
  RETURN (
    SELECT Value FROM OPENROWSET (
    'SQLOLEDB', 'Trusted_Connection=yes;  Integrated Security=SSPI; Server=localhost; Initial_Catalog=master;',
    'WAITFOR DELAY ''00:00:02'' SELECT 0 AS Value'
  ))
END

GO
CREATE VIEW Wait AS
SELECT dbo.WaitForDelay() AS Value

GO
SELECT * FROM Wait /* Takes sql view 2 seconds to respond */

Shows Management Studio executing simple view slow down intentionally to 2 seconds: 显示Management Studio执行简单视图有意减慢到2秒:

https://i.stack.imgur.com/fE60u.png https://i.stack.imgur.com/fE60u.png

As easy as it gets, use SELECT SLEEP(n);尽可能简单,使用SELECT SLEEP(n); , where n is time in seconds. ,其中 n 是以秒为单位的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM