微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有什么方法可以更好地构建这个 LINQ 查询?

如何解决有什么方法可以更好地构建这个 LINQ 查询?

我正在开发一个新的 WPF 应用程序。它需要在启动时在数据网格中显示查询的结果。在测试中,我发现在某些情况下,运行查询所需的时间可能是 2 分钟 - 长得令人无法接受。但是,在其他情况下,它可以更快。差异部分取决于应用程序的运行位置,我怀疑其他因素会影响它的运行速度。靠近服务器(本地)那么它只是几秒钟的事情,尽管它会在远离服务器的其他办公室运行得更快。我们大多数人仍然在家工作。在我家里,我的互联网速度非常快(就我所在的州而言),但我在该查询上的表现仍然很糟糕。

但我现在不知道对于与我一样远离服务器的用户来说,该查询的运行速度有多快。这是用于检索数据的 LINQ 查询。我可以更好地重构 LINQ 以获得更好的性能吗?

using (var ctx = new AppEntities())
{
    People = ctx.People.AsNoTracking().Where(p => p.Inactive == false)
        .Include(p => p.Agency)
        .Include(p => p.PersonnelCertifications.Select(pc => pc.CertificationType))
        .Where(p => p.PersonnelCertifications.Any(pc => pc.CertificationType.CertType == "Operator"))
        .OrderBy(p => p.LastName)
        .ThenBy(p => p.FirstName)
        .ToList();
}

这是主表 Person:

CREATE TABLE [app].[Person](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,[AgencyID] [bigint] NOT NULL,[LastName] [nvarchar](25) NOT NULL,[FirstName] [nvarchar](20) NOT NULL,--other columns removed for brevity
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF,STATISTICS_norECOmpuTE = OFF,IGnorE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON,FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY] TEXtimage_ON [PRIMARY]
GO

和表代理:

CREATE TABLE [app].[Agency](
    [ID] [bigint] IDENTITY(1,[AgencyCode] [nvarchar](5) NOT NULL,[AgencyName] [nvarchar](50) NULL,--other columns removed for brevity
 CONSTRAINT [PK_Agency] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF,FILLFACTOR = 100) ON [PRIMARY],CONSTRAINT [AK_AgencyCode] UNIQUE NONCLUSTERED 
(
    [AgencyCode] ASC
)WITH (PAD_INDEX = OFF,FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY] TEXtimage_ON [PRIMARY]
GO

并表PersonnelCertification:

CREATE TABLE [app].[PersonnelCertification](
    [ID] [bigint] IDENTITY(1,[CertificationTypeID] [bigint] NOT NULL,[PersonID] [bigint] NOT NULL,--other columns removed for brevity
 CONSTRAINT [PK_PersonnelCertification] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF,FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY]
GO

最后是表 CertificationType:

CREATE TABLE [app].[CertificationType](
    [ID] [bigint] IDENTITY(1,[CertType] [nvarchar](30) NOT NULL,--other columns removed for brevity
 CONSTRAINT [PK_CertificationType] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF,CONSTRAINT [CertType] UNIQUE NONCLUSTERED 
(
    [CertType] ASC
)WITH (PAD_INDEX = OFF,FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY]
GO

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。