0

我想获取基于 SQL Server 数据库的数据子集,这样我就可以为应用程序运行一些冒烟测试。我的典型工作流程如下,但删除查询需要一个多小时,因为 X、Y 和 Z 中有数亿行。我怎样才能让它更快?为了清楚起见,请在您的答案中使用 T-SQL 代码。

  • 通过为每个表运行以下命令来备份每个表:

    SELECT * INTO BACKUP_SRC_PATIENT FROM [SRC_PATIENT] -- has PK patient_id
    SELECT * INTO BACKUP_SRC_X FROM [SRC_X] -- has FK patient_id
    SELECT * INTO BACKUP_SRC_Y FROM [SRC_Y] -- has FK patient_id
    SELECT * INTO BACKUP_SRC_Z FROM [SRC_Z] -- has FK patient_id
    
  • 删除除 100 名患者外的所有患者

    DELETE FROM [SRC_PATIENT] 
    WHERE [PATIENT_ID_NONNUM] NOT IN 
          (SELECT top 100 [PATIENT_ID] FROM [SRC_PATIENT]
           WHERE BLA = 'BLA')
    
  • 从 SRC_X、SRC_Y 和 SRC_Z 中删除数据,其中患者 ID 不在 SRC_PATIENT 中

    DELETE FROM [SRC_X] 
    WHERE [PATIENT_ID] NOT IN 
          (SELECT [PATIENT_ID] FROM [SRC_PATIENT])
    
    DELETE FROM [SRC_Y] 
    WHERE [PATIENT_ID] NOT IN 
          (SELECT [PATIENT_ID] FROM [SRC_PATIENT])
    
    DELETE FROM [SRC_Z] 
    WHERE [PATIENT_ID] NOT IN 
          (SELECT [PATIENT_ID] FROM [SRC_PATIENT])
    
4

2 回答 2

0

而不是删除除 100 之外的所有行 - 截断表并从备份中复制超过 100 行。

TRUNCATE剪辑表中的所有数据,而不必逐行(或逐页删除,视情况而定):

TRUNCATE TABLE [SRC_PATIENT]
TRUNCATE TABLE [SRC_X]
TRUNCATE TABLE [SRC_Y]
TRUNCATE TABLE [SRC_Z]

INSERT INTO [SRC_PATIENT]
SELECT TOP 100 * FROM [BACKUP_SRC_PATIENT]
WHERE BLA = 'BLA' 

INSERT INTO [SRC_X]
SELECT * FROM [BACKUP_SRC_X]
WHERE [PATIENT_ID] IN (SELECT [PATIENT_ID] FROM [SRC_PATIENT])

INSERT INTO [SRC_Y]
SELECT * FROM [BACKUP_SRC_Y]
WHERE [PATIENT_ID] IN (SELECT [PATIENT_ID] FROM [SRC_PATIENT])

INSERT INTO [SRC_Z]
SELECT * FROM [BACKUP_SRC_Z]
WHERE [PATIENT_ID] IN (SELECT [PATIENT_ID] FROM [SRC_PATIENT])
于 2013-04-08T18:14:45.513 回答
0

使用模式开关,然后将您想要的行拉到非备份表中

您将需要创建一个具有相同元数据的表,但切换几乎是立即发生的。

CREATE TABLE dbo.Backup-Table1
(Col1 int
Col2 int)

ALTER TABLE dbo.Table1
SWITCH TO dbo.Backup-Table1

INSERT INTO dbo.Table1
SELECT TOP 100 *
FROM dbo.Backup-Table1
WHERE Condition = Met
于 2013-04-08T18:16:18.967 回答