このページはまだ日本語版がないため、英語で表示しています。
Schema Drift Detection
How to detect schema drift between SQL Server environments: comparing object definitions with hashes, tracking changes with DDL triggers or audit, and stopping drift at the source.
Schema drift is when two databases that should have the same structure quietly stop matching. Production has an index that test doesn't. A stored procedure was hot-fixed directly in production and never checked into source control. A column was widened on one environment during an incident. Each difference is small, but together they mean testing no longer reflects reality, deployments fail unexpectedly and nobody is quite sure which version of the code is the real one.
This guide covers where drift comes from, several ways to detect it and how to stop it happening in the first place.
Where drift comes from
Most drift has a handful of causes:
- **Emergency fixes** applied directly to production and never back-ported.
- **Manual changes** made outside the normal deployment process, often by well-meaning people.
- **Partial deployments**, where a release ran on one environment but failed or was skipped on another.
- **Vendor or tool changes**, such as an application upgrade or monitoring tool adding objects.
- **Environment-specific tweaks**, such as an extra index added to test for a performance investigation and never removed.
Knowing the causes helps with the fix: most drift is a process problem as much as a technical one.
Quick check: what changed recently?
The simplest signal is modification dates. `sys.objects` records when each object was created and last modified, which makes recent changes easy to spot:
```sql
SELECT SCHEMA_NAME(o.schema_id) AS schema_name, o.name, o.type_desc, o.create_date, o.modify_date
FROM sys.objects o
WHERE o.is_ms_shipped = 0
AND o.modify_date >= DATEADD(DAY, -30, GETDATE())
ORDER BY o.modify_date DESC;
```
Compare this list against your deployment records. Anything changed outside a release window is worth asking about. This won't tell you whether two environments match, but it is a quick way to spot unexpected changes.
Compare definitions with hashes
To check whether two environments really match, compare the objects themselves. For code objects such as procedures, views, functions and triggers, a hash of the definition makes comparison easy:
```sql
SELECT SCHEMA_NAME(o.schema_id) + '.' + o.name AS object_name, o.type_desc,
HASHBYTES('SHA2_256', m.definition) AS definition_hash
FROM sys.sql_modules m
JOIN sys.objects o ON o.object_id = m.object_id
WHERE o.is_ms_shipped = 0
ORDER BY object_name;
```
Run it in each environment and compare the results. Matching hashes mean identical definitions; different hashes mean something changed, even if it was only whitespace. For tables, compare columns, data types, nullability, defaults, indexes and constraints from the catalog views in the same way.
Use a schema comparison tool
For a full comparison, a dedicated tool does a better job than hand-written queries. SQL Server Data Tools and `sqlpackage` can compare a database with another database or with a project in source control, and list every difference, from tables and indexes to permissions. Our guide to [SQL schema comparison](/guides/sql-schema-comparison) covers how to use them. Running a comparison against source control on a schedule, say nightly, turns drift detection into a routine report rather than an occasional discovery.
Record changes as they happen
Comparisons tell you that drift exists. Recording changes tells you who made them and when. Two common approaches:
- **A database DDL trigger** that writes every CREATE, ALTER and DROP to a log table, with the login, time and statement. Simple to set up, though it adds a small overhead and must itself be protected from being dropped.
- **SQL Server Audit** with schema change action groups, such as `SCHEMA_OBJECT_CHANGE_GROUP`, writing to a file or the Windows event log. More robust and harder to tamper with.
The default trace also records object creation and deletion for a short period, which can help in an investigation, but it only keeps a limited amount of history and it is deprecated.
Decide what counts as drift
Not every difference is a problem. Environments may legitimately differ in permissions, users, file sizes, some configuration and data. Define which object types and properties should always match, such as tables, columns, indexes, procedures and views, and which are allowed to differ. Configure your comparison to ignore the rest, otherwise the report fills up with expected differences and the real ones get missed.
Stop drift at the source
Detection is useful, but prevention is better:
- **Keep the schema in source control**, and treat it as the source of truth.
- **Deploy only through the pipeline**, using scripts or generated deployments, never by hand.
- **Limit who can change production schemas**, so ad hoc changes need a deliberate step.
- **Make emergency fixes easy to back-port**, with a simple process for getting a hot-fix into source control the same day.
When drift is found, fix it in source control first, then redeploy, rather than patching the environment that drifted.
Troubleshooting Scenarios
A deployment failed because an object already existed
Someone created it manually. Compare the environment with source control before the next deployment to find other surprises.
Hashes differ but the code looks the same
Whitespace, comments or casing differ. Normalise definitions before hashing, or use a schema comparison tool that can ignore them.
Production has an index that isn't in source control
It was probably added during a performance investigation. Decide whether to keep it, then add it to source control.
Nobody knows who changed a procedure
Add a DDL trigger or SQL Server Audit for schema changes so future changes are recorded with the login and time.
Where SQL Cockpit fits
Spotting drift depends on being able to see the same objects across several environments side by side. SQL Cockpit includes object search and schema exploration across connected SQL Server databases, which helps when comparing structures and tracking down where an object differs. Source control, deployment pipelines and comparison tools remain the foundation of keeping environments aligned.
FAQ
What is schema drift?
It is when the structure of databases that should match, such as test and production, gradually becomes different through untracked changes.
How do I detect schema drift in SQL Server?
Compare object definitions between environments using hashes or a schema comparison tool, and check recent changes in sys.objects modify_date.
How can I track who changed a database schema?
Use a database DDL trigger or SQL Server Audit with schema change action groups to record each change, the login and the time.
What causes schema drift?
Emergency fixes in production, manual changes outside the deployment process, partial deployments and environment-specific tweaks.
How do I prevent schema drift?
Keep the schema in source control, deploy only through a pipeline, limit who can change production and back-port emergency fixes quickly.
Semantic Keywords
- schema drift detection
- schema drift
- table synchronisation
- environment comparison
- migration orchestration
- SQL Server monitoring
- SQL Agent monitoring
- runtime analysis
- runtime baselines
- runtime variance
- SQL Server operations
- database telemetry
- historical analysis
- operational dashboards
- cross-server monitoring