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

Query with multiple level subqery takes tremendous about of time to execute #6526

Closed
dvdobrovolskiy opened this issue Sep 14, 2016 · 1 comment
Assignees

Comments

@dvdobrovolskiy
Copy link

dvdobrovolskiy commented Sep 14, 2016

Query is like that

_context.Jobs.Select(s => new Job
            {
                ClosedDate = s.ClosedDate,
                JobId = s.JobId,
                User = s.User,
                UserId = s.UserId,
                Zayavka = _context.Zayavkas.Select(s1=> new Zayavka
                {
                    ZayavkaId = s1.ZayavkaId,
                    ZayavkaStatus = s1.ZayavkaStatus,
                    Block = _context.Blocks.SingleOrDefault(d1=> d1.BlockId == s1.BlockId),
                    Apartment = _context.Apartments.SingleOrDefault(d1 => d1.ApartmentId == s1.ApartmentId),
                    Description = s1.Description,
                    Creator = _context.Users.SingleOrDefault(d1 => d1.Id == s1.CreatorId),
                    Modifier = _context.Users.SingleOrDefault(d1 => d1.Id == s1.ModifierId),
                    CreatedDate = s1.CreatedDate,
                    EditedDate = s1.EditedDate,
                }).SingleOrDefault(d => d.ZayavkaId == s.ZayavkaId),
                ZayavkaId = s.ZayavkaId,
                CreatedDate = s.CreatedDate,
                Description = s.Description
            })
            .Where(w=>!w.ClosedDate.HasValue)
            .Where(w=>w.CreatedDate <= DateTime.Now)
            .OrderBy(o => o.ZayavkaId)
            .Skip(offset)
            .Take(DefaultPageSize)
            .ToList()

In SQL profiler I see multiple separate queries (open per first level query row then open per second query row)

like

SELECT` [d11].[Id], [d11].[AccessFailedCount], [d11].[ConcurrencyStamp], [d11].[Email], [d11].[EmailConfirmed], [d11].[FIO], [d11].[LockoutEnabled], [d11].[LockoutEnd], [d11].[MainPhone], [d11].[NormalizedEmail], [d11].[NormalizedUserName], [d11].[OtherPhone], [d11].[PasswordHash], [d11].[PhoneNumber], [d11].[PhoneNumberConfirmed], [d11].[SecondaryPhone], [d11].[SecurityStamp], [d11].[TwoFactorEnabled], [d11].[UserName], [d11].[UserStatus], [d11].[UserType]
FROM [Users] AS [d11]

SELECT [d11].[Id], [d11].[AccessFailedCount], [d11].[ConcurrencyStamp], [d11].[Email], [d11].[EmailConfirmed], [d11].[FIO], [d11].[LockoutEnabled], [d11].[LockoutEnd], [d11].[MainPhone], [d11].[NormalizedEmail], [d11].[NormalizedUserName], [d11].[OtherPhone], [d11].[PasswordHash], [d11].[PhoneNumber], [d11].[PhoneNumberConfirmed], [d11].[SecondaryPhone], [d11].[SecurityStamp], [d11].[TwoFactorEnabled], [d11].[UserName], [d11].[UserStatus], [d11].[UserType]
FROM [Users] AS [d11]

SELECT [s1].[ZayavkaId], [s1].[ZayavkaStatus], [s1].[BlockId], [s1].[ApartmentId], [s1].[Description], [s1].[CreatorId], [s1].[ModifierId], [s1].[CreatedDate], [s1].[EditedDate]
FROM [Zayavkas] AS [s1]

SELECT [d12].[Id], [d12].[AccessFailedCount], [d12].[ConcurrencyStamp], [d12].[Email], [d12].[EmailConfirmed], [d12].[FIO], [d12].[LockoutEnabled], [d12].[LockoutEnd], [d12].[MainPhone], [d12].[NormalizedEmail], [d12].[NormalizedUserName], [d12].[OtherPhone], [d12].[PasswordHash], [d12].[PhoneNumber], [d12].[PhoneNumberConfirmed], [d12].[SecondaryPhone], [d12].[SecurityStamp], [d12].[TwoFactorEnabled], [d12].[UserName], [d12].[UserStatus], [d12].[UserType]
FROM [Users] AS [d12]

SELECT [d12].[Id], [d12].[AccessFailedCount], [d12].[ConcurrencyStamp], [d12].[Email], [d12].[EmailConfirmed], [d12].[FIO], [d12].[LockoutEnabled], [d12].[LockoutEnd], [d12].[MainPhone], [d12].[NormalizedEmail], [d12].[NormalizedUserName], [d12].[OtherPhone], [d12].[PasswordHash], [d12].[PhoneNumber], [d12].[PhoneNumberConfirmed], [d12].[SecondaryPhone], [d12].[SecurityStamp], [d12].[TwoFactorEnabled], [d12].[UserName], [d12].[UserStatus], [d12].[UserType]
FROM [Users] AS [d12]

SELECT [s1].[ZayavkaId], [s1].[ZayavkaStatus], [s1].[BlockId], [s1].[ApartmentId], [s1].[Description], [s1].[CreatorId], [s1].[ModifierId], [s1].[CreatedDate], [s1].[EditedDate]
FROM [Zayavkas] AS [s1]

and so on. lots of separate queries. And it takes tremendous amount of time (like minute on where main table is 90 rows long).

Is any way EntityFramework to translate in one sql query?

@rowanmiller rowanmiller added this to the 1.1.0 milestone Sep 14, 2016
@maumar maumar modified the milestones: 1.1.0, 1.1.0-preview1 Oct 5, 2016
@anpete
Copy link
Contributor

anpete commented Oct 7, 2016

Closing as this should be much better now (@dvdobrovolskiy, it would be great if you could confirm against a recent nightly) There will still be some client eval. for the sub-queries in projections, but we are tracking that as part of #4007

@anpete anpete closed this as completed Oct 7, 2016
@rowanmiller rowanmiller removed this from the 1.1.0 milestone Oct 12, 2016
@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants