Speed Up SQL Server Apps -- Visual Studio Magazine


                                                                                                  More Redmond Developer Network Sites                      >>>     RedDevNews.com         |   ADTmag.com          |     Events




         Redmond Report             Redmond IT                Redmond Partner                             Redmond Developer                     Visual Studio                 MCPmag.com               Virtualization Review




                                                                                                                                                                                     Enter Search Term or FindIT Code

                                                                                                                                                                                                                        Search
                                                                                                                                                                                                             Advanced Search


            HOME      SUBSCRIBE/RENEW        ADVERTISE         FREE NEWSLETTERS                          ABOUT US                TECH LIBRARY      EVENTS         RSS FEEDS




    Hot Topics                                             like
                                                           http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx
                                                           AVo3oxuK
                                                                                                                                                                                               Most Popular Articles
    Visual Studio 2010                    Tweet      0          Like            5 people like this. Add a comment4
                                                                                people like this. Sign Up to see what
    NEW! Visual Studio                                                                                                                                                                            4 Must-Know
    2012/Win 8 5
    NEW! HTML                                                                                                                                                                                     Visual Studio
                                   Database Design                                                                                                                                                Keyboard
    ALM
    Agile/Scrum                    Speed Up SQL Server Apps                                                                                                                                       Shortcuts
                                                                                                                                                                                                  Animating
    SQL Server and SDS             Learn 10 useful tips and tricks that help boost SQL Server application performance.                                                                            Windows Phone
    Web Services                                                                                                                                                                                  Listbox Items
    .NET Framework                 By Roman Rehak          07/01/2004                                                                                                                             Free eBooks for
    SharePoint                                                                                                                                                                                    Data Developers
                                   Technology Toolbox: SQL Server 2000, ADO, ADO.NET
    XML/XAML                                                                                                                                                                                      Improved
                                   Developers love database programming tips, tricks, and workarounds—especially those that slash development time or                                             Combinations with
    C#
                                   boost application performance. Here's a collection of such nuggets for developing SQL Server applications.                                                     the BigInteger
    VB.NET                                                                                                                                                                                        Data Type
                                   1) Replace COUNT(*) With EXISTS When Checking for Existence
    Resources                                                                                                                                                                                     Develop Faster
                                   Developers often use the value from the COUNT(*) function when enforcing business rules in Transact-SQL code.
    2010 Buyers Guide                                                                                                                                                                             with Customized
                                   However, try the EXIST clause instead if you're using the COUNT(*) value only to evaluate whether you have at least one
                                                                                                                                                                                                  Visual Studio
    News                           row that meets certain conditions. For example, consider this code from the Northwind database:                                                                Templates
    Blogs
    In-Depth
    Code
    Columns
    Product Reviews
    Tech Library
    Sponsored Webcasts
    Subscribe/Renew
    Visual Studio Live!
    About Us
    Sitemap
    RSS Feeds




                                   IF (SELECT COUNT(*) FROM Orders
                                      WHERE ShipVia = 3) > 0
                                   PRINT 'You cannot delete this shipper'

                                   The execution plan shows that SQL Server has to read all 255 rows in the Orders table before evaluating the IF
                                   expression. You can achieve the same result more efficiently with EXISTS because the IF condition evaluates to true as
                                   soon as SQL Server finds the first occurrence of 3 in the ShipVia column:
                                   IF EXISTS (SELECT * FROM Orders
                                      WHERE ShipVia = 3)
                                   PRINT 'You cannot delete this shipper'

                                   The difference in total execution time isn't much in a sample database such as Northwind, but use this efficient query
                                   against an Orders table with millions of rows and you'll see a major speed improvement.

                                   2) Be Careful When Using WHERE IN and WHERE NOT IN
                                   SQL Server doesn't always choose an optimal execution plan when you have a substantial list of values in the WHERE IN
                                   clause. Using WHERE IN and WHERE NOT IN clauses in T-SQL code can produce an execution plan involving one or
                                   more nested loops. This increases the number of comparisons SQL Server must perform exponentially. Use the WHERE
                                   IN clause only if you have a short list of values you need to evaluate:
                                   USE Northwind
                                      --This query takes 9 ms to execute
                                   SELECT *
                                      FROM Customers




http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx[08/29/2012 4:29:12 PM]
Speed Up SQL Server Apps -- Visual Studio Magazine

                                       WHERE CustomerID NOT IN
                                       (SELECT CustomerID FROM Orders)                                                                                           FREE WHITEPAPERS
                                   Replace the WHERE IN clause with OUTER JOIN if you're using a subquery to generate a potentially large list. Doing so          Five Tips for Delivering
                                   can improve performance significantly:                                                                                         Working Software with
                                                                                                                                                                  Agile
                                   USE Northwind
                                       --This query takes 3 ms to execute                                                                                         5 Ways Today’s Backup
                                   SELECT c.*                                                                                                                     and Restore
                                      FROM Customers c                                                                                                            Technologies Can Put
                                      LEFT OUTER JOIN Orders o
                                      ON o.CustomerID = c.CustomerID                                                                                              Your Business on the
                                      WHERE o.CustomerID IS NULL                                                                                                  Fast Track
                                   In this case, the second query uses LEFT OUTER JOIN, producing an execution plan that lets it run about three times            Microsoft Uses
                                                                                                                                                                  LiteSpeed® for SQL
                                   faster than the first query.
                                                                                                                                                                  Server from Quest
                                   The LEFT OUTER JOIN selects all rows from the Customer table—whether or not a customer placed any orders—and                   Software to Eliminate
                                                                                                                                                                  the Need for Additional
                                   joins them with the Orders table. Then the WHERE clause filters out the rows where the columns from the Orders table
                                                                                                                                                                  Storage
                                   have NULL values. Either way, you get a list of customers who placed no orders, but the second way gives SQL Server a
                                                                                                                                                                  Top 6 LiteSpeed
                                   lot less work to do. I rewrote a query recently using this technique, and the execution time went from 50 seconds to about     Features DBAs Should
                                   500 ms.                                                                                                                        Know About

                                   3) Randomize Resultset Orders With NewID()                                                                                            > MORE TECHLIBRARY

                                   You occasionally might need to randomize the order of the resultset retrieved from SQL Server. This is often the case in
                                   database searches where certain products or services would gain unfair advantage against others based simply on their
                                   name. I've seen a few clever (and not so clever) solutions for randomizing resultsets, but the solution is actually simple.
                                   You can use the NewID() function in Transact-SQL to generate a GUID for each row, then order the results by the
                                   generated GUID:
                                   SELECT * FROM Products
                                      ORDER BY NEWID()

                                   SQL Server returns products in a different order every time you run the query. You also can use this technique to return a
                                   random row from a table:
                                   SELECT TOP 1 * FROM Products
                                      ORDER BY NEWID()

                                   However, be careful when using this technique with large tables. You're only asking for one random row, but the execution
                                   plan shows that SQL Server gives you that random row only after reading each row in the table, generating a GUID for
                                   each row, then sorting all the rows. Consequently, SQL Server needs several seconds to give you a random row from a
                                   table with a few million rows. So don't use the "SELECT TOP 1?" technique on huge tables. Instead, restrict the random
                                   selection to a subset of the large table. Select a random range, then use "SELECT TOP 1?" within that range.

                                   4) Increase Default Packet Size for Large Data Fields
                                   SQL Server client applications communicate with instances of SQL Server through Net-Libraries. Client Net-Libraries and
                                   Server Net-Libraries communicate over the network by exchanging network packets. The size of network packets depends
                                   on the data access API you're using. The default packet size is 4,096 bytes in ADO and 8,192 bytes in ADO.NET.

                                   These sizes work well in most scenarios, but sometimes you can improve data exchange velocity greatly by increasing
                                   packet size—especially if you're sending or receiving large amounts of XML data, or if you're storing images in SQL Server.
                                   The client and server libraries must exchange and process fewer packets when you increase packet size. Maximum packet
                                   size is 32,767 bytes. You can increase packet size in ADO or ADO.NET simply by including the Packet Size property in the
                                   connection string:
                                   "?;Packet Size=32767;?"

                                   The speed gained in data upload and download depends on the size of your data fields and on your network topology. One
                                   of my applications stores XML data in SQL Server, with each XML field about 500K in size. My benchmarks show that the
                                   application exchanged XML data with SQL Server about twice as fast after increasing the packet size to 32,767 bytes.


                                                                                      1    2    3    NEXT »



                                                                          PRINTABLE FORMAT             E-MAIL THIS PAGE




                                    READER COMMENTS:




http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx[08/29/2012 4:29:12 PM]
Speed Up SQL Server Apps -- Visual Studio Magazine


                                              Add Your Comments Now:
                                              Your Name: (optional)



                                              Your Email: (optional)



                                              Your Location: (optional)



                                              Comment:




                                              Please type the letters/numbers you see above




                                                                          Submit




                                                                                        Sponsored Links:

                                       Need to Clean Addresses or other Contact Data?
                                       Get the developer tools that make it easy; download a free trial.

                                       Have greater confidence in your barcode recognition
                                       Integrate 1D and 2D barcode recognition with Barcode Xpress v8

                                       dtSearch® Instantly Search Terabytes� for multiple file & data types
                                       25+ search options; 64-bit APIs; full eval; "lightening fast"-Red. Mag.




http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx[08/29/2012 4:29:12 PM]

Speed up sql server apps - visual studio magazine

  • 1.
    Speed Up SQLServer Apps -- Visual Studio Magazine More Redmond Developer Network Sites >>> RedDevNews.com | ADTmag.com | Events Redmond Report Redmond IT Redmond Partner Redmond Developer Visual Studio MCPmag.com Virtualization Review Enter Search Term or FindIT Code Search Advanced Search HOME SUBSCRIBE/RENEW ADVERTISE FREE NEWSLETTERS ABOUT US TECH LIBRARY EVENTS RSS FEEDS Hot Topics like http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx AVo3oxuK Most Popular Articles Visual Studio 2010 Tweet 0 Like 5 people like this. Add a comment4 people like this. Sign Up to see what NEW! Visual Studio 4 Must-Know 2012/Win 8 5 NEW! HTML Visual Studio Database Design Keyboard ALM Agile/Scrum Speed Up SQL Server Apps Shortcuts Animating SQL Server and SDS Learn 10 useful tips and tricks that help boost SQL Server application performance. Windows Phone Web Services Listbox Items .NET Framework By Roman Rehak 07/01/2004 Free eBooks for SharePoint Data Developers Technology Toolbox: SQL Server 2000, ADO, ADO.NET XML/XAML Improved Developers love database programming tips, tricks, and workarounds—especially those that slash development time or Combinations with C# boost application performance. Here's a collection of such nuggets for developing SQL Server applications. the BigInteger VB.NET Data Type 1) Replace COUNT(*) With EXISTS When Checking for Existence Resources Develop Faster Developers often use the value from the COUNT(*) function when enforcing business rules in Transact-SQL code. 2010 Buyers Guide with Customized However, try the EXIST clause instead if you're using the COUNT(*) value only to evaluate whether you have at least one Visual Studio News row that meets certain conditions. For example, consider this code from the Northwind database: Templates Blogs In-Depth Code Columns Product Reviews Tech Library Sponsored Webcasts Subscribe/Renew Visual Studio Live! About Us Sitemap RSS Feeds IF (SELECT COUNT(*) FROM Orders WHERE ShipVia = 3) > 0 PRINT 'You cannot delete this shipper' The execution plan shows that SQL Server has to read all 255 rows in the Orders table before evaluating the IF expression. You can achieve the same result more efficiently with EXISTS because the IF condition evaluates to true as soon as SQL Server finds the first occurrence of 3 in the ShipVia column: IF EXISTS (SELECT * FROM Orders WHERE ShipVia = 3) PRINT 'You cannot delete this shipper' The difference in total execution time isn't much in a sample database such as Northwind, but use this efficient query against an Orders table with millions of rows and you'll see a major speed improvement. 2) Be Careful When Using WHERE IN and WHERE NOT IN SQL Server doesn't always choose an optimal execution plan when you have a substantial list of values in the WHERE IN clause. Using WHERE IN and WHERE NOT IN clauses in T-SQL code can produce an execution plan involving one or more nested loops. This increases the number of comparisons SQL Server must perform exponentially. Use the WHERE IN clause only if you have a short list of values you need to evaluate: USE Northwind --This query takes 9 ms to execute SELECT * FROM Customers http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx[08/29/2012 4:29:12 PM]
  • 2.
    Speed Up SQLServer Apps -- Visual Studio Magazine WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders) FREE WHITEPAPERS Replace the WHERE IN clause with OUTER JOIN if you're using a subquery to generate a potentially large list. Doing so Five Tips for Delivering can improve performance significantly: Working Software with Agile USE Northwind --This query takes 3 ms to execute 5 Ways Today’s Backup SELECT c.* and Restore FROM Customers c Technologies Can Put LEFT OUTER JOIN Orders o ON o.CustomerID = c.CustomerID Your Business on the WHERE o.CustomerID IS NULL Fast Track In this case, the second query uses LEFT OUTER JOIN, producing an execution plan that lets it run about three times Microsoft Uses LiteSpeed® for SQL faster than the first query. Server from Quest The LEFT OUTER JOIN selects all rows from the Customer table—whether or not a customer placed any orders—and Software to Eliminate the Need for Additional joins them with the Orders table. Then the WHERE clause filters out the rows where the columns from the Orders table Storage have NULL values. Either way, you get a list of customers who placed no orders, but the second way gives SQL Server a Top 6 LiteSpeed lot less work to do. I rewrote a query recently using this technique, and the execution time went from 50 seconds to about Features DBAs Should 500 ms. Know About 3) Randomize Resultset Orders With NewID() > MORE TECHLIBRARY You occasionally might need to randomize the order of the resultset retrieved from SQL Server. This is often the case in database searches where certain products or services would gain unfair advantage against others based simply on their name. I've seen a few clever (and not so clever) solutions for randomizing resultsets, but the solution is actually simple. You can use the NewID() function in Transact-SQL to generate a GUID for each row, then order the results by the generated GUID: SELECT * FROM Products ORDER BY NEWID() SQL Server returns products in a different order every time you run the query. You also can use this technique to return a random row from a table: SELECT TOP 1 * FROM Products ORDER BY NEWID() However, be careful when using this technique with large tables. You're only asking for one random row, but the execution plan shows that SQL Server gives you that random row only after reading each row in the table, generating a GUID for each row, then sorting all the rows. Consequently, SQL Server needs several seconds to give you a random row from a table with a few million rows. So don't use the "SELECT TOP 1?" technique on huge tables. Instead, restrict the random selection to a subset of the large table. Select a random range, then use "SELECT TOP 1?" within that range. 4) Increase Default Packet Size for Large Data Fields SQL Server client applications communicate with instances of SQL Server through Net-Libraries. Client Net-Libraries and Server Net-Libraries communicate over the network by exchanging network packets. The size of network packets depends on the data access API you're using. The default packet size is 4,096 bytes in ADO and 8,192 bytes in ADO.NET. These sizes work well in most scenarios, but sometimes you can improve data exchange velocity greatly by increasing packet size—especially if you're sending or receiving large amounts of XML data, or if you're storing images in SQL Server. The client and server libraries must exchange and process fewer packets when you increase packet size. Maximum packet size is 32,767 bytes. You can increase packet size in ADO or ADO.NET simply by including the Packet Size property in the connection string: "?;Packet Size=32767;?" The speed gained in data upload and download depends on the size of your data fields and on your network topology. One of my applications stores XML data in SQL Server, with each XML field about 500K in size. My benchmarks show that the application exchanged XML data with SQL Server about twice as fast after increasing the packet size to 32,767 bytes. 1 2 3 NEXT » PRINTABLE FORMAT E-MAIL THIS PAGE READER COMMENTS: http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx[08/29/2012 4:29:12 PM]
  • 3.
    Speed Up SQLServer Apps -- Visual Studio Magazine Add Your Comments Now: Your Name: (optional) Your Email: (optional) Your Location: (optional) Comment: Please type the letters/numbers you see above Submit Sponsored Links: Need to Clean Addresses or other Contact Data? Get the developer tools that make it easy; download a free trial. Have greater confidence in your barcode recognition Integrate 1D and 2D barcode recognition with Barcode Xpress v8 dtSearch® Instantly Search Terabytes� for multiple file & data types 25+ search options; 64-bit APIs; full eval; "lightening fast"-Red. Mag. http://visualstudiomagazine.com/articles/2004/07/01/speed-up-sql-server-apps.aspx[08/29/2012 4:29:12 PM]