The symptom
The database server's CPU is at 90% and the monitoring does not say why. Databases burn CPU on three things: logical reads (every page touched is work), sorting and hashing, und per-row functions or recompiles. The statement history ranks all three, because CPU time is recorded per query.
Diagnose it
SQL Server: the plan cache and the schedulers
SELECT TOP 10
qs.total_worker_time / 1000 AS cpu_ms,
qs.execution_count,
qs.total_worker_time / qs.execution_count / 1000 AS cpu_ms_per_run,
SUBSTRING(st.text, (qs.statement_start_offset / 2) + 1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset
END - qs.statement_start_offset) / 2) + 1) AS sql_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY qs.total_worker_time DESC;
cpu_ms_per_run separates the query called 10 times an hour that burns 8 seconds of CPU each from one called a million times at 3 ms. Both need fixing; differently. Confirm the pressure is real work with sys.dm_os_schedulers (runnable_tasks_count persistently above zero on many schedulers means CPU starvation, not a runaway query).
PostgreSQL: pg_stat_statements
SELECT calls, round(total_exec_time::numeric) AS total_ms,
round(mean_exec_time::numeric, 1) AS mean_ms,
shared_blks_hit + shared_blks_read AS blocks,
left(query, 80) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
das blocks column is the tell: high blocks per call means the CPU is chasing pages, and an index fixes it; low blocks with high mean time means the CPU is spent in functions, regexes or sorting inside the query itself.
MySQL: the digest table
SELECT LEFT(DIGEST_TEXT, 80) AS query, COUNT_STAR,
round(SUM_TIMER_WAIT / 1e12, 1) AS total_seconds,
SUM_ROWS_EXAMINED, SUM_ROWS_SENT,
SUM_NO_INDEX_USED AS runs_without_index
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_ROWS_EXAMINED DESC
LIMIT 10;
MySQL spends most of its CPU reading and filtering rows, so rank by rows examined: a digest that examines millions of rows to send a few hundred is where the cores go. SUM_NO_INDEX_USED counts executions that did a full scan, not rows. sys.statements_with_full_table_scans lists the same offenders in plainer words.
Oracle: ASH and v$sqlstats
SELECT sql_id, COUNT(*) AS cpu_samples
FROM v$active_session_history
WHERE session_state = 'ON CPU'
AND sample_time > SYSTIMESTAMP - INTERVAL '30' MINUTE
GROUP BY sql_id
ORDER BY cpu_samples DESC
FETCH FIRST 10 ROWS ONLY;
Each ON CPU sample is roughly a second of CPU (or of waiting in the OS run queue for one), so this is the CPU budget by query. ASH needs the Diagnostics Pack; without it, order v$sqlstats by cpu_time and compare two snapshots taken a few minutes apart.
Fix it
Kill the scans first. The query with the most logical reads per execution is the CPU leader almost everywhere; add the index its predicate wants and CPU falls with it. The slow query guide's plan method applies unchanged.
Then kill the row-by-row work. Functions on columns in WHERE clauses, per-row scalar subqueries, application-side loops re-running a prepared statement millions of times (visible as a huge execution_count/calls) - each is CPU the engine cannot amortize. A set-based rewrite or a bigger batch turns a million cheap CPU burns into one cheap pass.
Watch for compile churn. Ad-hoc SQL with literal values gets compiled on every run; on SQL Server it fills the plan cache with single-use plans (usecounts = 1 in sys.dm_exec_cached_plans) and burns CPU compiling. The app switching to bind parameters fixes it; database-level PARAMETERIZATION FORCED is the stopgap. PostgreSQL plans every unprepared statement afresh, and pg_stat_statements folds the literals together, so the cost shows up as planning time: set pg_stat_statements.track_planning = on and compare total_plan_time mit total_exec_time.
Only then resize. If the top statements are fixed and CPU still peaks at business hours, more cores may be right - but a server at 100% CPU serving an unindexed query stays at 100% CPU on bigger hardware too. Capture the plans of the remaining leaders with the method in the slow query guide, and paste them into the query plan viewer so the expensive operators are visible at a glance.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Rank statements by total CPU from the engine's statement history (queries above).
- Split the list by calls: one heavy query vs one query called a million times.
- For the heavy one, capture the plan and find the scans and spills.
- For the chatty one, find the call site in the app and batch or cache it.
- Add the index the top scan wants; re-measure CPU over the same period next day.
- Check for per-row functions in WHERE/SELECT and rewrite them set-based.
- Only consider resizing after the top three offenders are fixed.
Questions that come up
How do I tell CPU pressure from other load?
Count the sessions that want CPU against the cores you have. SQL Server: runnable_tasks_count in sys.dm_os_schedulers, plus a high share of signal_wait_time_ms in sys.dm_os_wait_stats. PostgreSQL: active backends in pg_stat_activity whose wait_event is NULL are running on CPU (there is no CPU wait event), compared with the OS load average. MySQL: the Threads_running status counter. Oracle: ON CPU sessions in v$session. If more sessions want CPU than there are cores, CPU is the bottleneck; if they wait on I/O or locks, the CPU spike is a symptom, not the cause.
Can a query burn CPU without reading a lot of rows?
Yes - CPU goes to parsing, compiling, functions, regexp and sorting. A query over a 20-row table called a million times can be the top CPU consumer while the 'big' report never registers. That is why the ranking splits by execution count before you optimize anything.
Does more RAM reduce CPU?
Often, indirectly: if pages are read from disk repeatedly, the CPU pays for every read. Fixing the working set (buffer pool, shared_buffers) lowers both I/O and the CPU that services it. But a plan that scans 10 million rows to return 50 stays expensive at any memory size.
Should I limit parallelism to reduce CPU load?
Only when the evidence says so: CXPACKET-dominated waits on SQL Server, or many parallel workers in pg_stat_activity (backend_type = 'parallel worker') on PostgreSQL, can mean parallel plans are amplifying load. On SQL Server the usual first change is raising cost threshold for parallelism from its default of 5, not cutting MAXDOP to 1. Lowering parallelism reduces per-query CPU but raises latency - it is a trade to make with the plan in hand, not a first move.
What if CPU is high but the database shows idle sessions?
Then the CPU is not the database's: check the OS (backup agents, monitoring, antivirus, other processes on the box). The database's own accounting - the queries here - only tallies what the database itself spends.