I'm executing a query every 1 second and most of the time it returns no results (SQL generated by Entity Framework):
SELECT TOP (5)
[Extent1].[ID] AS [ID],
***30 more columns***
FROM ParentTable AS [Extent1]
WHERE ([Extent1].[ImageTaken] = 1) AND ([Extent1].[ImageProjected] <> 1) AND ( EXISTS (SELECT
1 AS [C1]
FROM ChildTable AS [Extent2]
WHERE [Extent1].[ID] = [Extent2].[Parent_ID]
))
The above query take about 400ms. However, if I exclude all the columns except ID from the result it takes about 100ms.
Why is the execution time so different if there are no results? I looked at the execution plans and they look identical (I've never looked at an execution plan before today, so take that with a grain of salt).
I'd like to include all the columns, but obviously I only need them if there is a result.
Actual Execution Plans
EDIT More details:
- I'm using SQL Server Express 2017
- The SQL Server is on my local machine
- The parent table has ~1 million rows
- The child table has ~40k rows
Parent Table Definition
USE [DB]
GO
/****** Object: Table [dbo].[Inspection_CapturedImageQueueItem] Script Date: 1/8/2018 6:23:45 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Inspection_CapturedImageQueueItem](
[ID] [INT] IDENTITY(1,1) NOT NULL,
[X] [FLOAT] NOT NULL,
[Y] [FLOAT] NOT NULL,
[Z] [FLOAT] NOT NULL,
[rX] [FLOAT] NOT NULL,
[rY] [FLOAT] NOT NULL,
[rZ] [FLOAT] NOT NULL,
[Priority] [INT] NOT NULL,
[TimeTakenUTC] [datetime] NOT NULL,
[ImageTaken] [bit] NOT NULL,
[PartProgramInstance_Id] [INT] NULL,
[PolarizerAngle1] [FLOAT] NOT NULL,
[PolarizerAngle2] [FLOAT] NOT NULL,
[ImageProjected] [bit] NOT NULL,
[LaserTransform_X] [FLOAT] NOT NULL,
[LaserTransform_Y] [FLOAT] NOT NULL,
[LaserTransform_Z] [FLOAT] NOT NULL,
[LaserTransform_rX] [FLOAT] NOT NULL,
[LaserTransform_rY] [FLOAT] NOT NULL,
[LaserTransform_rZ] [FLOAT] NOT NULL,
[LaserPosition_X] [FLOAT] NOT NULL,
[LaserPosition_Y] [FLOAT] NOT NULL,
[LaserPosition_Z] [FLOAT] NOT NULL,
[LaserPosition_rX] [FLOAT] NOT NULL,
[LaserPosition_rY] [FLOAT] NOT NULL,
[LaserPosition_rZ] [FLOAT] NOT NULL,
[ProjectionError] [INT] NOT NULL,
[LocalTransform_X] [FLOAT] NOT NULL,
[LocalTransform_Y] [FLOAT] NOT NULL,
[LocalTransform_Z] [FLOAT] NOT NULL,
[LocalTransform_rX] [FLOAT] NOT NULL,
[LocalTransform_rY] [FLOAT] NOT NULL,
[LocalTransform_rZ] [FLOAT] NOT NULL,
[IsHighAngleOfIncidence] [bit] NOT NULL,
CONSTRAINT [PK_dbo.Inspection_CapturedImageQueueItem] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [PolarizerAngle1]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [PolarizerAngle2]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [ImageProjected]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserTransform_X]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserTransform_Y]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserTransform_Z]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserTransform_rX]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserTransform_rY]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserTransform_rZ]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserPosition_X]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserPosition_Y]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserPosition_Z]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserPosition_rX]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserPosition_rY]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LaserPosition_rZ]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [ProjectionError]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LocalTransform_X]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LocalTransform_Y]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LocalTransform_Z]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LocalTransform_rX]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LocalTransform_rY]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [LocalTransform_rZ]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] ADD DEFAULT ((0)) FOR [IsHighAngleOfIncidence]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] WITH CHECK ADD CONSTRAINT [FK_dbo.Inspection_CapturedImageQueueItem_dbo.Inspection_PartProgramInstance_PartProgramInstance_Id] FOREIGN KEY([PartProgramInstance_Id])
REFERENCES [dbo].[Inspection_PartProgramInstance] ([Id])
GO
ALTER TABLE [dbo].[Inspection_CapturedImageQueueItem] CHECK CONSTRAINT [FK_dbo.Inspection_CapturedImageQueueItem_dbo.Inspection_PartProgramInstance_PartProgramInstance_Id]
GO
Child Table Definition
USE [DB]
GO
/****** Object: Table [dbo].[Inspection_CapturedImageItem] Script Date: 1/11/2018 9:01:34 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Inspection_CapturedImageItem](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PolarizerAngle] [float] NOT NULL,
[ImageRequest_ID] [int] NULL,
[TimeTakenUTC] [datetime] NOT NULL,
[ProjectorNumber] [int] NOT NULL,
[AngleOfIncidence] [float] NOT NULL,
[MirrorRx] [float] NOT NULL,
[MirrorRy] [float] NOT NULL,
CONSTRAINT [PK_dbo.Inspection_CapturedImageItem] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] ADD DEFAULT ('1900-01-01T00:00:00.000') FOR [TimeTakenUTC]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] ADD DEFAULT ((0)) FOR [ProjectorNumber]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] ADD DEFAULT ((0)) FOR [AngleOfIncidence]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] ADD DEFAULT ((0)) FOR [MirrorRx]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] ADD DEFAULT ((0)) FOR [MirrorRy]
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] WITH CHECK ADD CONSTRAINT [FK_dbo.Inspection_CapturedImageItem_dbo.Inspection_CapturedImageQueueItem_ImageRequest_ID] FOREIGN KEY([ImageRequest_ID])
REFERENCES [dbo].[Inspection_CapturedImageQueueItem] ([ID])
GO
ALTER TABLE [dbo].[Inspection_CapturedImageItem] CHECK CONSTRAINT [FK_dbo.Inspection_CapturedImageItem_dbo.Inspection_CapturedImageQueueItem_ImageRequest_ID]
GO
Indices