Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linq Navigation join with nullable FK loads ALL(redundant) columns from second table. #8191

Closed
borisdj opened this issue Apr 17, 2017 · 5 comments
Assignees

Comments

@borisdj
Copy link

borisdj commented Apr 17, 2017

Only one column from second table is specified for join, but when query is analysed from SqlProfiler it selects all columns.

Steps to reproduce

Main Table

User
	Guid UserId (PK)
	string Email
	string FullName
	Guid? PartyId (FK nullable)

Join Table

Party
	Guid PartyId (PK)
	string Name
	string Desc
	string SomeInfo

Linq:

var users = userRepos.Query().Where(a => !a.Deleted).OrderBy(u => u.UserId).Skip(0).Take(10);
var query = from u in users
            select new UserModel
	    {
	        UserId =  = u.UserId,
	        Email = u.Email,
		FullName = u.FullName,
		PartyId = u.PartyId,
		PartyName = u.Party.Name,
	    };

Profiler:

SELECT [dto.Party].[PartyId], [dto.Party].[Name], [dto.Party].[Desc], [dto.Party].[SomeInfo],
[t].[UserId], [t].[FullName], [t].[Email], [t].[PartyId]
FROM (
    SELECT [u0].*
    FROM [dbo].[User] AS [u0]
    WHERE [u0].[Deleted] = 0
    ORDER BY [u0].[UserId]
    OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
) AS [t]
LEFT JOIN [fin].[Party] AS [dto.Party] ON [t].[PartyId] = [dto.Party].[PartyId]
ORDER BY [t].[PartyId]

Columns Desc and SomeInfo should NOT be in query.
This showcase was simplified just to emphasis the issues.
In real scenario Party table has much more columns and some tables have more then one nullable FK, so I don't want all those column loaded from DB when I do not need them.

I am actually using AutoMapper instead of manually mapping these Properties but generated query is same in both cases. Showcase is with manual mapping just to point that it's not because of AM.

Further technical details

EF Core version: 1.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017

@borisdj borisdj changed the title Linq join with nullable FK loads ALL(reundant) columns from second table. Linq Navigation join with nullable FK loads ALL(reundant) columns from second table. Apr 17, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Apr 17, 2017
@ajcvickers
Copy link
Contributor

@maumar to find the dupe.

@maumar
Copy link
Contributor

maumar commented Apr 17, 2017

dupe of #6647

@maumar maumar closed this as completed Apr 17, 2017
@borisdj borisdj changed the title Linq Navigation join with nullable FK loads ALL(reundant) columns from second table. Linq Navigation join with nullable FK loads ALL(redundant) columns from second table. Apr 17, 2017
@borisdj
Copy link
Author

borisdj commented Apr 17, 2017

So it is already fixed, great.
Thx for quick response.

@maumar
Copy link
Contributor

maumar commented Apr 17, 2017

@borisdj on our current bits the following SQL is generated for this scenario:

SELECT [a].[UserId], [a].[Email], [a].[FullName], [a].[PartyId], [a.Party].[Name]
FROM [Users] AS [a]
LEFT JOIN [Parties] AS [a.Party] ON [a].[PartyId] = [a.Party].[PartyId]
WHERE [a].[Deleted] = 0
ORDER BY [a].[UserId]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY

@borisdj
Copy link
Author

borisdj commented Apr 18, 2017

That SQL looks much better.

@divega divega removed this from the 2.0.0-preview1 milestone May 8, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants