简体   繁体   English

性能优化-MS SQL Delete查询,NOT IN无效

[英]Performance optimisation for - MS SQL Delete query with NOT IN caluse

I have following SQL query, and it is taking long time to complete. 我有以下SQL查询,并且需要很长时间才能完成。 I wanted to check if a different query can be used to achieve the same result, but could also help with performance. 我想检查是否可以使用其他查询来获得相同的结果,但同时也可以提高性能。

Delete from table1 WHERE fk_id = '1234' AND pk_id NOT IN ('aaaa', 'bbbb', 'cccc');

Note: pk_id is PK column and fk_id is a FK for table1 . 注意: pk_idPK列, fk_idtable1的FK。

I don't have any suggestions about the query, it's pretty basic. 我对查询没有任何建议,这很基本。

But you might want to try adding an index: 但您可能想尝试添加索引:

ALTER TABLE table1 ADD INDEX (fk_id);

With the data you have provided there seems to be no problem with your query as you have used 'AND' condition. 使用您提供的数据,因为您使用了“ AND”条件,所以查询似乎没有问题。 Now you need to consider and analyze your database that why is your query taking so much time. 现在,您需要考虑和分析数据库,为什么您的查询要花费这么多时间。 So you might want to check out with the following things. 因此,您可能需要检查以下内容。

1) select count(1) 1)选择计数(1)
from table1 来自表1
where fk_id = '1234' AND pk_id NOT IN ('aaaa', 'bbbb', 'cccc') 其中fk_id ='1234'和pk_id NOT IN('aaaa','bbbb','cccc')
This will give you the count of rows to be deleted. 这将为您提供要删除的行数。

2) Try something like 2)尝试类似
SELECT 选择
T.name AS TableName ,O.name TriggerName T.name AS表名,O.name触发器名
FROM sysobjects O 从sysobjects O
INNER JOIN sys.tables T ON T.object_id = O.parent_obj 内部联接系统表T ON T.object_id = O.parent_obj
WHERE O.type = 'TR' AND T.name = 'table1' O.type ='TR'和T.name ='table1'

This will tell you the triggers associated with your table. 这将告诉您与表关联的触发器。

3) You need to further investigate on the table properties for indexes, any constraints present over there. 3)您需要进一步研究索引的表属性,以及那里存在的任何约束。

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

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