我在同一服务器的同一数据库中制作了具有相同索引的表的副本。 然后它在一个表和另一个表中执行相同的查询,但执行计划是不同的。
为什么?
这些是我的疑问:
select top 1 * from SFMatl
where AppUpdated = 0 and UpdLock = 0 and CompanyId = 'ent'
order by recid
select top 1 * from SFMatl_Backup20042017
where AppUpdated = 0 and UpdLock = 0 and CompanyId = 'ent'
order by recid
这些执行计划:
我的创建表:
/****** Object: Table [dbo].[SFMatl_Backup20042017] Script Date: 20/04/2017 14:40:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SFMatl_Backup20042017](
[RecId] [int] NOT NULL,
[CompanyId] [char](3) NOT NULL,
[PrOdId] [varchar](10) NOT NULL,
[OprNum] [tinyint] NOT NULL,
[WrkCtrId] [varchar](10) NOT NULL,
[ItemId] [varchar](20) NOT NULL,
[SizeId] [varchar](20) NOT NULL,
[SizeId2] [varchar](20) NOT NULL,
[ColorId] [varchar](20) NOT NULL,
[MatlPropId] [varchar](8) NOT NULL,
[LineNum] [tinyint] NOT NULL,
[SubForLineNum] [tinyint] NOT NULL,
[SerialNum] [varchar](20) NOT NULL,
[PalletNum] [varchar](20) NOT NULL,
[BatchNum] [varchar](20) NOT NULL,
[PQty] [numeric](28, 12) NOT NULL,
[PUnit] [varchar](10) NOT NULL,
[SQty] [numeric](28, 12) NOT NULL,
[SUnit] [varchar](10) NOT NULL,
[TQty] [numeric](28, 12) NOT NULL,
[TUnit] [varchar](10) NOT NULL,
[StkQty] [numeric](28, 12) NOT NULL,
[StkUnit] [varchar](10) NOT NULL,
[FtQty] [numeric](28, 12) NOT NULL,
[LbQty] [numeric](28, 12) NOT NULL,
[MQty] [numeric](28, 12) NOT NULL,
[KgQty] [numeric](28, 12) NOT NULL,
[Yield] [numeric](28, 12) NOT NULL,
[YieldUnit] [varchar](10) NOT NULL,
[WPQty] [numeric](28, 12) NOT NULL,
[WPUnit] [varchar](10) NOT NULL,
[WSQty] [numeric](28, 12) NOT NULL,
[WSUnit] [varchar](10) NOT NULL,
[SetNum] [int] NOT NULL,
[LaneNum] [tinyint] NOT NULL,
[RowNum] [smallint] NOT NULL,
[TranType] [varchar](10) NOT NULL,
[WasteId] [varchar](10) NOT NULL,
[Shift] [varchar](10) NOT NULL,
[ShiftTime] [datetime] NOT NULL,
[ShiftDate] [datetime] NOT NULL,
[PRGCode] [varchar](10) NOT NULL,
[UserRemark] [text] NOT NULL,
[JobType] [varchar](10) NOT NULL,
[WareHouseId] [varchar](10) NOT NULL,
[LocationId] [varchar](10) NOT NULL,
[AppUpdated] [tinyint] NOT NULL,
[OnOffUpdated] [tinyint] NOT NULL,
[InventTransId] [varchar](20) NOT NULL,
[OrgSerialNum] [varchar](100) NOT NULL,
[ReworkFlag] [tinyint] NOT NULL,
[Reworked] [tinyint] NOT NULL,
[Category] [varchar](50) NOT NULL,
[GroupTranNum] [varchar](10) NOT NULL,
[SystemCreated] [tinyint] NOT NULL,
[OfflineTran] [tinyint] NOT NULL,
[UpdLock] [tinyint] NOT NULL,
[UpdLockTime] [datetime] NOT NULL,
[SFBOMRevision] [int] NOT NULL,
[UDF01] [varchar](50) NOT NULL,
[UDF02] [varchar](50) NOT NULL,
[AddEdit] [tinyint] NOT NULL,
[CalcGauge] [numeric](28, 12) NOT NULL,
[MasterSerial] [varchar](100) NOT NULL,
[OprSerialCount] [smallint] NOT NULL,
[QACheck] [tinyint] NOT NULL,
[SpliceCount] [tinyint] NOT NULL,
[NumAcross] [tinyint] NOT NULL,
[PermitNum] [int] NOT NULL,
[TransAs] [char](1) NOT NULL,
[CreateTime] [datetime] NOT NULL,
[ModifyTime] [datetime] NOT NULL,
[OSUser] [varchar](20) NOT NULL,
[ComputerName] [varchar](40) NOT NULL,
[Reassign] [tinyint] NOT NULL,
[Reassigned] [tinyint] NOT NULL,
[OprCode] [varchar](10) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[SFMatl] Script Date: 20/04/2017 14:40:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SFMatl](
[RecId] [int] IDENTITY(1,1) NOT NULL,
[CompanyId] [char](3) NOT NULL,
[PrOdId] [varchar](10) NOT NULL,
[OprNum] [tinyint] NOT NULL,
[WrkCtrId] [varchar](10) NOT NULL,
[ItemId] [varchar](20) NOT NULL,
[SizeId] [varchar](20) NOT NULL,
[SizeId2] [varchar](20) NOT NULL,
[ColorId] [varchar](20) NOT NULL,
[MatlPropId] [varchar](8) NOT NULL,
[LineNum] [tinyint] NOT NULL,
[SubForLineNum] [tinyint] NOT NULL,
[SerialNum] [varchar](20) NOT NULL,
[PalletNum] [varchar](20) NOT NULL,
[BatchNum] [varchar](20) NOT NULL,
[PQty] [numeric](28, 12) NOT NULL,
[PUnit] [varchar](10) NOT NULL,
[SQty] [numeric](28, 12) NOT NULL,
[SUnit] [varchar](10) NOT NULL,
[TQty] [numeric](28, 12) NOT NULL,
[TUnit] [varchar](10) NOT NULL,
[StkQty] [numeric](28, 12) NOT NULL,
[StkUnit] [varchar](10) NOT NULL,
[FtQty] [numeric](28, 12) NOT NULL,
[LbQty] [numeric](28, 12) NOT NULL,
[MQty] [numeric](28, 12) NOT NULL,
[KgQty] [numeric](28, 12) NOT NULL,
[Yield] [numeric](28, 12) NOT NULL,
[YieldUnit] [varchar](10) NOT NULL,
[WPQty] [numeric](28, 12) NOT NULL,
[WPUnit] [varchar](10) NOT NULL,
[WSQty] [numeric](28, 12) NOT NULL,
[WSUnit] [varchar](10) NOT NULL,
[SetNum] [int] NOT NULL,
[LaneNum] [tinyint] NOT NULL,
[RowNum] [smallint] NOT NULL,
[TranType] [varchar](10) NOT NULL,
[WasteId] [varchar](10) NOT NULL,
[Shift] [varchar](10) NOT NULL,
[ShiftTime] [datetime] NOT NULL,
[ShiftDate] [datetime] NOT NULL,
[PRGCode] [varchar](10) NOT NULL,
[UserRemark] [text] NOT NULL,
[JobType] [varchar](10) NOT NULL,
[WareHouseId] [varchar](10) NOT NULL,
[LocationId] [varchar](10) NOT NULL,
[AppUpdated] [tinyint] NOT NULL,
[OnOffUpdated] [tinyint] NOT NULL,
[InventTransId] [varchar](20) NOT NULL,
[OrgSerialNum] [varchar](100) NOT NULL,
[ReworkFlag] [tinyint] NOT NULL,
[Reworked] [tinyint] NOT NULL,
[Category] [varchar](50) NOT NULL,
[GroupTranNum] [varchar](10) NOT NULL,
[SystemCreated] [tinyint] NOT NULL,
[OfflineTran] [tinyint] NOT NULL,
[UpdLock] [tinyint] NOT NULL,
[UpdLockTime] [datetime] NOT NULL,
[SFBOMRevision] [int] NOT NULL,
[UDF01] [varchar](50) NOT NULL,
[UDF02] [varchar](50) NOT NULL,
[AddEdit] [tinyint] NOT NULL,
[CalcGauge] [numeric](28, 12) NOT NULL,
[MasterSerial] [varchar](100) NOT NULL,
[OprSerialCount] [smallint] NOT NULL,
[QACheck] [tinyint] NOT NULL,
[SpliceCount] [tinyint] NOT NULL,
[NumAcross] [tinyint] NOT NULL,
[PermitNum] [int] NOT NULL,
[TransAs] [char](1) NOT NULL,
[CreateTime] [datetime] NOT NULL,
[ModifyTime] [datetime] NOT NULL,
[OSUser] [varchar](20) NOT NULL,
[ComputerName] [varchar](40) NOT NULL,
[Reassign] [tinyint] NOT NULL,
[Reassigned] [tinyint] NOT NULL,
[OprCode] [varchar](10) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
答案 0 :(得分:1)
与这些查询的主要区别在于它们不相同
SELECTed表不一样。
这意味着在查询计划方面有两件事
例如,由于数据量不同,统计数据不同
不同的索引或索引碎片
您可以检查索引及其碎片(如果需要,可以重建/重新组织它们),您也可以重新计算统计数据。
如果仍然保持相同的查询计划,则
那么差异主要是由于表中包含的数据。
答案 1 :(得分:0)
主要参数之一,sql server在创建计划时使用的是统计数据。所以即使两个表中的数据相同,统计数据也会不同,这会导致执行计划不同
要查看表格的统计信息,您可以使用以下
dbcc show_statistics('SFMatl','AppUpdated')
dbcc show_statistics('SFMatl','SFMatl_Backup20042017')
如果您现在,如何推断上述结果,您将能够看到,上述两个表的统计数据不同,这进一步决定了计划质量