//
you're reading...
Technical Topics

How Microsoft Reporting Services Handles String Parameters Oddly

One of the benefits of using Microsoft SQL Server Reporting Services (SSRS) to distribute data to end-users is that you can set parameters on a report that allows the end user to configure what data is returned by the report. For instance, we have a Course Detail report that returns section-by-section details of courses offered include credits, number students enrolled, instructor, meeting time, etc. That report has parameters that allows the end user to choose what terms to return data for, what course subjects, which instructors, etc.

This particular report can run very slow, especially when it is run for a many terms all at once. The SQL query behind the report runs fast, and we’ve optimized indices for the query, but the SSRS report runs slowly. Very slowly. We see this with many of our reports: we can retrieve the data quickly when we pull it directly from the SQL Server, but we have significant performance issues when we run the query via SSRS.

We stumbled across the culprit recently. When an SSRS parameter contains a string value, SSRS coerces the selected values to unicode before passing them to the SQL Server. For instance, if we wanted to pull all our student registration records for 5 specific terms, we would write a query similar to:

select *  
     from ST_COU
     where YRTR in ('20113', '20115', '20121', '20123', '20125')

But, if YRTR (year-term) were a parameter in a SSRS report, SSRS would configure the query as:

select *
      from ST_COU
      where YRTR in (N'20113', N'20115', N'20121', N'20123', N'20125')

Those “N” prefixes coerce the values to a unicode type. But, our YRTR variable is a char(5), not a nchar(5), so to do the comparison SQL Server must cast YRTR to the nchar format.

In a simple query like the one above it really doesn’t matter. But in a more complex query the effect on performance can be significant. Before we fixed our Course Detail report, it ran for over ten minutes before our DBA called and said the server’s CPU usage was at 95%. After adjusting for this issue, the report runs in about 10 seconds.

There are several possible fixes, and you might think of even more. One, obviously, is to refactor your database to use Unicode types, like nchar and nvarchar. If you are already using those variable types, you are not experiencing this problem. Changing to Unicode though risks causing performance problems in existing applications.

Another fix is to re-write your queries to force the IN statements to work on a smaller set. For instance, while our ST_COU table is huge because it includes a record for every registration that has occurred at WSU, there is another much smaller table YRTR_CAL that contains one record for every term. So, the following query is far more efficient for us:

select *  
     from ST_COU s  
     where exists (     
          select *    
               from YRTR_CAL    
               where YTRT=s.YRTR    
               and YRTR in (N'20113', N'20115', N'20121', N'20123', N'20125')     
     )

Another technique we have used is to create temporary tables in our SSRS query, casting certain fields to nchar and nvarchar, and using those temp tables to subset with the WHERE statement. We have even created indices on those temporary tables to improve performance.

We don’t want to change the types of existing fields in our database to address this problem for fear of causing performance problems in other applications. But, we are considering adding additional fields to some of our tables to address the most critical issues. For instance, we could create a YRTR_N field in our ST_COU table of type nchar(5) that otherwise holds the same values as the YRTR field, and use that new field in our WHERE statements. This has the advantage of not requiring extra joins in our queries, and allows us to have existing indices on the new field that would improve performance. Or, we could create a Unicode YRTR_N in our YRTR_CAL table which would still be more efficient because we could build indices on that new field.

This issue can sometimes just be ignored in reports where the IN statements are short and the query is not complex. However, in other reports the cost of ignoring this issue in terms of performance is huge. And Microsoft doesn’t appear to be about to change how this feature works, as their article at http://support.microsoft.com/kb/2000524 states, this SSRS behavior is “by design”.

About Ed Callahan

I am IT staff assigned to Institutional Planning, Assessment and Research (IPAR) at Winona State University. On the best days my title is Data Analyst and I get to answer high level questions by exploring data and write-up an thorough analysis. Other appropriate titles, depending on our workload at the time, would be Reporting Specialist, Database Developer or Ad Hoc Data Request Satisfier. I'm also a boy scout leader, school board president and have an unnatural obsession with riding my motorcycle.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

IPAR Posts

May 2012
S M T W T F S
« Feb   Oct »
 12345
6789101112
13141516171819
20212223242526
2728293031  

The WSU IPAR Team

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 6 other followers

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: