r/SQLServer • u/Simple_Brilliant_491 • 22d ago
Community Share Open Source AI-enabled Index Tuning Stored Proc
Disclosure: I’m the founder of AI SQL Tuner LLC.
Azure SQL Database has had automatic indexing for years. If you run SQL Server on-premises, on a VM in another cloud, or on Azure SQL Managed Instance, you’ve been managing indexes by hand. I built aisqltuner-index-tuner to fill that gap, and it’s free and MIT-licensed. It was inspired by Brent Ozar's post on Microsoft’s 2006 AutoIndex.sql prototype: I'm Probably Not Supposed To Show You Microsoft's Auto-Indexing Tool. - Brent Ozar Unlimited®.
It’s a stored procedure that collects index diagnostics from your database, sends them to your own Azure OpenAI deployment, and stores the recommendations in a table. It can also create the new indexes it is able to verify.
sql
EXEC dbo.sp_AIIndexTuner; -- recommend only (the default)
EXEC dbo.sp_AIIndexTuner = 'CREATE'; -- also create verified new indexesWhat it looks at
- Missing indexes and index usage
- Duplicate and overlapping indexes
- Plan cache and Query Store
- Statistics, heaps, and skewed columns
- Non-SARGable predicates
While the right indexes can solve a lot of performance issues, there may be other root causes. The stored proc can recommend rebuilds, constraint fixes, statistics refreshes, setting changes, and query rewrites. Each recommendation includes a priority, confidence level, and rationale.
Why it’s safe to try
- It only recommends by default. Nothing changes until you ask.
- The model’s script is never executed. The proc builds the DDL itself from structured fields, validates every table and column against the catalog, and quotes every name.
- Drops, columnstore, and the maximum number of new indexes are controlled by switches. You can also set minimum priority and confidence levels for what gets applied.
- It refuses drops of clustered, PK/unique-backed, FK-referenced, hinted, or forced-plan indexes.
- Every applied change stores an
UndoDDL. - Your diagnostics go only to the Azure OpenAI endpoint you configure.
Does it work?
We (Claude Opus 5.5 and I) benchmarked it on SQL Server 2025 Enterprise Developer with gpt-5.6-luna: 16 known tuning problems, 37 rubric checks, 5 trials per arm. On a 10 GB TPC-H-derived workload, CREATE mode cut logical reads by 86%, and no query returned different results. We saw gains on the other two scenarios too, and the full numbers, including where CREATE mode falls short of reviewing every recommendation by hand, are in BENCHMARK.md. We found gpt-5.6-luna with medium effort to be fast, cheap, and accurate in testing.
Known limitations
- Answers vary between runs, so two CREATE runs can apply different sets.
- There’s no automatic impact measurement or rollback. You get
UndoDDLto reverse a change by hand. - Only OpenAI models are supported for now.
- It’s not yet tested on Azure SQL Database, Fabric SQL Database on Managed Instance.
- Please test it in a non-production database first.
Getting started
- Provision an Azure OpenAI deployment if you don’t have one.
- Run the install script in the database you want to tune.
- Fill in and run
setup/Configure-Endpoint.sqlwith your endpoint and key. - Run
EXEC dbo.sp_AIIndexTuner;and read the results inAIIndexTuner.Recommendation.
Requirements: SQL Server 2025, Azure SQL Database, Azure SQL Managed Instance (SQL Server 2025 or Always-up-to-date update policy), or Fabric SQL Database. Since it’s a stored procedure, you can schedule it from SQL Agent or any other scheduler.
- Code, diagrams, and benchmarks: https://github.com/AI-SQL-Tuner/aisqltuner-index-tuner
Please let me know what you think, if you have any questions or if there is anything you would change.



3
u/General-Savings8118 22d ago
Answers vary between runs, so two CREATE runs can apply different sets. - yea about that…
2
u/AbelianCommuter 22d ago
Am stoked to play with this. Thanks for your work and willingness to help grow our tribe
2
u/SQLBek 1 22d ago
Interesting first stab at this - at quick glance, this looks like it has some interesting potential! Appreciate that you were up-front about everything that the MIT licensing.
If I were still an operational DBA, I'd throw this at a non-Prod env just to see what it generated, vs my own manual assessment. I'm a little concerned with the "answers vary between runs" aspect but I also get the "deterministic vs non-deterministic nature" and the "indexing is both an art & a science" aspects too. I'd be curious to dig deeper to understand the weighting of different factors that come into play when giving a new index recommendation.
Nice work!