-2

I have SQL Server running on my local machine. I write integration tests in c# and I need to clean up data after each test run. Only I have access to the server, and it does pretty much nothing besides what is in the tests. There are about ~50 raw sql queries that delete records from various tables. I don't run any heavy deletes, at most I delete 1-10 records within single query. I don't see any heavy cpu usage and a lack of memory. I measure statistics of the clean up queries on c# side using a stopwatch, and I noticed an odd behaviour. As a whole it takes around 2 seconds to clean up data and each query takes a small portion of it, however this number is unstable, usually it takes 2 seconds, but sometimes it drops to 0.2 seconds (where all underlying queries just run faster), or goes up to 7 seconds, but these fluctuations are not as frequent. I run the same test multiple times, it works roughly on the same data each time. I decided to change sql process priority to the highest via Task Manager. And, I see big improvement, it now takes 0.2 seconds to run deletes and I don't observe slow deletes at all. What can cause slowness in the first place, and is there any indicator to pinpoint it?

Processes with higher priority run faster, however in my opinion the difference shouldn't be that dramatic.

My spec Microsoft SQL Server Developer (64-bit), 16.0.1145.1, Windows 10 Pro (10.0), 13th gen intel(r) core(tm) i9-13980hx.

UPD. I don't think it's related to queries themselves, and there is any point in posting query plans. There is something wrong with overall process on my server. I run following test in db with a single table.

create table abc
(
    id int primary key
)

If I run following code

delete from abc
declare @count int = 1
while @count < 1000
begin
    insert into abc values (@count)
    set @count = @count + 1
end
set @count = 1

DECLARE @StartTime DATETIME2 = GETDATE();
while @count < 1000
begin
    delete from abc where id = @count
    set @count = @count + 1
end


DECLARE @EndTime DATETIME2 = GETDATE();
SELECT DATEDIFF(millisecond, @StartTime, @EndTime) AS [DurationInMilliseconds];

I ran it 10 times. And in normal priority I got following measurements:

200 244 380 336 283 310 464 407 350 480 
avg=345.4 stddev=70.8

in high priority I get following measurements:

120 77 57 74 153 80 123 66 74 67
avg=89.1 stddev=25.74

Also, it definitely doesn't affects only delete's, it feels like everything runs multiple times faster when I change priority. It just that I started to measure this particular case. I've stumbled upon priority by accident, and in my experience changing priority usually has negligible or zero impact, I don't think the original delete's are run in reasonable time in normal. There is no data in the tables, almost all tables are empty or have one or two rows.

Here are plans for the delete when it performs slow

https://www.brentozar.com/pastetheplan/?id=Z1irmeobbE https://www.brentozar.com/pastetheplan/?id=9XWnRDRdHf

Here are plans that perform well

https://www.brentozar.com/pastetheplan/?id=DS94YXry8t https://www.brentozar.com/pastetheplan/?id=1cpMHw4pqo

Don't hesitate to ask me to do additional tests.

UPD2. Actually some of my colleagues have the same issue, we use the same laptops and similar set of software. But they don't know reason for that either, they have some hypothesis, but nothing convincing in particular. What they have found that enabling 'Boost SQL Server Priority' property also solves the issue. They also noticed that they db tests are running slower on that laptops, and tests don't consume CPU (and they usually do) unless they change priority.

0

1 Answer 1

4

I run following test in db with a single table.

The test is essentially a CPU driven test. If you tell the OS to schedule the threads in the process more often (by changing the priority) then it'll complete the work faster.

They also noticed that they db tests are running slower on that laptops, and tests don't consume CPU (and they usually do) unless they change priority.

The tests are consuming CPU, changing the priority would not make the tests run any faster if they weren't waiting on CPU. Laptop chips generally don't have as many or as fast of a CPU compared to desktop, workstation, or server class chips.

Processes with higher priority run faster, however in my opinion the difference shouldn't be that dramatic.

Yes and no. Threads with higher priority won't run faster, they will be scheduled more often, ahead of other threads. If you're scheduled to do something 4x more than something else, it's not shocking when you get something completed ~4x faster or do ~4x more items.

Here are plans for the delete when it performs slow

Outside of a short compile time, there's nothing in there of bother. The actual query execution completes in less than 1 ms.

Running that 1000 times takes 345.4ms, and if I switch priority it takes 89.1ms.

IMO there's nothing wrong if you're running it 1000 times in a tight loop and it takes 1/3 of a second. Change it to delete 1000 rows at a time and it'll take less time, because it's not death by 1000 cuts.

Ok, I see difference. If I insert 10k rows, I get ~6ms on high priority and ~30ms on normal

Everything that is being observed is how Windows OS works (or any other mainstream multi-process, non-realtime OS) when it has to share resources and how SQL Server works within the OS. Of course changing the priority of all the threads in a process will change the behavior in terms of scheduling and make CPU intensive tasks seem to run faster.

2
  • 4x or anything greater cannot be practically observed. People that believe it can do the test on their own. Commented Sep 12 at 9:57
  • The comment shows you've completely missed the entire point :/ Commented Sep 15 at 12:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.