MomentaAnalytics
Let's talk
On SQL Server, the business logic your reports run on lives in two places: the queries people send, and the stored procedures and views those queries call. Both are readable today, and neither survives a migration on its own. This guide shows where SQL Server keeps that evidence, how long it keeps it, and how to turn it into a semantic layer before the old server is switched off.
Context 101 · Method

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.

Key takeaways
  • 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

SourceWhat it holdsHow long it lastsWatch out for
Query StoreQuery text, plans and runtime statistics, per databaseDefault cleanup after 30 days without a run; size-cappedOff by default before SQL Server 2022; AUTO capture can skip cheap queries
Plan cache DMVsStatements with cached plans and their execution countsOnly while the plan stays cachedEvicted under memory pressure; a snapshot, not a log
Extended EventsWhatever events your session collects, written to a fileAs long as you keep the filesCaptures nothing from before the session started
sys.sql_modulesFull SQL definitions of procedures, views, functions, triggersFor as long as the object existsShows the definition, not how often it is used; encrypted modules return NULL
Report definitionsThe reports built on top of the SQLFor as long as the report is publishedShows 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

A note from us

Why we built this

Before you go, a few words from the person who started it. And a few ways to reach us

Hi. We're Momenta, a small team in Toronto

Omnya El MassadOmnya

We built Neuron after watching the same thing happen three times: a brilliant analyst leaves, and six months later nobody can explain why the board number doesn't match the ops number. The logic wasn't lost. It was sitting in query history the whole time. Nobody had read it

So we built something that does. It's read-only, it runs in your cloud, and the first draft is on your desk in two days. If that sounds useful, we'd love to show you on your own data

Thanks for reading this far

Omnya El MassadFounder, Momenta Analytics
MomentaAnalytics
connect@momentaanalytics.com104 Crockford Blvd., Scarborough, ON M1R 3C3, CanadaAbout usContent
© 2026 Momenta Analytics · Made in Toronto