LINQ to SQL Listing 1: Beware of Server Round-Trips (SQL)

TLINQ to SQL generates this complex T-SQL query to reduce server round-trips from 922 (lazy loading) or 92 (eager loading) to two (eager loading). But it takes SQL Server 2005 more than three seconds to hydrate all Customer, Order, and Order_Detail entities from the Northwind persistence database (see Table 1, Test 3B). Depending on the round-trip cost, 92 queries, which execute in about 1.2 seconds, might be the optimum choice (Table 1, Test 2B).

SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], 
[t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], 
[t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].
	[ShipAddress], 
[t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], 
[t0].[ShipCountry], [t3].[OrderID] AS [OrderID2], [t3].
	[ProductID], 
[t3].[UnitPrice], [t3].[Quantity], [t3].[Discount], (
	SELECT COUNT(*)
	FROM [dbo].[Order Details] AS [t4]
	WHERE [t4].[OrderID] = [t0].[OrderID]
	) AS [count], [t2].[test], [t2].[CustomerID] AS 
	[CustomerID2], 
[t2].[CompanyName], [t2].[ContactName], [t2].[ContactTitle], 
[t2].[Address], [t2].[City], [t2].[Region], [t2].[PostalCode], 
[t2].[Country], [t2].[Phone], [t2].[Fax] 
FROM [dbo].[Orders] AS [t0] 
LEFT OUTER JOIN (
	SELECT 1 AS [test], [t1].[CustomerID], [t1].[CompanyName], 
[t1].[ContactName], [t1].[ContactTitle], [t1].[Address], [t1].[City], 
[t1].[Region], [t1].[PostalCode], [t1].[Country], [t1].[Phone], [t1].[Fax]
	FROM [dbo].[Customers] AS [t1]
	) AS [t2] ON [t2].[CustomerID] = [t0].[CustomerID]
LEFT OUTER JOIN [dbo].[Order Details] AS [t3] 
	ON [t3].[OrderID] = [t0].[OrderID]
WHERE [t0].[ShipCountry] = 'USA' 
ORDER BY [t0].[OrderID], [t2].[CustomerID], [t3].[ProductID] 
comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube