It’s interesting how a lot of the work I’ve been doing lately has in some way involved a kind of performance tuning. Previously I’ve talked about how to increase the performance of .NET applications by using delegates instead of reflection in code that runs frequently.
This time it is all about performance in database processing.
The scenario
Imagine an application that manages a wait list. Users of this application put themselves in line and wait for their turn to gain access to some kind of shared resource. Here are the basic rules of this system:
- The same user can appear in the wait list multiple times, once for every resource she is queuing for.
- The users’ position in the wait list at any given time is decided by a score.
- This score is calculated based on the number of credits each user has in the system compared to the amount required by the resource they wish to access.
Let’s say that this wait list is modeled in a Microsoft SQL Server database with the following schema:
The position of the different users in the wait list is periodically updated by a Stored Procedure that calculates the current score for each and every row in the WaitList table.
So far so good. Now, imagine that this WaitList table contains somewhere around 30 millions rows, and the Stored Procedure that updates all of the scores takes about 9 hours to complete. And now we have problem.
The imperative SQL approach
Before going into all kinds of general database optimization techniques, let’s start off by looking at how that Stored Procedure is implemented.
Here is a slightly simplified version of it:
CREATE PROCEDURE CalculateWaitListScores_Imperative AS BEGIN DECLARE @rowsToCalculate INT SELECT @rowsToCalcualte = COUNT(*) FROM WaitList AND Score IS NULL WHILE ( @rowsToCalculate > 0 ) BEGIN DECLARE @userID INT DECLARE @resourceID INT DECLARE @score INT SELECT TOP 1 @userID = UserID, @resourceID = ResourceID FROM WaitList AND Score IS NULL -- The actual calculation of the score is omitted for clarity. -- Let's just say that it involves a SELECT query that joins -- the [WaitList] table with the [User] and [Resource] tables -- and applies a formula that associates the values -- of the [Credit] columns in each of them. -- For the sake of this example we just set it to a constant value SET @score = 150 UPDATE WaitList SET Score = @score WHERE UserID = @userID AND ResourceID = @resourceID AND Score IS NULL SELECT @rowsToCalcualte = COUNT(*) FROM WaitList AND Score IS NULL END END
If you aren’t into the Transact-SQL language syntax, let me spell out the algorithm for you:
- Get the number of rows in the WaitList table where the score has never been calculated
- If there are any such rows, get the user and the resource IDs for the first row in the WaitList table where the score has never been calculated
- Calculate the score for that user and resource
- Update the score with the newly calculated value
- Go to Step 1
In the worst case, this set of operations will be repeated 30 millions times, that is once for every row in the WaitList table. Think about it for a moment.
While looking at this code, I immediately imagined this dialogue taking place between SQL Server and the developer(s) who wrote the Stored Procedure:
Developer: Listen up, SQL Server. I want you to calculate a new score and update all of those 3o millions rows, but do it one row at a time.
SQL Server: That’s easy enough, but I’m pretty sure I can find a faster way to do this, if you’ll let me.
Developer: No, no. I want you to do exactly what I said. That way it’s easier for me to understand what’s going on and debug if any problem occurs.
SQL Server: Alright, you’re the boss.
Jokes aside, the bottom line here is this:
And that basically means trading performance and scalability for more fine-grained control.
The declarative SQL approach
Let’s see if we can make this Stored Procedure run any faster, by changing our approach to the problem altogether.
Here is a rewritten version of the original Stored Procedure:
CREATE PROCEDURE CalculateWaitListScores_Declarative AS BEGIN UPDATE WaitList SET Score = dbo.CalculateScore(UserID, ResourceID) WHERE Score IS NULL END
What we did is basically removing the explicit loop and merging all operations into a single UPDATE statement executed on the WaitList table, which invokes a custom a scalar function (CalculateScore) to calculate the score with the value of the current row.
Now, let’s look at some performance comparison:

That’s a pretty significant leap in speed. How is that possible? A look at the CPU usage on the database server while running the two versions of the Stored Procedure pretty much explains it all:
CPU usage while executing CalculateWaitListScores_Imperative:

CPU usage while executing CalculateWaitListScores_Declarative:
As you see, in the first picture the CPU is steadily at 9-10% and is basically using only one out of four available cores. This is because SQL Server is forced to do its work sequentially and has to wait until the score for the current row has been calculated and updated before proceeding to the next.
In the second picture, we are simply telling SQL Server our intent, rather than dictating exactly how it should be done. This allows SQL Server to parallelize the workload than can now be executed on multiple CPU/Cores at once leveraging the full power of the hardware.
Lessons learned
Here are a couple of getaways I learned from this exercise:
- SQL is a declarative language at its core, designed to work with sets of rows. That’s what it does best and that’s how you should use it.
- Whenever possible, try to avoid applying an imperative programming mindset when implementing database operations, even if the constructs available in SQL-derived languages like T-SQL make it easy to do so
- Don’t be afraid to give up some control over what happens at runtime when your database code runs. Let the database find out the best way to do things, and get ready to experience some great performance improvements.
Hope this helps.
/Enrico
Lately I have been involved in the performance profiling work of a Windows client application, which customers had lamented to be way too slow for their taste.
The application was originally developed a couple of years ago on top of the .NET Framework 2.0. Its user interface is built using Windows Forms and it retrieves its data from a remote remote server through Web Services using ASMX.
Everything worked just fine from a functionality standpoint. However customers complained over long delays as data was being retrieved from the Web Services and slowly populated the widgets on the screen.
Something had to be done to speed things up.
Reflection is a bottleneck
A look with a .NET profiler tool such as JetBrains DotTrace revealed that a lot of time was spent sorting large collections of objects by the value of one of their properties. This would typically be done before binding them to various list controls in the UI.
The code would typically look like this and was spread out all over the code base:
// Retrieves the entire list of customers from the DAL
List<Customer> customerList = CustomerDAO.GetAll();
// Sorts the list of 'Customer' objects
// by the value of the 'FullName' property
customerList.Sort(new PropertyComparer("FullName"));
// Binds the list to a ComboBox control for display
cmbCustomers.DataSource = customerList;
Apparently line 6 was the one that takes forever to execute. Now, since the sorting algorithm used in the IList.Sort method can’t be changed from outside the class, the weak link here must be the PropertyComparer. But what is it doing? Well, here it is:
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Thoughtology.Samples.Collections
{
public class PropertyComparer<T> : IComparer<T>
{
private string propertyName;
public PropertyComparer(string propertyName)
{
this.propertyName = propertyName;
}
public int Compare(T x, T y)
{
Type targetType = x.GetType();
PropertyInfo targetProperty = targetType.GetProperty(propertyName);
string xValueText = targetProperty.GetValue(x, null).ToString();
string yValueText = targetProperty.GetValue(y, null).ToString();
int xValueNumeric = Int32.Parse(xValueText);
int yValueNumeric = Int32.Parse(yValueText);
if (xValueNumeric < yValueNumeric)
{
return -1;
}
else if (xValueNumeric == yValueNumeric)
{
return 0;
}
else
{
return 1;
}
}
}
}
Likely not the prettiest code you have ever seen. However, it’s pretty easy to see what it’s doing:
- Extracts the value of the specified property from the input objects using reflection.
- Converts that value to a String.
- Parses the converted value to an Integer.
- Compares the numeric values to decide which one is bigger.
That seems like a lot of extra work for a simple value comparison to me.
I’m sure the method was built that way for a reason. This IComparer class is designed to be “generic” and work on any type of value on any object. However my guess is that it won’t work with anything but primitive types (numbers, strings and booleans). In fact the default implementation of the Object.ToString() (used in lines 22-23) method returns the fully qualified name of the class, and that usually doesn’t isn’t much of a sorting criteria in most cases.
Use delegates instead
At this point it is clear that we need to refactor this class to improve its performance and still retain its original functionality, that is to provide a generic way to compare object by the value of one of their properties.
The key is to find a better way to retrieve the value of a property from any type of object without having to use reflection.
Well, since we do know the type of the objects we are comparing through the generic parameter T, we could let the caller specify which value to compare the objects with by. This can be done by having the caller pass a reference to a method, which would return that value when invoked inside of the Compare method. Let’s try it and see how it works.
Implementing the solution in .NET 2.0
Since the application was on .NET 2.0, we need to define our own delegate type that will allow callers to pass the reference to a method returning the comparable value . Here is the complete implementation of the refactored PropertyComparer class:
using System;
using System.Collections.Generic;
namespace Thoughtology.Samples.Collections
{
public class PropertyComparer<T> : IComparer<T>
{
public delegate IComparable ComparableValue(T arg);
public PropertyComparer(ComparableValue propertySelector)
{
this.PropertySelector = propertySelector;
}
public ComparableValue PropertySelector { get; set; }
public int Compare(T x, T y)
{
if (this.PropertySelector == null)
{
throw new InvalidOperationException("PropertySelector cannot be null");
}
IComparable firstValue = this.PropertySelector(x);
IComparable secondValue = this.PropertySelector(y);
return firstValue.CompareTo(secondValue);
}
}
}
Our delegate, called ComparableValue, takes an object of the generic type T as input and returns a value to compare that object by.
The comparison itself is than performed by the returned value itself, by invoking the IComparable.CompareTo method on it (see line 27).
The caller can now invoke the Sort method by specifying the property to compare the items by with an anoymous delegate:
customerList.Sort(new PropertyComparer(delegate(Customer c)
{
return c.FullName;
});
Notice how the property name is no longer passed a a string. Instead it is actually invoked on the object providing compile-time type checking.
Alternative implementation in .NET 3.5
This same solution can be implemented slightly differently in .NET 3.5 by taking advantage of the built in Func<T,TResult> delegate type:
using System;
using System.Collections.Generic;
namespace Thoughtology.Samples.Collections
{
public class PropertyComparer<T> : IComparer<T>
{
public PropertyComparer(Func<T, IComparable> propertySelector)
{
this.PropertySelector = propertySelector;
}
public Func<T, IComparable> PropertySelector { get; set; }
public int Compare(T x, T y)
{
if (this.PropertySelector == null)
{
throw new InvalidOperationException("PropertySelector cannot be null");
}
IComparable firstValue = this.PropertySelector(x);
IComparable secondValue = this.PropertySelector(y);
return firstValue.CompareTo(secondValue);
}
}
}
Great, this saved us exactly one line of code.
Don’t worry, things get much nicer on the caller’s side where the anonymous delegate is substituted by a much more compact lambda expression:
customerList.Sort(new PropertyComparer(c => c.FullName));
The results
Now that we put reflection out of the picture, it is a good time to run a simple test harness to see how the new comparison strategy performs. For this purpose we will sort an increasingly large collection of objects with the two PropertyComparer implementations and compare how long it takes to complete the operation. Here are the results in a graph:

As you see, by using delegates the sorting algorithm stays on the linear O(n). On the other hand with reflection it quickly jumps over in the exponential O(cn) space, where c is the time it takes to make a single comparison.
Lessons learned
This exercise teaches three general guidelines that can be applied when programming in .NET:
- Reflection is expensive. Use it sparingly and avoid it whenever possible in code that is executed very often, such as loops.
- Generic delegates allow to build flexible code in a fast and strongly-typed fashion. This can be achieved by letting callers “inject” custom code into an algorithm by passing a delegate as argument to a method. The code referred to by the delegate will then be executed at the appropriate stage in the algorithm inside the method.
- When reflection is used to dynamically invoke members on a class, the same thing can be achieved by using generic delegates instead, like demonstrated in this article. This technique is widely used by modern isolation frameworks such as Rhino Mocks, Moq and TypeMock Isolator.
Download Sort Test Harness Sample
/Enrico


