Your SlideShare is downloading. ×
Sql server performance tuning and optimization
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Sql server performance tuning and optimization

1,501
views

Published on

Sql server performance tuning and optimization …

Sql server performance tuning and optimization
SQL Server Concepts/Structure
Performance Measuring & Troubleshooting Tools
Locking
Performance Problem : CPU
Performance Problem : Memory
Performance Problem : I/O
Performance Problem : Blocking
Query Tuning
Indexing

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,501
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
75
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Pages & Extent: ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/ddc311fe-f3cc-4e99-b86b-43ce822ceda3.htm dbcc page (undocumented) 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo – sql Not only for monitoring but also for 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • --demo 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Locking types/modes: ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/108297fa-35fc-4cbe-a1ac-369aabd8cf44.htm Locking & compatibility: ms-help://MS.SQLCC.v9/MS.SQLMobile.v3.en/SSMMain3/html/fb7c1c79-e392-444f-873f-888ad0556631.htm Locking Hints: ms-help://MS.SQLCC.v9/MS.SQLMobile.v3.en/SSMMain3/html/7657c311-2121-4c45-8a36-4a6579384f24.htm Show sp_lock demo and not been able to delete database when different connection has put shared lock 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Running: worker is currently executing on the CPU Runnable: worker is currently executing waiting for its turn on the the CPU Suspended: worker is waiting on a resource (eg a lock) Demo : Perfmon 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo : SQL Queries 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo : show the perfmon counter and SQL demo for Latch 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo – sql How performance dashboard 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo - sql 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo : use profiler to identify slow running queries. Look for CPU, Reads column and Duration column DMV demo – missing Index, DMV 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo : 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Demo : 08/15/10 22:46 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • Transcript

    • 1. SQL Server Performance Tuning & Optimization By Manish Rawat http://www.linkedin.com/in/manishra
    • 2. Agenda
      • SQL Server Concepts/Structure
      • Performance Measuring & Troubleshooting Tools
      • Locking
      • Performance Problem : CPU
      • Performance Problem : Memory
      • Performance Problem : I/O
      • Performance Problem : Blocking
      • Query Tuning
      • Indexing
    • 3. Performance Focus
      • Response Time
        • Time interval when a request is submitted and when 1 st character of response is received.
      • Throughput
        • Number of transaction processed in fixed unit of time
      • Scalability
        • Throughput and response time changes as we add more hardware resources
    • 4. Data - Physical & Logical
      • mdf (data) and ndf (transaction) file
      • Pages (8KB)
        • Fundamental unit of data storage
        • Row size (8096 bytes) : SQL2000
        • Row size can be > 8096 :SQL2008
        • with varchar(max) data type
        • The disk space in database is logically divided into pages numbered contiguously from 0 to n ..
    • 5. What happens when you CRUD
      • Two places get affected
        • Memory: Data looked into Memory. If doesn’t exist, Page brought into Memory(RAM). Operation performed on Page in memory
        • Transaction Log: Entry in transaction log file (.ndf file)
      • No “instant” changes to mdf file (data file)
      • Latest Changes are in Memory and Transaction Log file
      • Backup transaction Log file in case of SQL failure
      • Checkpoint : Lazywriter write to data changes in memory to disk
    • 6. Performance Tools: No Extra cost
      • SQL Server Profiler
      • System Monitor (windows performance monitor)
      • Dynamic Management Views (DMV) : SQL 2005+
      • Database tuning advisor (DTA)
      • Microsoft Free tools (SQLDiag, PSSDiag, SQL Nexus – www.codeplex.com)
      • Performance Studio (SQL 2008)
    • 7. SQL Profiler
      • Graphical user interface to SQL Trace for monitoring
        • Events to Capture
        • Filters to capture only certain type of info
        • Information captured can be input to DTA
      • Watch for following Columns
        • Reads (represent I/O)
        • Duration (time in millisecond)
        • CPU (CPU consumed)
      • Great Integration with Perfmon
    • 8. Transaction Isolation Levels Level Definition Read Un-Committed Don’t need a lock to read a data Read Committed “ Default” Read only committed data otherwise wait. Request ‘S’ lock to read. No guarantee that read is repeatable Repeatable read Guarantees that data read in a transaction will not change for it duration. Holds the ‘S’ lock until the duration of transaction. Serializable Prevent phantoms In any of above transaction, an ‘X’ lock acquired is held for total duration of transaction.
    • 9. Locks
      • Lock types/modes
        • X (exclusive), S (shared) , U(Update), IX (Intent Exclusive), IS….. imposed on DB (database), RID (row id), PAG(page), TAB(table)
      • Locks Compatibility (matrix)
      • Lock Hints
        • Select * from employee with (Nolock)
        • More - Rowlock, HoldLock, Tablock…….
      • Locks Hierarchy
        • Database (DB) ->Table (TB) -> Page (PG)-> Row (key)
      Lock modes Shared(S) Exclusive (X) Shared (S) – READ LOCKS OK NO Exclusive(X) NO NO
    • 10. Performance Problem 1 : CPU
      • Detecting
        • Perfmon
          • % processer is 80% & above for 15 – 20 minutes.
          • System : Processer Queue length
        • The SQL process has set of workers threads .
          • Running: worker is currently executing on the CPU
          • Runnable: worker is currently executing waiting for its turn on the the CPU
          • Suspended: worker is waiting on a resource (eg a lock)
    • 11. Performance Problem 1 : CPU
      • Isolating & Troubleshooting
        • Inefficient Query Plan:
          • Top 10 queries that are taking most CPU
          • Beware of one time query VS. frequently executed ones
        • Excessive Compilation & Recompilation
          • http://support.microsoft.com/kb/308737
          • Schema change : Mix of DDL, DML
          • Set statements like ANSI_NULL, ARITHBORT etc in SP
          • Update Statistics
          • Look for
            • SQL Server: SQL Statistics Batch Request/sec
            • SQL Server: SQL Statistics Compilation/sec
            • SQL Server: SQL Statistics Recompilation/sec
    • 12. Performance Problem 2: Memory
      • Facts
        • By Default, SQL will consume all memory it can
      • Detecting
          • Memory: Available Bytes is LOW
          • SQL Server Buffer Manager: Buffer Cache Hit Ratio is < 90%
          • SQL Server Buffer Manager: Page Life Expectancy is LOW
          • SQL Server Buffer Manager: Checkpoint pages/sec is increasing
    • 13. Performance Problem 2 : Memory
      • Isolating & Troubleshooting
        • Identify how much is required
        • Add more
          • on 32 bit boxes, SQL Server process can address 2GB only or 3GB if /3GB option exist in boot.ini. Enable AWE option to 1 so SQL can see up to 64 GB. On 64 bit this is it’s not required.
          • More memory available: Use AWE option (0 or 1): sp_configure
    • 14. Performance Problem 3: I/O
      • Detecting
          • PhysicalDisk Object : Avg Disk Queue Length Average read and write Queued. It should not be consistently >2 /physical disk
          • PhysicalDisk Object : Avg Disk Sec/Read & Sec/Write Average time in read & write. It should not be consistently >20 ms
          • PhysicalDisk Object : Disk Reads/sec & Writes/sec Rate of read & write.. It should be < 85 % of disk capacity
          • Latch waits (DMV ) Physical I/O waits when page is addressed to read/write and not in Buffer pool
    • 15. Performance Problem 3 : I/O
      • Isolating & Troubleshooting
        • Use DMV to find out Queries with most I/O
          • Is it missing Index? (Most Common Reason)
          • Is it Poor query ? (Query tuning in later slides)
          • Disk read/write capacity peaking?/ Network peaking?/Incorrect hardware configuration?
    • 16. Performance Problem 4 : Blocking
      • Locking (locks) and Blocking (Resource blocks to ensure data integrity) are expected
        • You can only design to MINIMIZE it
      • Blocking Detection & Troubleshooting
        • Identity what’s getting Blocked and who is blocking it (sp_who/sp_who2 other tools)
        • Identity Wait Types by DMV
        • Master..sysprocesses system table
        • http://support.microsoft.com/kb/271509/en-us
        • Is nolock OK?
    • 17. Query Tuning
      • Detection
        • Profiler
          • Look for Queries/Stored Proc with High reads, CPU, & Duration. These are candidates of tuning.
          • Look for Stored proc that’s Recompiling (it’s an event)
        • DMV’s
          • Find Queries with missing indexes
          • Find tables that are defragmented
          • Find TempDB database bottlenecks
    • 18. Query Tuning cont.
      • Troubleshoot : Query Execution Plan
        • Operator types
          • Seek (Best and preferred)
          • Scan (not preferred)
          • Bookmark lookup (better than scan and mostly with non-clustered index)
        • Join type
          • Nested
          • Merge
          • Hash (Avoid)
        • Graphical Execution Plan Icons : http://msdn2.microsoft.com/en-us/library/ms175913.aspx
        • http://www.sql-server-performance.com/articles/per/select_indexes_p1.aspx
    • 19. Index Rules
      • Clustered Index
        • Choose wisely. Only one per table possible
        • Primary key by default is clustered. Evaluate default behaviour
      • Non-Clustered Index
        • More than one possible.
        • Foreign keys are always good candidate for non-clustered index
          • (because of joins)
      • Evaluate ‘Included Columns’ in Indexing. Every NonClustered index contains Clustered Keys
      • Choose Index Fill Factor wisely.
      • Find out tables with large rowcount but no indexing. May be it needs index.