Skip to Content

 Performance Tuning at Crystal Reports using SQL Expression

Benefits

1)      SQL Expressions are used to boost performance in Crystal Reports by reducing the network traffic and time needed to refresh the report.

2)      SQL Expressions are similar to a Crystal Formulas. The only difference is that they are written in a Structured Query Language rather than the Crystal Formula Language.

3)      They are executed at the Database Server.

 

Traditional Approach vs. SQL Expressions in Crystal Reports 2008

                                                             

 Traditional Approach

 

Let us say we have requirement to create a Crystal Report, which uses a SQL Server as a Database and has Table Order.

 

In general if we have to fetch the details from the Order Table, which includes an Order_Date field.

 Now, if we want records to be fetched according to the day of the month on which the order was placed, following steps are required to be taken

 

1)      Create a Crystal Formula, lets say fr_Order_Day to return the required day number by pasting the below code

                                Day ({Order.Order_Date})

 

2)      Go to the select expert, plug the above created formula there so that we have the Crystal Reports return only the required sub set of data as per any of the below set conditions

 

A) fr_Order_Day = 1

B) fr_Order_Day=@input parameter

( Note :-Use either A or B condition based on the requirement)

Now, if you go to the Database Menu and select Show SQL Query, you will see an SQL statement similar to the below one

 

SELECT

  order.”order_id”,

  order.”cust_id”,

  order.”order_date”,

  order.”order_amt”

FROM

“Demo”.”dbo”.”order” order

  This is the SQL code that Crystal Reports uses to get the records it needs from the database.

The important point about this statement is that there is no WHERE clause. All the records in the table will be retrieved from the server, regardless of whether they are needed for the report. It is up to Crystal Reports to select the required records.

 

 

SQL Expression Approach

 

For the above Business requirement, following steps are required

 

1)      At the Field Explorer, go to the SQL Expression and create a new expression, say Exp_1 and paste the below code

 

                Datepart (dd, Orders.Order_Date)         

 

Note: – Instead of using the Crystal or basic syntax we would be required to use a Database Syntax. In the above case since the Database used is SQL we would use the syntax which is understood by the SQL Server.

 

2)      Go to the Select Expert plug the above created SQL Expression Exp_1 with some assignment value say:

Exp_1=1

 

After you have closed the expert and refreshed the report, you should see exactly the same records as before. However, if you now choose the Show SQL Query command from the database menu, the SQL code will look something like below.

 SELECT

  order.”order_id”,

  order.”cust_id”,

  order.”order_date”,

  order.”order_amt”

FROM

“Demo”.”dbo”.”order” order

WHERE

                (Datepart (dd, order.”order_date”)) =1

 The big difference is that there is now a WHERE clause. This means that the record selection will be done on the server, and only records which meet the criterion will travel across the network.

 With a small table – a few hundred records or so – you might not notice much of a difference.

 But if the table contains tens of thousands of records, and if you only want to select a small percentage of them, the performance gains could be substantial.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply