Optimizing query performance

By Hatnest · Reviewed by Saurav Raj Joshi · Updated Nov 30, 2026

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_history has one row per completed request, with duration and data scanned. Its data_scanned_remote_storage_mb column shows whether the run was a cold start.
  • long_running_queries and frequently_run_queries aggregate by query shape, so queries that differ only in their parameters are grouped by a query hash.
  • sql_pool_insights shows 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 NULL when possible.
  • Result set caching serves repeated eligible SELECT queries 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_XML to view the plan. Hints are an advanced last resort, as is OPTION (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.

  1. Reduce the data first. Reference only the tables you need and put where right after the table.
  2. Filter by time first. Predicates on datetime columns use an efficient index, so apply them before string, dynamic and numeric predicates.
  3. Use column filters. Filter on a real column, not a calculated one created with extend.
  4. Prefer has over contains when you look for whole terms; it uses the term index. Avoid searching with * across all columns.
  5. Case-sensitive comparisons such as == and in are faster than =~ and in~.
  6. 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().
  7. Joins and summarize. Put the smaller table on the left of a join. Use lookup when the right side is small. Use hint.strategy=broadcast when the left side is small (up to about 100 MB), and hint.shufflekey when a join or summarize key has high cardinality.
  8. Use materialize() for a let value used more than once.
  9. Limit new queries with take or count while 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.
  • contains where has would 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

  • queryinsights keeps 30 days of history; long_running_queries and frequently_run_queries find 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.

Sources