r/SQLServer • • 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 indexes

What 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 UndoDDL to 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

  1. Provision an Azure OpenAI deployment if you don’t have one.
  2. Run the install script in the database you want to tune.
  3. Fill in and run setup/Configure-Endpoint.sql with your endpoint and key.
  4. Run EXEC dbo.sp_AIIndexTuner; and read the results in AIIndexTuner.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.

Please let me know what you think, if you have any questions or if there is anything you would change.

0 Upvotes

7 comments sorted by

3

u/General-Savings8118 22d ago

Answers vary between runs, so two CREATE runs can apply different sets. - yea about that…

1

u/Simple_Brilliant_491 22d ago

That is a fair point, but getting the perfect indexes is not a deterministic problem. If you had different database experts look at a database with a complex workload and make recommendations, I would not expect you would get identical answers from each of them.

Two more practical points:

  1. You can run in recommend-only mode, so you can review the recommendations and choose what to apply.
  2. You can schedule it to run every night if you want, so if there is a recommendation missed on an initial run, it will likely be picked up on a subsequnt run.

2

u/AbelianCommuter 22d ago

Am stoked to play with this. Thanks for your work and willingness to help grow our tribe

2

u/Simple_Brilliant_491 22d ago

Awesome, let me how it works for you and if you have any feedback!

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!

1

u/Simple_Brilliant_491 22d ago

Thank you! I've done testing across different models, and even with identical information and prompts passed to the models, the results are different.

I've also done a good amount of "lab" testing, but if real-world testing shows any issues, the data collection and/or prompting can be adjusted to address it.