我想获取基于 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])