As part of a stress tests I'm doing I'm trying to figure out if there is an SQL query (quite specifically SQL Server query) that will max all CPUs usage to 100% or close enough.
Suggestions anyone?
As part of a stress tests I'm doing I'm trying to figure out if there is an SQL query (quite specifically SQL Server query) that will max all CPUs usage to 100% or close enough.
Suggestions anyone?
SELECT SUM(CONVERT(BIGINT, o1.object_id) + CONVERT(BIGINT, o2.object_id) + CONVERT(BIGINT, o3.object_id) + CONVERT(BIGINT, o4.object_id))
FROM sys.objects o1
CROSS JOIN sys.objects o2
CROSS JOIN sys.objects o3
CROSS JOIN sys.objects o4
Here's a parallel version:
USE master
SELECT MyInt = CONVERT(BIGINT, o1.object_id) + CONVERT(BIGINT, o2.object_id) + CONVERT(BIGINT, o3.object_id)
INTO #temp
FROM sys.objects o1
JOIN sys.objects o2 ON o1.object_id < o2.object_id
JOIN sys.objects o3 ON o1.object_id < o3.object_id
SELECT SUM(CONVERT(BIGINT, o1.MyInt) + CONVERT(BIGINT, o2.MyInt))
FROM #temp o1
JOIN #temp o2 ON o1.MyInt < o2.MyInt
For some reason I cannot get the optimizer to parallelize the first query. So I just materialize some huge tables (~400k rows) and loop join them.
ORDER BY ...
to really make the CPU work ;) –
Graehme sum()
could lead to overflow, can add sin
making it as sum(sin(CONVERT(BIGINT, o1.MyInt)))
. –
Montague I've talked at length in How to analyse SQL Server performance about why practically your query never 'executes': is always waiting on something (IO, locks).
To create a workload that drives 100% CPU, even on one core, is no small feat. You need to make sure your query always execute and never waits. Never blocks for IO (all data must be in memory), never blocks for locks (no contention), never blocks for memory (no grant). You should look as scans of hot in-memory data. An artificial, totally bogus, workload that achieves this would probably self-join a medium size table many times.
Now if you want to do this with a realistic workload, including various operations, then good luck. Achieving 100% CPU is basically the benchmarks golden standard. You need super performant IO subsystem to eliminate all waits and you need very fancy test driver to be able to feed the workload fast enough, without creating contention.
I think the better way for keeping CPU busy is using POWER function in all compilers
DECLARE @T DATETIME, @F BIGINT;
SET @T = GETDATE();
WHILE DATEADD(SECOND,60,@T)>GETDATE()
SET @F=POWER(2,30);
You can make several query run at same time depends your CPU capacity
Take a look at this link below (Original article)
https://blog.sqlauthority.com/2013/02/22/sql-server-t-sql-script-to-keep-cpu-busy
The idea comes from usr, to makes it a little more general, no SQL Server peculiar things used, so you can use it for any kind of DB/DM. Also there is no write, which will avoid the I/O affect.
Need to select a big use or system table "table1" at first.
If this table has record N, following query produces O(N^4) complexity which optimizer cannot improve it.
with t1 as
(
select
d1.f1 as i1, d2.f1 as i2, d3.f1 as i3, d3.f2 as f2
from
table1 d1
inner join
table1 d2 on d1.f1 < d2.f1
inner join
table1 d3 on d2.f1 < d3.f1
order by
f2
)
select
avg(sin(o1.i1)) + avg(cos(o2.i1))
from
t1 o1
join
t1 o2 on o1.i1 < o2.i1
This is very old yet interesting question, but this link (from Brent Ozar) is the best solution compared with all other solutions here listed
https://www.brentozar.com/archive/2018/09/one-hundred-percent-cpu/
CREATE OR ALTER PROCEDURE dbo._keep_it_100
AS
BEGIN
WITH e1(n) AS
(
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL
),
e2(n) AS (SELECT TOP 2147483647 NEWID() FROM e1 a, e1 b, e1 c, e1 d, e1 e, e1 f, e1 g, e1 h, e1 i, e1 j)
SELECT MAX(ca.n)
FROM e2
CROSS APPLY
(
SELECT TOP 2147483647 *
FROM (
SELECT TOP 2147483647 *
FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
UNION ALL SELECT * FROM e2
) AS x
WHERE x.n = e2.n
ORDER BY x.n
) AS ca
OPTION(MAXDOP 0, LOOP JOIN, QUERYTRACEON 8649);
END;
© 2022 - 2024 — McMap. All rights reserved.