Optimizing Data Accessin Sq Lserver2005


Published on

Data access optimization techniques in SQL server

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Optimizing Data Accessin Sq Lserver2005

  1. 1. Presented By : M.M.Al-Farooque http://www.linkedin.com/in/thisisshubho (Shubho)
  2. 2. Topics <ul><li>Handling Index fragmentation </li></ul><ul><li>Optimizing files and partitioning tables </li></ul><ul><li>Effective use of SQL Profiler </li></ul><ul><li>Performance Monitor </li></ul><ul><li>Performance troubleshooting methodology </li></ul><ul><li>10 steps for performance optimization </li></ul>
  3. 3. Handling Index Fragmentations <ul><li>What is Index Fragmentation? </li></ul><ul><li>Fragmentation occurs in index/data pages as a result of data insert/update/delete operations in the tables. If indexes have high fragmentations, either scanning/seeking the indexes takes much time or the indexes are not used at all (Resulting in table scan). </li></ul><ul><li>Two types fragmentation can occur: </li></ul><ul><li>Internal Fragmentation: Occurs due to the data deletion/update operation in the index pages which ends up in distribution of data as sparse matrix in the index/data pages. Also results in increase of index/data pages that increase query execution time. </li></ul><ul><li>External Fragmentation: Occurs due to the data insert/update operation in the index/data pages which ends up in page splitting and allocation of new index/data pages that are not contiguous. That reduces performance in determining range and read-ahead operations. </li></ul>
  4. 4. Handling Index Fragmentations <ul><li>How to determine whether index fragmentation occurred or not? </li></ul><ul><li>Execute the following SQL: </li></ul><ul><li>select object_name(dt.object_id) Tablename,si.name IndexName,dt.avg_fragmentation_in_percent as ExternalFragmentation,dt.avg_page_space_used_in_percent as InternalFragmentation </li></ul><ul><li>from </li></ul><ul><li>( </li></ul><ul><li>select object_id,index_id,avg_fragmentation_in_percent,avg_page_space_used_in_percent </li></ul><ul><li>from sys.dm_db_index_physical_stats (db_id('AdventureWorks'),null,null,null,'DETAILED' </li></ul><ul><li>) </li></ul><ul><li>where index_id <> 0) as dt inner join sys.indexes si on si.object_id=dt.object_id </li></ul><ul><li>and si.index_id=dt.index_id and dt.avg_fragmentation_in_percent>10 </li></ul><ul><li>and dt.avg_page_space_used_in_percent<75 order by avg_fragmentation_in_percent desc </li></ul><ul><li>ExternalFragmentation value > 10 indicates External fragmentation occurred for corresponding index </li></ul><ul><li>InternalFragmentation value < 75 indicates Internal fragmentation occurred for corresponding index </li></ul>
  5. 5. Handling Index Fragmentations <ul><li>Defragment index fragmentation </li></ul><ul><li>Use “ALTER INDEX ALL ON TableName RECOGNIZE” when </li></ul><ul><li>External Fragmentation Between 10-15 and </li></ul><ul><li>Internal Fragmentation between 60-75 </li></ul><ul><li>Use “ALTER INDEX ALL ON TableName REBUILD WITH (FILLFACTOR=90,ONLINE=ON)” when </li></ul><ul><li>External Fragmentation >15 and </li></ul><ul><li>Internal Fragmentation < 60 </li></ul>
  6. 6. Optimizing files and partitioning tables <ul><li>Consider creating a &quot;User defined file group&quot; for your database if total database size is over 100 MB. Make this as &quot;Default&quot; file group. </li></ul><ul><li>Consider creating multiple files per &quot;User defined file group&quot; so that, SQL server can concurrently access those files while retrieving data </li></ul><ul><li>Place heavily accessed big fat tables in one filegroup and place the table's indexes and/or text/image columns in a different filegroup on different physical disks. </li></ul><ul><li>Because logging is more write-intensive, place the log files on different physical disk(s) than data files. </li></ul><ul><li>Consider assigning the &quot;Read only&quot; tables into a file group that is marked as &quot;Read Only&quot; </li></ul><ul><li>Consider creating &quot;History tables&quot; that contains archived read only data where select operations will be applied. Use SQL server Maintanence feature to populate those history tables. </li></ul><ul><li>Consider partitioning big fat tables into different file groups so that the table spans across different files and SQL server engine can concurrently access the table data. </li></ul>
  7. 7. Partitioning tables <ul><li>Table partitioning </li></ul><ul><li>Partitioning lets us split a table (And,Indexes) across multiple filegroups, based upon a user specification. </li></ul><ul><li>Partitioning enables database engine to read/write data, and, also to calculate (Say, calculating aggregate functions() in parallel) data operations faster. </li></ul><ul><li>Data could be transferred for analysis purpose based upon age, so, data volume for OLTP operation remains minimal. </li></ul><ul><li>Partitioning enables easy/manageable/faster way of archive/backup/recovery </li></ul>
  8. 8. Partitioning tables <ul><li>Following tasks are to be performed to partition a table or index </li></ul><ul><li>1. Add user defined file groups to the database </li></ul><ul><li>2. Create partition function </li></ul><ul><li>3. Create a partition schema </li></ul><ul><li>4. Create/Modify the table or index on the partition schema. </li></ul>
  9. 9. Partitioning tables <ul><li>1. Add user defined file groups to the database </li></ul><ul><li>ALTER DATABASE AdventureWorks ADD FILEGROUP [2003Q3] </li></ul><ul><li>ALTER DATABASE AdventureWorks ADD FILE (NAME = N'2003Q3', FILENAME = N'C:AdventureWorks2003Q3.ndf', SIZE = 5MB, MAXSIZE = 100MB, FILEGROWTH = 5MB) TO FILEGROUP [2003Q3] </li></ul>
  10. 10. Partitioning tables <ul><li>2. Create Partition function </li></ul><ul><li>A partition function is an object that defines the boundary points for partitioning data. </li></ul><ul><li>Following Command creates a partition function </li></ul><ul><li>CREATE PARTITION FUNCTION OrderDateRangePFN(datetime) AS RANGE LEFT FOR VALUES ('20000930 23:59:59.997', '20001231 23:59:59.997', '20010331 23:59:59.997', '20010630 23:59:59.997') </li></ul>
  11. 11. Partitioning tables <ul><li>3. Create Partition Scheme </li></ul><ul><li>The partition scheme defines the file groups that will be used with a partition function to partition the table/index </li></ul><ul><li>Following command creates a partition schema </li></ul><ul><li>CREATE PARTITION SCHEME OrderDatePScheme AS PARTITION OrderDateRangePFN TO ([2000Q3], [2000Q4], [2001Q1], [2001Q2], [PRIMARY]) </li></ul>
  12. 12. Partitioning tables <ul><li>4. Create the table or index on the partition schema. </li></ul><ul><li>With the partition function (the logical structure) and the partition scheme (the physical structure) defined, the table can be created to take advantage of them. The table defines which scheme should be used, and the scheme defines the function </li></ul><ul><li>ALTER TABLE Orders </li></ul><ul><li>ADD CONSTRAINT OrdersPK </li></ul><ul><li>PRIMARY KEY CLUSTERED </li></ul><ul><li>ON OrderDatePScheme (OrderDate) </li></ul>
  13. 13. Partitioning tables <ul><li>Partitioning Index and indexed views </li></ul><ul><li>Following command creates a partitioned index </li></ul><ul><li>CREATE NONCLUSTERED INDEX indxCustomerAddress on dbo.CustomerAddress(City) on partscheme(CustomerAddressID) </li></ul><ul><li>In case of an existing index, drop the index first </li></ul><ul><li>Partitioning indexed view means partitioning the index of the view </li></ul>
  14. 14. Effective use of SQL Profiler <ul><li>Tips & Tricks </li></ul><ul><li>1. Use existing templates, but, create your own template when in need. </li></ul><ul><li>2. Save profiler trace into table for further analysis </li></ul><ul><li>3. Export particular events (Say, TSQLs) from profiler trace for particular analysis. </li></ul><ul><li>4. Create Replay trace and replay the trace on test server to diagnose problems. </li></ul><ul><li>5. Capture deadlock events on production and diagnose on test server. </li></ul><ul><li>6. Capture ShowPlan to include SQL plans in the profiler. </li></ul><ul><li>7. Capture Table Scan events in profiler. </li></ul>
  15. 15. Performance Monitor <ul><li>Profiler enables us to capture long running queries but, cannot provide the context to explain the reasons. (e.g. Same query takes 3 seconds in production but, takes 30 ms in test server, cause, it lacks context). </li></ul><ul><li>Performance Monitor tool (System monitor) could be used to generate a counter log that can be used to provide the profiler trace with the context. </li></ul><ul><li>Performance Monitor gathers statistical data related to hardware and software metrics. </li></ul>
  16. 16. Performance Monitor <ul><li>Windows has lots of built in objects with their performance counters. </li></ul><ul><li>When installation, Performance counters for SQL server also get installed. </li></ul><ul><li>Create a Performance Counter log by incorporating the following common performance counters: (Next slide) </li></ul>
  17. 17. Performance Monitor <ul><li>Include the following common counters </li></ul><ul><li>Network InterfaceOutput Queue length </li></ul><ul><li>ProcessorProcessor Time </li></ul><ul><li>SQL Server:Buffer ManagerBuffer Cache Hit Ratio </li></ul><ul><li>SQL Server:Buffer ManagerPage Life Expectancy </li></ul><ul><li>SQL Server:SQL StatisticsBatch Requests/Sec </li></ul><ul><li>SQL Server:SQL StatisticsSQL Compilations </li></ul><ul><li>SQL Server:SQL StatisticsSQL Re-compilations/Sec </li></ul>
  18. 18. Performance troubleshooting methodology <ul><li>SQL server dynamically generates different query plans based on: </li></ul><ul><li>--Volume of Data </li></ul><ul><li>--Statistics </li></ul><ul><li>--Index variation </li></ul><ul><li>--Parameter value in TSQL </li></ul><ul><li>--Load on server </li></ul><ul><li>So, when diagnosing any SQL performance related problem , a methodology should be followed to simulate the production environment in Test server. </li></ul>
  19. 19. Performance troubleshooting methodology <ul><li>Performance troubleshoot methodology </li></ul><ul><li>--Capture trace at production server and include ShowPlan in the trace. Do the same in the Test server and compare the plans. </li></ul><ul><li>--Create Replay Trace in production and replay the trace on test server to create a similar load. </li></ul><ul><li>--In the Database Engine Tuning Advisor too, use the production trace as the workload to view tuning suggestions. </li></ul>
  20. 20. Performance troubleshooting methodology <ul><li>--Take performance counter log in the production server and correlate it with the SQL profiler trace to diagnose the bottleneck. </li></ul><ul><li>--Analyze the Query plans in the production SQL profiler trace to diagnose whether indexes are properly utilized in the SQL’s </li></ul><ul><li>--Execute the TSQL (Provider earlier) to view the External/Internal fragmentation values for indexes in the target database. </li></ul>
  21. 21. 10 steps for performance optimization <ul><li>1. Make sure that, indexing is properly done in the database (All tables has primary keys, and, tables has appropriate non-clustered indexes on columns which </li></ul><ul><li>  </li></ul><ul><li>-Are used as search criteria fields </li></ul><ul><li>-Are used to join other tables </li></ul><ul><li>-Are used as foreign key fields </li></ul><ul><li>-Have low selectivity </li></ul><ul><li>-Are used in the ORDER BY clause </li></ul><ul><li>While creating indexes, create appropriate &quot;Covering indexes&quot; involving frequently accessed fields </li></ul><ul><li>Use &quot;SQL server management studio&quot; to view the query execution plan and identify the scopes for improvement in the SQLs </li></ul><ul><li>Use &quot;SQL server tuning advisor&quot; to get help from SQL server in creating indexes. </li></ul>
  22. 22. 10 steps for performance optimization <ul><li>  </li></ul><ul><li>2. Consider moving your SQL's from application to Stored Procedures/Views if there is any </li></ul><ul><li>  </li></ul><ul><li>3. Re-factor the SQL's used in Stored procedures/Views/Triggers and apply the best practices to write optimized queries </li></ul><ul><li>4. Consider using the &quot;Full text search&quot; feature to perform search on textual columns. </li></ul><ul><li>5. Re-build indexes if fragmentation occurs. </li></ul><ul><li>6. Create &quot;Indexed Views&quot; with expensive select queries that internally saves result sets on queries </li></ul><ul><li>  </li></ul><ul><li>7. Consider refactoring your table design and apply De-normalizations to improve select operations. </li></ul>
  23. 23. 10 steps for performance optimization <ul><li>8. Consider creating different &quot;User defined file groups&quot; and use these based upon </li></ul><ul><li>-Frequency of table access </li></ul><ul><li>-Read/write nature of tables </li></ul><ul><li>-Types of objects (Tables/Index) </li></ul><ul><li>  </li></ul><ul><li>9. Consider creating &quot;History tables&quot; that contains archived read only data where select operations will be applied. Use SQL server Maintenance feature to populate those history tables. </li></ul><ul><li>  </li></ul><ul><li>10. Consider partitioning big fat tables into different file groups so that the table spans across different files and SQL server engine can concurrently access the table data. </li></ul>