在sql server中添加其他联接时查询速度较慢

在sql server中添加其他联接时查询速度较慢,sql,sql-server,entity-framework,Sql,Sql Server,Entity Framework,我有一个对数据库的查询,返回结果需要很多时间。查询是通过EntityFramework6创建的,但是我已经得到了生成的查询,并且正在SQLManagementStudio中测试该查询 在做了一些测试之后,我意识到当我从连接中退出一个特定的表时,查询只需不到一秒钟的时间就可以工作,如果不是的话,查询需要超过30秒 我的第一个想法是,这个表的主键没有索引,但数据库模式是通过EF迁移创建的,因此所有主键都有其适当的索引 这就是问题所在 SELECT TOP (10) [Join4].[Id1

我有一个对数据库的查询,返回结果需要很多时间。查询是通过EntityFramework6创建的,但是我已经得到了生成的查询,并且正在SQLManagementStudio中测试该查询

在做了一些测试之后,我意识到当我从连接中退出一个特定的表时,查询只需不到一秒钟的时间就可以工作,如果不是的话,查询需要超过30秒

我的第一个想法是,这个表的主键没有索引,但数据库模式是通过EF迁移创建的,因此所有主键都有其适当的索引

这就是问题所在

SELECT TOP (10) 
    [Join4].[Id1] AS [Id], 
    [Join4].[Title] AS [Title], 
    [Join4].[IdServiceType] AS [IdServiceType], 
    [Join4].[Description] AS [Description], 
    [Join4].[DescriptionLarge] AS [DescriptionLarge], 
    [Join4].[DescriptionAgency] AS [DescriptionAgency], 
    [Join4].[DescriptionCentral] AS [DescriptionCentral], 
    [Join4].[Pvp] AS [Pvp], 
    [Join4].[PreviousPvp] AS [PreviousPvp], 
    [Join4].[PriceAgency] AS [PriceAgency], 
    [Join4].[PriceProvider] AS [PriceProvider], 
    [Join4].[PriceSamurai] AS [PriceSamurai], 
    [Join4].[IdProvider] AS [IdProvider], 
    [Join4].[IdDestination] AS [IdDestination], 
    [Join4].[IdOrigin] AS [IdOrigin], 
    [Join4].[ExpirationDate] AS [ExpirationDate], 
    [Join4].[DistanceToCenter] AS [DistanceToCenter], 
    [Join4].[Longitude1] AS [Longitude], 
    [Join4].[Latitude1] AS [Latitude], 
    [Join4].[CreationDate] AS [CreationDate], 
    [Join4].[ValidationDateStart] AS [ValidationDateStart], 
    [Join4].[ValidationDateEnd] AS [ValidationDateEnd], 
    [Join4].[ExecutionDateStart] AS [ExecutionDateStart], 
    [Join4].[ExecutionDateEnd] AS [ExecutionDateEnd], 
    [Join4].[Nights] AS [Nights], 
    [Join4].[Document] AS [Document], 
    [Join4].[Id2] AS [Id1], 
    [Join4].[Code1] AS [Code], 
    [Join4].[Name1] AS [Name], 
    [Join4].[IdSuperDestination1] AS [IdSuperDestination], 
    [Join4].[Iata1] AS [Iata], 
    [Join4].[Level1] AS [Level], 
    [Join4].[IdDestinationType1] AS [IdDestinationType], 
    [Join4].[TouristInfo1] AS [TouristInfo], 
    [Join4].[Longitude2] AS [Longitude1], 
    [Join4].[Latitude2] AS [Latitude1], 
    [Join4].[Language1] AS [Language], 
    [Join4].[IdCurrency1] AS [IdCurrency], 
    [Join4].[CurrencyName1] AS [CurrencyName], 
    [Join4].[Vaccine1] AS [Vaccine], 
    [Join4].[Visa1] AS [Visa], 
    [Join4].[Embassy1] AS [Embassy], 
    [Join4].[Recommendations1] AS [Recommendations], 
    [Join4].[Id3] AS [Id2], 
    [Join4].[Code2] AS [Code1], 
    [Join4].[Name2] AS [Name1], 
    [Join4].[IdSuperDestination2] AS [IdSuperDestination1], 
    [Join4].[Iata2] AS [Iata1], 
    [Join4].[Level2] AS [Level1], 
    [Join4].[IdDestinationType2] AS [IdDestinationType1], 
    [Join4].[TouristInfo2] AS [TouristInfo1], 
    [Join4].[Longitude3] AS [Longitude2], 
    [Join4].[Latitude3] AS [Latitude2], 
    [Join4].[Language2] AS [Language1], 
    [Join4].[IdCurrency2] AS [IdCurrency1], 
    [Join4].[CurrencyName2] AS [CurrencyName1], 
    [Join4].[Vaccine2] AS [Vaccine1], 
    [Join4].[Visa2] AS [Visa1], 
    [Join4].[Embassy2] AS [Embassy1], 
    [Join4].[Recommendations2] AS [Recommendations1], 
    [Join4].[Id4] AS [Id3], 
    [Join4].[Name3] AS [Name2], 
    [Join4].[Id5] AS [Id4], 
    [Join4].[Name4] AS [Name3], 
    [Join4].[Observations] AS [Observations], 
    [Join4].[Address] AS [Address], 
    [Join4].[Web] AS [Web], 
    [Join4].[Phone] AS [Phone], 
    [Join4].[Mobile] AS [Mobile], 
    [Join4].[Fax] AS [Fax], 
    [Join4].[Email] AS [Email], 
    [Join4].[EmailBook] AS [EmailBook], 
    [Join4].[Cif] AS [Cif], 
    [Join4].[Contact] AS [Contact], 
    [Join4].[Account] AS [Account], 
    [Join4].[AccountIban] AS [AccountIban], 
    [Join4].[City] AS [City], 
    [Join4].[Province] AS [Province], 
    [Join4].[CorporateName] AS [CorporateName], 
    [Join4].[PostalCode] AS [PostalCode], 
    [Join4].[Code3] AS [Code2]
    FROM ( SELECT [Extent1].[Id] AS [Id1], [Extent1].[Title] AS [Title], [Extent1].[IdServiceType] AS [IdServiceType], [Extent1].[Description] AS [Description], [Extent1].[DescriptionLarge] AS [DescriptionLarge], [Extent1].[DescriptionAgency] AS [DescriptionAgency], [Extent1].[DescriptionCentral] AS [DescriptionCentral], [Extent1].[Pvp] AS [Pvp], [Extent1].[PreviousPvp] AS [PreviousPvp], [Extent1].[PriceAgency] AS [PriceAgency], [Extent1].[PriceProvider] AS [PriceProvider], [Extent1].[PriceSamurai] AS [PriceSamurai], [Extent1].[IdProvider] AS [IdProvider], [Extent1].[IdDestination] AS [IdDestination], [Extent1].[IdOrigin] AS [IdOrigin], [Extent1].[ExpirationDate] AS [ExpirationDate], [Extent1].[DistanceToCenter] AS [DistanceToCenter], [Extent1].[Longitude] AS [Longitude1], [Extent1].[Latitude] AS [Latitude1], [Extent1].[CreationDate] AS [CreationDate], [Extent1].[ValidationDateStart] AS [ValidationDateStart], [Extent1].[ValidationDateEnd] AS [ValidationDateEnd], [Extent1].[ExecutionDateStart] AS [ExecutionDateStart], [Extent1].[ExecutionDateEnd] AS [ExecutionDateEnd], [Extent1].[Nights] AS [Nights], [Extent1].[Document] AS [Document], [Extent2].[Id] AS [Id2], [Extent2].[Code] AS [Code1], [Extent2].[Name] AS [Name1], [Extent2].[IdSuperDestination] AS [IdSuperDestination1], [Extent2].[Iata] AS [Iata1], [Extent2].[Level] AS [Level1], [Extent2].[IdDestinationType] AS [IdDestinationType1], [Extent2].[TouristInfo] AS [TouristInfo1], [Extent2].[Longitude] AS [Longitude2], [Extent2].[Latitude] AS [Latitude2], [Extent2].[Language] AS [Language1], [Extent2].[IdCurrency] AS [IdCurrency1], [Extent2].[CurrencyName] AS [CurrencyName1], [Extent2].[Vaccine] AS [Vaccine1], [Extent2].[Visa] AS [Visa1], [Extent2].[Embassy] AS [Embassy1], [Extent2].[Recommendations] AS [Recommendations1], [Extent3].[Id] AS [Id3], [Extent3].[Code] AS [Code2], [Extent3].[Name] AS [Name2], [Extent3].[IdSuperDestination] AS [IdSuperDestination2], [Extent3].[Iata] AS [Iata2], [Extent3].[Level] AS [Level2], [Extent3].[IdDestinationType] AS [IdDestinationType2], [Extent3].[TouristInfo] AS [TouristInfo2], [Extent3].[Longitude] AS [Longitude3], [Extent3].[Latitude] AS [Latitude3], [Extent3].[Language] AS [Language2], [Extent3].[IdCurrency] AS [IdCurrency2], [Extent3].[CurrencyName] AS [CurrencyName2], [Extent3].[Vaccine] AS [Vaccine2], [Extent3].[Visa] AS [Visa2], [Extent3].[Embassy] AS [Embassy2], [Extent3].[Recommendations] AS [Recommendations2], [Extent4].[Id] AS [Id4], [Extent4].[Name] AS [Name3], [Extent5].[Id] AS [Id5], [Extent5].[Name] AS [Name4], [Extent5].[Observations] AS [Observations], [Extent5].[Address] AS [Address], [Extent5].[Web] AS [Web], [Extent5].[Phone] AS [Phone], [Extent5].[Mobile] AS [Mobile], [Extent5].[Fax] AS [Fax], [Extent5].[Email] AS [Email], [Extent5].[EmailBook] AS [EmailBook], [Extent5].[Cif] AS [Cif], [Extent5].[Contact] AS [Contact], [Extent5].[Account] AS [Account], [Extent5].[AccountIban] AS [AccountIban], [Extent5].[City] AS [City], [Extent5].[Province] AS [Province], [Extent5].[CorporateName] AS [CorporateName], [Extent5].[PostalCode] AS [PostalCode], [Extent5].[Code] AS [Code3], row_number() OVER (ORDER BY [Extent1].[Id] DESC) AS [row_number]
        FROM     [dbo].[Services] AS [Extent1]
        INNER JOIN [dbo].[Destinations] AS [Extent2] ON [Extent1].[IdDestination] = [Extent2].[Id]
        LEFT OUTER JOIN [dbo].[Destinations] AS [Extent3] ON [Extent1].[IdOrigin] = [Extent3].[Id]
        INNER JOIN [dbo].[ServiceTypes] AS [Extent4] ON [Extent1].[IdServiceType] = [Extent4].[Id]
        INNER JOIN [dbo].[Providers] AS [Extent5] ON [Extent1].[IdProvider] = [Extent5].[Id]
    )  AS [Join4]
    WHERE [Join4].[row_number] > 0
    ORDER BY [Join4].[Id1] DESC
当我从“from”子句中删除表“Providers”时,查询将在不到一秒钟的时间内完成,但如果不是,查询将花费大量时间


有什么想法吗?

提供商
上的
Id
列的数据类型是什么?您是否只在子查询中获得相同的行为?您是否尝试过使用SQL查询分析器工具?这是一个内置工具为什么要使用子查询?所有ID都是整数。子查询是必需的,因为此查询是由使用.skip().take()的linq查询生成的,执行计划告诉您什么。检查你的费用在哪里。它正在对提供者进行表扫描。有很多因素可以使它变慢。