<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Troubleshooting on Rudi Bruchez</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/</link><description>Recent content in Troubleshooting on Rudi Bruchez</description><generator>Hugo</generator><language>en</language><atom:link href="https://www.pachadata.com/en/docs/howtos/troubleshooting/index.xml" rel="self" type="application/rss+xml"/><item><title>Analyze queries</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/analyze-queries/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/analyze-queries/</guid><description>&lt;p&gt;Here is a condensed checklist of the method for analysing the performance of a query, or of a SQL Server database.&lt;/p&gt;&#10;&lt;h2 id="1-look-at-the-io--set-statistics-io-on"&gt;1. Look at the I/O — &lt;code&gt;SET STATISTICS IO ON&lt;/code&gt;&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;If there is a lot of I/O on a table, is there an index on it? (&lt;a href="https://youtu.be/BqmaDUumoxk"&gt;video, in French&lt;/a&gt;)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="2-look-at-the-time--set-statistics-time-on"&gt;2. Look at the time — &lt;code&gt;SET STATISTICS TIME ON&lt;/code&gt;&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;SET STATISTICS IO, TIME ON&lt;/code&gt; (&lt;a href="https://youtu.be/wThNVZciebQ"&gt;video, in French&lt;/a&gt;)&lt;/li&gt;&#10;&lt;li&gt;Mind the difference between CPU time and total elapsed time.&lt;/li&gt;&#10;&lt;li&gt;CPU &amp;lt; elapsed ⇒ waits. Not the query&amp;rsquo;s fault: lock blocking, disk reads, or the behaviour of the client&amp;rsquo;s data reader (&lt;a href="https://youtu.be/AbmUxVNF-Pc"&gt;video, in French&lt;/a&gt;).&lt;/li&gt;&#10;&lt;li&gt;CPU &amp;gt; elapsed ⇒ parallelism (&lt;a href="https://youtu.be/pqaZPhpXt1w"&gt;video, in French&lt;/a&gt;). Not a problem in itself.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="3-look-at-the-actual-execution-plan"&gt;3. Look at the actual execution plan&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Is there a discrepancy in the cardinality estimate? Under-estimation is the dangerous direction.&lt;/li&gt;&#10;&lt;li&gt;Prefer &lt;a href="https://www.solarwinds.com/free-tools/plan-explorer"&gt;Plan Explorer&lt;/a&gt; for reading plans.&lt;/li&gt;&#10;&lt;li&gt;If there is a discrepancy, update the statistics (&lt;a href="https://youtu.be/a-0JJNXdVsw"&gt;video&lt;/a&gt;, and &lt;a href="https://youtu.be/FEZSpL36O_w"&gt;this one&lt;/a&gt; for updating them from the maintenance plan — both in French).&lt;/li&gt;&#10;&lt;li&gt;It could be a table variable: if so, use &lt;code&gt;OPTION (RECOMPILE)&lt;/code&gt;, or SQL Server 2017 and later.&lt;/li&gt;&#10;&lt;li&gt;It could be the Cardinality Estimator itself.&#10;&lt;ul&gt;&#10;&lt;li&gt;On a cardinality estimation error in a complex query, experiment with the CE version.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;OPTION (USE HINT('FORCE_LEGACY_CARDINALITY_ESTIMATION'))&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;OPTION (USE HINT('ASSUME_JOIN_PREDICATE_DEPENDS_ON_FILTERS'))&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;OPTION (USE HINT('FORCE_DEFAULT_CARDINALITY_ESTIMATION'))&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = ON;&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="4-analyse-performance-across-the-whole-workload"&gt;4. Analyse performance across the whole workload&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Use Extended Events, filtering on duration.&lt;/li&gt;&#10;&lt;li&gt;Use Extended Events with no filter for a short while, to see whether there are repetitive or duplicated queries.&lt;/li&gt;&#10;&lt;li&gt;Look at the query statistics: &lt;a href="https://github.com/rudi-bruchez/tsql-scripts/blob/main/diagnostics/execution-stats/dm_exec_query_stats.sql"&gt;&lt;code&gt;dm_exec_query_stats&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="use-the-query-store-sql-server-2016-and-later"&gt;Use the Query Store (SQL Server 2016 and later)&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://youtu.be/Adwtl1QtYvI"&gt;DataFrogs session — Nicolas Souquet, Tout savoir sur le Query Store&lt;/a&gt; (in French)&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://youtu.be/EVchvLCk-IQ"&gt;DataFrogs session — Query Store, bientôt incontournable&lt;/a&gt; (in French)&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>Extract deadlock information</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/deadlock/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/deadlock/</guid><description>&lt;p&gt;A deadlock happens when two or more processes block each other in a cycle while trying to access the same resources in the database. When that happens, SQL Server picks one of the processes as the victim and kills it, which lets the others get to the resources they were competing for.&lt;/p&gt;&#10;&lt;p&gt;When a deadlock occurs on a SQL Server, you need to diagnose what caused it.&lt;/p&gt;&#10;&lt;p&gt;SQL Server raises an event whenever a deadlock occurs and a victim is chosen. That event is automatically stored in an event session — a trace, if you prefer. The information it stores is called the &lt;em&gt;deadlock graph&lt;/em&gt;, an XML representation of the situation. SQL Server Management Studio (SSMS) can display it as a diagram.&lt;/p&gt;</description></item><item><title>Retrieve the error logs</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/retrieve-error-log/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/retrieve-error-log/</guid><description>&lt;p&gt;To analyse problems on a SQL Server instance, retrieve the SQL Server error logs (&lt;code&gt;ERRORLOG&lt;/code&gt;). They hold a great deal of useful information.&lt;/p&gt;&#10;&lt;h2 id="retrieve-the-logs"&gt;Retrieve the logs&lt;/h2&gt;&#10;&lt;p&gt;The error logs are text files, in a specific directory under the SQL Server data folder.&lt;/p&gt;&#10;&lt;p&gt;You can find its path with this query:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt; SERVERPROPERTY(&lt;span style="color:#e6db74"&gt;&amp;#39;ErrorLogFileName&amp;#39;&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There you will find &lt;code&gt;ERRORLOG&lt;/code&gt;, the current log, and the &lt;code&gt;ERRORLOG.*&lt;/code&gt; files, where &lt;code&gt;*&lt;/code&gt; is a number. After a rotation — usually at instance restart — the previous log becomes number 1, the one before it number 2, and so on.&lt;/p&gt;</description></item><item><title>Identify excessive memory grants</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/memory-grants/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/memory-grants/</guid><description>&lt;p&gt;When an execution plan has to sort, or to build a hash table in memory, the query optimizer has to work out how much memory to reserve for those operations.&lt;/p&gt;&#10;&lt;p&gt;Take this query as an example.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;FROM&lt;/span&gt; Contact.Contact &lt;span style="color:#66d9ef"&gt;c&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;ORDER&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;BY&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;c&lt;/span&gt;.LastName, &lt;span style="color:#66d9ef"&gt;c&lt;/span&gt;.FirstName;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;SQL Server has to reserve enough memory to hold the whole table — &lt;code&gt;SELECT *&lt;/code&gt; asks for everything — and perform the sort.&lt;/p&gt;&#10;&lt;p&gt;The estimate of how much memory to reserve depends on the cardinality estimate and on the size of the data. A bad estimate therefore has two possible causes:&lt;/p&gt;</description></item><item><title>Use sp_whoisactive to see the active queries</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/sp-whoisactive/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/sp-whoisactive/</guid><description>&lt;p&gt;&lt;a href="https://github.com/amachanic/sp_whoisactive/releases"&gt;&lt;code&gt;sp_whoisactive&lt;/code&gt;&lt;/a&gt; is Adam Machanic&amp;rsquo;s stored procedure. It shows what is running on an instance right now, in a far more readable form than &lt;code&gt;sp_who2&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="useful-parameters"&gt;Useful parameters&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;@get_plans&lt;/code&gt; — &lt;code&gt;tinyint&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt; (default) or &lt;code&gt;1&lt;/code&gt;. Set to &lt;code&gt;1&lt;/code&gt; to include the query plan in the output.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;@find_block_leaders&lt;/code&gt; — &lt;code&gt;bit&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt; by default. Set to &lt;code&gt;1&lt;/code&gt; to identify the sessions at the head of a blocking chain, and the number of sessions each one blocks.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;@get_memory_info&lt;/code&gt; — exposes the memory grant information for the query.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;@get_query_stats&lt;/code&gt; — exposes the query execution statistics.&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>Trace execution plans</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/trace-execution-plans/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/trace-execution-plans/</guid><description>&lt;p&gt;To follow what an application or a batch process really does, it helps to capture the actual execution plans — the plans after execution — of the queries it runs.&lt;/p&gt;&#10;&lt;p&gt;Extended Events can do that.&lt;/p&gt;&#10;&lt;p&gt;Actual execution plans can be captured with no noticeable performance impact thanks to the lightweight profiling infrastructure, on SQL Server 2017 from CU21 onwards, and on SQL Server 2019 and later.&lt;/p&gt;&#10;&lt;h2 id="create-the-event-session"&gt;Create the event session&lt;/h2&gt;&#10;&lt;p&gt;Run the following on your server. It creates the session without starting it, so there is no impact yet.&lt;/p&gt;</description></item><item><title>Detect an active transaction</title><link>https://www.pachadata.com/en/docs/howtos/troubleshooting/active-transaction/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/howtos/troubleshooting/active-transaction/</guid><description>&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;DBCC SQLPERF (LOGSPACE)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt; name, log_reuse_wait_desc&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;FROM&lt;/span&gt; sys.databases&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;DBCC OPENTRAN&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;EXEC&lt;/span&gt; sp_who2&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;DBCC INPUTBUFFER (&lt;span style="color:#ae81ff"&gt;51&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;KILL &lt;span style="color:#ae81ff"&gt;71&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item></channel></rss>