Speed up sql server database queries with these tips tech republic

560 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
560
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Speed up sql server database queries with these tips tech republic

  1. 1. Speed up SQL Server database queries with these tips | TechRepublic ZDNet Asia SmartPlanet TechRepublic Log In Join TechRepublic FAQ Go Pro! Blogs Downloads Newsletters Galleries Q&A Discussions News Research Library IT Management Development IT Support Data Center Networks Security Search Home / Blogs / The Enterprise Cloud Follow this blog: The Enterprise Cloud s Speed up SQL Server database queries with these tips By Tim Chapman September 10, 2007, 11:06 PM PDT Takeaway: If you’ve worked with SQL Server databases for any amount of time, it’s practically a given that you’ve run into slow running queries. Sometimes the reason for the slow response time is due to the load on the system, and other times it is because the query is not written to perform as efficiently as possible (I find this reason much more common). Learn how you may be able to improve performance by knowing what to look for in your query designs. Improve Productivity If you’ve worked with SQL Server databases for any amount of time, it’s practically a given that Apps For Business- 30 Day Free Trial. No you’ve run into slow running queries. Sometimes the reason for the slow response time is due to Credit Card Required! the load on the system, and other times it is because the query is not written to perform as www.google.com/apps efficiently as possible (I find this reason much more common). Learn how you may be able to Dynamics in Romania improve performance by knowing what to look for in your query designs. Microsoft Dynamics NAV Microsoft Dynamics AX www.llpdynamics.ro Relational Database Data Operate on small sets Automatic RDB Data Logging Enterprise The fewer rows you are dealing with, the quicker you will find what you want. If the query Transaction Modules optimizer sees that the number of rows in the table is sufficiently small, no indexes on that table www.OLDI.com will be used. The optimizer will realize that it is faster to scan the rows in the table to satisfy the request rather than traverse an index structure to find the rows. Limit the columns returned Keep Up with TechRepublic A CNET PROFESSIONAL BRAND On TechRepublic: Does anyone want Linux.conf.au 2014? When returning data in your applications, the less data that is returned, the faster the information is transmitted over the network — this goes for the amount of rows returned, as well as the number of columns returned. This is why I am against using the SELECT * statement, especially Your Email in a production environment. In my experience with database administration and development, I Five Apps have seen very few times that have warranted using a SELECT * statement. The reason for this is twofold. It doesn’t make sense to bring back columns that you are not going to be using. The Google in the Enterprise second reason (which I feel is more important) is that using SELECT * can break existing code. Consider the following example. Subscribe Today I have an INSERT statement in my production environment. I use a SELECT * statement as a data source in my INSERT statement. This isn’t a big deal because my SourceTable has the same Follow us however you choose! number of columns in it as the DestinationTable. SELECT INTO DestinationTable (Fld1, Fld2, Fld3) SELECT *http://www.techrepublic.com/blog/datacenter/speed-up-sql-server-database-queries-with-these-tips/173[08/29/2012 11:54:58 AM]
  2. 2. Speed up SQL Server database queries with these tips | TechRepublic FROM SourceTable. A business situation arises in which I need to add a field to my SourceTable table. Media Gallery ALTER TABLE SourceTable ADD Fld4 INT Adding this new field will break my INSERT statement, which will cause problems in my production environment. Searching for rows PHOTO GALLERY (1 of 15) The manner in which rows are searched for in a database table will always be one of the more Curiositys autonomous seven minutes of... vital implementations in your database environment. The SQL Server query optimizer will operate much more efficiently for some WHERE statements as compared to other WHERE statements More Galleries » based upon how the statement is written even if the outcome of the statements is the same. The following example uses the IN() statement to specify a series of values being searched for. For this example, assume the OrderID column as a NonClustered index. SELECT * FROM ProductSales WHERE OrderID IN(4494, 4495, 4496) VIDEO (1 of 13) This statement is exaactly the same as using an OR operator to specify the three values being Cracking Open: HTC Titan II searched for. Either statement will cause SQL Server not to use the index on the field and to cycle through the rows in the table searching for the values. Since the values used in the example are More Videos » contiguous, I can use the BETWEEN operator instead. This will allow the query optimizer to effectively use the index. SELECT * FROM ProductSales Hot Questions View All WHERE OrderID BETWEEN 4494 AND 4496 In general, most types of exclusion statements in your WHERE clause will cause SQL Server to 3 SSL redirection not be able to use an index. The following are some additional examples: , !, OR, NOT IN, NOT EXISTS How to Remove the Disk is Write 10 The manner in which LIKE statements are used also makes a difference. If you are able to specify Protected error for USB pendrive the beginning character(s) of the statement you are searching for, you will have a better chance of your statement using an index. If you specify a wildcard(%) before any type of search string, the 2 can anyone suggest if any such optimizer will be unable to use an index. software exist with similar functionality? SELECT * FROM Customers WHERE LastName LIKE %TR% Date searches 3 Switching from a Job to a career in the IT field: Need an IT pros Date searches are sometimes a little tricky to perform in the database; for instance, I have seen advice numerous situations where date functions are used for date searches. In the following example, the sales records are being retrieved from the SalesHistorry table for August 15, 2005: Ask a Question SELECT SaleID FROM SalesHistory WHERE MONTH(SaleDate) = 8 AND Hot Discussions View All YEAR(SaleDate) = 2005 AND DAY(SaleDate) = 15 218 Should developers be sued for The functions in the WHERE clause will cause SQL Server to perform the function on every row security holes? being searched, which means the index will not be used. This is why I discourage the returns values from functions to be used as criteria in queries. The following code shows how you can 67 The sitting duck that is open rewrite the statement so that an index is used, and the results are returned in a much quicker source fashion. SELECT SaleID Five fast Windows desktop search FROM SalesHistory 27 utilities WHERE SaleDate = 8/15/2005 AND SaleDate ‘8/16/2005 Five speedy Windows desktop 24 Next time search apps In my next column, I plan to look at some of the more advanced techniques that you can use to Start a Discussion optimize queries. Tim Chapman is a SQL Server database administrator who works for a bank in Louisville, KY, and has more than 7 years of IT experience. He is also Microsoft certified in SQL Server 2000 and White Papers, Webcasts, andhttp://www.techrepublic.com/blog/datacenter/speed-up-sql-server-database-queries-with-these-tips/173[08/29/2012 11:54:58 AM]
  3. 3. Speed up SQL Server database queries with these tips | TechRepublic SQL Server 2005. If you would like to contact Tim, please e-mail him at chapman.tim@gmail.com. Downloads —————————————————————————————– Get SQL tips in your inbox Webcasts TechRepublic’s free SQL Server newsletter, delivered each Tuesday, contains hands-on tips that Live Webcast: will help you become more adept with this powerful relational database management system. Succeeding in a World Gone Mobile Automatically subscribe today! The mobile revolution impacts every business and every individual, so its Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublic’s free imperative you understand SMS, MMS, newsletters. iOS and all the rest. How can location services help? Can apps save you time and money? Watch this webinar to explore a world gone mobile! From Citrix Online About Tim Chapman Whitepapers Full Bio Contact Solutions for Todays Smart Business From IBM Webcasts On-demand Webcast: Get from Wow to How with mobile workstyles and cloud services Use host-based replication to Five tips for working with From Citrix Systems, Inc keep data available Windows Server 2003 Group Whitepapers Policy Objects Information Assurance Education From Regis University 44 Join the conversation! Add Your Opinion Comments Follow via: Blog Archive Staff Picks Top Rated Most Recent My Contacts See All Comments August 2012 December 2011 July 2012 November 2011 June 2012 October 2011 implement a search engine using php 0 May 2012 September 2011 ravishakya 29th Mar 2011 Votes April 2012 August 2011 March 2012 July 2011 I believe you could use FullText indexing and search feature offered by most of the RDBMS. February 2012 June 2011 January 2012 View in thread RE: Speed up SQL Server database queries with these 0 tips Votes tesfayenator@... 10th Apr 2008 Retrieve the name of the employee whose firstname ends with ???E???.Don???t use the ???%??? wild character. if you want to contact me use this email tesfayenator@gmail.com View in thread Typos 0 jimmy_ho@... Updated - 13th Oct 2007 Votes In your article, you wrote: SELECT INTO DestinationTable (Fld1, Fld2, Fld3) SELECT * FROM SourceTable. Did you mean: INSERT INTO DestinationTable (Fld1, Fld2, Fld3) SELECT * FROM SourceTable. If that... Read Whole Comment + View in threadhttp://www.techrepublic.com/blog/datacenter/speed-up-sql-server-database-queries-with-these-tips/173[08/29/2012 11:54:58 AM]
  4. 4. Speed up SQL Server database queries with these tips | TechRepublic See all comments Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion. Join Login Search Trending Topics Server virtualization Microsoft Excel SECURITY business analyst SOFTWARE/WEB DEVELOPMENT router SOFTWARE database administration Linux anti-malware Help desk it consultant Operating systems Project management Featured TechRepublic Pro Downloads Explore Services Blogs About Us Downloads Membership Members Newsletters QA RSS Feeds DIscussions Site Map Windows 8: Pre- Quick Reference: IT Managers 500 Things You Training Site Help Release Primer Linux Commands Toolkit for Staying Need To Know To Store Feedback Legal Succeed In Your IT Research Library FAQ career Photos Advertise Videos Reprint Policy Popular on CBS sites: US Open | PGA Championship | iPad | Video Game Reviews | Cell Phones © 2012 CBS Interactive. All rights reserved. Privacy Policy | Ad Choice | Terms of Use | Advertise | Jobs A ZDNet Web Site | Visit other CBS Interactive Sites: CBS Cares CBS Cares Gohttp://www.techrepublic.com/blog/datacenter/speed-up-sql-server-database-queries-with-these-tips/173[08/29/2012 11:54:58 AM]

×