r/SQLServer • u/oleg_mssql • Mar 30 '26
Discussion Using AI for indexing
Has anyone used AI (copilot or ChatGPT) for query tuning or index suggestions in real workloads?
0
Upvotes
r/SQLServer • u/oleg_mssql • Mar 30 '26
Has anyone used AI (copilot or ChatGPT) for query tuning or index suggestions in real workloads?
5
u/Black_Magic100 Mar 30 '26
Unless you have an MCP server stood up or are using SSMS in copilot, it's mostly worthless. The key to indexing is understanding cardinality.
Just 30 minutes ago I fed it a simple execution plan and it recommended an index on a bit column where the query I was tuning was filtering on the value that pulled back 99% of the table. Had it had the ability to understand cardinality of the column, it would've never recommended the index.
So I do think it can be quite powerful if you have a proper agent skill created in tandem with the access necessary to do the same analysis that a human would do.
You could even feed it hundreds of other queries to understand if the index would impact more than just the query you are working with, which is ultimately how you build a scalable system IMO.
24
u/elephant_ua Mar 30 '26
i feel, you shouldn't not do it. You either understand what you are doing, or this will end badly
4
u/InsoleSeller 1 Mar 30 '26
As always, context is really important for AI, if you provide enough background, query execution plans, column patterns, they can be helpful and give good answers.
If you ask "hey chat, here is a SQL query, give me an index" you will probably get bad answers
4
u/pragmatica Mar 30 '26
Yes. Copilot can access query store.
Results can vary though.
If you know what you’re doing already, this can be a time saver.
4
u/SQLDevDBA 3 Mar 30 '26 edited Mar 30 '26
Check out https://PasteThePlan.com where you can paste your Execution plan XML.
It has a new AI Suggestions tab which is really cool.
1
u/Ooogaleee Mar 30 '26
If you wanna see how good suggestions that an AI source provides, then by all means, go at it. But I'd never never let a program as such "take over" my index maintenance routines. At the very most, I'd be happy to review any suggestions it makes, but in the end I always have the final say.
2
u/cutecupcake11 Mar 30 '26
I have done it but using a dab / data api builder config file and asked copilot how to improve performance fot apis in a database project. It gave me some hints, i havent implemented those blindly but tried to understand the reasoning what could have prompted AI to suggest those and then adjusted scenarios and met in the middle and implemented which made sense.. use it to understand the reasoning for indexing, ask why was it suggested and in which scenarios it will be counterproductive..
2
u/FreedToRoam Mar 30 '26
Its all fun and games when the tables are small.
Wheels fall off the wagon once you start dealing with millions or billions of records
2
u/Simple_Brilliant_491 Mar 30 '26
There are three keys to using it successfully:
Here is a blog post I wrote about this a few months ago (which I probably need to update since there are newer models now): 4 Game-Changing Tips For Using AI With SQL Server Databases
Also here is more detail on what you need for tuning a query: SQL Query Tuner - AI-Powered Query Optimization | AI SQL Tuner
2
u/B1zmark 1 Apr 01 '26
The community has created great tools for looking at indexes already. The problem is that indexes don't exist in isolation, the workload of the server impacts it as well.
Sometimes it feels like your making some things worse to make everything else better.
1
u/SirGreybush 1 Mar 30 '26
You need to set it up, copilot needs additional gateway software inside your to be able to see your sql server.
Similar to PowerBI gateway, same principle. Copilot lives in the Azure cloud and the gateway allows it to see your server.
I’m still waiting on my Org IT Security to approve having it installed on a Prod server, maybe do Dev first as a POC.
IOW, it’s not easy, and you need to be a full Admin of everything.
1
u/Dats_Russia Mar 30 '26
My two cents and two cautions:
Assuming good design and query writing practices you don’t need the perfect index, you just need a good enough index. Indexing is very important but it’s important that indexes aren’t the silver bullet to solve all problems. Indexing can help mitigate bad design by making queries faster but it won’t fix bad queries or bad table design.
1
u/JamesRandell Mar 30 '26
I think there is enough meta data stored in sql to build the functionality that generates useful indexes for a given workload.
These tools already exist, are free, and won’t burn though compute/tokens to operate.
1
u/alecc Mar 30 '26
AI can be a decent idea generator for indexing, but only if you feed it the same inputs you’d use as a DBA: actual plan, row counts, and (ideally) the stats histogram behind the predicates. Otherwise it’ll confidently recommend nonsense (e.g., indexing a low-selectivity bit column where 99% of rows match).
A good workflow is: update stats → capture top offenders in Query Store → grab actual plan + STATISTICS IO/TIME → have AI propose 1–2 candidate indexes (incl. INCLUDE/filtered index) → validate across the workload (Query Store before/after + write overhead).
VERY helpful is a skill or MCP that will allow your agent to actually access the database and check stuff on it's own, but as much as helpful that is - it's also TREMENDOUSLY risky. So you need to be really sure that it has purely read-only access, and even then that it doesn't read data it shouldn't if you are on a personal account where the data might be used for training.
1
u/zangler Mar 31 '26
Yes...but learn to do it correctly.
1
u/throwaway18000081 Mar 31 '26
Yes, I use AI daily as a tool for assistance in indexing. You need to feed the proper information to the LLM for it to provide you with a good indexing strategy.
Ultimately, add proper instructions to your AI model to model your indexes that way you like, with proper prefixes, included columns, and index options.
For larger and complex queries, this has reduced my indexing time from hours to minutes. You always need to validate AI results and proceed with caution and validation, but it’s an amazing supplemental tool.
This should be on your internal AI model, obviously don’t feed your schema information to an online model. Best practices blah blah blah, we should all know that already.
1
u/DisMuhUserName Apr 03 '26
You can query SQL for needed indexes (not my query):
SELECT
DB_NAME(mid.database_id) AS [Database],
OBJECT_NAME(mid.object_id, mid.database_id) AS [Table],
migs.user_seeks,
migs.user_scans,
migs.avg_total_user_cost * migs.avg_user_impact AS [ImprovementMeasure],
mid.equality_columns,
mid.inequality_columns,
mid.included_columns,
'CREATE INDEX [IX_' + OBJECT_NAME(mid.object_id, mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(mid.equality_columns,''),'[',''),']',''),', ','_') + ']'
+ ' ON ' + mid.statement
+ ' (' + ISNULL(mid.equality_columns, '')
+ CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END
+ ISNULL(mid.inequality_columns, '') + ')'
+ ISNULL(' INCLUDE (' + mid.included_columns + ')', '') AS [CreateIndexStatement]
FROM sys.dm_db_missing_index_groups mig
JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle
JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle
WHERE mid.database_id = DB_ID()
ORDER BY ImprovementMeasure DESC;
1
u/enrightmcc Apr 04 '26
In my experience, copilot AI SUCKS for SQL Server related intelligence. I have found a LOT of hallucinations, conflicting information, and outright wrong results.Â