A slowdown that lasts a few minutes at the same time every day, with no explanation on the SQL Server side, is often the trace of another job running on the server itself. Tasks in Windows Task Scheduler (file copy scripts, old backup tools, antivirus scans, disk maintenance, a reporting program someone set up once and forgot) share the same CPU, memory and disk as SQL Server. Because they don’t show up in SQL Server’s own tools, they’re usually the last thing anyone thinks of.
This article shows how to list scheduled tasks on a database server, how to correlate them with performance drops and how to sort them out.
Unexplained slowdowns at certain times?
Aryasoft reviews SQL Server together with the server it runs on, and finds the source of periodic slowdowns.
Symptoms
- A slowdown that recurs at the same time every day or every week.
- No heavy query or Agent job running in SQL Server at that time.
- Disk latency or CPU usage rises at that time, but SQL Server’s share doesn’t.
PAGEIOLATCHorWRITELOGwaits rise in certain periods.
Listing Scheduled Tasks
Windows’ own maintenance tasks live under the \Microsoft\ folder. The ones to look at are the tasks added later:
# Enabled non-Microsoft tasks with their last and next run times
Get-ScheduledTask |
Where-Object { $_.State -ne 'Disabled' -and $_.TaskPath -notlike '\Microsoft\*' } |
ForEach-Object {
$info = Get-ScheduledTaskInfo -TaskName $_.TaskName -TaskPath $_.TaskPath
[pscustomobject]@{
Task = $_.TaskPath + $_.TaskName
RunAs = $_.Principal.UserId
Command = ($_.Actions | ForEach-Object { $_.Execute + ' ' + $_.Arguments }) -join '; '
LastRun = $info.LastRunTime
NextRun = $info.NextRunTime
}
} | Sort-Object NextRun | Format-Table -AutoSize
Compare the tasks’ run times with the times the slowdown occurs. Which account a task runs as (RunAs) matters too: a task of unknown ownership running as a local or domain administrator is also a security risk.
Common Culprits
- File-level backups and copies: scripts that copy SQL Server data and log files or the backup folder. Copying files SQL Server holds open both creates load and doesn’t produce a consistent backup.
- Antivirus scans: if SQL Server files and folders aren’t in the exclusion list, a full scan touches the data files too.
- Disk defragmentation and maintenance: can create unnecessary I/O on SAN or virtual disks; review it with the storage team.
- Old reporting and integration scripts: programs that query the database through sqlcmd or PowerShell that nobody uses any more.
- Update and inventory tools: scan and update tasks set to run during business hours.
Warning
Backing up database files (mdf, ndf, ldf) by copying them doesn’t produce a consistent backup; restoring from files copied while SQL Server was running is often impossible. Database backups should always be taken with the BACKUP command or with a backup solution integrated with SQL Server.
What to Do
- Identify the owner and purpose of every task; disable tasks with no owner and no known purpose.
- Move database-related work into SQL Server Agent, so its history, notifications and schedules are managed in one place. See our SQL Server Agent guide.
- Move the tasks that must stay to times that don’t collide with SQL Server’s maintenance windows.
- Define antivirus exclusions for SQL Server.
- Add the task list to the server documentation and review it regularly in health checks.
Tip
When investigating slowdowns that recur at certain times, record SQL Server’s wait statistics and disk latency in a table periodically. A baseline showing what changed at which hour reveals within a few days whether the problem comes from inside SQL Server or outside it.
Checklist
- All non-Microsoft scheduled tasks are listed.
- Every task’s owner, purpose and run-as account are known.
- Database work has been moved to Agent.
- Remaining tasks don’t collide with maintenance windows.
- Databases aren’t backed up by copying files.
Related Reading
SQL Server Performance Consulting
See the whole server
Aryasoft’s senior DBA team reviews SQL Server together with the server it runs on, and finds and fixes periodic slowdowns caused by scheduled tasks, antivirus and other software.