<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>DBA Database on Andy M Mallon - AM²</title><link>https://am2.co/tag/dba-database/</link><description>Recent content in DBA Database on Andy M Mallon - AM²</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Fri, 07 Mar 2025 16:17:43 -0500</lastBuildDate><atom:link href="https://am2.co/tag/dba-database/index.xml" rel="self" type="application/rss+xml"/><item><title>Consuming SQL Agent dates &amp; durations</title><link>https://am2.co/2025/03/consuming-sql-agent-dates-durations/</link><pubDate>Fri, 07 Mar 2025 16:17:43 -0500</pubDate><guid>https://am2.co/2025/03/consuming-sql-agent-dates-durations/</guid><description>&lt;p&gt;SQL Agent&amp;rsquo;s schema is older than me. It handles dates, times, and durations like it&amp;rsquo;s 1980 by using integers instead of date/time data types. My buddy Aaron Bertrand talks more about &lt;a href="https://sqlblog.org/dates" target="_blank"&gt;Dating Responsibly&lt;/a&gt; so that you can have a good datetime with your own database.&lt;/p&gt;&#10;&lt;p&gt;I was writing a query to pull recent job failures from SQL Agent&amp;rsquo;s &lt;code&gt;msdb&lt;/code&gt; job history, and knew that I didn&amp;rsquo;t want to deal with the wonky date/time formats. Specifically, I was querying &lt;code&gt;msdb.dbo.sysjobhistory&lt;/code&gt; to find the Start Time, End Time, and Duration of job runs that failed. If you aren&amp;rsquo;t familiar with that table, you can look at it over in &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/system-tables/dbo-sysjobhistory-transact-sql" target="_blank"&gt;the docs&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Not all stats should be auto-updated</title><link>https://am2.co/2024/08/not-all-stats-should-be-auto-updated/</link><pubDate>Thu, 29 Aug 2024 11:19:07 -0400</pubDate><guid>https://am2.co/2024/08/not-all-stats-should-be-auto-updated/</guid><description>&lt;p&gt;Statistics auto-updates are generally a good thing&amp;ndash;auto-update of &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-ver16" rel="noopener" target="_blank"&gt;statistics&lt;/a&gt; is the default behavior in SQL Server. Having the freshest information about the data &amp;ldquo;shape&amp;rdquo; helps to inform the best selection of an execution plan for queries.&lt;/p&gt;&#10;&lt;h2 id="except-for-when-it-doesnt"&gt;Except for when it doesn&amp;rsquo;t.&lt;/h2&gt;&#10;&lt;h3 id="because-it-depends"&gt;Because It Depends™️&lt;/h3&gt;&#10;&lt;p&gt;Let&amp;rsquo;s talk about the trade-offs.&lt;/p&gt;</description></item><item><title>AG secondary is out of sync for hours after index maintenance</title><link>https://am2.co/2023/02/ag-secondary-is-out-of-sync-for-hours-after-index-maintenance/</link><pubDate>Tue, 21 Feb 2023 13:17:04 -0500</pubDate><guid>https://am2.co/2023/02/ag-secondary-is-out-of-sync-for-hours-after-index-maintenance/</guid><description>&lt;p&gt;&lt;a href="https://dba.stackexchange.com/q/323858/52344"&gt;&lt;img src="https://am2.co/images/stack-exchange-question-mark.svg" alt="Question on Stack Exchange" width="96" height="96" class="stack-exchange-question-icon" /&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;h2 id="question"&gt;Question&lt;/h2&gt;&#10;&lt;p&gt;We have a 3TB OLTP database running Siemens Opcenter MES software at one of our manufacturing plants in China. This is setup in an Always-On Availability group with one secondary node. As this is a manufacturing plant, we are only provided 11 1/2 hours every two weeks to run index maintenance when the plant is not running production. We use Hallengren scripts to run this index maintenance.&lt;/p&gt;</description></item><item><title>Querying SERVERPROPERTY() and DATABASEPROPERTYEX() from a view (without knowing magic)</title><link>https://am2.co/2021/12/querying-serverproperty-and-databasepropertyex-from-a-view/</link><pubDate>Fri, 17 Dec 2021 11:11:50 -0500</pubDate><guid>https://am2.co/2021/12/querying-serverproperty-and-databasepropertyex-from-a-view/</guid><description>&lt;p&gt;I&amp;rsquo;m not going to beat around the bush. I hate the &lt;code&gt;SERVERPROPERTY()&lt;/code&gt; and &lt;code&gt;DATABASEPROPERTYEX()&lt;/code&gt; system functions.&lt;/p&gt;&#10;&lt;h2 id="loathing"&gt;Loathing&lt;/h2&gt;&#10;&lt;p&gt;The thing I hate the most about these two functions is that you need to know the right magic spells to make them work. Let&amp;rsquo;s look at &lt;code&gt;SERVERPROPERTY()&lt;/code&gt; first. The syntax for the function is &lt;code&gt;SERVERPROPERTY( &amp;lsquo;propertyname&amp;rsquo; )&lt;/code&gt;, which is easy enough syntax, but the list of values for &lt;code&gt;propertyname&lt;/code&gt; isn&amp;rsquo;t discoverable from SQL Server metadata, DMVs, or even IntelliSense. Instead, I need to check &lt;a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver15" rel="noopener" target="_blank"&gt;the docs&lt;/a&gt; for the list of allowable values. These property names are essentially magic words, and I need to check my &lt;a href="https://amzn.to/3H6p0Ph" rel="noopener" target="_blank"&gt;spell book&lt;/a&gt; to make sure I get it right.&lt;/p&gt;</description></item><item><title>log_reuse_wait_desc = AVAILABILITY_REPLICA, log is 100% full but AG is all fine</title><link>https://am2.co/2021/11/log_reuse_wait_desc-availability_replica-log-is-100-full-but-ag-is-all-fine/</link><pubDate>Sun, 28 Nov 2021 18:58:02 -0500</pubDate><guid>https://am2.co/2021/11/log_reuse_wait_desc-availability_replica-log-is-100-full-but-ag-is-all-fine/</guid><description>&lt;p&gt;&lt;a href="https://dba.stackexchange.com/q/302301/52344"&gt;&lt;img src="https://am2.co/images/stack-exchange-question-mark.svg" alt="Question on Stack Exchange" width="96" height="96" class="stack-exchange-question-icon" /&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;h2 id="question"&gt;Question&lt;/h2&gt;&#10;&lt;p&gt;That's a weird scenario. The transaction log is full. It waits on AG. AG is all good. No latency, last commit/hardened time is almost real-time on all the nodes. The Estimated Recovery Time is zero. I checked this from the primary node and secondaries as well (in case if Dashboard is not updating).&#10;This &lt;a href="https://learn.microsoft.com/en-us/troubleshoot/sql/availability-groups/error-9002-transaction-log-large" rel="nofollow noreferrer"&gt;page&lt;/a&gt; lists only two reasons for this error, a delivery latency and redo latency. Nothing applies to my system. How to troubleshoot this further?&lt;/p&gt;</description></item><item><title>T-SQL Tuesday #143: My favorite short scripts</title><link>https://am2.co/2021/10/t-sql-tuesday-143-my-favorite-short-scripts/</link><pubDate>Tue, 12 Oct 2021 10:00:22 -0400</pubDate><guid>https://am2.co/2021/10/t-sql-tuesday-143-my-favorite-short-scripts/</guid><description>&lt;blockquote&gt;For &lt;a href="https://johnmccormack.it/2021/10/t-sql-tuesday-143-short-code-examples/"&gt;this month's T-SQL Tuesday&lt;/a&gt;, John McCormack (&lt;a href="https://johnmccormack.it/" rel="noopener" target="_blank"&gt;blog&lt;/a&gt;) asks us: “What are your go-to short, handy scripts”? I've got so many handy dandy scripts, it was hard to choose.&lt;/blockquote&gt;&#10;&lt;h2 id="where-to-start"&gt;Where to start?&lt;/h2&gt;&#10;&lt;p&gt;I have so many go-to scripts that are short, amazing, and I use all the time. I&amp;rsquo;ve been using SQL Server since the turn of the century, and over the last two decades, I&amp;rsquo;ve found and written a lot of helpful things. And I have a terrible memory, so even if it&amp;rsquo;s just a few lines, I often keep it bookmarked or saved for when I need it. As an aside&amp;ndash;remember that having a folder of utility scripts isn&amp;rsquo;t a sign of weakness or incompetence. If anything, it&amp;rsquo;s the opposite&amp;ndash;those utility scripts are basically all battle scars for the database battles you&amp;rsquo;ve fought over your career. As we become senior, we just fine-tune our collection. With seniority, you don&amp;rsquo;t stop having that collection of scripts&amp;ndash;you just get better at curating them, at making them generic &amp;amp; reusable.&lt;/p&gt;</description></item><item><title>What time is it in Oz?</title><link>https://am2.co/2021/10/what-time-is-it-in-oz/</link><pubDate>Tue, 05 Oct 2021 15:54:03 -0400</pubDate><guid>https://am2.co/2021/10/what-time-is-it-in-oz/</guid><description>&lt;p&gt;There was a &lt;a href="https://dba.stackexchange.com/q/300618/52344" rel="noopener" target="_blank"&gt;recent post on Database Administrators&lt;/a&gt;, where there was some confusion over Daylight Saving Time(DST) in Australian timezones. Let&amp;rsquo;s be honest though&amp;ndash;timezones are confusing, especially when DST comes into play. Arizona Time doesn&amp;rsquo;t observe Daylight Saving Time, so sometimes it&amp;rsquo;s the same as Mountain Time, and sometimes it&amp;rsquo;s Pacific Time.&lt;/p&gt;</description></item><item><title>Stop using sp_hexadecimal &amp; sp_help_revlogin</title><link>https://am2.co/2021/07/dba-serverlogins/</link><pubDate>Tue, 06 Jul 2021 09:00:52 -0400</pubDate><guid>https://am2.co/2021/07/dba-serverlogins/</guid><description>&lt;p&gt;Since &lt;a class="tweet-ref" href="https://am2.co/2021/07/dba-serverlogins/#tweet-1384126645053771780" data-tweet-id="1384126645053771780"&gt;I started at Stack Overflow&lt;span class="visually-hidden"&gt; (read archived tweet)&lt;/span&gt;&lt;/a&gt;&#10;, I&amp;rsquo;ve had the chance to see some of the interesting challenges with running our databases at our scale.&lt;/p&gt;&#10;&lt;p&gt;My coworker Taryn Pratt (&lt;a href="https://tarynpivots.com/" rel="noopener" target="_blank"&gt;blog&lt;/a&gt;) recently blogged about &lt;a href="https://www.tarynpivots.com/post/2021/fighting-with-deadlocks/" rel="noopener" target="_blank"&gt;fighting with deadlocks&lt;/a&gt; related to &lt;a href="https://stackoverflow.com/teams/" rel="noopener" target="_blank"&gt;Stack Overflow for Teams&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h2 id="lots-of-logins"&gt;Lots of logins&lt;/h2&gt;&#10;&lt;p&gt;Taryn mentions in her post that each team gets their own login, user, schema, etc. This means we have a lot of logins. Additionally, we use Availability Groups for our High Availability, which means we need to keep those logins in sync across AG Replicas.&lt;/p&gt;</description></item><item><title>Blocking with OBJECT_NAME()</title><link>https://am2.co/2019/12/blocking-with-object_name/</link><pubDate>Tue, 24 Dec 2019 10:38:36 -0500</pubDate><guid>https://am2.co/2019/12/blocking-with-object_name/</guid><description>&lt;p&gt;As a DBA, you probably use the &lt;code&gt;OBJECT_NAME()&lt;/code&gt; function quite a bit. I know I do. If you aren&amp;rsquo;t familiar with it, you can read &lt;a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/object-name-transact-sql?view=sql-server-ver15" rel="noopener noreferrer" target="_blank"&gt;the documentation&lt;/a&gt; if you want, but it&amp;rsquo;s a pretty straightforward function that converts object IDs into object names.&lt;/p&gt;&#10;&lt;p&gt;I ran into a scenario where one of my scripts in my stored procedures in my &lt;a href="https://github.com/amtwo/dba-database/" rel="noopener noreferrer" target="_blank"&gt;DBA Database&lt;/a&gt; was being blocked, and it turned out that using &lt;code&gt;OBJECT_NAME()&lt;/code&gt; was the culprit.&lt;/p&gt;</description></item><item><title>Code to purge data based on creation date</title><link>https://am2.co/2019/04/code-to-purge-data-on-creation-date/</link><pubDate>Thu, 04 Apr 2019 08:30:16 -0400</pubDate><guid>https://am2.co/2019/04/code-to-purge-data-on-creation-date/</guid><description>&lt;p&gt;Yesterday, &lt;a href="https://am2.co/2019/04/03/purging-data-with-batched-deletes/"&gt;I talked about batching deletes&lt;/a&gt;, and the day before I talked about &lt;a href="https://am2.co/2019/04/02/the-thrill-of-purging-old-data/"&gt;how much I like to delete data&lt;/a&gt;, and why it&amp;rsquo;s necessary to do it, despite being a data packrat.&lt;/p&gt;&#10;&lt;p&gt;Today, let&amp;rsquo;s look at just one scenario, and how I like to handle it. In my experience, this is one of the most common purge requirements (and probably the easiest), so first, let&amp;rsquo;s look at the business requirements.&lt;/p&gt;</description></item><item><title>TSQL Tuesday #104: dbo.Check_FileSize</title><link>https://am2.co/2018/07/tsql-tuesday-104/</link><pubDate>Mon, 09 Jul 2018 23:18:12 -0400</pubDate><guid>https://am2.co/2018/07/tsql-tuesday-104/</guid><description>&lt;blockquote&gt;It’s T-SQL Tuesday,  the blog party that SQL Server expert &lt;strong&gt;Adam Machanic&lt;/strong&gt; (&lt;a href="http://dataeducation.com/blog/" target="_blank" rel="noopener noreferrer"&gt;blog&lt;/a&gt;) started. This month’s episode is hosted by &lt;strong&gt;Bert Wagner&lt;/strong&gt; (&lt;a href="https:/bertwagner.com/" target="_blank" rel="noopener noreferrer"&gt;blog&lt;/a&gt; | &lt;a href="http://youtube.com/bertwagner" target="_blank" rel="noopener noreferrer"&gt;vlog&lt;/a&gt;). The topic: &lt;strong&gt;&lt;a href="https://bertwagner.com/2018/07/03/code-youd-hate-to-live-without-t-sql-tuesday-104-invitation/" target="_blank" rel="noopener noreferrer"&gt;Code You Would Hate To Live Without &lt;/a&gt;&lt;/strong&gt;&lt;/blockquote&gt;&#10;&lt;p&gt;It&amp;rsquo;s been a little bit since the last T-SQL Tuesday I joined, when I blogged about religion &amp;amp; politics (aka, &lt;a href="https://am2.co/2018/02/13/tsql-tuesday-99-tabs-vs-spaces/"&gt;Tabs vs Spaces&lt;/a&gt;). When I saw Bert&amp;rsquo;s excellent topic this month, I decided I&amp;rsquo;d try to get back on the horse again and do join the blog party again this month.&lt;/p&gt;</description></item><item><title>Parsing file names from paths using T-SQL</title><link>https://am2.co/2018/01/parsing-file-names-paths-using-t-sql/</link><pubDate>Wed, 17 Jan 2018 07:00:40 -0500</pubDate><guid>https://am2.co/2018/01/parsing-file-names-paths-using-t-sql/</guid><description>&lt;p&gt;SQL Server is great at a lot of things. I spend a lot of time blogging about all the cool things that SQL Server does. But (you knew there was a &amp;ldquo;but&amp;rdquo;, didn&amp;rsquo;t you) SQL Server isn&amp;rsquo;t great at everything. Lets write about something that SQL Server is bad at.&lt;/p&gt;</description></item><item><title>Finding active transactions</title><link>https://am2.co/2018/01/finding-active-transactions/</link><pubDate>Tue, 02 Jan 2018 09:04:07 -0500</pubDate><guid>https://am2.co/2018/01/finding-active-transactions/</guid><description>&lt;p&gt;Have you ever forgot to commit a transaction? Maybe you&amp;rsquo;ve even &lt;a href="https://am2.co/2017/10/26/finding-leader-blocker/"&gt;left for lunch&lt;/a&gt; and caused a problem while you were gone. Uncommitted transactions can cause all sorts of problems&amp;ndash;not just blocking, but it can affect your Availability Groups &amp;amp; Mirroring, your TempDB version store if you&amp;rsquo;re using one of the snapshot &lt;a href="https://www.sql.kiwi/2014/07/isolation-levels/" target="_blank" rel="noopener noreferrer"&gt;isolation levels&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Alerting on SQL Server Blocking</title><link>https://am2.co/2017/12/alerting-sql-server-blocking/</link><pubDate>Mon, 18 Dec 2017 09:00:02 -0500</pubDate><guid>https://am2.co/2017/12/alerting-sql-server-blocking/</guid><description>&lt;p&gt;Identifying blocking is one of the most basic things that a DBA should be monitoring for. I previously wrote about how to &lt;a href="https://am2.co/2017/10/26/finding-leader-blocker/"&gt;identify blocking in SQL Server and find the leading blocker&lt;/a&gt;. Recently, my friend Jim Donahoe (&lt;a href="http://sqlflipflopsdba.com/" target="_blank" rel="noopener noreferrer"&gt;blog&lt;/a&gt;) asked me about setting up email alerts related to blocking, which prompted me to clean up some additional code I have that does just this. If you haven&amp;rsquo;t already read &lt;a href="https://am2.co/2017/10/26/finding-leader-blocker/"&gt;my previous post&lt;/a&gt;, you should probably do that now.&lt;/p&gt;</description></item><item><title>Finding the Leader Blocker</title><link>https://am2.co/2017/10/finding-leader-blocker/</link><pubDate>Thu, 26 Oct 2017 23:55:09 -0400</pubDate><guid>https://am2.co/2017/10/finding-leader-blocker/</guid><description>&lt;p&gt;During the course of normal processing in a database, blocking happens. Blocking is &amp;ldquo;by design&amp;rdquo; because &lt;a href="https://en.wikipedia.org/wiki/ACID" target="_blank" rel="noopener noreferrer"&gt;your database is on ACID&lt;/a&gt;. In an OLTP database, most of your transactions are short in duration, so we expect blocking to also be short in duration. If blocking occurs for a long time, this can cause problems.&lt;/p&gt;</description></item><item><title>xp_sqlagent_enum_jobs alternative</title><link>https://am2.co/2016/02/xp_sqlagent_enum_jobs_alt/</link><pubDate>Thu, 18 Feb 2016 10:00:21 -0500</pubDate><guid>https://am2.co/2016/02/xp_sqlagent_enum_jobs_alt/</guid><description>&lt;h2&gt;What's &lt;code&gt;xp_sqlagent_enum_jobs&lt;/code&gt;?&lt;/h2&gt;&#10;If you landed on this post, I'm guessing you know the answer to this, so I'll be quick. `xp_sqlagent_enum_jobs` is an undocumented (and thus, unsupported) extended procedure that gives information on whether a job is currently running, when it last ran, when it runs next, etc.&#10;&lt;p&gt;The supported alternative is to use &lt;code&gt;sp_help_job&lt;/code&gt; (which calls &lt;code&gt;xp_sqlagent_enum_jobs&lt;/code&gt;), but &lt;code&gt;sp_help_job&lt;/code&gt; is difficult to use in automation because trying to do &lt;code&gt;INSERT #temp EXEC sp_help_job;&lt;/code&gt; will result in an error due to nested &lt;code&gt;INSERT...EXEC&lt;/code&gt; statements (Error 8164: &amp;ldquo;An INSERT EXEC statement cannot be nested.&amp;rdquo;). As a result, people often turn to calling the unsupported xp directly.&lt;/p&gt;</description></item><item><title>Planning for an outage: Phone numbers</title><link>https://am2.co/2015/09/who-ya-gonna-call/</link><pubDate>Tue, 15 Sep 2015 13:29:00 -0400</pubDate><guid>https://am2.co/2015/09/who-ya-gonna-call/</guid><description>&lt;h2 id="who-ya-gonna-call"&gt;Who ya gonna call?&lt;/h2&gt;&#10;&lt;blockquote&gt;&lt;i&gt;If there's somethin' strange in your neighborhood&#10;Who ya gonna call?&#10;If it's something weird and it won't look good&#10;Who ya gonna call?&lt;/i&gt;&lt;/blockquote&gt;&#10;&amp;nbsp;&#10;&lt;div class="legacy-image-flow-clear"&gt;&lt;a class="legacy-image-float-right" href="https://www.youtube.com/watch?v=vntAEVjPBzQ"&gt;&lt;img class="alignnone" src="https://am2.co/wp-content/uploads/2015/09/ghostbustercar-300x213.jpg" width="320" height="227" border="0" / alt="The Ghostbusters Ecto-1 car on a city street."&gt;&lt;/a&gt;&lt;/div&gt;&#10;This summer, they've been taping the &lt;a href="http://www.imdb.com/title/tt1289401/" target="_blank"&gt;new Ghostbusters movie&lt;/a&gt; in Boston. It's pretty cool to see the Ghostbusters car rolling through the city.&#10;&lt;p&gt;Unfortunately, the Ghostbusters aren&amp;rsquo;t the right person to call when something goes boom in your data center. I&amp;rsquo;m not saying I haven&amp;rsquo;t used ghosts and gremlins as part of a root cause analysis before&amp;ndash;but even Dan Aykroyd and Bill Murray can&amp;rsquo;t help you when there&amp;rsquo;s a storage or network issue. I&amp;rsquo;m skeptical that Kristen Wiig will be much better.&lt;/p&gt;</description></item><item><title>Formatting HTML emails in SQL</title><link>https://am2.co/2014/11/formatting-html-emails-in-sql/</link><pubDate>Wed, 26 Nov 2014 15:34:00 -0500</pubDate><guid>https://am2.co/2014/11/formatting-html-emails-in-sql/</guid><description>&lt;p&gt;When I am writing monitoring/alerting stored procedures for my SQL Servers, its important to have those alerts be well-formatted. I like to minimize the amount of time I spend sorting through emails &amp;amp; alerts, and part of that is making sure that I can determine the urgency &amp;amp; priority of an email alert at a mere glance. More importantly, it has to be equally useful to me at my desk &amp;amp; on my mobile.&lt;/p&gt;</description></item></channel></rss>