Recently Viewed...
SnowCovered Top Sellers

Version 5 of the perennial best-selling tool for creating data-based solutions in DNN without custom programming. This version focuses on greater flexibility, expandability, and ease-of-use.

Live Content uses Web 2.0 approach to provide a Rich User Interface and streamlines content presentation by overlaying content on current page. Overlay images, videos, audio, text/html content, flash, dotnetnuke modules, and external content. Experience the demo...

Ultra Video Gallery is a brother product of Ultra Media Gallery, UVG allows you to add videos in various format and automatically convert them to flv format, you also can add videos from embed code and play them in our integrated flash video player.

Powerful, Ajax Enabled, Easy to Use. Document Management has never been better. Open-DocumentLibrary allows DotNetNuke users to share and manage documents in a flexible, intelligent way, offering granular control over Folder and Document access.

'Relationship Building' and 'Communication' are two essential nuts and bolts for a business to prosper. This module allows you to bridge both of these and easily generate continuous awareness of your web site, products and services. Your prospects and customers will greatly appreciate this featur

In this day and age, knowing as much detailed information as possible about your customer, prospect or web site user is essential. Thankfully, the new 'Dynamics Forms' module from Data Springs, makes it easier than ever to segment your data collection efforts.

Capture your users attention, enrich your site with multimedia flash, and create and opt in distribution list for your DNN site. These are just a few of the many features the Data Springs Module Collection can provide you.

Ultra Media Gallery is the most popular photo gallery and media gallery solution for DotNetNuke, UMG offers 10 different flash player to browse your gallery with completely different user interface experience.

The Catalyst skins are professionally designed, coded and packaged by a team of DotNetNuke experts. The skins are available in 12 great colours. This skin is easily customisable with our unique DrNuke EasyMod technology. Try our demo!

ALL NEW ! - Minimalist includes skin packs in 12 great colors. Each color has Flat, Gradient and Glass versions. Feature rich XML Flash header, perfect for just about any purpose. 9 Different menu options in each skin pack; 3 horizontal menus, 3 vertical menus and 3 all-new Twin level menus . . .

    |   Register   |   Thursday, November 20, 2008   
You are here:Resources  Articles & Information  Key SQL Performance Situations - Expressions  


SQL Server Expressions- Key SQL Performance Situations to Watch Out For

Key SQL Performance Situations to Watch Out For - Part Two of Three

 

View Part One - SQL Server Blocking

 View Part Three - Full Table Scans

 

Expression = Expression

a2 b2 = c2.  Anyone remember what this is?  Pythagorean Theorem anyone?  It clearly states that in any right triangle, the square on the hypotenuse is equal to the sum of the squares on the other two sides.  So what?  What does it have to do with SQL Server or performance?  Look closely at the formula.  Let’s consider the letter “c” in the equation as a column in a table used in a where clause of a select statement. There is a component on the left side of the equal sign that requires calculation.  There is also a component on the right side of the equal sign that also requires a calculation.  What does this mean to performance in SQL Server?

 

When SQL Server has to perform calculations on both sides of a comparison operator within a Where clause, then this criteria cannot be considered when evaluating the usage of indexes.  SQL Server must try to use other criteria within the query to figure out which indexes to use, and if there are none, the query processor will scan all of the rows in the table, evaluating these expressions one row at a time, in order to determine which rows to process or return.

Let’s consider the following change to the Pythagorean Theorem formula. 

 

 c = \sqrt{a^2   b^2}. \, 

 

Notice that by using some simple algebra to solve for c, now we have a different scenario.  On the left side of the equal sign we now have a column with no calculations performed on it, while on the right side we have an expression.  The query optimizer can now consider indexes that include the “c” column, since it can evaluate the expression at compile time and not at run-time.

Here are some examples of poorly performing SQL statements that research shows to occur a great deal in applications.

 

SELECT A.*

FROM Customer as A

WHERE A.City ', ' A.State = @SearchField ;

 

SELECT A.*

FROM Customer as A

WHERE A.FirstName ' ' A.LastName  = @SearchField ;

 

SELECT A.*

FROM OrderDetail as A

INNER JOIN Product As B on (A.ProductID = B.ProductID)

WHERE (A.Qty * B.ItemPrice) >= 100.00 ;

 

In two of the three example queries, the SQL could be adjusted to solve for a column on one side of the comparison and have the potential to be evaluated by indexes if they are available.  There are expressions on both sides of the comparison, however, so these will perform full scans of the tables involved to find the criteria in question.

 

Here are examples of changes that could be made to these queries to allow them to work more optimally, should indexes be present on the columns involved.

 

SELECT A.*

FROM Customer as A

WHERE A.City  = @SearchFieldCity

AND   A.State = @SearchFieldState ;

 

SELECT A.*

FROM Customer as A

WHERE A.FirstName = @SearchFieldFirstName

AND   A.LastName  = @SearchFieldLastName ;

 

Why isn’t the third query able to be optimized in this way?  In the first two examples, the expression on the right of the comparison operator can be resolved at compile time.  With the third query, no matter how you manipulate the formula, there will be elements on both sides of the comparison operator that cannot be known until the query is executed.  Consider the following two examples of variations of the third query.

 

SELECT A.*

FROM OrderDetail as A

INNER JOIN Product As B on (A.ProductID = B.ProductID)

WHERE  A.Qty  >= 100.00 / B.ItemPrice ;

 

SELECT A.*

FROM OrderDetail as A

INNER JOIN Product As B on (A.ProductID = B.ProductID)

WHERE  B.ItemPrice >= 100.00 / A.Qty ;

 

As you can see, no matter how it is manipulated, there is a column on both sides of the equation and one of them must be involved with the calculation. 

Here is another example that includes the query plan of each case.

 

SELECT COUNT(*)

FROM dbo.TransactionTable1 AS A

WHERE  A.IntFlag 1 = 3;

 

 

Notice how the first thing that happens (you read these from right to left) is an Index Scan.  This is looping through an index, but it has to look at every node of the index to process the data for the query because of the way the WHERE clause is written with an Expression = Expression.  On the test machine, with the test example database, this yielded a cost of 2.78.  However, by making the subtle change below, the cost was reduced to 0.0032 on the same machine in the same database.

 

SELECT COUNT(*)

FROM dbo.TransactionTable1 AS A

WHERE  A.IntFlag = 3 – 1;

 

 

Notice how the first thing this query does (again, read from right to left) is an Index Seek.  This means that it can actually use the index as intended instead of having to scan through all of the data.  Also take note that the next activity processed by the query, Stream Aggregate, is drastically reduced in cost between the first query and the second query.  This is very common.  The estimated cost for the other activities of the query will go down when altering queries to quit scanning because the query optimizer tries to guess at how many rows will be processed by each step.  When an index is used, the query optimizer can use statistics about the data in the index to more accurately guess at how many rows will be processed.  There are cases however, as in the case of Filter tasks, or Bookmark Lookup tasks where the actual physical time will be saved performing these steps when and index is used.  This is because these types of activities must actually be performed for each row that is returned by the data gathering steps (Scans and Seeks) and therefore the performance of the query overall would be improved if they didn’t have to perform their activities on every row and instead only have to perform them on the necessary rows found by the Seek.

 

 

So what does all of this mean?  To summarize, the goal is to avoid the processing of every single row of a table when returning rows is a desirable thing.  However, having expressions on both sides of the comparison operator can cause this very thing to happen.  To optimize these situations, look for areas where simple algebra can be used to solve for one of the columns or where multiple parameters can be used instead of comparing a value to a calculation of columns.

 

 

View previous article on Performance Situations to Watch Out for  - SQL Server Blocking.

 

View next article on Performance Situations to Watch Out for  - SQL  Server Expressions.

 

 

Feedback Comments
Records per Page
Page 1 of 1First   Previous   Next   Last   
Feedback





Enter the code shown above in the box below
Send

 

DNN Modules
SharePoint Web Parts
Flash Image Rotator for SharePoint 2007

Flash Image Rotator Web Part for SharePoint 2007 

 

Who would have thought? Flash with Sharepoint! The FIRST and ONLY flash rotation web part for Sharepoint. The Flash Image Rotator displays selected images and then rotates between the images. Several extended and optional features allow you to select the time to rotate each image, fade between i...more

Price: $129.99
 
Flash News Ticker for SharePoint 2007

Flash News Ticker Web Part for SharePoint 2007 

 

Provide current news items with a user-friendly news ticker for your Sharepoint Portal. With millions of web sites offering information you need a fun way to display information and the solution is Flash News Ticker....more

Price: $139.99
 
View Stock Quote Web Part

Stock Quote Web Part for SharePoint 2007

 

Giving your site visitors relevant information is critical. With the Data Springs Stock Web Part you can provide your users with up to date financial information....more

Price: $149.99
 
Random Image Web Part for SharePoint / MOSS 2007

Random Image Web Part for SharePoint 2007

With Random Image for Sharepoint 2007, you can select multiple images to display randomly when the web part loads...

Price: $139.99
 
SharePoint Charts Web Part

MOSS Charts Web Part for SharePoint 2007

The MOSS Chart Web Part is a web part built by Data Springs for the purpose of rendering several chart types based on data from a SharePoint list on a MOSS 2007 or WSS 3.0 Site ... more

Price: $269.99
 
Copyright 2005 - 2008 by Data Springs, Inc.
Contact Us | Terms Of Use | Privacy Statement