This post describes how SQL Server will PROCESS a query the first time someone is running an Ad Hoc query or Stored Procedure. #SQLServer #QueryTuning
SQL Server Query Processing for Ad Hoc and Stored Procedures
More Relevant Posts
-
🚀 SQL Server Spooling Explained (The Hidden Performance Hero ⚡) While analyzing execution plans, you might have seen an operator called “Spool” and thought: 👉 “What is this doing here?” Let’s break it down simply 👇 🔍 What is Spooling? Spooling in SQL Server means temporarily storing data (usually in tempdb) so it can be reused within the same query. Think of it like: 📦 “Store once, reuse multiple times instead of recalculating.” 🧠 Why SQL Server Uses Spooling SQL Server introduces a Spool operator when: ✔ Data needs to be reused multiple times ✔ Prevent expensive re-computation ✔ Optimize nested loops or complex joins ✔ Improve overall query performance ⚙️ Common Types of Spools 🔹 Table Spool → Stores intermediate result set 🔹 Index Spool → Creates a temporary index for faster lookups 🔹 Lazy Spool → Loads data only when needed 🔹 Eager Spool → Loads all data upfront 💼 Real-World Scenario In one of my projects, we had a query with a Nested Loop Join between Orders and OrderDetails. Execution plan showed: 👉 Repeated scans on OrderDetails (very expensive 😬) SQL Server added a Table Spool to fix this. 📉 Before Spooling ❌ OrderDetails scanned multiple times ❌ High CPU usage ❌ Slow performance 📈 After Spooling ✅ Data stored once in tempdb ✅ Reused efficiently ✅ Significant performance improvement 🚀 ⚠️ But Wait… Spooling Isn’t Always Good Spooling can sometimes indicate underlying issues: ❗ Poor indexing ❗ Bad query design ❗ Missing join optimizations 👉 Overuse of tempdb can also become a bottleneck 🧠 Key Takeaways ✔ Spooling = temporary data caching by SQL Server ✔ Helps avoid repeated work ✔ Can improve performance significantly ✔ But excessive spooling = signal to optimize query/indexes 💬 Final Thought Spooling is like a smart shortcut… But if SQL Server is using it too often, it might be hiding a deeper problem. #SQLServer #DatabasePerformance #ExecutionPlan #BackendDevelopment #DotNet #SoftwareEngineering
To view or add a comment, sign in
-
-
New blog post: In which I go over three points that are vital in preventing SQL injection when working with dynamic T-SQL. https://lnkd.in/dyfxnYu4 #sqlserver #sqldba #microsoftsqlserver #mssqlserver #mssql #mssqldba #sql
To view or add a comment, sign in
-
Hi SQL SERVER Guys, SQL Server: Indexes Are NOT Your First Optimization Tool (Here’s What Is) (Learn How To Optimize Like a PRO, Step-by-Step with ME💪 part 1 of ... 😃 😉 ) #sqlserver #sqlperformance #dba #sql #queryoptimization #microsoft https://lnkd.in/dYf5xzkx
To view or add a comment, sign in
-
SQL Server Table-Valued Parameter Join Issues in Stored Procedures by Mehdi Ghapanvari >>> https://lnkd.in/eSi6E_KW Many developers pass a data table from the application to a stored procedure. Then they use it in a join operator to filter results. Stored procedures can receive this data as an input parameter by using a table-valued parameter (TVP). To send multiple rows by using a TVP, you must define a user-defined table type.
To view or add a comment, sign in
-
The choice between static and dynamic SQL is a fundamental architectural decision that directly impacts your application's performance and security. Static SQL is hardcoded and compiled at build-time, allowing the database engine to optimize execution paths for maximum speed. In contrast, dynamic SQL offers runtime flexibility for complex, non-uniform data requirements, though it requires more careful handling to avoid performance bottlenecks and potential security risks. For most enterprise systems, static SQL remains the standard for efficiency and predictable execution. However, when building highly adaptable applications that require user-driven queries, dynamic SQL provides the necessary agility. Balancing these two approaches is essential for building a robust and scalable database layer. 📈 Read the full breakdown here: https://lnkd.in/gy-V-J5x #SQL #Databases #SoftwareArchitecture #BackendDevelopment #DataEngineering
To view or add a comment, sign in
-
SQL Server 2025 upgrade for string splitting! STRING_SPLIT had limitations (single delimiter, messy workarounds). Now 👉 REGEXP_SPLIT_TO_TABLE makes it simple: ✅ Multiple delimiters ✅ Regex support ✅ Cleaner T-SQL 💡 A big step toward modern, developer-friendly SQL. 🔗 https://lnkd.in/g6_UiGpb #SQLServer #SQLServer2025 #TSQL #DataEngineering #SQLTips
To view or add a comment, sign in
-
SQL Server Table-Valued Parameter Join Issues in Stored Procedures by Mehdi Ghapanvari >>> https://lnkd.in/eCbe6ePV Many developers pass a data table from the application to a stored procedure. Then they use it in a join operator to filter results. Stored procedures can receive this data as an input parameter by using a table-valued parameter (TVP). To send multiple rows by using a TVP, you must define a user-defined table type.
To view or add a comment, sign in
-
🚀 𝗗𝗮𝘆 𝟴 – 𝗦𝗤𝗟 𝗗𝗕𝗔 𝗦𝗲𝗿𝗶𝗲𝘀 𝗧𝗲𝗺𝗽𝗗𝗕 – 𝗠𝗼𝘀𝘁 𝗔𝘀𝗸𝗲𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗧𝗼𝗽𝗶𝗰 💥 As a DBA, TempDB issues = Production emergencies. One of the most common interview questions and real-time incidents I’ve handled multiple times is TempDB full & TempDB contention. Today I’m sharing a complete practical guide 👇 🔥 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗧𝗲𝗺𝗽𝗗𝗕? TempDB is a global temporary workspace used by SQL Server for: ✔ Sorting / Hash operations ✔ Index rebuilds ✔ Temp tables / Table variables ✔ Snapshot isolation & version store ✔ Query spills (memory pressure) ⚠️ If TempDB stops → SQL Server stops. 💥 𝗧𝗲𝗺𝗽𝗗𝗕 𝗙𝘂𝗹𝗹 – 𝗥𝗲𝗮𝗹 𝗧𝗶𝗺𝗲 𝗜𝗻𝗰𝗶𝗱𝗲𝗻𝘁 Common causes: • Long running queries using temp tables • Index rebuilds / large sorts • Version store growth (RCSI/SI) • Improper autogrowth settings • Disk full 🧠 𝗛𝗼𝘄 𝗠𝗮𝗻𝘆 𝗧𝗲𝗺𝗽𝗗𝗕 𝗙𝗶𝗹𝗲𝘀 𝗦𝗵𝗼𝘂𝗹𝗱 𝗪𝗲 𝗛𝗮𝘃𝗲? This depends on Logical CPU count 🔥 👉 Microsoft best practice: • Up to 8 cores → 1 file per core • Above 8 cores → Start with 8 files and increase gradually All files must be: ✔ Equal size ✔ Equal autogrowth ✔ Same disk Why? → To prevent allocation contention ⚡ 𝗪𝗵𝘆 𝗠𝗼𝘃𝗲 𝗧𝗲𝗺𝗽𝗗𝗕 𝘁𝗼 𝗙𝗮𝘀𝘁 𝗦𝗦𝗗? TempDB is the most I/O intensive database. Moving to SSD gives: 🚀 Faster sorts & spills 🚀 Reduced PAGEIOLATCH waits 🚀 Better overall performance 🛠 𝗦𝘁𝗲𝗽𝘀 𝗧𝗼 𝗠𝗼𝘃𝗲 𝗧𝗲𝗺𝗽𝗗𝗕 𝗧𝗼 𝗡𝗲𝘄 𝗗𝗶𝘀𝗸 Step 1️⃣ Check current files SQL SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID('tempdb'); Step 2️⃣ Modify file location SQL ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'E:\TempDB\tempdb.mdf'); ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'E:\TempDB\templog.ldf'); Step 3️⃣ Restart SQL Server 🔁 TempDB recreates automatically in a new location. ⚠️ 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗧𝗲𝗺𝗽𝗗𝗕 𝗖𝗼𝗻𝘁𝗲𝗻𝘁𝗶𝗼𝗻? When many sessions try to access the same TempDB pages simultaneously. This causes Latch waits. 🔍 𝗣𝗔𝗚𝗘𝗟𝗔𝗧𝗖𝗛 𝘃𝘀 𝗣𝗔𝗚𝗘𝗜𝗢𝗟𝗔𝗧𝗖𝗛 🧷 PAGELATCH Memory latch → waiting for page access in RAM 👉 Sign of contention 💽 PAGEIOLATCH Disk I/O latch → waiting for page from disk 👉 Sign of slow storage This difference is a favorite interview question 🎯 🚨 𝗛𝗼𝘄 𝗧𝗼 𝗣𝗿𝗲𝘃𝗲𝗻𝘁 𝗧𝗲𝗺𝗽𝗗𝗕 𝗖𝗼𝗻𝘁𝗲𝗻𝘁𝗶𝗼𝗻 ✔ Multiple TempDB data files ✔ Move TempDB to SSD ✔ Pre-size TempDB (avoid frequent autogrowth) ✔ Enable trace flags (older versions): 1117, 1118 ✔ Avoid heavy temp table usage ✔ Optimize bad queries 💬 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗧𝗶𝗽 If interviewer asks Top SQL waits → Always mention: 👉 PAGELATCH 👉 PAGEIOLATCH 👉 WRITELOG This shows real production experience 💯 📌 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 A well-configured TempDB can dramatically improve SQL Server performance 🚀 #SQLServer #SQLDBA #TempDB #PerformanceTuning #DatabaseAdministration #SQLTips #DBACommunity #TechLearning #WomenInTech #InterviewPreparation #SQLServerDBA
To view or add a comment, sign in
-
-
𝐐𝐮𝐞𝐫𝐲 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐏𝐥𝐚𝐧𝐬 When a SQL query is slow, many people look only at the query text. But SQL Server does not execute SQL the way people read it. It executes a **plan**. That is why **Query Execution Plans** are one of the most important tools in SQL Server performance tuning. A simple way to think about it: The query is your request. The execution plan is SQL Server’s strategy for answering that request. And that strategy decides: - how data is accessed - which indexes are used - how joins are performed - whether sorting is needed - how much data moves through the operators This is why two queries that look very similar can perform very differently. Because the real cost is often not in the SQL text itself. It is in the plan shape chosen by the optimizer. A plan can reveal problems like: - index scans instead of efficient seeks - expensive key lookups - poor join choices - missing indexes - inaccurate row estimates - sorts and spills - unnecessary parallelism That is the real value of execution plans. They help answer not just: **What is slow?** But: **Why is SQL Server executing it this way?** Good tuning starts when you stop reading only the query and start reading the path SQL Server took to execute it. Because a slow query is often not just bad syntax. It is an expensive plan. #SQLServer #ExecutionPlan #QueryPerformance #SQLInternals #DatabasePerformance #PerformanceTuning #DatabaseAdministration
To view or add a comment, sign in
-
-
ADR Comes to TempDB in SQL Server 2025. Read This Before Enabling. Two weeks ago I covered the Resource Governor changes in SQL Server 2025 — specifically, capping how much tempdb data space a workload group can consume. That was the data-file side. For the log side, SQL Server 2025 now lets you enable Accelerated Database Recovery (ADR) on tempdb. Enable it and cancelled queries stop grinding, the tempdb log stops bloating, and recovery gets faster. Sounds like an easy yes — but you've got to read the fine print……more https://lnkd.in/eN_Ff4cv
To view or add a comment, sign in
More from this author
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development