Problem Description / Requirement

The customer has developed an application that he himself has developed and swears by the use of user-defined functions. The developers argue that user-defined functions create reusable code that makes work easier.

A calculated attribute [sales_volume] is used in the [dbo].[customers] table. Users complain that the search for individual customers is satisfactory, but reports that have to list several customers sometimes take more than 30 seconds to display the data.

The customer was advised to equip the server with more CPUs so that queries can be executed more quickly. Initial tests have shown, however, that despite faster CPUs, performance is not noticeably improved.

They are now turning to you to solve the problem.

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

/* Search pattern for searching for a customer using the primary key */
SELECT * FROM [dbo].[customers]
WHERE c_custkey = @c_custkey;

/* Search pattern for list of customers in a reporting application */
SELECT * FROM [dbo].[customers]
WHERE c_custkey BETWEEN @c_custkey_start AND @c_custkey_end;

Challenge

Analyze the situation. Check the runtime behavior and implement the resolution practices that seem best to you to solve the problem. You may use any techniques that you know.

Condition

  • The table [dbo].[customers] must continue to have a calculated attribute [sales_volume].
  • The parameters used to calculate [sales_volume] must not be changed!
Category

User Defined Functions

Level 300

Technical Information

Two tables are created in the challenge database that are relevant for this task:

Table Name Number of Rows Storage Space
dbo.customers 1.607.957 219 MB
dbo.orders 2.275.781 296 MB

Both tables have a clustered primary key and several nonclustered indexes. The tables use a FOREIGN KEY to enforce referential integrity.

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 expects queries across the customer base of up to 10,000 customer records to be executed in less than 1 second.

Solution

I hope you had fun with this challenge. You want to know one possible solution. I helped the customer with this solution:

https://www.sqlchallenges.de/sql-challenge-4-solution/

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.