growled on Friday, April 22, 2005 4:15:06 PM (Pacific Standard Time, UTC-08:00)
barked at coding

As I said in a previous post, I spent the past 3 days in class with Kimberly Tripp (from SQLSkills.com) teaching us performance tuning & optimization for SQL 2000.  Here are some of the key points I took away from the class.  This list is an abridged version of my notes and far from encompassing all of the topics.  If you ever get the opportunity to take a SQL class from Kimberly I highly recommend you do so!

 

Key Take-Aways:

  1. Do NOT use "Select *" queries!
    1. This tells the query optimizer to not even bother using any of the indexes and goes straight to a table scan to gather the information.
    2. Use "narrow" queries (fewer select columns) wherever possible to give the optimizer more options!
  2. Use stored procedures whenever possible and reduce ad-hoc query usage!
    1. This way you can control the dataset being queried and returned which allows you to tune your indexes accordingly.
    2. Also allows for better security:
      1. Give INSERT, DELETE, UPDATE access to the stored proc instead of to individuals.
      2. Require an input parameter(s) for the SPs so that only a specific row/column can be UPDATED or DELETED preventing accidents that might occur in ad-hoc queries.
  3. Do NOT name stored procedures to start with "sp_" because that is a special identifier used by SQL to denote System Procedures.  When a SP starting with "sp_" is called SQL looks for it in the Master database first instead of the current database which causes a hit to performance.
  4. Indexes - Use fewer, wider indexes tuned to your biggest queries (SPs) instead of many, narrow indexes.
    1. By adding non-clustered indexes with a few columns that are used extensively by SPs/queries you can significantly reduce the time for the queries.
    2. Narrow indexes must still hit the clustered index or a table scan in order to reference additional columns.
    3. Example:  For a query on Customer Fname & Lname you can create a NC-index on Fname & Lname instead of a NC-index on only Fname.
  5. Non-Clustered indexes are always sorted by the order of the columns from left-to-right and in ascending order.
    1. Keep this in mind if you have SPs that query for each of the two columns, but in different orders (i.e.: Fname & Lname).
  6. Use the Index Tuning Wizard (ITW) for suggested index improvements after you create indexes on your PKs, FKs, UKs and for your large/frequently used SPs.
  7. Create and tune your NC indexes for your expensive and frequently used SPs/queries. 
  8. Regular index maintenance is a must (defrag & rebuild)!
    1. Use DBCC SHOW_STATISTICS (tablename, indexname) to find out the current state of the index and the last time it was rebuilt.
  9. Do NOT use Simple Recovery!
    1. ...unless you don't care about losing data since your last full backup! 
    2. Use Full Recovery with transaction log backups to retain all current data.
    3. If disk space is an issue then set the transaction log backups to only be retained for a specified period of time then deleted.
  10. Rebuild tables when they become highly fragmented or have low scan density (lots of empty space on leaf pages).
    1. Use DBCC SHOWCONTIG (tablename) to find out this information.
    2. To do this programatically you can use DBCC SHOWCONTIG (tablename) WITH TABLERESULTS to put the scan results into a table for further processing.
 
Comments Comments [0]  

Related posts:
Expect it NOT to work
software development == football
Playing around with CSS a bit
You might have a legacy enterprise application if...
C# or VB.NET isn't really the question
how2: Generate an XML Schema file (.xsd) automatically
Tracked by:
"http://www.google.com/search?q=ouwkcajk" (http://www.google.com/search?q=ouwkca... [Pingback]
"http://www.pharmiweb.com/PressReleases/pressrel.asp?ROW_ID=3268" (http://www.ph... [Pingback]
"http://www.crt.state.la.us/ltgovernor/media_view.aspx?id=276" (http://www.crt.s... [Pingback]
"http://www.jugend.essen.de/module/bands/BandMaske.asp?BID=701016" (http://www.j... [Pingback]
"http://archive.slamdance.com/2006/festival/film_detail.asp?film_id=1327" (http:... [Pingback]
"http://www.mysitespace.com/franchise_opportunities/franchisedetails.asp?FID=139... [Pingback]
"http://ertr.tamu.edu/conferenceabstracts.cfm?abstractid=3006" (http://ertr.tamu... [Pingback]
"http://www.google.com/search?q=piwthpzc" (http://www.google.com/search?q=piwthp... [Pingback]
"http://www.google.com/search?q=rorbzhru" (http://www.google.com/search?q=rorbzh... [Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=181" (http://clutchfans.net/feature... [Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=192" (http://clutchfans.net/feature... [Pingback]
"http://www.seriea.tv/NewsDetails.asp?articolo=963" (http://www.seriea.tv/NewsDe... [Pingback]
"http://www.landandfarm.com/lf/news/display_article.asp?cid=731" (http://www.lan... [Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=173" (http://clutchfans.net/feature... [Pingback]
"http://www.seriea.tv/NewsDetails.asp?articolo=984" (http://www.seriea.tv/NewsDe... [Pingback]
"http://www.csp.state.co.us/press_releases.cfm?Article_ID=310" (http://www.csp.s... [Pingback]
"http://www.landandfarm.com/lf/news/display_article.asp?cid=737" (http://www.lan... [Pingback]
"http://www.southafrica.net/satourism/media/newsreleasedetail.cfm?NewsReleaseID=... [Pingback]
"http://www.southafrica.net/satourism/media/newsreleasedetail.cfm?NewsReleaseID=... [Pingback]
"http://www.anaheimoc.org/press/pressNews.asp?pid=124" (http://www.anaheimoc.org... [Pingback]
"http://www.edinburgh.gov.uk/environmentaldirectory/orgDetail.jsp?id=164" (http:... [Pingback]
"http://www.epiphone.com/news.asp?NewsID=1096" (http://www.epiphone.com/news.asp... [Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7603" (http://www.... [Pingback]
"http://www.edinburgh.gov.uk/environmentaldirectory/orgDetail.jsp?id=179" (http:... [Pingback]
"http://www.se.pg.com/view.aspx?id=211" (http://www.se.pg.com/view.aspx?id=211) [Pingback]
"http://www.epiphone.com/news.asp?NewsID=1054" (http://www.epiphone.com/news.asp... [Pingback]
"http://www.anaheimoc.org/press/pressNews.asp?pid=131" (http://www.anaheimoc.org... [Pingback]
"http://www.se.pg.com/view.aspx?id=193" (http://www.se.pg.com/view.aspx?id=193) [Pingback]
"http://www.se.pg.com/view.aspx?id=216" (http://www.se.pg.com/view.aspx?id=216) [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2091" (http://ww... [Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=205" (http://www.myanmarmp3.net/... [Pingback]
"http://asp1.umbc.edu/newmedia/studio/stream/qtdetail.cfm?recordID=557" (http://... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=211" (http://www.myanmarmp3.net/... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=212" (http://www.myanmarmp3.net/... [Pingback]
"http://www.yuwie.com/clubs/club.asp?id=27618" (http://www.yuwie.com/clubs/club.... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32632" (http://www.colle... [Pingback]
"http://asp1.umbc.edu/newmedia/studio/stream/qtdetail.cfm?recordID=562" (http://... [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2083" (http://ww... [Pingback]
"http://asp1.umbc.edu/newmedia/studio/stream/qtdetail.cfm?recordID=578" (http://... [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2089" (http://ww... [Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent... [Pingback]
"http://www.yuwie.com/clubs/club.asp?id=27611" (http://www.yuwie.com/clubs/club.... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32639" (http://www.colle... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32653" (http://www.colle... [Pingback]
"http://realvideopornoo.com" (http://realvideopornoo.com) [Pingback]
"http://yesihavemoneyy.com" (http://yesihavemoneyy.com) [Pingback]
Comments are closed.