Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Category: SQL

SQL

SQL — OR Conditions Can Disable Indexes

- 28.12.25 - ErcanOPAK comment on SQL — OR Conditions Can Disable Indexes

WHERE Status = 1 OR Status = 2 ❌ Effect Index seek → index scan. ✅ Fix Rewrite with IN or UNION.

Read More
SQL

SQL — Floating Point Columns Corrupt Financial Data

- 28.12.25 - ErcanOPAK comment on SQL — Floating Point Columns Corrupt Financial Data

FLOAT ≠ money. ❌ Result Rounding errors accumulate silently. ✅ Fix Use: DECIMAL(18, 2)  

Read More
SQL

SQL Server — LEFT JOIN + WHERE Turns Into INNER JOIN

- 27.12.25 - ErcanOPAK comment on SQL Server — LEFT JOIN + WHERE Turns Into INNER JOIN

This is a classic silent bug: LEFT JOIN Orders o ON o.UserId = u.Id WHERE o.Status = ‘Paid’ ❌ Result Rows without orders are removed. ✅ Fix Move condition into JOIN.

Read More
SQL

SQL Server — GETDATE() Breaks Deterministic Queries

- 27.12.25 - ErcanOPAK comment on SQL Server — GETDATE() Breaks Deterministic Queries

Using GETDATE() inside queries makes results non-cacheable. ❌ Problem Query plans cannot be reused efficiently ✅ Fix Pass time as a parameter instead.

Read More
SQL

SQL Server — Scalar UDFs Kill Query Parallelism

- 25.12.25 - ErcanOPAK comment on SQL Server — Scalar UDFs Kill Query Parallelism

Scalar functions force serial execution. ❌ Result CPU spikes Slow reports ✅ Fix Inline logic or use inline table-valued functions.

Read More
SQL

SQL Server — TOP (1) Without ORDER BY Is Undefined

- 25.12.25 - ErcanOPAK comment on SQL Server — TOP (1) Without ORDER BY Is Undefined

SELECT TOP 1 * FROM Orders ❌ Problem Result can change between executions. ✅ Always do ORDER BY CreatedAt DESC  

Read More
SQL

SQL Server — Nonclustered Index INCLUDE Is a Superpower

- 21.12.25 - ErcanOPAK comment on SQL Server — Nonclustered Index INCLUDE Is a Superpower

CREATE INDEX IX_User_Email ON Users(Id) INCLUDE (Email, CreatedAt) Why this matters Eliminates key lookups Massive speedups for read-heavy queries

Read More
SQL

SQL Server — COUNT(*) vs COUNT(column) Is Not the Same

- 21.12.25 - ErcanOPAK comment on SQL Server — COUNT(*) vs COUNT(column) Is Not the Same

SELECT COUNT(*) FROM Orders Why COUNT(*) is faster Doesn’t check for NULL Uses metadata when possible Avoid COUNT(column) unless required.

Read More
SQL

Implicit Data Type Conversion Breaks Index Usage

- 19.12.25 - ErcanOPAK comment on Implicit Data Type Conversion Breaks Index Usage

WHERE UserId = ‘123’ Column is INT. ❌ Result Index ignored Full scan ✅ Fix Match types exactly.

Read More
SQL

Why MERGE Can Corrupt Data Under Concurrency

- 19.12.25 | 19.12.25 - ErcanOPAK comment on Why MERGE Can Corrupt Data Under Concurrency

MERGE looks elegant… but has known race issues. ❌ Risks Duplicate inserts Missed updates Hard-to-reproduce bugs ✅ Safer Pattern Explicit UPDATE + INSERT with transaction.

Read More
SQL

Why NVARCHAR(MAX) Can Destroy Query Plans

- 17.12.25 - ErcanOPAK comment on Why NVARCHAR(MAX) Can Destroy Query Plans

Using NVARCHAR(MAX) everywhere is tempting. ❌ Hidden cost Prevents index usage Breaks memory grants ✅ Rule Use fixed lengths when possible: NVARCHAR(255)  

Read More
SQL

DATEDIFF in WHERE Clause Disables Indexes

- 17.12.25 - ErcanOPAK comment on DATEDIFF in WHERE Clause Disables Indexes

This looks innocent: WHERE DATEDIFF(day, CreatedAt, GETDATE()) = 0 ❌ Why it’s bad Forces full scan Index becomes useless ✅ Fix WHERE CreatedAt >= CAST(GETDATE() AS date)  

Read More
SQL

NOT IN vs NOT EXISTS — One Can Return Wrong Results

- 17.12.25 - ErcanOPAK comment on NOT IN vs NOT EXISTS — One Can Return Wrong Results

WHERE Id NOT IN (SELECT UserId FROM BannedUsers) ❌ Problem If subquery returns NULL, results are wrong. ✅ Always Safer WHERE NOT EXISTS ( SELECT 1 FROM BannedUsers b WHERE b.UserId = u.Id )  

Read More
SQL

SELECT * Breaks Index Usage

- 17.12.25 - ErcanOPAK comment on SELECT * Breaks Index Usage

This kills performance: SELECT * FROM Orders 🧠 Why Forces key lookups Breaks covering indexes ✅ Fix Select only what you need: SELECT Id, Total FROM Orders  

Read More
SQL

Missing WHERE on UPDATE — Disaster Prevention Trick

- 16.12.25 - ErcanOPAK comment on Missing WHERE on UPDATE — Disaster Prevention Trick

Human error happens. ✅ Safety Pattern BEGIN TRAN UPDATE Users SET IsActive = 0 WHERE LastLogin < ‘2022-01-01’ — sanity check SELECT @@ROWCOUNT ROLLBACK  

Read More
SQL

Parameter Sniffing — The Query That Works… Until It Doesn’t

- 16.12.25 - ErcanOPAK comment on Parameter Sniffing — The Query That Works… Until It Doesn’t

Same query, different performance. 🧠 Root cause SQL caches execution plans based on the first parameter. ✅ Fix Options OPTION (RECOMPILE) or OPTIMIZE FOR UNKNOWN  

Read More
SQL

SQL “DELETE Is Slow” — Use Batch Deletes

- 15.12.25 - ErcanOPAK comment on SQL “DELETE Is Slow” — Use Batch Deletes

Deleting millions of rows at once is dangerous. ❌ DELETE FROM Logs WHERE CreatedAt < ‘2023-01-01’; ✅ Safe Batch Delete WHILE 1 = 1 BEGIN DELETE TOP (1000) FROM Logs WHERE CreatedAt < ‘2023-01-01’; IF @@ROWCOUNT = 0 BREAK; END Why Avoids log explosion Prevents long locks

Read More
SQL

SQL “TOP 1 WITH ORDER BY” Without Index = Full Scan

- 15.12.25 - ErcanOPAK comment on SQL “TOP 1 WITH ORDER BY” Without Index = Full Scan

This query is slow without the right index: SELECT TOP 1 * FROM Orders ORDER BY CreatedAt DESC; ✅ Correct Index CREATE INDEX IX_Orders_CreatedAt ON Orders (CreatedAt DESC); Result Instant response No table scan

Read More
SQL

SQL SELECT * — The Hidden Future Bug

- 14.12.25 - ErcanOPAK comment on SQL SELECT * — The Hidden Future Bug

Works today, breaks tomorrow. ❌ SELECT * FROM Users; ✅ SELECT Id, Name, Email FROM Users; Why Schema changes break consumers Pulls unnecessary data Slower network and memory usage

Read More
SQL

SQL “Index Fragmentation Panic” — When NOT to Rebuild

- 14.12.25 - ErcanOPAK comment on SQL “Index Fragmentation Panic” — When NOT to Rebuild

Many rebuild indexes blindly. ❌ Bad Habit ALTER INDEX ALL ON Orders REBUILD; ✅ Smart Rule Fragmentation < 5% → do nothing 5–30% → REORGANIZE 30% → REBUILD ALTER INDEX IX_Orders_Date ON Orders REORGANIZE; Why Rebuild = blocking + log growth.

Read More
SQL

SQL Index Exists but Still Not Used? Here’s Why

- 13.12.25 - ErcanOPAK comment on SQL Index Exists but Still Not Used? Here’s Why

SQL ignores indexes when: Data type mismatch Implicit conversion Example problem: WHERE UserId = ‘123’ — string ✅ Fix WHERE UserId = 123 — int Golden rule Indexes only work when types match exactly.

Read More
SQL

SQL COUNT(*) Is Slower Than You Think

- 13.12.25 - ErcanOPAK comment on SQL COUNT(*) Is Slower Than You Think

This query scans rows: SELECT COUNT(*) FROM Orders; ✅ Faster Metadata Count (Approximate) SELECT SUM(row_count) FROM sys.dm_db_partition_stats WHERE object_id = OBJECT_ID(‘Orders’) AND index_id IN (0,1); Use when Dashboards Monitoring Large tables

Read More
SQL

SQL “Dirty Reads & Weird Data” — Fix With SNAPSHOT Isolation

- 12.12.25 - ErcanOPAK comment on SQL “Dirty Reads & Weird Data” — Fix With SNAPSHOT Isolation

Readers block writers, writers block readers → chaos. ✔ The Fix Enable snapshot isolation: ALTER DATABASE MyDB SET READ_COMMITTED_SNAPSHOT ON; 💡 Why Reads no longer block updates → faster API.

Read More
SQL

SQL “Scalar Functions Destroy Performance” — The Hidden Anti-Pattern

- 12.12.25 - ErcanOPAK comment on SQL “Scalar Functions Destroy Performance” — The Hidden Anti-Pattern

Scalar UDFs run row-by-row → slowest possible execution. ✔ Replace with Inline Table-Valued Function CREATE FUNCTION GetPrice(@id INT) RETURNS TABLE AS RETURN SELECT Price FROM Products WHERE Id = @id; Then: SELECT p.*, f.Price FROM Orders o CROSS APPLY GetPrice(o.ProductId) f; 💡 Boost CPU ↓, IO ↓, query ↑.

Read More
SQL

SQL “Parameter Sniffing Hell” — The REAL Fix Nobody Uses

- 11.12.25 - ErcanOPAK comment on SQL “Parameter Sniffing Hell” — The REAL Fix Nobody Uses

When SQL caches a plan for one specific parameter, other queries become slow. Most people use OPTION RECOMPILE.BUT the REAL fix? ✔ Create an optimized local variable DECLARE @local INT = @UserId; SELECT … WHERE UserId = @local; SQL can no longer sniff the parameter → balanced plan for all users.

Read More
Development / SQL

SQL “Zombie TempDB” — The Invisible Performance Killer

- 11.12.25 - ErcanOPAK comment on SQL “Zombie TempDB” — The Invisible Performance Killer

TempDB grows silently until EVERYTHING slows. ✔ Fix: Right-size files ALTER DATABASE tempdb MODIFY FILE (NAME=’tempdev’, SIZE=4GB); And create multiple TempDB files: 4–8 files = massive improvement 💡 Why? TempDB is used for: joins sorting hashing version store This tweak alone boosts 90% of sluggish systems.

Read More
SQL

SQL “Blocking Chains” — The Magic of READPAST

- 09.12.25 - ErcanOPAK comment on SQL “Blocking Chains” — The Magic of READPAST

Ever seen queries freeze because a row is locked? Use the magical, rarely-used hint: SELECT * FROM Orders WITH (READPAST) WHERE Status = ‘Pending’; This skips locked rows instead of waiting. ✔ Perfect for: Queue tables Background workers Batch processors

Read More
SQL

SQL “High CPU for No Reason” — The Missing Statistics Update

- 09.12.25 - ErcanOPAK comment on SQL “High CPU for No Reason” — The Missing Statistics Update

Sometimes SQL goes to 80–90% CPU “out of nowhere”because stats are stale. ✔ Emergency Fix: UPDATE STATISTICS Orders WITH FULLSCAN; 💡 Why It Works The optimizer chooses WILDLY wrong plans if statistics aren’t fresh. ⚠ Pro Tip If your DB is huge → don’t FULLSCAN often.Use: EXEC sp_updatestats; Much lighter.

Read More
SQL

SQL “Blocking Sessions” — The Hidden Killer: Orphaned Transactions

- 08.12.25 - ErcanOPAK comment on SQL “Blocking Sessions” — The Hidden Killer: Orphaned Transactions

Long-running transaction = entire DB slowdown. ✔ Quick Diagnostic SELECT * FROM sys.dm_tran_active_transactions; If you see an uncommitted transaction running for hours → that’s your culprit. ✔ The Fix Enable XACT_ABORT to auto-kill faulty transactions: SET XACT_ABORT ON; This is rarely known but prevents ghost locks forever.

Read More
SQL

SQL “Slow Pagination” — The Secret Trick: Seek Method

- 08.12.25 - ErcanOPAK comment on SQL “Slow Pagination” — The Secret Trick: Seek Method

Most devs use: ORDER BY Id OFFSET @Skip ROWS FETCH NEXT @Take ROWS This is painfully slow after page 5000+because SQL scans all skipped rows. ✔ REAL FIX: Seek Pagination SELECT TOP (@Take) * FROM Orders WHERE Id > @LastSeenId ORDER BY Id; 💡 Benefit No full scans No offsets Linear, predictable speed

Read More
Page 5 of 9
« Previous 1 2 3 4 5 6 7 8 9 Next »

Posts navigation

Older posts
Newer posts
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (448)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com