I'm working on a new WPF application. It needs to display the results of a query in a datagrid when it launches. In testing I've found that under certain circumstances the time it takes for the query to run can be 2 minutes - unacceptably long. However, under other circumstances it can be much faster. The difference depends in part upon where the application is running and I suspect other factors contribute to how fast it can run. Being closer to the server (on-prem) then it is only a matter of a few seconds, although it will run faster at other offices even further away from the server. Most of us are still working from home. At my home I have incredibly good Internet speeds (for my state), but I still have abysmal performance on that query.
But I've no idea at this point how fast that query will run for the users, who are as far away from the server as I am. Here's the LINQ query to retrieve the data. Can I restructure the LINQ better for better performance?
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();
}
Here's the primary table 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
And table Agency:
CREATE TABLE [app].[Agency](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[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, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY],
CONSTRAINT [AK_AgencyCode] UNIQUE NONCLUSTERED
(
[AgencyCode] 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
And table PersonnelCertification:
CREATE TABLE [app].[PersonnelCertification](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[CertificationTypeID] [bigint] NOT NULL,
[PersonID] [bigint] NOT NULL,
[AgencyID] [bigint] NOT NULL,
--other columns removed for brevity
CONSTRAINT [PK_PersonnelCertification] 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]
GO
And finally table CertificationType:
CREATE TABLE [app].[CertificationType](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[CertType] [nvarchar](30) NOT NULL,
--other columns removed for brevity
CONSTRAINT [PK_CertificationType] 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],
CONSTRAINT [CertType] UNIQUE NONCLUSTERED
(
[CertType] 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]
GO