/**
* Find SQL queries that have the most latency (how much overall time it takes).
*/
SELECT
	-- The raw SQL statement being executed.
	t.query,
	t.exec_count,
	-- Latency in the "x$" tables are reported as picoseconds. This built-in function
	-- formats the raw time into human-friendly values, like "3.5 s" and "56.07 ms".
	sys.format_time( t.total_latency ) AS total_latency,
	sys.format_time( t.max_latency ) AS max_latency,
	sys.format_time( t.avg_latency ) AS avg_latency,
	sys.format_time( t.lock_latency ) AS lock_latency,
	t.rows_sent,
	t.rows_sent_avg,
	t.rows_examined,
	-- The total number of internal in-memory temporary tables created by occurrences of
	-- this query.
	t.tmp_tables,
	-- The total number of internal on-disk temporary tables created by occurrences of
	-- this query.
	t.tmp_disk_tables,
	t.rows_sorted
FROM
	sys.x$statement_analysis t
WHERE
	t.db = @db
ORDER BY
	t.total_latency DESC
;