Summary
This blog offers a real-world account of how AI for DBA can be a powerful partner for advanced database administration tasks, from debugging complex stored procedures to unraveling restore scripts.
Table of contents
I decided to run an experiment. We hear constantly about how AI will change the landscape for technical professionals, but the examples are often generic. So, I saved the history of my recent interactions with an AI assistant to see if it could truly function as a co-pilot for a real-world SQL Server DBA. The goal was to move beyond simple questions and throw the messy, complex, and sometimes tedious problems at it that we all face. This article is a summary of that journey.
Beyond Basic Queries: Untangling a Monstrous Restore Script
The first test was a common but dreaded task: modifying a massive database restore script. The script was for a database with hundreds of filegroups spread across numerous drives, resulting in a RESTORE DATABASE
command with over 200 MOVE
clauses. The challenge was that the old server’s drive letters were embedded in the logical file names, and the new server had a completely different drive layout.
My prompt wasn’t simple. It was a set of complex rules:
Analyze this restore script. The physical path for all files is wrong. The primary
.mdf
and the log file goes to theG:
drive. All the _d#
files need to go to theE:
drive. For every other file, the destination drive letter must match the letter embedded at the end of its logical name (e.g., a file named..._202201_K$
must be moved to theK:
drive).
This is a task that would require a significant amount of manual time with a high risk of find-and-replace errors.
The AI parsed the logic and generated a complete, corrected script of over 200 lines in seconds, which executed perfectly. This wasn’t just knowledge; it was the practical application of logic at scale, a perfect task for an AI partner.
The AI as a Performance Tuning Consultant
Next, we moved to performance. This is where a DBA’s experience really counts, so I was skeptical. We tackled it from two angles: indexes and query analysis.
First, I provided a long list of indexes flagged by a tool as “duplicates” and asked for a consolidation plan. The AI’s initial response was good, but this is where collaboration became key. I gave it a crucial piece of human expertise:
These are not exact duplicates. We have to be careful with the key list… but includes are fair game. Consolidate the includes and keep the one with the more inclusive key list.
This guidance transformed the interaction. The AI understood the nuance and generated a perfect script that created a new, superior index and dropped the two redundant ones. It demonstrated that, with expert guidance, AI can effectively handle complex optimization logic.
Second, I gave it a monster of a query—a multi-level subquery with a PIVOT
, numerous CONVERT()
functions in the WHERE
clause, and a DIFFERENCE()
function in the ORDER BY
. I included the XML execution plan, which showed an “Optimizer Timeout.” The AI’s analysis was immediate and accurate:
- It flagged the
CONVERT(float, [column])
calls as non-SARGable, explaining that they force a full scan because SQL Server can’t use an index. - It identified the late-stage filtering, pointing out that all the expensive joins and pivots were occurring before the final
WHERE
clause eliminated most of the rows. - It proposed a complete rewrite, structuring the logic into Common Table Expressions (CTEs) to filter the dataset early and then perform the expensive operations on a much smaller number of rows.
The AI acted exactly like a senior tuning consultant, explaining not only what was slow but also why, and providing a clear, actionable path to remediation.
Debugging the “Impossible”: Finding a Needle in a Collation Haystack
This was the most impressive test. I had a massive, thousand-line maintenance stored procedure designed to run against every database on a server. It uses dynamic SQL, and it was failing, but only on specific databases, with a cryptic error:
Implicit conversion of varchar value to varchar cannot be performed because the collation of the value is unresolved due to a collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AI"...
This is a nightmare scenario for any DBA. The error occurs inside dynamic SQL, making it incredibly hard to pinpoint. I pasted the entire procedure and the error into the chat.
The AI didn’t just give a generic definition of a collation error. It read through the entire procedure, identified the specific section that built dynamic SQL, and pinpointed the exact point of failure: concatenating string literals (which had the utility database’s collation) with object names read from system views (which had the target database’s collation).
It then provided the precise one-line fix: add COLLATE DATABASE_DEFAULT
to the column names being pulled from the system views. It was a surgical, expert-level solution to a problem that could have taken hours of manual debugging.
The Final Takeaway: AI as a Force Multiplier
Throughout this experiment, one thing became clear: AI is not a replacement for a DBA’s expertise. It’s a force multiplier. It excels at parsing complex patterns, generating tedious code, and analyzing systems with a speed no human can match. However, it still requires our direction, domain knowledge, and understanding of the underlying business rules to be truly effective.
The journey demonstrated that by partnering with AI, we can offload the rote aspects of our job, allowing us to focus on the strategic elements—asking the right questions, guiding the analysis, and making the final architectural decisions. The DBAs of the future aren’t being replaced; they’re being empowered.
Please contact us for any questions!