Problem Description / Requirement

This example comes from a customer from the automtive sector. Whenever a vehicle is built, EVERY single step must be precisely recorded. So-called “telegrams” are used for this purpose. These telegrams can convey simple information (e.g., entry into an assembly station) or complex information. The crucial thing is that EVERY piece of information must be stored in a traceable manner!

Each process step in a vehicle”s production is logged in a table as “telegrams.”
– The type of telegram is identified by a flag.
– Each telegram has a start and end date.

In the event of a malfunction, you want to know which processes were active at that exact time (timeline).
– These can be very short processes (login, tool change, etc.).
– These can be very long processes (painting, wedding, etc.).

The customer complains about very long response times. Especially if a timeline spans several days (which is possible), the query sometimes takes several minutes.

Problem

The problem with executing the query is the unpredictable amount of data that can be returned depending on the time specification. For example, if a query is started with a time range that is very close to the end or beginning of the table, the query runs very well. However, the closer you get to the middle stored date value, the longer the query takes.

 

Quick running query

SELECT id, [t_status], begin_ts, end_ts
FROM   dbo.telegram
WHERE  begin_ts <= '2025-06-11 14:00'
       AND end_ts >= '2025-06-11 13:00';
GO

Long running query

SELECT id, [t_status], begin_ts, end_ts
FROM   dbo.telegram
WHERE  begin_ts <= '2025-05-26 14:00'
       AND end_ts >= '2025-05-26 13:00';
GO

Note

When you run both queries without optimization you will see the same IO and CPU statistics. When you try the same with an appropriate index the first query should run faster than the second one.
The customer wants a performance for the query independent from the date and time but only accepts longer runtime when more records are found!

Challenge

  • The table [dbo].[telegram] has only a clustered Primary Key on [internal_id], no other indexes are set
  • Use ANY technique you know to optimize the queries.
  • The customer wants a predictable runtime for the query.
  • The runtime must be independent of the time period in which a process is queried.

Condition

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

The creation of the challenge can take up to 5 Minutes and creates the following objects / data

  • Table [dbo].[telegram]
  • 40 Mio rows
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 #8 - querying time ranges';
will create the challenge for you

 

customer expectations

The runtime may not vary due to a different time window; however, it may vary due to the amount of data.

Solution

This challenge is certainly one of the most difficult. Whoever finds this solution can call themselves an “experienced DBA.”

SQL Challenge #8 – 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.