Problem Description / Requirement

A new customer contacts you. Their developers have created a dashboard that contains five KPI components that visualize the importance of orders according to a given status. The status of an order can only have 3 states!

  1. high
  2. normal
  3. low

To take future states into account, two more KPI components were added to the dashboard. The customer was told that querying additional values ​​would not affect performance because there was a CHECK constraint on the [o_orderpriority] attribute that only coveres values ​​1 – 3. SQL Server would ignore other values.

When examining the performance, it was found that for ALL parameter values ​​(1 – 5) a FULL SCAN was always performed on the index.

– Why does this happen?
– Explain to the customer how this could be prevented.

The basis for the analysis are the following SQL statements, which you have been able to evaluate:

/* Dashboard KPI Query #1 */
SELECT COUNT_BIG(*) FROM dbo.orders WHERE o_orderpriority = 1;
GO
/* Dashboard KPI Query #2 */
SELECT COUNT_BIG(*) FROM dbo.orders WHERE o_orderpriority = 2;
GO
/* Dashboard KPI Query #3 */
SELECT COUNT_BIG(*) FROM dbo.orders WHERE o_orderpriority = 5;
GO

Challenge

All queries generate the same execution plan and your customer wants to know why this happens. He also wants to receive a solution from you on how to avoid this.

Condition

  • No new indexes are allowed to add to the table due to concerns about write performance.
  • Metadata of the table must not be changed.
Category

Constraints / Consulting

Level 300

Technical Information

On table is created in the challenge database that are relevant for this task:

Table Name Number of Rows Storage Space
dbo.orders 1.000.000 14,25 MB

The table has a clustered primary key and a CHECK Constraint on o_orderpriority.

How to start the challenge

When you are connected to the SQL Server instance…

EXECUTE sqlchallenge.dbo.create_challenge 'list';
will give you a list of open challenges
EXECUTE sqlchallenge.dbo.create_challenge 'name_of_challenge';
will create the challenge for you

After the creation just get into the database for your challenge

customer expectations

The customer wants to know why queries against predicates which are not in the range of the CHECK Constraints will cause a SCAN of the table. Furthermore he wants to have a solution to fix it without adding new indexes to the table.

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.