|
| 1 | +namespace Microsoft.SqlServer.SmoSamples |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Diagnostics; |
| 6 | + using System.Text; |
| 7 | + using Microsoft.SqlServer.Management.Smo; |
| 8 | + using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | + using NUnit.Framework; |
| 10 | + using Assert = NUnit.Framework.Assert; |
| 11 | + |
| 12 | + [TestClass] |
| 13 | + public class CollectionSamples |
| 14 | + { |
| 15 | + public VisualStudio.TestTools.UnitTesting.TestContext TestContext { get; set; } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// SetDefaultInitFields tells the Server object which properties to include in the initial query |
| 19 | + /// to populate of a given object type when initialized a collection of that type. |
| 20 | + /// The test demonstrates the effect of using this call to enumerate Tables when accessing the FileGroup |
| 21 | + /// property of each Table object |
| 22 | + /// </summary> |
| 23 | + [TestMethod] |
| 24 | + public void Collection_iteration_is_faster_with_SetDefaultInitFields() |
| 25 | + { |
| 26 | + using (var connectionMetrics = ConnectionMetrics.SetupMeasuredConnection(TestContext, 50)) |
| 27 | + { |
| 28 | + var server = new Management.Smo.Server(connectionMetrics.ServerConnection); |
| 29 | + var database = server.Databases[TestContext.GetTestDatabaseName()]; |
| 30 | + connectionMetrics.Reset(); |
| 31 | + foreach (Table table in database.Tables) |
| 32 | + { |
| 33 | + // Accessing FileGroup triggers a query to fetch it |
| 34 | + Trace.TraceInformation( |
| 35 | + $"Unoptimized table Name: {table.Name}\tSchema:{table.Schema}\tFileGroup:{table.FileGroup}"); |
| 36 | + } |
| 37 | + |
| 38 | + var unoptimizedMetrics = (QueryCount: connectionMetrics.QueryCount, |
| 39 | + BytesSent: connectionMetrics.BytesSent, BytesRead: connectionMetrics.BytesRead, |
| 40 | + ConnectionCount: connectionMetrics.ConnectionCount); |
| 41 | + Trace.TraceInformation(string.Join($"{Environment.NewLine}\t", new[] |
| 42 | + { |
| 43 | + "Unoptimized metrics:", |
| 44 | + $"QueryCount:{unoptimizedMetrics.QueryCount}", $"ConnectionCount:{unoptimizedMetrics.ConnectionCount}", |
| 45 | + $"BytesSent:{unoptimizedMetrics.BytesSent}", $"BytesRead:{unoptimizedMetrics.BytesRead}" |
| 46 | + })); |
| 47 | + |
| 48 | + connectionMetrics.Reset(); |
| 49 | + server.SetDefaultInitFields(typeof(Table), "Name", "Schema", "FileGroup"); |
| 50 | + database.Tables.Refresh(); |
| 51 | + foreach (Table table in database.Tables) |
| 52 | + { |
| 53 | + // The FileGroup property is already populated, so no extra query is needed |
| 54 | + Trace.TraceInformation( |
| 55 | + $"Optimized table Name: {table.Name}\tSchema:{table.Schema}\tFileGroup:{table.FileGroup}"); |
| 56 | + } |
| 57 | + |
| 58 | + var optimizedMetrics = (QueryCount: connectionMetrics.QueryCount, |
| 59 | + BytesSent: connectionMetrics.BytesSent, BytesRead: connectionMetrics.BytesRead, |
| 60 | + ConnectionCount: connectionMetrics.ConnectionCount); |
| 61 | + Trace.TraceInformation(string.Join($"{Environment.NewLine}\t", new[] |
| 62 | + { |
| 63 | + "Optimized Metrics:", |
| 64 | + $"QueryCount:{optimizedMetrics.QueryCount}", $"ConnectionCount:{optimizedMetrics.ConnectionCount}", |
| 65 | + $"BytesSent:{optimizedMetrics.BytesSent}", $"BytesRead:{optimizedMetrics.BytesRead}" |
| 66 | + })); |
| 67 | + Assert.That(optimizedMetrics.BytesRead, Is.LessThan(unoptimizedMetrics.BytesRead), "BytesRead"); |
| 68 | + Assert.That(optimizedMetrics.BytesSent, Is.LessThan(unoptimizedMetrics.BytesSent), "BytesSent"); |
| 69 | + Assert.That(optimizedMetrics.ConnectionCount, Is.AtMost(unoptimizedMetrics.ConnectionCount), "ConnectionCount"); |
| 70 | + Assert.That(optimizedMetrics.QueryCount, Is.LessThan(unoptimizedMetrics.QueryCount), "QueryCount"); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments