Posts

SQL SERVER INDEX (Part 1): Index Seek vs Index Scan and Statistics

Image
Firstly, I recommend watching this  How to Think Like the Engine - YouTube  by Brent Ozar on YouTube (all parts). It is incredibly helpful foundational material before you begin your SQL performance tuning journey. The SQL Server optimizer   is responsible for determining which operations —such as scans or seeks — should be used to query the least number of 8KB pages. However, the optimizer sometimes needs a DBA's intervention. For example, an ideal index for a query might not exist yet. In such cases, it is up to the DBA to create the index so that the optimizer can make use of it. Similarly, inaccurate row count estimations on the execution plan can lead to suboptimal choices by the optimizer. As DBAs, we may need to update statistics or tune the code depending on the cause of the misestimations. When we have accurate indexes and correct row estimations , the optimizer's choices are usually optimal, and we can stop our part of tuning the query. In this post, I will...

My favorite DBA resources and tools

In this blog, I’ll list and briefly review some of the resources and tools that I genuinely find valuable — the ones I often return to, whether for learning or hands-on use. I’m especially thankful to be part of a DBA community filled with talented people who generously share their knowledge and experience. I’ll continue adding more great content to this list as I come across resources/tools that have helped me throughout my career journey. πŸ”§ Performance Tuning Blogs and Videos Brent Ozar Unlimited – How to Think Like the Engine Watch on YouTube A must-watch series that breaks down how SQL Server processes queries internally by showing how data is stored on disk as 8KB pages. It’s a foundational class for anyone getting into query performance tuning. I always enjoy watching Brent Ozar’s videos—he’s humorous, knowledgeable, and a great communicator. His lessons are packed with interesting examples. Always ⭐⭐⭐⭐⭐ from me! Erik ...

Troubleshoot SQL Server Cardinality Estimate

Image
Today, I will show you why good cardinality estimate is essential for an efficient execution plan and how I troubleshoot inaccurate cardinality estimates. I will use the database StackOverflow2010 to demonstrate. INSERT 50,000 rows I insert 50,000 rows with OwnerUserID 26837 into the table. Assuming this is a real life workload! DECLARE   @date   AS   DATETIME   =   Getdate ( ) ; INSERT   INTO   posts              ( body ,               lastactivitydate ,               creationdate ,               score ,               viewcount ,               posttypeid , ...

Tools I Used During Query Performance Troubleshooting

Image
1. Execution Plan - An execution plan in SQL Server is a detailed roadmap created by the SQL Server Query Optimizer to determine the most efficient way to execute a query. It shows which operators being used (e.g. scans, seeks, nested join, hash match, ...), data volume, access order, indexes being used, row estimates, and other valuable information that helps understand how the optimizer views and optimizes queries. - There are two types of execution plan: estimated execution plan and actual execution plan. An Actual Execution Plan includes both estimated and run time properties, thus I prefer using actual execution plans for troubleshooting. An actual execution plan allows me to identify if there is a discrepancy in the estimated number of rows and the actual value. - To view actual execution plans, we enable  Include Actual Execution Plan on the tool bar and execute queries. - However, if we encounter a situation when we can't run queries to obtain actual execution...

Multi Statement Table-Valued function (MSTVF) VS Inline Table-Valued function (ITVF) – Inline is better

Image
π–³π—π—‚π—Œ 𝗐𝖾𝖾𝗄, 𝖨 π–Ύπ—‡π–Όπ—ˆπ—Žπ—‡π—π–Ύπ—‹π–Ύπ–½ 𝖺 π—Šπ—Žπ–Ύπ—‹π—’ π—‰π–Ύπ—‹π–Ώπ—ˆπ—‹π—†π–Ίπ—‡π–Όπ–Ύ π—‚π—Œπ—Œπ—Žπ–Ύ π–½π—Žπ–Ύ π—π—ˆ 𝗍𝗁𝖾 π—Žπ—Œπ–Ύ π—ˆπ–Ώ 𝖺 π—†π—Žπ—…π—π—‚-π—Œπ—π–Ίπ—π–Ύπ—†π–Ύπ—‡π— 𝗍𝖺𝖻𝗅𝖾𝖽 π—π–Ίπ—…π—Žπ–Ύπ–½ π–Ώπ—Žπ—‡π–Όπ—π—‚π—ˆπ—‡ (𝖬𝖲𝖳𝖡π–₯). 𝖳𝗁𝖾 π—Šπ—Žπ–Ύπ—‹π—’ π—‰π–Ύπ—‹π–Ώπ—ˆπ—‹π—†π—Œ π–Όπ—‹π—ˆπ—Œπ—Œ 𝖺𝗉𝗉𝗅𝗒 π—π—ˆ 𝖺 𝖬𝖲𝖳𝖡π–₯. 𝖠𝖿𝗍𝖾𝗋 π—†π—ˆπ–½π—‚π–Ώπ—’π—‚π—‡π—€ 𝗍𝗁𝖾 π–Ώπ—Žπ—‡π–Όπ—π—‚π—ˆπ—‡ π—π—ˆ 𝖺𝗇 𝗂𝗇𝗅𝗂𝗇𝖾 𝗍𝖺𝖻𝗅𝖾-π—π–Ίπ—…π—Žπ–Ύπ–½ π–Ώπ—Žπ—‡π–Όπ—π—‚π—ˆπ—‡ (𝖨𝖳𝖡π–₯), 𝗍𝗁𝖾 π—‰π–Ύπ—‹π–Ώπ—ˆπ—‹π—†π–Ίπ—‡π–Όπ–Ύ π—‚π—†π—‰π—‹π—ˆπ—π–Ύπ–½ 𝖻𝗒 𝟣πŸͺ𝗑.  π– π—Œ 𝗍𝗁𝖾 𝗇𝖺𝗆𝖾 π—Œπ—Žπ—€π—€π–Ύπ—Œπ—π—Œ, 𝖬𝖲𝖳𝖡π–₯ π–Όπ—ˆπ—‡π—π–Ίπ—‚π—‡π—Œ π—†π—Žπ—…π—π—‚π—‰π—…π–Ύ π—Œπ—π–Ίπ—π–Ύπ—†π–Ύπ—‡π—π—Œ 𝗐𝗂𝗍𝗁𝗂𝗇 𝗍𝗁𝖾 π–Ώπ—Žπ—‡π–Όπ—π—‚π—ˆπ—‡, 𝗐𝗁𝗂𝗅𝖾 𝖺𝗇 𝖨𝖳𝖡π–₯ π—‚π—Œ 𝖺 π—Œπ—‚π—‡π—€π—…π–Ύ π—Œπ—π–Ίπ—π–Ύπ—†π–Ύπ—‡π—. π–’π—ˆπ—‡π—Œπ–Ύπ—Šπ—Žπ–Ύπ—‡π—π—…π—’, 𝖲𝖰𝖫 𝖲𝖾𝗋𝗏𝖾𝗋 π—π–Ίπ—‡π–½π—…π–Ύπ—Œ 𝗍𝗁𝖾𝗆 𝖽𝗂𝖿𝖿𝖾𝗋𝖾𝗇𝗍𝗅𝗒.   𝖲𝖰𝖫 𝖲𝖾𝗋𝗏𝖾𝗋 π—π—‹π–Ύπ–Ίπ—π—Œ 𝖺𝗇 𝖨𝖳𝖡π–₯ π—Œπ—‚π—†π—‚π—…π–Ίπ—‹π—…π—’ π—π—ˆ 𝖺 𝗏𝗂𝖾𝗐, 𝗂𝗇𝗅𝗂𝗇𝗂𝗇𝗀 𝗍𝗁𝖾 π—…π—ˆπ—€π—‚π–Όπ–Ίπ—… π—Šπ—Žπ–Ύπ—‹π—’ π—ˆπ–Ώ 𝗍𝗁𝖾 π–Ώπ—Žπ—‡π–Όπ—π—‚π—ˆπ—‡ π—‚π—‡π—π—ˆ 𝗍𝗁𝖾 π—ˆπ—Žπ—π–Ύπ—‹ π—Šπ—Žπ–Ύπ—‹?...

SQL Server Service Broker: a small change in the activation procedure can spike CPU usage and why benchmarking matters

Image
In this post, I will demonstrate how a small change in Service Broker code can significantly increase CPU usage. This post may be helpful for those troubleshooting high CPU consumption related to Service Broker if their case is similar to mine.  CPU before and after the bug in Service Broker got fixed However, the main purpose of the post is to highlight that even when following Microsoft's documentation to setup some database feature, things can still go wrong. This is why benchmarking and testing are crucial steps before deploying new features to your production system. Additionally, it is interesting to see that a small change in the code can completely alter the service broker behavior. If you'd like, you can code along with me and see the results for yourself, provided you have SQL Server installed. Architecture The service broker setup I will use is as follows: There are two services and two corresponding queues and their activation procedures: the...