dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

Get-DbaAgentJob slowness on applying DatabaseName filter

Open xshen1 opened this issue 10 months ago • 4 comments

Verified issue does not already exist?

I have searched and found no existing issue

What error did you receive?

No errors, just lots of time spent waiting for output when we want to return job with Database filter

Steps to Reproduce

 Get-DbaAgentJob -SqlInstance sqlserver2014a -Database test_database

Please confirm that you are running the most recent version of dbatools

It takes around 30~40 min of each calling if we choose to apply -Database filter

Other details or mentions

            if ($Database) {
                $jobs = $jobs | Where-Object { $_.JobSteps | Where-Object DatabaseName -in $Database }
            }

this is more like n^2log(n) wonder if we can use the first jobstep or something to reduce the filter time by a lot

What PowerShell host was used when producing this error

Windows PowerShell ISE (powershell_ise.exe)

PowerShell Host Version

Name Value


PSVersion 5.1.17763.5576
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.5576
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

SQL Server Edition and Build number

Microsoft SQL Server 2019 (RTM-CU25) (KB5033688) - 15.0.4355.3 (X64) Jan 30 2024 17:02:22 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2019 Datacenter 10.0 <X64> (Build 17763: ) (Hypervisor)

.NET Framework Version

PSChildName Version


Client 4.8.03761 Full 4.8.03761 Client 4.0.0.0

xshen1 avatar Apr 24 '24 14:04 xshen1

30-40 minutes?!?!

  • How many jobs does your instance have?
  • What is the average number of steps per job on the instance?
  • How many databases does the instance have?
  • Do you get the same result in PowerShell or Windows PowerShell (don't use ISE)?

wsmelton avatar Apr 28 '24 04:04 wsmelton

We got like 10 job per db. A LOT of DBs and on average 4~5 steps per job. Yeap same result ummm

xshen1 avatar May 16 '24 20:05 xshen1

A LOT of DBs? Is that under 100, over 500, under 3,000? (Everyone's definition of a lot isn't the same 😉)

Be aware that if you have a large instance (once you get over ~1k databases) then the performance can play in from your master database on performance. What are the waits showing on the sessions for your scripts?

wsmelton avatar May 21 '24 00:05 wsmelton

Maybe a -FirstJobStepDatabaseName would go better on Find-DbaAgentJob. It is already a bit confusing on what parameters are supported on Get-DbaAgentJob versus Find-DbaAgentjob.

I saw about a 30% improvement on my 132 job server with about two steps per job which has only 6 dbs including system. Went from 13 seconds to about 8 seconds

Get-DbaAgentJob mysqli | ? { $_.jobsteps[0].databasename -eq 'master' }

mattcargile avatar Aug 02 '24 21:08 mattcargile