简体   繁体   English

SQL DELETE性能

[英]SQL DELETE performance

delete from a A where a.ID = 132.

The table A contains around 5000 records and A.ID is the primary key in the table A. But it is taking a long time to delete . 表A包含大约5000条记录,A.ID是表A中的主键。但是它需要很长时间才能删除。 Sometimes its getting timed out also . 有时它也会超时。 That table contains three indexes and it is referenced by three foreign keys . 该表包含三个索引,并由三个外键引用。 Can anyone explain me why its taking long time even though we are deleting based on the primary key . 任何人都可以解释为什么即使我们基于主键删除它也需要很长时间。 And please tell me some way to optimize this problem ...? 请告诉我一些优化这个问题的方法......?

Possible causes: 可能的原因:

1) cascading delete operations 1)级联删除操作

2) trigger(s) 2)触发器

3) the type of your primary key column is something other than an integer, thereby forcing a type conversion on each pk value to do the comparison. 3)主键列的类型不是整数,因此强制对每个pk值进行类型转换以进行比较。 this requires a full table scan. 这需要全表扫描。

4) does your query really end in a dot like you posted it in the question? 4)你的查询真的以你在问题中发布的点结尾吗? if so, the number may considered to be a floating point number instead of an integer, thereby causing a type conversion similar to 3) 如果是这样,该数字可以被认为是浮点数而不是整数,从而导致类似于3的类型转换

5) your delete query is waiting for some other slow query to release a lock 5)您的删除查询正在等待其他一些慢查询释放锁定

Obviously it should not be taking a long time. 显然它不应该花费很长时间。 However, there isn't enough information here to figure out exactly why. 但是,这里没有足够的信息来确定原因。 I can tell you, though, that you should focus on the Foreign Keys. 不过,我可以告诉你,你应该专注于外键。

These can slow things down if they impose constraints from other, much larger, tables. 如果它们从其他更大的表中施加约束,这些可能会减慢速度。 You may also find out that your timeouts are due to integrity checks that prevent the delete (then the question is why you aren't getting exceptions instead of a timeout). 您可能还会发现您的超时是由于完整性检查阻止删除(然后问题是您没有获得异常而不是超时的原因)。

My next step would be to remove the foreign keys and then check performance. 我的下一步是删除外键然后检查性能。 Then add each one back in at a time and check performance as you go. 然后一次重新添加每个,并随时检查性能。

Are other operations (eg Inserts, Selects, Updates) taking a long time? 其他操作(例如插入,选择,更新)需要很长时间吗?

First thought: Indexes on foreign keys? 首先想到的是:外键索引?

  • This is related to cascading deletes mentioned 这与提到的级联删除有关
  • All child tables muts be checked and if you have a total of 500,000 child rows, this might take some time of course... 将检查所有子表muts,如果您总共有500,000个子行,这可能需要一些时间...

Second thought: Triggers firing? 第二个想法:触发射击?

  • On this table or on child tables or trying to cascade via code etc 在此表或子表或尝试通过代码级联等
  • God forbid, cursor for each row in DELETED... 上帝禁止,光标在DELETED的每一行......

Try to update the statistics. 尝试更新统计信息。 5000 rows is not a big deal. 5000行并不是什么大问题。 If you're doing this regularly you should schedule maintenance on that table as well (ie re-build indexes, update stats etc.) 如果您经常这样做,您也应该安排该表的维护(即重建索引,更新统计数据等)

As others have observed, the probable suspects are the foreign keys. 正如其他人所观察到的那样,可能的嫌疑人是外键。

Firstly because the ON DELETE CASCADE can gather momentum if the dependent tables in turn are referenced by other tables, which in turn may be referenced, and so on. 首先,因为如果依赖表依次被其他表引用,那么ON DELETE CASCADE可以收集动量,而其他表又可以被引用,依此类推。

Secondly, because other users may have locks on the rows which need to be deleted. 其次,因为其他用户可能对需要删除的行有锁。 This is the most likely cause of the timeouts. 这是超时的最可能原因。 Quite how this works will depend on the flavour and version of your database. 它的工作原理将取决于数据库的风格和版本。 For instance, older versions of Oracle (<=8.0) needed to lock the entire dependent table unless the foreign key columns were indexed. 例如,旧版本的Oracle(<= 8.0)需要锁定整个依赖表,除非索引外键列。

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

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