SQL Server性能调优与不相等

时间:2017-09-01 17:13:29

标签: sql sql-server performance equals

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE A(
    [SegFacCode] [nvarchar](100) NULL,
    [CISurveyName] [nvarchar](50) NULL,
    [StartDate] [datetime2](3) NULL,
    [SurveyInterval] [decimal](2, 1) NULL,
    [SurveyType] [nvarchar](20) NULL,
    [SurveyPurpose] [nvarchar](20) NULL,
    [StationNumber] [float] NULL,
    [CISStructurePS] [decimal](6, 3) NULL,
    [CISStructureIRF] [decimal](6, 3) NULL,
    [CISNativePS] [decimal](6, 3) NULL,
    [InspectionDateTime] [datetime2](3) NULL,
    [SurveyRemarks] [nvarchar](max) NULL,
    [Latitude] [decimal](12, 8) NULL,
    [Longitude] [decimal](12, 8) NULL,
    [Elevation] [decimal](5, 0) NULL,
    [TSID] [decimal](15, 0) NULL,
    [TSConnectionType] [nvarchar](8) NULL,
    [CISShortLead] [bit] NULL,
    [LastModifiedDateSurvey] [datetime2](3) NULL,
    [LastModifiedDateReading] [datetime2](3) NULL,
    [IsDeletedSurvey] [bit] NOT NULL,
    [IsDeletedReading] [bit] NOT NULL,
    [CPMSID] [decimal](15, 0) NULL,
    [TelluricInd] [int] NULL,
    [SurveyGUID] [uniqueidentifier] NOT NULL,
    [ReadingGUID] [uniqueidentifier] NOT NULL,
    [Hash] [nvarchar](32) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [B](
    [OBJECTID] [int] NOT NULL,
    [CreatedBy] [nvarchar](45) NULL,
    [CreatedDate] [datetime2](7) NULL,
    [EffectiveFromDate] [datetime2](7) NULL,
    [EffectiveToDate] [datetime2](7) NULL,
    [EventID] [uniqueidentifier] NULL,
    [OriginEventID] [uniqueidentifier] NULL,
    [LastModified] [datetime2](7) NULL,
    [ModifiedBy] [nvarchar](45) NULL,
    [HistoricalState] [nvarchar](30) NULL,
    [ProcessFlag] [nvarchar](10) NULL,
    [SourceGCL] [nvarchar](25) NULL,
    [GlobalID] [uniqueidentifier] NOT NULL,
    [CLEditResponse] [nvarchar](30) NULL,
    [CLValidityTolerance] [numeric](15, 2) NULL,
    [RouteEventID] [uniqueidentifier] NULL,
    [SeriesEventID] [uniqueidentifier] NULL,
    [Status] [nvarchar](30) NULL,
    [SymbolRotation] [numeric](15, 2) NULL,
    [POINT_X] [numeric](38, 8) NULL,
    [POINT_Y] [numeric](38, 8) NULL,
    [POINT_Z] [numeric](38, 8) NULL,
    [InspectionDate] [datetime2](7) NULL,
    [PSOn] [numeric](7, 4) NULL,
    [PSOff] [numeric](7, 4) NULL,
    [Static] [numeric](7, 4) NULL,
    [DCPotentialVolts] [numeric](7, 4) NULL,
    [Description] [nvarchar](50) NULL,
    [PICPInspectionEventID] [uniqueidentifier] NULL,
    [Shape] [geography] NULL,
    [Station] [numeric](16, 3) NULL,
    [Measure] [numeric](16, 3) NULL,
    [TCTestLeadEventID] [uniqueidentifier] NULL,
    [TCShortLeadIndicatorLF] [nvarchar](1) NULL,
    [Comments] [nvarchar](2000) NULL,
    [VendorUniqueID] [uniqueidentifier] NULL,
    [VendorChainage] [numeric](16, 3) NULL,
    [TCPCSReadingHash] [nvarchar](32) NULL,
 CONSTRAINT [R121_pk] PRIMARY KEY CLUSTERED 
(
    [OBJECTID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 75) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [C](
    [OBJECTID] [int] NOT NULL,
    [CreatedBy] [nvarchar](45) NULL,
    [CreatedDate] [datetime2](7) NULL,
    [EffectiveFromDate] [datetime2](7) NULL,
    [EffectiveToDate] [datetime2](7) NULL,
    [EventID] [uniqueidentifier] NULL,
    [OriginEventID] [uniqueidentifier] NULL,
    [LastModified] [datetime2](7) NULL,
    [ModifiedBy] [nvarchar](45) NULL,
    [HistoricalState] [nvarchar](30) NULL,
    [ProcessFlag] [nvarchar](10) NULL,
    [SourceGCL] [nvarchar](25) NULL,
    [GlobalID] [uniqueidentifier] NOT NULL,
    [CLEditResponse] [nvarchar](30) NULL,
    [CLValidityTolerance] [numeric](15, 2) NULL,
    [RouteEventID] [uniqueidentifier] NULL,
    [InServiceDate] [datetime2](7) NULL,
    [OperationalStatus] [nvarchar](30) NULL,
    [SiteEventID] [uniqueidentifier] NULL,
    [SeriesEventID] [uniqueidentifier] NULL,
    [SymbolRotation] [numeric](15, 2) NULL,
    [POINT_X] [numeric](38, 8) NULL,
    [POINT_Y] [numeric](38, 8) NULL,
    [POINT_Z] [numeric](38, 8) NULL,
    [TypeCL] [nvarchar](50) NULL,
    [StatusCL] [nvarchar](50) NULL,
    [DateInstalled] [datetime2](7) NULL,
    [Shape] [geography] NULL,
    [TCTestStationID] [numeric](10, 0) NULL,
    [Comments] [nvarchar](2000) NULL,
    [Station] [numeric](16, 3) NULL,
    [Measure] [numeric](16, 3) NULL,
    [Name] [nvarchar](50) NULL,
    [TCOperatingStatusCL] [nvarchar](50) NULL,
    [SubSystemEventID] [uniqueidentifier] NULL,
    [TCInactiveReasonCL] [nvarchar](50) NULL,
    [TCHasRMULF] [nvarchar](1) NULL,
    [VendorUniqueID] [uniqueidentifier] NULL,
    [Description] [nvarchar](320) NULL,
 CONSTRAINT [R144_pk] PRIMARY KEY CLUSTERED 
(
    [OBJECTID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 75) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO
ALTER TABLE [B] CHECK CONSTRAINT [g41_ck]

GO
ALTER TABLE [C] CHECK CONSTRAINT [g64_ck]
GO

我有以下查询 - 表A和B有一个连接条件,在WHERE子句中也不相同:

SELECT 
    [SegFacCode]
    ,[CISurveyName]
    ,[StartDate]
    ,[SurveyInterval]
    ,[SurveyType]
    ,[SurveyPurpose]
    ,[StationNumber]
    ,[CISStructurePS]
    ,[CISStructureIRF]
    ,[CISNativePS]
    ,[InspectionDateTime]
    ,[SurveyRemarks]
    ,[Latitude]
    ,[Longitude]
    ,[Elevation]
    ,[TSID]
    ,[TSConnectionType]
    ,[CISShortLead]
    ,[LastModifiedDateSurvey]
    ,[LastModifiedDateReading]
    ,[IsDeletedSurvey]
    ,[IsDeletedReading]
    ,[CPMSID]
    ,[TelluricInd]
    ,[SurveyGUID]
    ,[ReadingGUID]
    ,[Hash] RecordHash
    ,tl.EventID as TCTestLeadEventID
    ,pr.OBJECTID
    ,pr.GlobalID
    ,pr.EventID
    ,pr.CreatedDate
    ,pr.EffectiveFromDate
FROM 
    A r 
INNER JOIN 
    B pr ON pr.VendorUniqueID = r.ReadingGUID 
LEFT JOIN 
    C tl ON r.TSID = tl.TCTestStationID  
WHERE 
    r.[Hash] != pr.TCPCSReadingHash

运行此查询需要16到35分钟。我正在尝试调整此查询并需要帮助。 How to Open FMB Files

表上的行数是:

  • A - 6,471,772
  • B - 6,456,735
  • C - 147,417

以下是使用的索引:

Execution Plan

1 个答案:

答案 0 :(得分:0)

考虑到您不想在一分钟内更改索引,可以将查询更改为如下所述。这会降低您的数据吞吐量。

SELECT X.*, C.EventID as TCTestLeadEventID FROM (SELECT [SegFacCode]
,[CISurveyName]
,[StartDate]
,[SurveyInterval]
,[SurveyType]
,[SurveyPurpose]
,[StationNumber]
,[CISStructurePS]
,[CISStructureIRF]
,[CISNativePS]
,[InspectionDateTime]
,[SurveyRemarks]
,[Latitude]
,[Longitude]
,[Elevation]
,[TSID]
,[TSConnectionType]
,[CISShortLead]
,[LastModifiedDateSurvey]
,[LastModifiedDateReading]
,[IsDeletedSurvey]
,[IsDeletedReading]
,[CPMSID]
,[TelluricInd]
,[SurveyGUID]
,[ReadingGUID]
,[Hash] RecordHash
,pr.OBJECTID
,pr.GlobalID
,pr.EventID
,pr.CreatedDate
,pr.EffectiveFromDate
FROM A r 
INNER JOIN B pr on r.ReadingGUID = pr.VendorUniqueID AND r.[Hash] != pr.TCPCSReadingHash 
  ) X LEFT JOIN C tl ON C.TCTestStationID = X.TSID 

检查以下链接和有关逻辑查询处理的文章。这是一个9部分系列。

http://m.sqlmag.com/author/itzik-ben-gan 1