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.

Mssql opt

1,386 views

Published on

  • Be the first to comment

  • Be the first to like this

Mssql opt

  1. 1. Использование Microsoft SQL Management Studio при оптимизации запросов.
  2. 2. • Microsoft Windows Server 2003 • Microsoft SQL Server 2008 • Microsoft SQL Server Management Studio
  3. 3. База данных
  4. 4. Количество записей • Customers – 10 тыс. • OrderItems – 500 тыс. • Products – 200 тыс. • Orders – 100 тыс.
  5. 5. Ожидаемый план выполнения Реальный план выполнения Статистика выполнения запроса
  6. 6. SELECT * FROM [Products] as p SQL Server Execution Times: CPU time = 204 ms, elapsed time = 12409 ms. SELECT p.[ID], p.[Name] FROM [Products] as p SQL Server Execution Times: CPU time = 141 ms, elapsed time = 3547 ms.
  7. 7. SELECT p.[ID], p.[Name] FROM [Products] as p
  8. 8. SELECT p.[ID], p.[Name] FROM [Products] as p
  9. 9. SELECT p.[ID], p.[Name] FROM [Products] as p
  10. 10. SELECT p.[Name], p.[Price] FROM [Products] AS p WHERE p.[Price] BETWEEN 10 and 100 SQL Server Execution Times: CPU time = 62 ms, elapsed time = 21 ms. (1794 row(s) affected)
  11. 11. Missing index /* Missing Index Details from SQLQuery2.sql The Query Processor estimates that implementing the following index could improve the query cost by 93.0671%. */ /* USE [demo] GO CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>] ON [dbo].[Products] ([Price]) INCLUDE ([Name]) GO */
  12. 12. Создаем индекс USE [demo] GO CREATE NONCLUSTERED INDEX [idx_price_name] ON [dbo].[Products] ([Price]) INCLUDE ([Name]) GO
  13. 13. SELECT p.[Name], p.[Price] FROM [Products] AS p WHERE p.[Price] BETWEEN 10 and 100 SQL Server Execution Times: CPU time = 0 ms, elapsed time = 13 ms. (1794 row(s) affected)
  14. 14. SELECT DISTINCT с.[ID], c.[FirstName], c.[LastName] FROM [OrderItems] AS oi LEFT JOIN [Orders] AS o ON o.[ID] = oi.[OrderId] LEFT JOIN [Customers] AS c ON o.[CustomerId] = c.[ID] WHERE oi.[ProductID] = 123
  15. 15. Ожидаемый план SQL Server Execution Times: CPU time = 47 ms, elapsed time = 45 ms.
  16. 16. Создаем индекс USE [demo] GO CREATE NONCLUSTERED INDEX [idx_ProductId] ON [dbo].[OrderItems] ([ProductId]) INCLUDE ([OrderId]) GO
  17. 17. Ожидаемый план SQL Server Execution Times: CPU time = 0 ms, elapsed time = 2 ms.
  18. 18. SELECT DISTINCT с.[ID], c.[FirstName], c.[LastName] FROM [OrderItems] AS oi LEFT JOIN [Orders] AS o ON o.[ID] = oi.[OrderId] LEFT JOIN [Customers] AS c ON o.[CustomerId] = c.[ID] WHERE oi.[ProductID] = 123 Без индекса по ProductId: C индексом: SQL Server Execution Times: CPU time = 47 ms, elapsed time = 45 ms. (3 row(s) affected) SQL Server Execution Times: CPU time = 0 ms, elapsed time = 2 ms. (3 row(s) affected)
  19. 19. SELECT DISTINCT c.[ID], c.[FirstName], c.[LastName] FROM [Customers] as c JOIN Orders as o on o.[CustomerId] = c.[ID] WHERE o.[Date] >= '12.01.2000' AND o.[Date]< '01.01.2001'
  20. 20. Ожидаемый план
  21. 21. /* Missing Index Details from SQLQuery5.sql The Query Processor estimates that implementing the following index could improve the query cost by 67.9358%. */ /* USE [demo] GO CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>] ON [dbo].[Orders] ([Date]) INCLUDE ([CustomerId]) GO */
  22. 22. USE [demo] GO CREATE NONCLUSTERED INDEX [idx_Date] ON [dbo].[Orders] ([Date]) INCLUDE ([CustomerId]) GO
  23. 23. С индексом: SQL Server Execution Times: CPU time = 0 ms, elapsed time = 13 ms. (843 row(s) affected) Без индекса: SQL Server Execution Times: CPU time = 16 ms, elapsed time = 81 ms. (843 row(s) affected)
  24. 24. SELECT DISTINCT p.[ID], p.[Name] FROM [Products] AS p JOIN [OrderItems] AS oi ON oi.[ProductId]=p.[ID] JOIN [Orders] as o on o.[ID] = oi.[OrderId] WHERE o.[Date] >= '12.01.2000' AND o.[Date]< '01.01.2001' AND p.Price BETWEEN 50 and 100
  25. 25. План выполнения SQL Server Execution Times: CPU time = 16 ms, elapsed time = 14 ms. (30 row(s) affected)
  26. 26. SELECT DISTINCT p.[ID], p.[Name] FROM [Products] AS p JOIN [OrderItems] AS oi ON oi.[ProductId]=p.[ID] JOIN [Orders] as o on o.[ID] = oi.[OrderId] WHERE o.[Date] >= '12.01.2000' AND o.[Date]< '01.01.2001'
  27. 27. План выполнения SQL Server Execution Times: CPU time = 329 ms, elapsed time = 243 ms. (4305 row(s) affected)
  28. 28. Ссылки http://msdn.microsoft.com/ru-ru/library/ms191227(v=SQL.100).aspx (Анализ запроса) http://msdn.microsoft.com/en-us/library/ms191426(v=SQL.100).aspx (Advanced Query Tuning Concepts) http://msdn.microsoft.com/ru-ru/library/ms178071(v=SQL.100).aspx (Графическое отображение планов выполнения) http://msdn.microsoft.com/en-us/library/ms178071(v=SQL.100).aspx (Displaying Graphical Execution Plans) http://www.red-gate.com/products/sql_data_generator/index.htm (SQL Data Generator) http://blog.sqlauthority.com

×