Top Banner
Использование Microsoft SQL Management Studio при оптимизации запросов.
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1. Microsoft SQL Management Studio .

2. Microsoft Windows Server 2003 Microsoft SQL Server 2008 Microsoft SQL Server Management Studio 3. 4. Customers 10 . OrderItems 500 . Products 200 . Orders 100 . 5. 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. SELECT p.[ID], p.[Name] FROM [Products] as p 8. SELECT p.[ID], p.[Name] FROM [Products] as p 9. SELECT p.[ID], p.[Name] FROM [Products] as p 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. 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 [] ON [dbo].[Products] ([Price]) INCLUDE ([Name]) GO */ 12. USE [demo] GO CREATE NONCLUSTERED INDEX [idx_price_name] ON [dbo].[Products] ([Price]) INCLUDE ([Name]) GO 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. 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. SQL Server Execution Times: CPU time = 47 ms, elapsed time = 45 ms. 16. USE [demo] GO CREATE NONCLUSTERED INDEX [idx_ProductId] ON [dbo].[OrderItems] ([ProductId]) INCLUDE ([OrderId]) GO 17. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 2 ms. 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. 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. 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 [] ON [dbo].[Orders] ([Date]) INCLUDE ([CustomerId]) GO */ 22. USE [demo] GO CREATE NONCLUSTERED INDEX [idx_Date] ON [dbo].[Orders] ([Date]) INCLUDE ([CustomerId]) GO 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. 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. SQL Server Execution Times: CPU time = 16 ms, elapsed time = 14 ms. (30 row(s) affected) 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. SQL Server Execution Times: CPU time = 329 ms, elapsed time = 243 ms. (4305 row(s) affected) 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