How to Build a Semantic Layer From SQL Server Query History
Recover the metrics, business rules and joins your team already relies on, from the SQL Server you already run, and carry them into Snowflake, Databricks or wherever you are going next.
- SQL Server has no single, permanent query log. Evidence is spread across Query Store, the plan cache, Extended Events sessions you start yourself, and the definitions of stored procedures and views.
- Each source forgets on its own schedule. Query Store's default cleanup removes queries not run in 30 days, and the plan cache drops plans under memory pressure, so decide your capture window before you need it.
- Stored procedures and views are the most durable evidence. Their full SQL text sits in sys.sql_modules for as long as the object exists.
- If you are migrating, capture the logic first. A warehouse-native tool on the new platform can only learn from history that has already arrived there.
- Extraction can be automated. Deciding which of several competing definitions is canonical still needs the people who own them.
Why SQL Server estates are hard to model
A long-lived SQL Server estate usually encodes its business logic in more layers than a cloud warehouse does. Ad-hoc queries from analysts, report datasets, scheduled jobs, and a library of stored procedures and views that every one of them calls. The definition of an active customer may live in a view written years ago, wrapped by a procedure, filtered again by a report.
Top-down modeling asks people to remember all of that in a workshop. They cannot, because nobody wrote it down. The bottom-up alternative is to read what the server already holds: the SQL that actually runs, how often it runs, and the objects it depends on.
Where SQL Server keeps the evidence
Query Store
Query Store automatically captures a history of queries, execution plans and runtime statistics, per database, and is the closest thing SQL Server has to a query log. It is available from SQL Server 2016. It is not on by default in SQL Server 2016, 2017 or 2019; from SQL Server 2022 it is enabled by default for new databases. A database created on an older version and upgraded may still have it off, so check each one with sys.database_query_store_options.
The query text is in sys.query_store_query_text, joined to sys.query_store_query, sys.query_store_plan and sys.query_store_runtime_stats, which gives you how often each query ran and when.
Three defaults decide what you will find:
Retention. The default cleanup policy removes queries that have not run for 30 days (STALE_QUERY_THRESHOLD_DAYS). Logic that only runs at quarter or year end can disappear between runs.
Size. The default maximum is 100 MB on SQL Server 2016 and 2017 and 1,000 MB from 2019. With the default size-based cleanup, Query Store removes the least expensive and oldest queries first as it nears the limit; if it hits the limit it switches to read-only until space is freed.
Capture mode. From SQL Server 2019 the default is AUTO, which captures queries judged relevant by execution count and resource use rather than every query. A cheap query that defines an important filter can be skipped.
The plan cache
sys.dm_exec_query_stats returns aggregate statistics for cached query plans, one row per statement, and sys.dm_exec_sql_text returns the SQL text for a given handle. Together they show what has run recently and how often. The catch is lifetime: rows are tied to the cached plan, plans are evicted under memory pressure, and when a plan leaves the cache its rows leave the view. Treat the plan cache as a snapshot of the present, not a history. Sample it repeatedly if Query Store is not available.
Extended Events
Extended Events is SQL Server's lightweight event-collection system, replacing the deprecated SQL Trace and SQL Server Profiler. A session you define can write completed statements to an event file, which gives you a complete, durable record from the moment it starts. It cannot recover the past: it only captures while the session is running. If Query Store is off and the migration is months away, start a session now.
Stored procedures, views and functions
sys.sql_modules returns the full SQL text that defines each stored procedure, view, function and trigger in a database. This is the most durable evidence you have, because it does not expire with time or memory pressure. It is also where much of the business logic in an older estate actually lives. Encrypted modules return NULL, so check for them early.
Report definitions
If your team uses Reporting Services, published report definitions are stored as items in the report server database. Exporting them alongside the query history tells you which queries sit behind the reports people actually open.
Comparing the sources
| Source | What it holds | How long it lasts | Watch out for |
|---|---|---|---|
| Query Store | Query text, plans and runtime statistics, per database | Default cleanup after 30 days without a run; size-capped | Off by default before SQL Server 2022; AUTO capture can skip cheap queries |
| Plan cache DMVs | Statements with cached plans and their execution counts | Only while the plan stays cached | Evicted under memory pressure; a snapshot, not a log |
| Extended Events | Whatever events your session collects, written to a file | As long as you keep the files | Captures nothing from before the session started |
| sys.sql_modules | Full SQL definitions of procedures, views, functions, triggers | For as long as the object exists | Shows the definition, not how often it is used; encrypted modules return NULL |
| Report definitions | The reports built on top of the SQL | For as long as the report is published | Shows what exists, not what is opened |
The practical answer is to combine them: Query Store or an Extended Events session for what runs and how often, sys.sql_modules for the logic inside the objects those queries call.
The method, step by step
- Inventory the evidence. For every database that feeds reporting, check whether Query Store is on, its retention and size settings, and its capture mode. Export sys.sql_modules for the same databases. Note any encrypted modules.
- Widen the window if you can. If you have time before a migration, enable Query Store where it is off, or start an Extended Events session, and let it run across at least one month end. A quarter is better.
- Export query text, not data. You need the SQL, the user or application, timestamps and execution counts. You do not need query results, and you should not export them.
- Rank by real usage. A calculation run hundreds of times by several people is a business definition. One run twice by one person is a one-off. Rank before you extract anything.
- Extract and connect. Pull out the measures, the filter conditions and the join paths from the ranked queries, and follow them into the views and procedures they call, so a metric defined three layers deep is captured where it is actually computed.
- Resolve the conflicts with their owners. Present the competing definitions side by side with how often each is used and who uses it. Choosing the canonical one becomes a short decision rather than a workshop.
- Write it out portably. Express the agreed definitions in a vendor-neutral format, then in the target platform's own format, so the logic arrives on the new platform ready to use.
Capture the logic before you migrate
Migration tooling moves tables, and increasingly translates procedures. What it does not tell you is which of five revenue calculations finance actually trusts, or which view quietly excludes test accounts. That knowledge is in the usage history on the old server.
Two consequences follow. First, the evidence has a deadline: once the source server is retired, its Query Store and plan cache go with it. Second, a semantic layer tool that runs natively on the new platform can only learn from history created there, which on day one is none. Capturing the definitions from SQL Server first means the new platform starts with the business logic instead of rediscovering it over the following year.
Honest limits
Usage is not correctness. A widely copied mistake still looks like a standard. Every extracted definition needs an owner to confirm it.
Logic outside SQL is invisible. Calculations done in a spreadsheet, a BI tool's own formula layer or application code leave no trace in SQL Server. Interview for those separately.
Short windows miss periodic logic. With default settings, Query Store may not hold anything that runs only at quarter end. Check your retention before trusting what is missing.
Where Neuron fits
Neuron by Momenta Analytics does this end to end. It reads exported query history from SQL Server, as well as Snowflake, Databricks, BigQuery and Redshift, using query text only. It runs read-only as a single Docker image inside your own environment and can run fully offline. It produces a metrics library, a business rules library with competing variants side by side, and a map of the joins in use, then exports them as OSI v1.0 YAML, Snowflake semantic YAML, Databricks JSON, dbt YAML and RAG-ready documentation.
A first working draft arrives in 48 hours. The full engagement typically runs four to six weeks, with 8 to 16 hours of your team's time, mostly one review session on the contested definitions.
FAQ
Does SQL Server keep a history of every query? Not by default. Query Store keeps a per-database history of queries, plans and runtime statistics, but it is off by default before SQL Server 2022, its default cleanup removes queries not run in 30 days, and from SQL Server 2019 its default capture mode skips queries it judges insignificant. For a complete record, run an Extended Events session that writes to a file.
How far back can I see queries on SQL Server? It depends on the source. Query Store keeps queries until its cleanup policy or size limit removes them, 30 days without a run by default. The plan cache only holds plans that are still cached. Extended Events only holds what was captured after the session started. Stored procedure and view definitions last as long as the objects do.
Is the plan cache enough on its own? Rarely. Rows in sys.dm_exec_query_stats disappear when their plan leaves the cache, which happens under memory pressure, so it shows recent activity rather than history. Use it as a supplement, or sample it repeatedly when Query Store is not available.
Do I need access to the data itself? No. The method reads query text and object definitions, not query results. The underlying rows never need to leave the server.
What about logic inside stored procedures? It is often the most important part. sys.sql_modules holds the full definition of every procedure, view and function, so follow the queries you rank into the objects they call. Encrypted modules return no text and need their source from wherever it is kept.
We are migrating to Snowflake or Databricks. When should we do this? Before the migration, or at least before the SQL Server is retired. The usage history exists only on the old server, and a tool on the new platform cannot learn from history it never saw. Capturing it first means the definitions arrive on the new platform in its own format.
How long does it take? With Neuron, a first working draft takes 48 hours once the query history is exported, and the full engagement typically runs four to six weeks with 8 to 16 hours of your team's time.
Your SQL Server already knows how your business measures itself. Read it before you switch it off.