Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

Monitoring and diagnosing slow queries

SELECT ... FROM sys.query_store_runtime_stats JOIN sys.query_store_plan ...

Investigates slowness with time windows, metrics and execution plans.

Monitoring and diagnosing slow queriesAdvPL
01 · OVERVIEW

Overview

First bound the routine, company/branch, time, volume and observed change. Compare duration, CPU, reads and waits; a blocked query differs from a CPU-heavy query. SQL Server Query Store retains queries, plans and statistics by time window, depending on enablement, retention and version. The SQL below reads per-row/interval metrics from the last 24 hours without query text or configuration changes. It is not an aggregated total-cost ranking. (Sources: refs. 1–2.)

02 · SYNTAX

Syntax

SELECT ... FROM sys.query_store_runtime_stats JOIN sys.query_store_plan ...
03 · PRACTICAL EXAMPLE

Commented example

-- SQL Server: per-row metric reads, no plan changes.
SELECT TOP (20)
    p.query_id, r.plan_id, r.runtime_stats_interval_id,
    r.count_executions, r.avg_duration, r.last_execution_time
FROM sys.query_store_runtime_stats AS r
JOIN sys.query_store_plan AS p ON p.plan_id = r.plan_id
WHERE r.last_execution_time >= DATEADD(hour,-24,SYSDATETIMEOFFSET())
ORDER BY r.avg_duration DESC;
Expected result

Up to 20 available metric rows; zero rows does not prove absence of slowness. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Run only with authorized diagnostic access. Compare equivalent windows and weight averages by execution count when aggregating. Review joins, filters, logical deletion and per-table branches. Validate index or forced-plan changes with a DBA, testing and rollback; avoid NOLOCK as a generic cure.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. Microsoft. Monitor performance by using the Query Store. Accessed on 2026-10-27.
  2. TOTVS. Desenvolvendo queries no Protheus. TDN. Accessed on 2026-10-27.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha