|
| 1 | +/* You can put the configuration tables in any database, and I personally |
| 2 | +prefer using a DBA utility database for this. I usually put the First Responder |
| 3 | +Kit stored procs in the master database so that I can refer to 'em from any |
| 4 | +database, but these tables will have prompts and configs that you worked hard |
| 5 | +to build, and you're probably going to want to restore them if something goes |
| 6 | +wrong, plus you may want to replicate them to other servers. |
| 7 | +
|
| 8 | +I'm going to use DBAtools, but you can use another name if you want: */ |
| 9 | +USE DBAtools; |
| 10 | +GO |
| 11 | + |
| 12 | +/* Add a list of AI providers: */ |
| 13 | +CREATE TABLE dbo.Blitz_AI_Providers |
| 14 | +(Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, |
| 15 | + Model_Nickname NVARCHAR(200), |
| 16 | + AI_Model NVARCHAR(100), |
| 17 | + AI_URL NVARCHAR(500), |
| 18 | + AI_Database_Scoped_Credential_Name NVARCHAR(500), |
| 19 | + AI_Parameters NVARCHAR(4000), |
| 20 | + Payload_Template NVARCHAR(4000), |
| 21 | + Timeout_Seconds TINYINT, |
| 22 | + Context INT, |
| 23 | + Default_Model BIT DEFAULT 0); |
| 24 | + |
| 25 | +/* OpenAI - fast, cheap model, default: */ |
| 26 | +INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model) |
| 27 | +VALUES (N'ChatGPT Fast', N'gpt-5-nano', N'https://api.openai.com/v1/chat/completions', |
| 28 | + N'https://api.openai.com/', 60, 1); |
| 29 | + |
| 30 | +/* OpenAI - highest quality, slowest, most expensive model: */ |
| 31 | +INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model) |
| 32 | +VALUES (N'ChatGPT Slow', N'gpt-5.4', N'https://api.openai.com/v1/chat/completions', |
| 33 | + N'https://api.openai.com/', 230, 0); |
| 34 | + |
| 35 | +/* Gemini - fast, cheap model: */ |
| 36 | +INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model) |
| 37 | +VALUES (N'Gemini Fast', N'gemini-3-flash-preview', N'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent', |
| 38 | + N'https://generativelanguage.googleapis.com/', 60, 0); |
| 39 | + |
| 40 | +/* Gemini - highest quality, slowest, most expensive model: */ |
| 41 | +INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model) |
| 42 | +VALUES (N'Gemini Slow', N'gemini-3-1-pro-preview', N'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent', |
| 43 | + N'https://generativelanguage.googleapis.com/', 230, 0); |
| 44 | + |
| 45 | + |
| 46 | +/* Create a default set of prompts: */ |
| 47 | +CREATE TABLE dbo.Blitz_AI_Prompts |
| 48 | +(Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, |
| 49 | + Prompt_Nickname NVARCHAR(200), |
| 50 | + AI_System_Prompt NVARCHAR(4000), |
| 51 | + Default_Prompt BIT DEFAULT 0); |
| 52 | + |
| 53 | +INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt) |
| 54 | + VALUES ('sp_BlitzCache Default', 1, 'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment. |
| 55 | +
|
| 56 | + You have a query that isn''t performing to end user expectations. You have been tasked with making serious improvements to it, quickly. You are not allowed to change server-level settings or make frivolous suggestions like updating statistics. Instead, you need to focus on query changes or index changes. |
| 57 | + |
| 58 | + Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.'); |
| 59 | + |
| 60 | +INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt) |
| 61 | + VALUES ('sp_BlitzCache Index Tuning', 0, 'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment. |
| 62 | +
|
| 63 | + You have a query that isn''t performing to end user expectations. You have been tasked with making serious improvements to it, quickly, but you are only allowed to make index changes. You are not allowed to make changes to the query, server-level settings, database settings, etc. |
| 64 | + |
| 65 | + Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.'); |
| 66 | + |
| 67 | +INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt) |
| 68 | + VALUES ('sp_BlitzCache Deadlock Tuning', 0, 'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment. |
| 69 | +
|
| 70 | + You have a query that is experiencing deadlocks and blocking. You have been tasked with making serious improvements to it, quickly. You are not allowed to change server-level or database-level settings nor make frivolous suggestions like updating statistics. Instead, you need to focus on query changes or index changes that will reduce blocking and deadlocks. |
| 71 | + |
| 72 | + Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.'); |
| 73 | + |
| 74 | +INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt) |
| 75 | + VALUES ('sp_BlitzCache Modernize', 0, 'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment. |
| 76 | +
|
| 77 | + You have been given a legacy query that needs to be modernized. Our goals are to make the query run faster, make it easier to understand, easier to maintain, and to take advantage of new features up to and including SQL Server 2025. You have been tasked with making serious improvements to it, quickly, without touching server-level settings, database-level settings, indexes, or statistics. |
| 78 | + |
| 79 | + Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.'); |
| 80 | +GO |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +/* Now switch over to the user database where you'll be tuning: */ |
| 86 | +USE StackOverflow; |
| 87 | +GO |
| 88 | + |
| 89 | +/* Create a master key for the database to allow for encryption of your AI |
| 90 | +provider keys: */ |
| 91 | +CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'SomeStrongPassword!123'; |
| 92 | + |
| 93 | +/* If you've already got one, you can just open it instead: */ |
| 94 | +OPEN MASTER KEY DECRYPTION BY PASSWORD = 'SomeStrongPassword!123'; |
| 95 | + |
| 96 | +/* Turn on API calls - remember, this only works in SQL Server 2025 or newer :*/ |
| 97 | +EXEC sp_configure 'external rest endpoint enabled', 1; |
| 98 | +RECONFIGURE WITH OVERRIDE; |
| 99 | +GO |
| 100 | + |
| 101 | + |
| 102 | +/* Go get API keys for OpenAI or Gemini (or both), and then copy/paste them |
| 103 | +into the secrets below. */ |
| 104 | +CREATE DATABASE SCOPED CREDENTIAL [https://api.openai.com/] |
| 105 | +WITH IDENTITY = 'HTTPEndpointHeaders', |
| 106 | + SECRET = '{"Authorization":"Bearer YourChatGPTKeyGoesHere"}'; |
| 107 | +GO |
| 108 | +/* Note that in the ChatGPT example above, you have to leave the "Bearer " |
| 109 | +string, including a space after the word Bearer, then paste your key over |
| 110 | +the string "YourChatGPTKeyGoesHere". */ |
| 111 | + |
| 112 | +CREATE DATABASE SCOPED CREDENTIAL [https://generativelanguage.googleapis.com/] |
| 113 | +WITH IDENTITY = 'HTTPEndpointHeaders', |
| 114 | + SECRET = N'{"x-goog-api-key":"YourGeminiKeyGoesHere"}'; |
| 115 | +GO |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +/* Grant permissions for that credential to your DBA group, |
| 120 | +or developers, or whoever you want to grant it to. I'm going |
| 121 | +to create a new database role: */ |
| 122 | +CREATE ROLE [DBA_AI]; |
| 123 | +GO |
| 124 | +/* Add users to that role if necessary - you're in it already: */ |
| 125 | +ALTER ROLE [DBA_AI] ADD MEMBER [MyBestFriend]; |
| 126 | +GO |
| 127 | + |
| 128 | +/* Let the role use the credential, so only our friends |
| 129 | +can run up charges on our API keys: */ |
| 130 | +GRANT REFERENCES ON DATABASE SCOPED CREDENTIAL::[https://api.openai.com/] TO [DBA_AI]; |
| 131 | +GO |
| 132 | +GRANT REFERENCES ON DATABASE SCOPED CREDENTIAL::[https://generativelanguage.googleapis.com/] TO [DBA_AI]; |
| 133 | +GO |
| 134 | + |
| 135 | + |
| 136 | +/* Test ChatGPT with an API call using the ChatGPT defaults. |
| 137 | +This will take 30-60 seconds: */ |
| 138 | +sp_BlitzCache @Top = 1, @AI = 1 |
| 139 | +GO |
| 140 | + |
| 141 | +/* Or if you used Gemini, use this, pointed at your config table: */ |
| 142 | +sp_BlitzCache @AI = 1, @Top = 1, |
| 143 | + @AIConfigTable = 'DBAtools.dbo.Blitz_AI_Providers', |
| 144 | + @AIModel = 'gemini-3-flash-preview'; |
| 145 | +GO |
| 146 | + |
| 147 | +/* Scroll across to the AI Advice column, and make sure you got advice. |
| 148 | +
|
| 149 | +If you got an error, read it. If it's a timeout, that's fine, your query was |
| 150 | +too complex for the default fast model to digest in the time allowed. |
| 151 | +
|
| 152 | +If it's any other error, read it - it's likely a configuration error or a |
| 153 | +network connectivity error if your firewall is blocking the SQL Server's access |
| 154 | +to the internet, which is fair. No hate there. |
| 155 | +*/ |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +/* OPTIONAL: Getting Better Advice |
| 161 | +
|
| 162 | +Add a database-level extended property with your company's code standards. |
| 163 | +Don't worry too much about formatting - you can copy/paste it straight out of |
| 164 | +any standards doc you've got - but keep it to a couple pages or less. |
| 165 | +
|
| 166 | +Everything you add in here gets sent to AI with each request, and the more text |
| 167 | +in here, the longer it'll take (and the more it'll cost) for each request. */ |
| 168 | +EXECUTE sp_addextendedproperty |
| 169 | + @name = N'CONSTITUTION.md', |
| 170 | + @value = N'Any objects and T-SQL in this database must comply with the organizational standards and guidelines outlined in this constitution document. |
| 171 | + |
| 172 | + ## Object Naming Standards |
| 173 | + |
| 174 | + Views must always be prefixed with vw_. |
| 175 | + Tables should never be prefixed with tbl_. |
| 176 | + Table and column names should be in PascalCase with a capitalized first letter, like UserProperties or SalesByMonth. |
| 177 | + Index names should be based on the key columns in the index. If the index has include columns, add an _inc suffix to the index name. |
| 178 | + Index names should never be prefixed with table names, idx_, ix_, or any variation thereof. |
| 179 | + |
| 180 | + ## Query Standards |
| 181 | + |
| 182 | + Queries should be written in a concise, easy-to-understand, performant way. |
| 183 | + Queries should prefer CTEs over temp tables unless that presents a performance issue for the query.'; |
| 184 | +GO |
| 185 | +/* You can only have one Constitution per database. You can update it, which |
| 186 | +writes over the existing content: */ |
| 187 | +EXECUTE sp_updateextendedproperty |
| 188 | + @name = N'CONSTITUTION.md', |
| 189 | + @value = N'These are our new, much better standards...'; |
| 190 | +GO |
| 191 | + |
| 192 | +/* Or you can drop it: */ |
| 193 | +EXEC sp_dropextendedproperty |
| 194 | + @name = N'CONSTITUTION.md'; |
| 195 | +GO |
| 196 | + |
0 commit comments