7

We're running into a performance issue with a query that utilizes a few M:M relationships with a single table.

Tables:

Schema

Currently, EF generates a MONSTROUS query that uses UNION ALL to the Contact table 3 times. Pastebin of SQL Once for each join. Our projection is like so

var contacts = query.Select(contact => new Contact
            {
                Brands = contact.Brands.Select(b => new Brand
                {
                    Id = b.Id,
                    Name = b.Name,
                    Aliases = b.Aliases.Select(a => a.Value).ToList()
                }).ToList(),
                Categories = contact.Categories.Select(category => new Category
                {
                    Id = category.Id
                }).ToList(),
                ContactTypes = contact.ContactTypes.Select(ct => new ContactType
                {
                    Id = ct.Id
                }).ToList(),
                Disabled = contact.Disabled,
                Email = contact.Email,
                Fax = contact.Fax,
                Id = contact.Id,
                Latitude = contact.Coordinates != null ? contact.Coordinates.Latitude : 0,
                Longitude = contact.Coordinates != null ? contact.Coordinates.Longitude : 0,
                Distance = searchLocation != null && contact.Coordinates != null ? searchLocation.Distance(contact.Coordinates) * conversionFactor : 0,
                PostalCode = contact.PostalCode,
                PhoneSupport = contact.PhoneSupport,
                PhoneSales = contact.PhoneSales,
                PhoneAfterHoursSupport = contact.PhoneAfterHoursSupport,
                Phone = contact.Phone,
                Preferred = contact.Preferred,
                ShopOnlineImageUrl = contact.ShopOnlineImageUrl,
                ContactTranslations = contact.NameContent.TranslatedContents.Where(tc => tc.IsoLanguageCode == searchModel.IsoLanguageCode ||
                                                                                         tc.IsoLanguageCode == SearchContactsHelpers.ISO_LANGUAGE_CODE_ENGLISH)
                                                                            .Select(tc => new ContactTranslationImpl
                                                                            {
                                                                                Name = tc.Value,
                                                                                IsoLanguageCode = tc.IsoLanguageCode,
                                                                                NameId = tc.MasterContentId
                                                                            }).ToList(),
                AdditionalInformationContentId = contact.AdditionalInformationContentId,
                AddressLine1ContentId = contact.AddressLine1ContentId,
                AddressLine2ContentId = contact.AddressLine2ContentId,
                CityContentId = contact.CityContentId,
                StateOrProvinceContentId = contact.StateOrProvinceContentId,
                CountryContentId = contact.Country.NameContentId,
                HoursOfOperationContentId = contact.HoursOfOperationContentId,
                WebsiteContentId = contact.WebsiteContentId,
                OnlineSellerHomePageUrlContentId = contact.OnlineSellerHomePageUrlContentId,
                ContactFormUrlContentId = contact.ContactFormUrlContentId,
                RequestAQuoteUrlContentId = contact.RequestAQuoteUrlContentId,
                NameContentId = contact.NameContentId
            });

Removing the M:M relationships from the query produces a sane single query (albeit with many subselects). As soon as a single M:M relationship is added to the projection, EF produces 2 queries with a UNION ALL for the results.

Here's our configuration as well:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Entity<Contact>()
                .HasMany(c => c.Brands)
                .WithMany(c => c.Contacts)
                .Map(mc =>
                {
                    mc.MapRightKey("BrandId");
                    mc.MapLeftKey("ContactId");
                    mc.ToTable("ContactBrands");
                });
            modelBuilder.Entity<Contact>()
                .HasMany(c => c.Categories)
                .WithMany(c => c.Contacts)
                .Map(mc =>
                {
                    mc.MapRightKey("CategoryId");
                    mc.MapLeftKey("ContactId");
                    mc.ToTable("ContactCategories");
                });
            modelBuilder.Entity<Contact>()
                .HasMany(c => c.ContactTypes)
                .WithMany(c => c.Contacts)
                .Map(mc =>
                {
                    mc.MapRightKey("ContactTypeId");
                    mc.MapLeftKey("ContactId");
                    mc.ToTable("ContactToContactType");
                });
        }

I am unsure of why EF is generating such a terrible query. Any insight on how to possibly improve the query is immensely helpful.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.