I am doing a delete query which should run pretty much instantly

我正在做一个删除查询,它应该立即运行

DELETE FROM
  TableA
WHERE
  TableA.SomeID IN (SELECT DISTINCT TableB.SomeID FROM TableB WHERE TableB.Something = Blah)

If I execute the query inside the IN statement on its own then it runs instantly. If I then comma separate the results of that and paste them into the outer query then that query runs instantly. However, when I run them together it takes 24 seconds. It's like the inner query is getting executed once per row. I also tried modifying it to use an EXISTS statement but that was also slow. Do I need to resort to a temp table for this?

如果我自己在IN语句中执行查询,那么它会立即运行。如果我然后用逗号分隔它的结果并将它们粘贴到外部查询中,则该查询立即运行。但是,当我一起运行它需要24秒。这就像内部查询每行执行一次。我也尝试修改它以使用EXISTS语句,但这也很慢。我需要求助于临时表吗?

2 个解决方案

#1


1

Nested subquery will be slow. Try using a JOIN instead:

嵌套子查询会很慢。请尝试使用JOIN:

DELETE A
FROM TableA AS A
INNER JOIN TableB AS B
    ON A.SomeID = B.SomeID 
WHERE B.Something = Blah

Also, ensure that TableA.SomeID and TableB.SomeID both have an index on them, to maximize the performance of the join.

此外,请确保TableA.SomeID和TableB.SomeID都具有索引,以最大化连接的性能。

更多相关文章

  1. 1.4.6 收集sql语句的执行计划 2
  2. 在SQL SELECT语句中重用别名字段
  3. sql语句中各子部分的执行顺序
  4. Oracle 尽量避免在SQL语句中使用 OR
  5. SQL Server调优系列进阶篇(查询语句运行几个指标值监测)
  6. 使用navicat for mysql 将查询语句过滤的结果导出成insert语句时
  7. oracle新接触的sql语句
  8. 参数化的Insert语句,事务抛出错误
  9. 求查询成绩表中两门科成绩90分以上的学生学号的SQL语句?

随机推荐

  1. Android大牛的博客
  2. Android布局文件中命名空间的解析
  3. android样式,主题设置心得
  4. 自定义View系列教程02--onMeasure源码详
  5. Android学习小结
  6. 关于Android(安卓)LCD和键盘背光亮度 .
  7. Android(安卓)编译系统 --- 版本信息
  8. [置顶] Android应用开发 第二讲:Android系
  9. [转载]Android及Robotium学习总结【环境
  10. Android RIL 架构