Query tuning follows a loop: find the queries that hurt, understand why, change the query or the data layout, and measure again. Fabric has three query engines you may be tuning, KQL for eventhouses, T-SQL for warehouses and SQL analytics endpoints, and Spark SQL for lakehouse notebooks. The principle is the same for all of them: process less data.
Step 1: find the slow queries
For T-SQL, use query insights. The service keeps 30 days of history in views of the queryinsights schema, in warehouses and SQL analytics endpoints:
exec_requests_historyhas one row per completed request, with duration and data scanned. Itsdata_scanned_remote_storage_mbcolumn shows whether the run was a cold start.long_running_queriesandfrequently_run_queriesaggregate by query shape, so queries that differ only in their parameters are grouped by a query hash.sql_pool_insightsshows pool pressure, which helps when slowness is a resource problem and not a query problem.
SELECT TOP 10 *
FROM queryinsights.long_running_queries
ORDER BY median_total_elapsed_time_ms DESC;
For live problems use the DMVs (sys.dm_exec_requests) and, for blocking, sys.dm_tran_locks. For Spark, use the Spark UI.
Never judge a query by its first run. The first run may include loading data from OneLake and creating statistics.
T-SQL: warehouse and SQL analytics endpoint
- Statistics feed the optimizer. They are created automatically, and you can create and update single-column statistics on columns used in filters, joins and grouping after big data changes.
- Return less. Name the columns instead of
SELECT *, filter in the query and filter before joining. - Match data types in joins and filters so no implicit conversion is needed.
- Right-size types. Short
varchar(n), integers for whole numbers,NOT NULLwhen possible. - Result set caching serves repeated eligible
SELECTqueries from a stored result, which suits dashboards that repeat the same query. - Data clustering (preview) on a warehouse table, or a clustered or partitioned layout on a lakehouse table, lets filters skip files.
- Plans and hints. Use
SHOWPLAN_XMLto view the plan. Hints are an advanced last resort, as isOPTION (FORCE DISTRIBUTED PLAN)for a "non-scalable operation" warning.
The SQL analytics endpoint reads lakehouse Delta tables, so its speed depends on the physical layout of the files. Too many small files add overhead and too few large files limit parallel scans. Enable auto compaction or schedule OPTIMIZE, run VACUUM to remove unreferenced files, avoid high-cardinality partitions, and keep lakehouse data in the same region as the capacity. The endpoint is also synced in the background, so very fresh lakehouse changes may take a short while to appear. V-order is not needed just for the endpoint.
KQL: eventhouse and KQL database
Follow the order of importance given by the product guidance.
- Reduce the data first. Reference only the tables you need and put
whereright after the table. - Filter by time first. Predicates on
datetimecolumns use an efficient index, so apply them before string, dynamic and numeric predicates. - Use column filters. Filter on a real column, not a calculated one created with
extend. - Prefer
hasovercontainswhen you look for whole terms; it uses the term index. Avoid searching with*across all columns. - Case-sensitive comparisons such as
==andinare faster than=~andin~. - Pre-compute at ingestion. If many queries extract fields from a dynamic column, or convert Unix time, do it once with an update policy. Store common aggregations in a materialized view and query it with
materialized_view(). - Joins and summarize. Put the smaller table on the left of a join. Use
lookupwhen the right side is small. Usehint.strategy=broadcastwhen the left side is small (up to about 100 MB), andhint.shufflekeywhen a join orsummarizekey has high cardinality. - Use
materialize()for aletvalue used more than once. - Limit new queries with
takeorcountwhile exploring.
Events
| where Timestamp > ago(1h)
| where Message has "timeout"
| summarize count() by bin(Timestamp, 5m), Service
Spark SQL
Apply the same ideas: read fewer columns and files, broadcast small tables, keep partitions about 128 to 256 MB, and fix skew. See the Spark and lakehouse articles for details.
Common mistakes
- Judging by the first run.
containswherehaswould do, and filters on computed columns.- Tuning the query when the real problem is a table full of small files.
- Putting the large table on the left of a KQL join.
For the exam
queryinsightskeeps 30 days of history;long_running_queriesandfrequently_run_queriesfind candidates.- KQL: filter time first, use
has, avoid computed-column filters, use materialized views and update policies. - T-SQL: current statistics, fewer columns and rows, matching types, clustering, result set caching.
- SQL analytics endpoint speed depends on lakehouse file layout.
- Ignore the cold first run when comparing.