<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[5 SQL Server Performance Mistakes Developers Make]]></title><description><![CDATA[In this article, we will discuss five common SQL Server performance mistakes developers make and how to avoid them. By the end of this blog, you will understand how to write more efficient queries and improve database performance]]></description><link>https://sqlservermistakes.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/69b086d6abc0d95001a5adc2/637d1e5a-2db5-4b2d-8aba-bd639f794753.png</url><title>5 SQL Server Performance Mistakes Developers Make</title><link>https://sqlservermistakes.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 14:09:56 GMT</lastBuildDate><atom:link href="https://sqlservermistakes.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Why Our .NET API Was Slow and How We Fixed It Using SQL Optimization and Caching]]></title><description><![CDATA[In many enterprise applications, API performance issues are often blamed on application code. However, in real production systems, the root cause is…]]></description><link>https://sqlservermistakes.hashnode.dev/why-our-net-api-was-slow-and-how-we-fixed-it-using-sql-optimization-and-caching</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/why-our-net-api-was-slow-and-how-we-fixed-it-using-sql-optimization-and-caching</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Sun, 31 May 2026 21:02:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/4a90787f-e5e3-4e3e-b82c-d8cac913694e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In many enterprise applications, API performance issues are often blamed on application code. However, in real production systems, the root cause is frequently a combination of database bottlenecks, inefficient queries, missing indexes, and repeated calls to the same data.</p>
<p>In this article, I will walk through a real-world style scenario where a .NET API response time increased significantly and how the issue was investigated and improved using SQL Server query optimization and caching.</p>
<h2>The Problem</h2>
<p>A .NET API endpoint that normally responded within 1–2 seconds started taking around 10–15 seconds during peak usage.</p>
<p>The endpoint was used to retrieve customer order summary information. Users reported that the page was loading slowly, especially during business hours.</p>
<p>The initial symptoms were:</p>
<ul>
<li><p>API response time increased from 2 seconds to 15 seconds</p>
</li>
<li><p>SQL Server CPU usage was high</p>
</li>
<li><p>Multiple users experienced delays at the same time</p>
</li>
<li><p>No application errors were logged</p>
</li>
<li><p>The issue was worse during peak traffic</p>
</li>
</ul>
<p>At first, it looked like an application performance issue, but deeper investigation showed that the main bottleneck was at the database layer.</p>
<p>How i Investigated?<br />Step 1: Checking Application Logs</p>
<p>The first step was to check the .NET application logs.</p>
<p>I reviewed:</p>
<ul>
<li><p>API response time</p>
</li>
<li><p>Error logs</p>
</li>
<li><p>Exception logs</p>
</li>
<li><p>Database timeout errors</p>
</li>
<li><p>External service calls</p>
</li>
</ul>
<p>There were no major exceptions, but the logs showed that the slow response was happening consistently on one specific API endpoint.</p>
<p>This helped narrow the investigation from the entire application to one endpoint.</p>
<h2>Step 2: Checking SQL Server Activity</h2>
<p>Next, I checked SQL Server to identify whether any queries were consuming high CPU or running for a long time.</p>
<p>Useful checks included:</p>
<ul>
<li><p>Active running queries</p>
</li>
<li><p>Blocking sessions</p>
</li>
<li><p>Wait statistics</p>
</li>
<li><p>CPU-intensive queries</p>
</li>
<li><p>Missing index recommendations</p>
</li>
</ul>
<p>The investigation showed that one query used by the API was scanning a large table instead of using an efficient index.</p>
<h2>Step 3: Reviewing the Query</h2>
<p>The query was joining multiple tables to return order summary data. During low traffic, the query performed acceptably. However, during peak hours, repeated executions caused high CPU usage and slower response times.</p>
<p>Common issues found were:</p>
<ul>
<li><p>Large table scan</p>
</li>
<li><p>Missing index on filtering columns</p>
</li>
<li><p>Repeated database calls for the same reference data</p>
</li>
<li><p>No caching for frequently requested data</p>
</li>
</ul>
<p>How i fixed the issue</p>
<h2>The Fix</h2>
<h3>1. Query Optimization</h3>
<p>The first improvement was to optimize the SQL query.</p>
<p>Actions taken:</p>
<ul>
<li><p>Removed unnecessary columns</p>
</li>
<li><p>Replaced <code>SELECT *</code> with required columns only</p>
</li>
<li><p>Reviewed joins</p>
</li>
<li><p>Added proper filtering</p>
</li>
<li><p>Checked the execution plan</p>
</li>
<li><p>Added an index on frequently used filter columns</p>
</li>
</ul>
<p>Example improvement:</p>
<p>Before optimization:</p>
<pre><code class="language-plaintext">SELECT *
FROM Orders o
INNER JOIN Customers c ON o.CustomerId = c.CustomerId
WHERE c.Region = 'London'
</code></pre>
<p>After optimization:</p>
<pre><code class="language-plaintext">SELECT 
    o.OrderId,
    o.OrderDate,
    o.TotalAmount,
    c.CustomerName
FROM Orders o
INNER JOIN Customers c ON o.CustomerId = c.CustomerId
WHERE c.Region = @Region
</code></pre>
<p>The optimized query reduced unnecessary data retrieval and improved execution efficiency</p>
<ol>
<li>Adding a Suitable Index</li>
</ol>
<p>A missing index was identified on the column frequently used in filtering.</p>
<p>Example:</p>
<p>CREATE NONCLUSTERED INDEX IX_Customers_Region ON Customers (Region) INCLUDE (CustomerName);</p>
<p>This helped SQL Server find relevant rows faster instead of scanning the full table.</p>
<ol>
<li>Adding Caching in .NET</li>
</ol>
<p>The API was repeatedly fetching the same reference data from the database.</p>
<p>To reduce database load, caching was introduced for data that did not change frequently.</p>
<p>Example using memory cache:</p>
<p>public async Task GetOrderSummaryAsync(string region) { string cacheKey = $"order-summary-{region}";</p>
<pre><code class="language-plaintext">if (!_cache.TryGetValue(cacheKey, out List&lt;OrderSummary&gt; orders))
{
    orders = await _repository.GetOrderSummaryAsync(region);

    var cacheOptions = new MemoryCacheEntryOptions()
        .SetAbsoluteExpiration(TimeSpan.FromMinutes(10));

    _cache.Set(cacheKey, orders, cacheOptions);
}

return orders;
</code></pre>
<p>}</p>
<p>Caching helped reduce repeated database calls and improved response time during peak usage.</p>
<h3>Step 4: Add measurable results</h3>
<p>Use realistic but honest numbers. If you do not have exact numbers, say “example result”.</p>
<pre><code class="language-plaintext">## Results

After applying query optimization, indexing, and caching, the API performance improved significantly.

Example results:

| Metric | Before | After |
|---|---:|---:|
| API response time | 10–15 seconds | 1–2 seconds |
| SQL CPU usage | High during peak time | Reduced |
| Database calls | Repeated calls | Reduced using cache |
| User experience | Slow page load | Faster response |
</code></pre>
<h2>Conclusion</h2>
<p>Performance issues are not always caused by application code. In many enterprise systems, inefficient SQL queries, missing indexes, and repeated database calls can significantly impact API response times.</p>
<p>By systematically investigating logs, analyzing SQL Server activity, optimizing queries, adding indexes, and introducing caching, it's possible to achieve substantial performance improvements.</p>
<p>The key lesson is to measure first, identify the actual bottleneck, and then optimize based on evidence rather than assumptions.</p>
]]></content:encoded></item><item><title><![CDATA[Automating Blocking Detection in SQL Server is a strong real world project]]></title><description><![CDATA[In production environments, blocking is one of the most common yet overlooked causes of application performance issues. A single long-running query can…]]></description><link>https://sqlservermistakes.hashnode.dev/automating-blocking-detection-in-sql-server-is-a-strong-real-world-project</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/automating-blocking-detection-in-sql-server-is-a-strong-real-world-project</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Sun, 05 Apr 2026 18:03:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/5164c6a3-8d85-41b3-8c63-1af58d5ab8d9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In production environments, blocking is one of the most common yet overlooked causes of application performance issues.</p>
<p>A single long-running query can block multiple sessions, leading to slow response times, user frustration, and increased support incidents. In many cases, blocking is only investigated after users report problems, making troubleshooting reactive rather than proactive.</p>
<p>To address this, I built an <strong>automated blocking detection solution in SQL Server</strong> that continuously monitors blocking sessions, logs critical details, and helps identify recurring patterns for faster resolution.</p>
<p>In real-world production systems:</p>
<ul>
<li><p>Blocking issues are often intermittent</p>
</li>
<li><p>Manual monitoring is not reliable</p>
</li>
<li><p>Short-lived blocking incidents can be missed</p>
</li>
<li><p>Root cause analysis becomes difficult without historical data</p>
</li>
</ul>
<p>This results in:</p>
<ul>
<li><p>Increased troubleshooting time</p>
</li>
<li><p>Repeated performance issues</p>
</li>
<li><p>Lack of visibility into database behavior</p>
</li>
</ul>
<h3>1. Creating a Blocking Log Table</h3>
<p>A custom table was created to store blocking session details such as session IDs, wait types, and SQL text.</p>
<pre><code class="language-plaintext">CREATE TABLE dbo.BlockingLog
(
    LogID INT IDENTITY(1,1) PRIMARY KEY,
    CaptureTime DATETIME DEFAULT GETDATE(),
    BlockingSessionID INT,
    BlockedSessionID INT,
    WaitType NVARCHAR(120),
    WaitTimeMs INT,
    DatabaseName NVARCHAR(128),
    SQLText NVARCHAR(MAX)
);
</code></pre>
<p>2.Capturing Blocking Sessions</p>
<p>A stored procedure was developed to detect blocking using DMVs and insert results into the log table.</p>
<p><code>SELECT r.blocking_session_id, r.session_id, r.wait_type, r.wait_time, DB_NAME(r.database_id), st.text FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) st WHERE r.blocking_session_id &lt;&gt; 0;</code></p>
<p>3.Automating with SQL Agent</p>
<p>A SQL Server Agent job was configured to run the blocking detection procedure every few minutes.</p>
<p>This ensures:</p>
<ul>
<li><p>Continuous monitoring</p>
</li>
<li><p>No manual intervention</p>
</li>
<li><p>Capturing even short-lived blocking incidents</p>
</li>
</ul>
<h3>4. Reporting and Analysis</h3>
<p>Using the logged data, I created queries to:</p>
<ul>
<li><p>Identify frequent blocking sessions</p>
</li>
<li><p>Detect high wait times</p>
</li>
<li><p>Analyze trends over time</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-plaintext">SELECT 
    BlockingSessionID,
    COUNT(*) AS OccurrenceCount
FROM dbo.BlockingLog
GROUP BY BlockingSessionID
ORDER BY OccurrenceCount DESC;
</code></pre>
<p>After implementing this solution:</p>
<ul>
<li><p>Blocking incidents were detected proactively</p>
</li>
<li><p>Troubleshooting time was significantly reduced</p>
</li>
<li><p>Historical data helped identify recurring issues</p>
</li>
<li><p>System performance improved during peak hours</p>
</li>
</ul>
<p>Instead of reacting to performance issues after they impact users, automation allows us to detect and analyze problems proactively.</p>
<p>This project demonstrates how a simple combination of DMVs, logging, and scheduling can significantly improve production support and database performance monitoring.</p>
]]></content:encoded></item><item><title><![CDATA[How I Fixed a Slow SQL Server Application in Production]]></title><description><![CDATA[A slow application in production is more than just a technical issue—it directly impacts users, business operations, and overall customer experience. In…]]></description><link>https://sqlservermistakes.hashnode.dev/how-i-fixed-a-slow-sql-server-application-in-production</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/how-i-fixed-a-slow-sql-server-application-in-production</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Sat, 04 Apr 2026 22:00:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/06dc4666-876e-4c0b-a543-a3e7c22635e2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A slow application in production is more than just a technical issue—it directly impacts users, business operations, and overall customer experience.</p>
<p>In real-world scenarios, even a few seconds of delay can lead to user frustration, reduced productivity, and potential business loss. For example, if an application used by multiple users takes more than 10 seconds to respond, it can disrupt workflows and increase support requests significantly.</p>
<p>During one such production support incident, I encountered a situation where application response time had increased to over <strong>12 seconds</strong>, affecting multiple users during peak hours. While the system was still running, the degraded performance created a noticeable impact on daily operations.</p>
<p>What made this issue more challenging was that there were no obvious signs of high CPU or memory usage, which are typically the first indicators of performance problems. This required a deeper investigation into database-level activities.</p>
<p>In this case study, I will walk through how I identified the root cause of the issue, resolved the performance bottleneck, and restored the application response time to under <strong>1 second</strong>.</p>
<p>1. Problem Description</p>
<p>The issue was reported during peak hours, where users experienced significant delays while accessing the application. Some requests took more than 10 seconds to complete, and in certain cases, users encountered timeout errors.</p>
<p>Despite the performance degradation, the application remained available, indicating that this was not a system failure but a performance-related issue.</p>
<p>2.Inital Checks</p>
<p>As part of the initial investigation, I checked the overall health of the system:</p>
<ul>
<li><p>CPU usage was within normal limits</p>
</li>
<li><p>Memory usage was stable</p>
</li>
<li><p>SQL Server service was running without issues</p>
</li>
</ul>
<p>Since there were no infrastructure-related problems, I focused on analyzing database activity.</p>
<p>3.Investigation</p>
<p>To identify the root cause, I used SQL Server Dynamic Management Views (DMVs) to analyze active sessions and query performance.</p>
<p>During this analysis, I noticed that multiple queries were waiting on a blocking session, which indicated that blocking was causing the slowdown.</p>
<p><code>SELECT blocking_session_id, session_id, wait_type FROM sys.dm_exec_requests WHERE blocking_session_id &lt;&gt; 0;</code></p>
<p>4. Root Cause</p>
<p>Further investigation revealed that a long-running UPDATE query was holding locks on a large table.</p>
<p>The table lacked proper indexing, which caused a full table scan. As a result, the query execution time increased, and locks were held for an extended period, blocking other queries.</p>
<p>5. Solution</p>
<p>To resolve the issue:</p>
<ul>
<li><p>Identified the blocking session</p>
</li>
<li><p>Created an appropriate index on the table</p>
</li>
<li><p>Optimized the query to reduce execution time</p>
</li>
<li><p>Terminated the blocking session to restore system performance immediately</p>
</li>
</ul>
<p>6. Result</p>
<p>After implementing the solution:</p>
<ul>
<li><p>Query execution time reduced from <strong>~12 seconds to under 1 second</strong></p>
</li>
<li><p>Blocking was eliminated</p>
</li>
<li><p>Application performance returned to normal</p>
</li>
<li><p>Users confirmed the issue was resolved</p>
</li>
</ul>
<p>7. Prevention</p>
<p>To prevent similar issues in the future:</p>
<ul>
<li><p>Implement proper indexing strategies</p>
</li>
<li><p>Monitor blocking using Extended Events</p>
</li>
<li><p>Regularly review long-running queries</p>
</li>
<li><p>Optimize queries during development</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Production Support & Incident Handling]]></title><description><![CDATA[During a production support shift, users reported that the application was responding very slowly, with some pages taking more than 10 seconds to load…]]></description><link>https://sqlservermistakes.hashnode.dev/production-support-incident-handling</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/production-support-incident-handling</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Sat, 04 Apr 2026 20:26:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/f4c7f8ce-35b0-4c01-8b1b-04349ee43d97.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>During a production support shift, users reported that the application was responding very slowly, with some pages taking more than 10 seconds to load. Since this issue was affecting multiple users during peak hours, it required immediate investigation and resolution.</p>
<p>Users were experiencing the following issues:</p>
<p>Slow response time Timeout errors No system crash Issue occurring mainly during peak usage</p>
<p>This indicated a performance issue rather than a complete system failure.</p>
<p>what is the Initial Checks usually i will do</p>
<p>As a first step, I checked the overall server health:</p>
<p>CPU usage → Normal Memory usage → Stable SQL Server status → Running</p>
<p>Since there were no infrastructure issues, I shifted my focus to database performance.</p>
<p>what are the Investigation i will do,</p>
<p>To identify the root cause, I analyzed active sessions using system DMVs.</p>
<p>I found that multiple queries were waiting on a blocking session, which indicated that blocking was causing the slowdown.</p>
<p>This Query helps to Identify Blocking Sessions</p>
<p>SELECT blocking_session_id, session_id, wait_type, wait_time, wait_resource FROM sys.dm_exec_requests WHERE blocking_session_id &lt;&gt; 0;</p>
<p>Identifying the Problem Query</p>
<p>To find the exact query causing the issue, I used:</p>
<p>SELECT text FROM sys.dm_exec_sql_text(&lt;blocking_session_id&gt;);</p>
<p>Finding: A long-running UPDATE query Missing proper index on the table</p>
<p><strong>what is the Root Cause</strong></p>
<p>The issue was caused by:</p>
<p>A long-running UPDATE query Missing index Full table scan</p>
<p>This resulted in:</p>
<p>Long execution time Locks being held for a long duration Blocking other queries</p>
<p><strong>Resolution</strong></p>
<p>To resolve the issue:</p>
<p>Created an appropriate index on the table Terminated the blocking session (if required) KILL &lt;session_id&gt;;</p>
<p><strong>Result</strong></p>
<p>Query execution time reduced from 12 seconds to less than 1 second Application performance was restored Users confirmed the issue was resolved</p>
<p><strong>Prevention</strong></p>
<p>To avoid similar issues in the future:</p>
<p>Implement proper indexing strategy Monitor blocking regularly Use Extended Events for proactive alerts</p>
]]></content:encoded></item><item><title><![CDATA[Hidden Features in SQL Server You Didn’t Know]]></title><description><![CDATA[The STRING_SPLIT function in Microsoft SQL Server is used to split a string into multiple rows based on a delimiter. It is especially useful when working…]]></description><link>https://sqlservermistakes.hashnode.dev/hidden-features-in-sql-server-you-didn-t-know</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/hidden-features-in-sql-server-you-didn-t-know</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Wed, 25 Mar 2026 20:09:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/aadcdacd-d994-45f7-a3e7-4ff47a105a3e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3>1. STRING_SPLIT Function</h3>
<p>The <code>STRING_SPLIT</code> function in Microsoft SQL Server is used to <strong>split a string into multiple rows based on a delimiter</strong>.</p>
<p>It is especially useful when working with <strong>comma-separated values (CSV)</strong> or any delimited data</p>
<p><code>SELECT value FROM STRING_SPLIT('SQL,Server,Azure,Database', ',');</code></p>
<h3>2. IIF Function (Inline IF)</h3>
<p>The <code>IIF</code> function in Microsoft SQL Server is a shorthand way to write conditional logic. It works like an <strong>inline IF-ELSE statement</strong>.</p>
<p>Instead of writing long <code>CASE</code> expressions, you can use <code>IIF</code> for simple conditions.</p>
<p><code>SELECT IIF(Salary &gt; 50000, 'High', 'Low') AS SalaryLevel FROM Employees;</code></p>
<h3>3. TRY...CATCH for Error Handling</h3>
<p>Error handling is very important when working with databases. A small mistake in a query or operation can cause failures or unexpected behavior.</p>
<p>In Microsoft SQL Server, the <code>TRY...CATCH</code> block allows you to <strong>handle runtime errors gracefully</strong>, instead of letting the query fail abruptly.</p>
<pre><code class="language-plaintext">BEGIN TRY
    SELECT 1 / 0;
END TRY
BEGIN CATCH
    SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
</code></pre>
<h3>4. FORMAT Function</h3>
<p>The <code>FORMAT</code> function in Microsoft SQL Server is used to <strong>format dates, numbers, and currency values into a readable string format</strong>.</p>
<p>It is especially useful when generating <strong>reports or user-friendly output</strong>.</p>
<pre><code class="language-plaintext">SELECT FORMAT(GETDATE(), 'dd-MM-yyyy') AS FormattedDate;
</code></pre>
<h3>5. Window Functions (Game Changer)</h3>
<p>Window functions are one of the most powerful features in Microsoft SQL Server.</p>
<p>They allow you to perform calculations <strong>across a set of rows (a “window”)</strong> without grouping the result into a single row.</p>
<p>👉 Unlike <code>GROUP BY</code>, window functions <strong>do not reduce rows</strong> — they keep all rows while adding calculated values.</p>
<pre><code class="language-plaintext">SELECT 
    EmployeeID,
    ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RankNum
FROM Employees;
</code></pre>
<h3>6. MERGE Statement</h3>
<p>The <code>MERGE</code> statement in Microsoft SQL Server is used to <strong>perform INSERT, UPDATE, and DELETE operations in a single query</strong>.</p>
<p>It is commonly used in <strong>data synchronization, ETL processes, and upsert operations</strong>.</p>
<p>👉 Think of <code>MERGE</code> as:</p>
<p>“Compare two tables and apply changes accordingly”</p>
<pre><code class="language-plaintext">MERGE TargetTable AS T
USING SourceTable AS S
ON T.ID = S.ID
WHEN MATCHED THEN
    UPDATE SET T.Name = S.Name
WHEN NOT MATCHED THEN
    INSERT (ID, Name) VALUES (S.ID, S.Name);
</code></pre>
<h3>7. OUTPUT Clause</h3>
<p>The <code>OUTPUT</code> clause in Microsoft SQL Server is used to <strong>return the affected rows from INSERT, UPDATE, DELETE, or MERGE operations</strong>.</p>
<p>👉 It allows you to capture:</p>
<ul>
<li><p>Newly inserted values</p>
</li>
<li><p>Updated values</p>
</li>
<li><p>Deleted records</p>
</li>
</ul>
<p>This is extremely useful for <strong>auditing, logging, and tracking changes</strong></p>
<pre><code class="language-plaintext">INSERT INTO Employees (Name)
OUTPUT INSERTED.*
VALUES ('John');
</code></pre>
<h3>8. Temporary Tables vs Table Variables</h3>
<p>When working with intermediate data in Microsoft SQL Server, developers often choose between <strong>Temporary Tables</strong> and <strong>Table Variables</strong>.</p>
<p>Both are used to store temporary data, but they behave differently in terms of <strong>performance, scope, and usage</strong>.</p>
<pre><code class="language-plaintext">-- Temporary table
CREATE TABLE #TempTable (ID INT);

-- Table variable
DECLARE @TempTable TABLE (ID INT);
</code></pre>
<h3>9.Dynamic Management Views (DMVs)</h3>
<p>Dynamic Management Views (DMVs) in Microsoft SQL Server are powerful system views that help you <strong>monitor the health, performance, and internal state of SQL Server in real time</strong>.</p>
<p>Monitor SQL Server performance.</p>
<p>SELECT * FROM sys.dm_exec_requests;</p>
<h3>10.JSON Support in SQL Server</h3>
<p>Modern applications often use JSON (JavaScript Object Notation) to exchange data. To support this, Microsoft SQL Server provides built-in functions to <strong>store, parse, and manipulate JSON data directly</strong>.</p>
<p>👉 This allows SQL Server to work easily with APIs, web apps, and NoSQL-style dat</p>
<pre><code class="language-plaintext">SELECT JSON_VALUE('{"name":"John"}', '$.name') AS Name;
</code></pre>
]]></content:encoded></item><item><title><![CDATA[SQL SERVER Timeout Errors - Causes and Fixes]]></title><description><![CDATA[Have you ever encountered this frustrating error in sql server? "Timeout expired.the timeout period elapsed prior to completion of the operation" This is…]]></description><link>https://sqlservermistakes.hashnode.dev/sql-server-timeout-errors-causes-and-fixes</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/sql-server-timeout-errors-causes-and-fixes</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Tue, 24 Mar 2026 20:06:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/50adc061-7b15-47f8-a1b6-d01cddb40013.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever encountered this frustrating error in sql server?</p>
<p>"Timeout expired.the timeout period elapsed prior to completion of the operation"</p>
<p>This is one of the most common issues developers and database admin face,especially when they are working with the larger datasets or poorly optimized queries.</p>
<p>In this blog ,we will break down:</p>
<ul>
<li><p>What SQL Server timeout errors are</p>
</li>
<li><p>Why they happen</p>
</li>
<li><p>How to detect the root cause</p>
</li>
<li><p>Practical solutions to fix them</p>
</li>
</ul>
<ol>
<li>What is a Sql Server Timout Error?</li>
</ol>
<p>A Time out error Occurs when a query or operation takes longer than the allowed time to execute. By default:</p>
<p>SQL Server query timeout is often 30 seconds</p>
<p>if the query doesn't finish within time its thrown the error</p>
<h2>Common SQL Server Timeout Error Messages</h2>
<p>You may see errors like:</p>
<ul>
<li><p>“Timeout expired. The timeout period elapsed prior to completion of the operation.”</p>
</li>
<li><p>“Execution timeout expired.”</p>
</li>
<li><p>“A network-related or instance-specific error occurred while establishing a connection to SQL Server.”</p>
</li>
<li><p>“Lock request time out period exceeded.”</p>
</li>
</ul>
<p>Now We will discuss about what are the common causes of Timeout Errors</p>
<h3>1) Slow or Unoptimized Queries</h3>
<p>Queries that scan large tables or use inefficient logic can take too long</p>
<p>Example problem:</p>
<p>Select * from employees;</p>
<p>above query we doesnt put the Filtering and it will loads entire table</p>
<p><strong>Examples of query issues:</strong></p>
<ul>
<li><p>missing WHERE clauses</p>
</li>
<li><p>SELECT *</p>
</li>
<li><p>functions in join or filter conditions</p>
</li>
<li><p>nested subqueries with poor performance</p>
</li>
<li><p>outdated execution plans</p>
</li>
</ul>
<p><strong>Fixes:</strong></p>
<ul>
<li><p>review the execution plan</p>
</li>
<li><p>add proper indexes</p>
</li>
<li><p>avoid returning unnecessary columns</p>
</li>
<li><p>rewrite inefficient joins and subqueries</p>
</li>
<li><p>update statistics</p>
</li>
</ul>
<h3>2.Missing Indexes</h3>
<p>Without an index sql server does a table <a href="http://scan.it">scan.</a> It will read every row in the table and checks the condition one by one.</p>
<p>For example,If the Table has 1 million rows the sql checks all the 1 millon rows</p>
<p>it will slow down the execution .and It will high CPU Usage,More memory usage as well.</p>
<h3>3)Blocking and Deadlocks</h3>
<p>Sometimes a query is not slow by itself. It is simply waiting for another transaction to release locks. This is called blocking. In more serious cases, deadlocks occur when two sessions wait on each other.</p>
<p><strong>Common reasons:</strong></p>
<ul>
<li><p>long open transactions</p>
</li>
<li><p>large update or delete operations</p>
</li>
<li><p>poor transaction handling in application code</p>
</li>
</ul>
<p><strong>Fixes:</strong></p>
<ul>
<li><p>keep transactions short</p>
</li>
<li><p>commit or roll back quickly</p>
</li>
<li><p>identify blocking sessions using system views</p>
</li>
<li><p>use appropriate isolation levels</p>
</li>
<li><p>tune queries to reduce lock duration</p>
</li>
</ul>
<h3>4)High Server Load</h3>
<p>SQL Server may time out when CPU, memory, disk I/O, or tempdb usage is too high. Even well-written queries can fail if the server is under heavy pressure.</p>
<p><strong>Possible causes:</strong></p>
<ul>
<li><p>too many concurrent users</p>
</li>
<li><p>insufficient hardware resources</p>
</li>
<li><p>large reporting queries during peak hours</p>
</li>
<li><p>tempdb contention</p>
</li>
</ul>
<p><strong>Fixes:</strong></p>
<ul>
<li><p>monitor CPU, memory, and I/O usage</p>
</li>
<li><p>move heavy jobs to off-peak times</p>
</li>
<li><p>optimize tempdb configuration</p>
</li>
<li><p>scale up or scale out resources where needed</p>
</li>
</ul>
<h3>5)Network Issues</h3>
<p>Not all timeout errors come from the database engine itself. Sometimes the problem lies between the application and SQL Server.</p>
<p><strong>Examples:</strong></p>
<ul>
<li><p>unstable network connection</p>
</li>
<li><p>DNS resolution delays</p>
</li>
<li><p>firewall issues</p>
</li>
<li><p>VPN latency</p>
</li>
<li><p>SQL Server port restrictions</p>
</li>
</ul>
<p><strong>Fixes:</strong></p>
<ul>
<li><p>test network latency and connectivity</p>
</li>
<li><p>verify SQL Server service and port availability</p>
</li>
<li><p>review firewall and security rules</p>
</li>
<li><p>use reliable connection settings</p>
</li>
</ul>
<p>6)<strong>Incorrect Timeout Settings</strong></p>
<p>In some cases, the database is healthy, but the timeout settings are too low for the workload. For example, a reporting query may need more than the default command timeout.</p>
<p><strong>Important note:</strong> increasing timeout values is not always the best solution. It may hide deeper performance problems.</p>
<p><strong>Fixes:</strong></p>
<ul>
<li><p>review application connection timeout settings</p>
</li>
<li><p>review command timeout values</p>
</li>
<li><p>increase timeouts only when justified</p>
</li>
<li><p>combine timeout adjustments with query tuning</p>
</li>
</ul>
<h3>7) Outdated Statistics</h3>
<p>SQL Server relies on statistics to generate efficient execution plans. If statistics are outdated, the optimizer may choose poor plans, causing slow queries and timeouts.</p>
<p><strong>Fixes:</strong></p>
<p>update statistics regularly enable auto update statistics consider full scan updates for critical tables when needed 8. Parameter Sniffing</p>
<p>Parameter sniffing happens when SQL Server creates an execution plan based on one parameter value and reuses it for others. This can cause inconsistent performance.</p>
<h3>8)Parameter Sniffing</h3>
<p>Parameter sniffing happens when SQL Server creates an execution plan based on one parameter value and reuses it for others. This can cause inconsistent performance.</p>
<p><strong>Fixes:</strong></p>
<ul>
<li><p>use OPTION (RECOMPILE) in specific cases</p>
</li>
<li><p>use OPTIMIZE FOR hints carefully</p>
</li>
<li><p>refactor procedures where parameter sensitivity is severe</p>
</li>
<li><p>consider Query Store for plan analysis</p>
</li>
</ul>
<h3>How to Troubleshoot SQL Server Timeout Errors</h3>
<p>A structured approach makes troubleshooting much easier.</p>
<p>Step 1: Identify the Type of Timeout</p>
<p>First, determine whether it is:</p>
<ul>
<li><p>connection timeout</p>
</li>
<li><p>command timeout</p>
</li>
<li><p>lock timeout</p>
</li>
</ul>
<p>This narrows the search quickly.</p>
<p>Step 2: Check Query Performance</p>
<p>Look at:</p>
<ul>
<li><p>execution plans</p>
</li>
<li><p>query duration</p>
</li>
<li><p>logical reads</p>
</li>
<li><p>missing index suggestions</p>
</li>
<li><p>wait statistics</p>
</li>
</ul>
<p>Step 3: Check for Blocking</p>
<p>Use tools such as:</p>
<ul>
<li><p>Activity Monitor</p>
</li>
<li><p><code>sp_who2</code></p>
</li>
<li><p>dynamic management views</p>
</li>
<li><p>Extended Events</p>
</li>
</ul>
<p>Find out whether the session is actively running or waiting on another process.</p>
<p>Step 4: Review Server Health</p>
<p>Check:</p>
<ul>
<li><p>CPU usage</p>
</li>
<li><p>memory pressure</p>
</li>
<li><p>disk latency</p>
</li>
<li><p>tempdb performance</p>
</li>
<li><p>concurrent workload</p>
</li>
</ul>
<p>Step 5: Review Application Settings</p>
<p>Sometimes the issue is in the application layer. Confirm:</p>
<ul>
<li><p>command timeout value</p>
</li>
<li><p>connection timeout value</p>
</li>
<li><p>connection pooling behavior</p>
</li>
<li><p>retry logic</p>
</li>
</ul>
<p>Useful SQL Checks for Timeout Troubleshooting</p>
<p>Find blocking sessions</p>
<p>This Query helps to find the blocking session</p>
<p><code>SELECT blocking_session_id, session_id, wait_type, wait_time, wait_resource FROM sys.dm_exec_requests WHERE blocking_session_id &lt;&gt; 0;</code></p>
<p>Check long-running requests</p>
<p><code>SELECT session_id, status, command, cpu_time, total_elapsed_time, reads, writes FROM sys.dm_exec_requests WHERE session_id &gt; 50 ORDER BY total_elapsed_time DESC;</code></p>
<h3>Update statistics</h3>
<pre><code class="language-plaintext">EXEC sp_updatestats;
</code></pre>
<p>Rebuild an index</p>
<p><code>ALTER INDEX [IndexName] ON [dbo].[YourTableName] REBUILD;</code></p>
<h2>Best Practices to Prevent SQL Server Timeout Errors</h2>
<p>To reduce the chance of timeout issues in production:</p>
<ul>
<li><p>optimize slow queries before deployment</p>
</li>
<li><p>create and maintain proper indexing</p>
</li>
<li><p>keep statistics updated</p>
</li>
<li><p>avoid long-running transactions</p>
</li>
<li><p>monitor blocking and wait types regularly</p>
</li>
<li><p>review timeout settings in application code</p>
</li>
<li><p>monitor server resource usage</p>
</li>
<li><p>schedule maintenance tasks properly</p>
</li>
</ul>
<h2>When Increasing Timeout Is Acceptable</h2>
<p>Increasing timeout values is acceptable in some situations, such as:</p>
<ul>
<li><p>large reporting queries</p>
</li>
<li><p>scheduled batch jobs</p>
</li>
<li><p>maintenance operations</p>
</li>
<li><p>known heavy processes with predictable duration</p>
</li>
</ul>
<p>However, for transactional systems, simply increasing the timeout should not be the first fix. The better approach is to find and correct the performance bottleneck.</p>
<h2>Final Thoughts</h2>
<p>SQL Server timeout errors are usually symptoms, not root causes. The real issue may be a slow query, blocking, missing indexes, poor server health, network instability, or application settings.</p>
<p>The best fix depends on identifying where the delay happens. By combining query tuning, indexing, server monitoring, and proper timeout configuration, you can significantly reduce timeout errors and improve overall SQL Server performance.</p>
]]></content:encoded></item><item><title><![CDATA[5 SQL Server Performance Mistakes Developers Make (And How to Fix Them)]]></title><description><![CDATA[Have you ever faced a situation where a SQL query that should take milliseconds suddenly takes several seconds? This often happens due to small mistakes in…]]></description><link>https://sqlservermistakes.hashnode.dev/5-sql-server-performance-mistakes-developers-make-and-how-to-fix-them</link><guid isPermaLink="true">https://sqlservermistakes.hashnode.dev/5-sql-server-performance-mistakes-developers-make-and-how-to-fix-them</guid><dc:creator><![CDATA[Gayathridevi Mukesh]]></dc:creator><pubDate>Thu, 19 Mar 2026 20:30:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69b086d6abc0d95001a5adc2/7305ae6f-9c8e-467e-82e5-d02a27a03aaf.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever faced a situation where a SQL query that should take milliseconds suddenly takes several seconds?</p>
<p>This often happens due to small mistakes in query design. Even experienced developers sometimes overlook important performance considerations when working with SQL Server.</p>
<p>In this article, we will look at five common SQL Server performance mistakes developers make and how you can avoid them to build faster and more efficient database applications.</p>
<p><strong>What You’ll Learn in This Article</strong></p>
<p>Before we dive into the common mistakes, let's understand what this article will help you achieve.</p>
<p>In this blog, you will learn:</p>
<ul>
<li><p>Why using <code>SELECT *</code> can negatively affect query performance</p>
</li>
<li><p>How missing indexes can slow down data retrieval</p>
</li>
<li><p>The impact of poorly written joins on database performance</p>
</li>
<li><p>Why large table scans occur and how to prevent them</p>
</li>
<li><p>How to write optimized SQL queries for better efficiency</p>
</li>
</ul>
<p>By the end of this article, you will have a better understanding of how to <strong>write faster and more efficient SQL queries</strong>.</p>
<p><strong>Mistake #1: Using SELECT *</strong></p>
<p>One of the most common mistakes developers make is using <code>SELECT *</code> in their queries.</p>
<p>Example:</p>
<pre><code class="language-plaintext">SELECT * FROM Employees;
</code></pre>
<p>Why this is a problem:</p>
<ul>
<li><p>Retrieves <strong>all columns</strong>, even if they are not needed</p>
</li>
<li><p>Increases <strong>memory and network usage</strong></p>
</li>
<li><p>Slows down query performance</p>
</li>
</ul>
<p>Better approach:</p>
<pre><code class="language-plaintext">SELECT EmployeeID, Name, Department
FROM Employees;
</code></pre>
<p>Selecting only the required columns improves <strong>performance and readability</strong>.<br />Mistake #2: Missing Indexes</p>
<p>Indexes play a crucial role in improving database performance. In <strong>SQL Server</strong>, indexes help the database engine locate data quickly without scanning the entire table.</p>
<p>When a table does not have the proper indexes, SQL Server must perform a <strong>full table scan</strong>, which means checking every row in the table to find the required data. This can significantly slow down query performance, especially when working with large datasets.</p>
<h3>Example Without an Index</h3>
<pre><code class="language-plaintext">SELECT Name 
FROM Employees
WHERE Department = 'IT';
</code></pre>
<p>If the <code>Department</code> column does not have an index, SQL Server will scan the entire <strong>Employees</strong> table to find matching rows.</p>
<h3>Why This Causes Performance Issues</h3>
<ul>
<li><p>S<code>lower query execution</code></p>
</li>
<li><p><code>Higher CPU usage</code></p>
</li>
<li><p><code>Increased database load</code></p>
</li>
<li><p><code>Poor application performance</code></p>
</li>
</ul>
<h3>How to Fix It</h3>
<p>You can improve performance by creating an index on the column used in the filter condition.</p>
<pre><code class="language-plaintext">CREATE INDEX idx_department
ON Employees(Department);
</code></pre>
<p>After creating the index, SQL Server can quickly locate the matching rows instead of scanning the entire table.</p>
<h3>Best Practice</h3>
<p>Always create indexes on:</p>
<ul>
<li><p>Columns frequently used in <strong>WHERE conditions</strong></p>
</li>
<li><p>Columns used in <strong>JOIN operations</strong></p>
</li>
<li><p>Columns used for <strong>sorting or filtering data</strong></p>
</li>
</ul>
<p>Proper indexing can dramatically improve query performance and make your database more efficient.</p>
<h1>Mistake #3: Poorly Written Joins</h1>
<p>Joins are one of the most powerful features in SQL Server because they allow you to retrieve data from multiple tables. However, poorly written joins can significantly impact database performance.</p>
<p>When joins are not optimized, SQL Server may process large amounts of unnecessary data, leading to slower queries and higher resource consumption.</p>
<h3>Example of a Poor Join</h3>
<pre><code class="language-plaintext">SELECT *
FROM Orders o
JOIN Customers c
ON o.CustomerID = c.CustomerID;
</code></pre>
<p>While this query works, it has some common issues:</p>
<ul>
<li><p>It uses <code>SELECT *</code>, which retrieves all columns from both tables.</p>
</li>
<li><p>If the join columns are not indexed, SQL Server may perform a <strong>large table scan</strong>.</p>
</li>
<li><p>Retrieving unnecessary data increases memory usage and query execution time.</p>
</li>
</ul>
<h3>Better Approach</h3>
<p>Instead of selecting all columns, only retrieve the required data.</p>
<pre><code class="language-plaintext">SELECT o.OrderID, c.CustomerName
FROM Orders o
JOIN Customers c
ON o.CustomerID = c.CustomerID;
</code></pre>
<h3>Best Practices for Writing Efficient Joins</h3>
<p>To improve join performance:</p>
<ul>
<li><p>Create <strong>indexes on join columns</strong></p>
</li>
<li><p>Avoid retrieving unnecessary columns</p>
</li>
<li><p>Use appropriate join types (<code>INNER JOIN</code>, <code>LEFT JOIN</code>, etc.)</p>
</li>
<li><p>Ensure tables are properly structured</p>
</li>
</ul>
<p>Efficient joins help reduce database workload and improve the overall performance of SQL queries.</p>
<h1>Mistake #4: Large Table Scans</h1>
<p>A table scan occurs when SQL Server reads every row in a table to find the required data. This usually happens when queries are written in a way that prevents SQL Server from using indexes efficiently.</p>
<p>Table scans can significantly slow down performance, especially when working with large tables containing thousands or millions of rows.</p>
<h3>Example</h3>
<pre><code class="language-plaintext">SELECT *
FROM Orders
WHERE YEAR(OrderDate) = 2024;
</code></pre>
<h3>Why This Is a Problem</h3>
<p>Using functions like <code>YEAR()</code> on indexed columns prevents SQL Server from using the index efficiently. As a result, SQL Server scans the entire table to evaluate the condition.</p>
<h3>Better Approach</h3>
<p>Instead of applying functions on the column, use a range condition.</p>
<pre><code class="language-plaintext">SELECT *
FROM Orders
WHERE OrderDate BETWEEN '2024-01-01' AND '2024-12-31';
</code></pre>
<h3>Best Practice</h3>
<p>To avoid large table scans:</p>
<ul>
<li><p>Use indexes on frequently searched columns</p>
</li>
<li><p>Avoid applying functions directly on indexed columns</p>
</li>
<li><p>Filter data using efficient conditions</p>
</li>
</ul>
<p>These practices help SQL Server retrieve data faster and improve query performance.</p>
<hr />
<h1>Mistake #5: Unoptimized Queries</h1>
<p>Another common mistake developers make is writing inefficient queries that consume unnecessary system resources.</p>
<p>Poorly structured queries can increase CPU usage, memory consumption, and query execution time.</p>
<h3>Example</h3>
<pre><code class="language-plaintext">SELECT *
FROM Employees
WHERE Department IN (
    SELECT Department
    FROM Departments
);
</code></pre>
<p>This query may cause additional processing because SQL Server has to execute the subquery separately.</p>
<h3>Better Approach</h3>
<p>Using joins can often improve performance.</p>
<pre><code class="language-plaintext">SELECT e.EmployeeID, e.Name
FROM Employees e
JOIN Departments d
ON e.Department = d.Department;
</code></pre>
<h3>Best Practice</h3>
<p>To optimize queries:</p>
<ul>
<li><p>Avoid unnecessary subqueries</p>
</li>
<li><p>Use joins when appropriate</p>
</li>
<li><p>Select only required columns</p>
</li>
<li><p>Write clear and efficient conditions</p>
</li>
</ul>
<p>Optimized queries help improve database performance and reduce server workload.</p>
<p><code>Conclusion</code></p>
<p><code>Optimizing database queries is essential for building fast and scalable applications. Small mistakes in query design can lead to significant performance issues in SQL Server.</code></p>
<p><code>By avoiding common mistakes such as using SELECT *, missing indexes, poorly written joins, large table scans, and unoptimized queries, developers can significantly improve database performance.</code></p>
<p><code>Writing efficient queries not only improves application speed but also reduces server load and enhances the overall user experience.</code></p>
]]></content:encoded></item></channel></rss>