简体   繁体   English

SQL Server删除性能问题

[英]SQL Server DELETE performance issues

I have a table with approximately 1 million rows. 我有一张约有100万行的表。 Part of our maintenance involves deleting old row each day, but this is taking about 40 minutes. 我们的部分维护工作涉及每天删除旧行,但这大约需要40分钟。

The delete statement is: delete语句为:

DELETE 
  FROM [dbGlobalPricingMatrix].[dbo].[tblPricing]
  WHERE type = 'car'
  AND capid NOT IN
  (SELECT cder_id  FROM PUB_CAR.dbo.CapDer WHERE cder_discontinued 
IS NULL OR DATEDIFF(dd,cder_discontinued,GETDATE()) <= 7)
  AND source = @source

Is there anything I can do to improve the performance? 我有什么办法可以改善性能?

Thanks 谢谢

As Requested: 按照要求:

CREATE TABLE [dbo].[tblPricing](
[id] [int] IDENTITY(1,1) NOT NULL,
[type] [varchar](50) NULL,
[capid] [int] NULL,
[source] [varchar](50) NULL,
[product] [varchar](50) NULL,
[term] [int] NULL,
[milespa] [int] NULL,
[maintained] [bit] NULL,
[price] [money] NULL,
[created] [datetime] NULL,
[updated] [datetime] NULL,
[notes] [varchar](1000) NULL,
[painttype] [char](1) NULL,
[activeflag] [bit] NULL,
[DealerId] [int] NULL,
[FunderId] [int] NULL,
[IsSpecial] [bit] NULL,
[username] [varchar](50) NULL,
[expiry] [datetime] NULL,
 CONSTRAINT [PK_tblPricing] 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 = 90) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[CAPDer](
[cder_ID] [int] NOT NULL,
[cder_capcode] [char](20) NULL,
[cder_mancode] [int] NULL,
[cder_rancode] [int] NULL,
[cder_modcode] [int] NULL,
[cder_trimcode] [int] NULL,
[cder_name] [varchar](50) NULL,
[cder_introduced] [datetime] NULL,
[cder_discontinued] [datetime] NULL,
[cder_orderno] [int] NULL,
[cder_vehiclesector] [tinyint] NULL,
[cder_doors] [tinyint] NULL,
[cder_drivetrain] [char](1) NULL,
[cder_fueldelivery] [char](1) NULL,
[cder_transmission] [char](1) NULL,
[cder_fueltype] [char](1) NULL,
 CONSTRAINT [PK_CapDer] PRIMARY KEY CLUSTERED 

( [cder_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] ) ON [PRIMARY] ([cder_ID] ASC)((PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON,FILLFACTOR = 90),ON [PRIMARY])ON [PRIMARY]

Okay, as I suspected, you do not seem to have indexes for the fields that you reference in your DELETE query. 好的,正如我所怀疑的,您似乎没有在DELETE查询中引用的字段的索引。 So, add indexes for type , capid , cder_discontinued , and source . 因此,添加索引typecapidcder_discontinuedsource

Additionally, you might want to try AND capid IN (SELECT cder_id FROM PUB_CAR.dbo.CapDer WHERE cder_discontinued IS NOT NULL AND DATEDIFF(dd,cder_discontinued,GETDATE()) > 7) . 另外,您可能想尝试AND capid IN (SELECT cder_id FROM PUB_CAR.dbo.CapDer WHERE cder_discontinued IS NOT NULL AND DATEDIFF(dd,cder_discontinued,GETDATE()) > 7) The optimizer of MS-SQL-Server should actually be doing this for you, but you never know, it is worth trying. MS-SQL-Server的优化器实际上应该为您执行此操作,但您永远不知道,值得尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM