Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Know your SQL Server - DMVs

959 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Know your SQL Server - DMVs

  1. 1. 23ª Reunião Lisboa - 24/09/2011 http://netponto.org Know your SQL Server - DMVs Vítor Pombeiro
  2. 2. Patrocinadores desta reunião
  3. 3. Patrocinadores desta reunião
  4. 4. Vítor PombeiroDeveloper no Banco Primus.Net 2.0 Web e Windows ApplicationsSQL Server 2000BI
  5. 5. Agenda• DMVs• CPU• Memória• Índices e Estatísticas• IO
  6. 6. DMVs• “Dynamic management views and functions return server state information that can be used to monitor the health of a server instance, diagnose problems, and tune performance.” (Books Online)
  7. 7. CPU
  8. 8. CPUselect scheduler_id, current_tasks_count, runnable_tasks_count -->Valor a verificarfrom sys.dm_os_schedulerswhere scheduler_id < 1048576
  9. 9. CPUSELECT TOP 10 (total_worker_time * 1.0) / 1000000 AS Total_Worker_Time_in_Seconds, execution_count, ((total_worker_time * 1.0) / 1000000) / execution_count AS Avg_Worker_Time_in_Seconds, (min_worker_time * 1.0) / 1000000 AS Min_Worker_Time_in_Seconds, (max_worker_time * 1.0) / 1000000 AS Max_Worker_Time_in_Seconds, textFROM sys.dm_exec_query_statsCROSS APPLY sys.dm_exec_sql_text(sql_handle)order by total_worker_time desc
  10. 10. CPUselect top 25 sql_text.text, plan_generation_num, execution_countfrom sys.dm_exec_query_stats across apply sys.dm_exec_sql_text(sql_handle) as sql_textwhere plan_generation_num >1order by plan_generation_num desc
  11. 11. CPUselect timestamp, convert(xml, record) as infofrom sys.dm_os_ring_bufferswhere ring_buffer_type = NRING_BUFFER_SCHEDULER_MONITORand record like %<SystemHealth>%
  12. 12. CPU• Causas de bottleneck no CPU – Estatísticas – “WITH RECOMPILE” nas SPs • OPTION (RECOMPILE) – Paralelismo OPTION (MAXDOP) hint
  13. 13. Memória
  14. 14. Memóriaselect text, query_plan, requested_memory_kb, granted_memory_kb, used_memory_kbfrom sys.dm_exec_query_memory_grants MGCROSS APPLY sys.dm_exec_sql_text(sql_handle)CROSS APPLY sys.dm_exec_query_plan(MG.plan_handle)
  15. 15. MemóriaSELECT Getdate() report_time, [cntr_value] as value_sec, cast(@@SERVERNAME as varchar) as ServerNameFROM sys.dm_os_performance_countersWHERE [object_name] LIKE %Manager% AND [counter_name] = Page life expectancy
  16. 16. Memória• Causas – Configuração do SQL Server – AWE inactivo (32 bits) – Nº sessões activas
  17. 17. Índices e estatísticas ROOT Intermediate Intermediate Leaf Leaf Leaf Leaf
  18. 18. Índices e estatísticasselect OBJECT_NAME(ps.object_id) as TableName ,si.Name ,ps.Avg_Fragmentation_in_percent ,STATS_DATE(ss.object_id,ss.stats_id) as LastUpdatedStatisticsfrom sys.dm_db_index_physical_stats(DB_ID(DB_NAME()),null,null,null,null) psjoin sysindexes si on ps.object_id = si.id and ps.index_id = si.indidleft outer join sys.stats ss on ss.object_id = ps.object_id and ss.name = si.namewhere ps.avg_fragmentation_in_percent > 10order by ps.avg_fragmentation_in_percent desc
  19. 19. Índices e estatísticasSELECT TOP 10 [Total Cost] = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0) , avg_user_impact , statement as TableName , equality_columns , inequality_columns , included_columnsFROM sys.dm_db_missing_index_groups gINNER JOIN sys.dm_db_missing_index_group_stats s ON s.group_handle = g.index_group_handleINNER JOIN sys.dm_db_missing_index_details d ON d.index_handle = g.index_handleORDER BY [Total Cost] DESC;
  20. 20. Índices e estatísticasSELECT TOP 10 DB_NAME() as DatabaseName , OBJECT_NAME(s.[object_id]) as TableName , IndexName = i.name, user_updates, system_updatesFROM sys.dm_db_index_usage_stats sINNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_idWHERE s.database_id = DB_ID() AND OBJECTPROPERTY(s.[object_id], IsMsShipped) = 0 AND user_seeks = 0 AND user_scans = 0 AND user_lookups = 0 AND i.name IS NOT NULL -- Ignore HEAP indexes.ORDER BY user_updates DESC
  21. 21. Índices e estatísticas• Causas – Muitos inserts, updates e deletes – Plano manutenção ineficaz – Excesso ou falta de índices – Shrink
  22. 22. IO
  23. 23. IOSELECT DB_NAME(fs.database_id) AS [Database Name], mf.physical_name, io_stall_read_ms, num_of_reads, CAST(io_stall_read_ms/(1.0 + num_of_reads) AS NUMERIC(10,1)) AS [avg_read_stall_ms],io_stall_write_ms, num_of_writes,CAST(io_stall_write_ms/(1.0+num_of_writes) AS NUMERIC(10,1)) AS [avg_write_stall_ms], io_stall_read_ms + io_stall_write_ms AS [io_stalls], num_of_reads + num_of_writes AS [total_io], CAST((io_stall_read_ms + io_stall_write_ms)/(1.0 + num_of_reads + num_of_writes) AS NUMERIC(10,1)) AS [avg_io_stall_ms]FROM sys.dm_io_virtual_file_stats(null,null) AS fsINNER JOIN sys.master_files AS mf ON fs.database_id = mf.database_id AND fs.[file_id] = mf.[file_id]ORDER BY avg_io_stall_ms DESC OPTION (RECOMPILE);
  24. 24. IOSelect wait_type, waiting_tasks_count, wait_time_ms / 1000 as wait_time_in_secfrom sys.dm_os_wait_statswhere wait_type like PAGEIOLATCH%order by wait_type
  25. 25. IO• tempDB• Índices• Recolha excessiva de dados• Base de dados com problemas no schema• Pouca memória
  26. 26. Questões?
  27. 27. ReferênciasBooks OnLine – http://msdn.microsoft.com/en-us/library/ms188754.aspxSQL Server DMV Starter Pack – http://www.red-gate.com/products/dba/sql-monitor/entrypage/dmvGlenn Berry – http://www.sqlservercentral.com/blogs/glennberry/archive/tags/DMV/defau lt.aspx – http://sqlserverperformance.wordpress.com
  28. 28. Patrocinadores desta reunião
  29. 29. Patrocinadores desta reunião
  30. 30. Próximas reuniões presenciais• 24/09/2011 – Setembro (2 anos!)• 29/10/2011 – Outubro• 19/11/2011 – Outubro (Coimbra)• 26/11/2011 – Novembro Reserva estes dias na agenda! :)
  31. 31. Obrigado!Vítor Pombeirovitor.pombeiro@hotmail.comvitor.pombeiro@gmail.comhttp://twitter.com/creative_byte

×