Problem Description / Requirement

A customer is reporting a deterioration in the performance of all queries after the software vendor updated the database objects using a T-SQL script. After the vendor completed the updates, the customer discovered that the query row estimates were no longer correct.

The customer identified the problem using a general query and is requesting support in determining the cause of the faulty “row estimates”.

SELECT  c_custkey,
c_mktsegment,
c_nationkey,
c_name,
c_address,
c_phone,
c_acctbal,
c_comment
FROM    dbo.customers
WHERE   c_nationkey = 1
ORDER BY
c_name;

Run the above query in your challenge database and watch the actual execution plan. Have a look to the row estimates and the executed number of rows.

Problem

All queries executed against the database are returning incorrect “estimate rows”. These faulty estimates are causing the system database TEMPDB to regularly reach its capacity limits. The vendor cannot find the cause of the error and recommends allocating more memory to the TEMPDB system database.

Note

Pay particular attention to the estimated number of records and the SORT Spill resulting from the miscalculation.

Challenge

  • Find and correct the cause of the massive miscalculation.
  • Explain to the customer the basis for the estimate and how Microsoft SQL Server calculates the estimated value.

Condition

  • none. You can do all to improve the query (adding indexes, tables, partitions, …)
Technical Information

The challenge #10 creates one table for demo purposes

  • [dbo].[customers] with 50,000 rows
    • nonclustered index: nix_customers_c_nationkey
How to start the challenge

When you are connected to the SQL Server instance, run the following commands:

EXECUTE sqlchallenge.dbo.create_challenge 'list';
will give you a list of open challenges
EXECUTE sqlchallenge.dbo.create_challenge 'SQL Challenge #9 - estimates going wrong';
will create the challenge for you

 

customer expectations

The query should generate an optimal execution plan and consume ideal memory the SORT operator

The query must not be changed because it is generated from the framework of the software

Connect to Microsoft SQL Server

SQL Server: sqlchallenges.germanywestcentral.cloudapp.azure.com
Port: 1433
Version:
16.0.4165.4
Edition: Developer Edition
Authentication Type: SQL Server Authentication
Login Name: sqlchallenge
Password: sqlchallenge#2025!
Encryption Mandatory
Trust Server Certificate: True
 
Improve Your Skills as Query Tuner for SQL Server During this workshop, you’ll gain the expertise and confidence needed to diagnose and fine-tune slow-performing queries, resulting in improved database responsiveness and end-user satisfaction. Whether you’re an experienced database administrator, developer, or data analyst, this workshop will equip you with the knowledge and practical skills to tackle the most challenging query optimization tasks.
Improve your skills as a database developer
Improve Your Skills as a Database Developer for SQL Server In this workshop, we will cover a wide range of topics and techniques that are essential for modern database developers working with SQL Server. From fundamental concepts to advanced best practices, you’ll gain the knowledge and practical experience needed to excel in your role.