更新查询中的性能问题 [英] Performance issue in update query

查看:91
本文介绍了更新查询中的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对查询性能有一个小疑问。基本上,我有一个包含1C条以上记录的表。 sl_id 是该表中的主键。目前,我正在将表列 status 更新为 true (默认为 false )使用 sl_id

I have one small doubt in query performance. Basically, I have a table with more than 1C records. sl_id is the primary key in that table. Currently, I am updating the table column status to true (default false) by using the sl_id.

在我的程序中,我将在一个数组中包含200个唯一的 sl_id 。我通过使用每个 sl_id 状态更新为 true (总是) $ c>。

In my program, I will have 200 unique sl_id in an array. I am updating the status to true(always) by using each sl_id.

我的疑问:

我应使用单个更新查询通过在一个位置条件中指定每个 sl_id 来更新状态?

Shall I use individual update queries by specifing each sl_id in a where condition to update the status?

(OR)

我应使用 IN 运算符并将所有200个唯一的 sl_id 放在一个查询中?

Shall I use IN operator and put all 200 unique sl_id in one single query?

哪个会更快?

推荐答案

变慢到更快:


  • 200个独立查询,每个都在自己的事务中

  • 200个独立查询,全部在一次交易中

  • 使用 WHERE ... IN(...)进行1个大查询存在的地方(选择...)

  • 1个大查询,其中 INNER JOIN VALUES 子句

  • (仅对于非常大的值列表才更快): COPY value列出临时表,为其编制索引,并在临时表上 JOIN

  • 200 Individual queries, each in their own transaction
  • 200 Individual queries, all in one transaction
  • 1 big query with WHERE ... IN (...) or WHERE EXISTS (SELECT ...)
  • 1 big query with an INNER JOIN over a VALUES clause
  • (only faster for very big lists of values): COPY value list to a temp table, index it, and JOIN on the temp table.

如果您使用数百个我真的建议加入一个 VALUES 子句。对于成千上万个值, COPY 到临时表并对其进行索引,然后对其进行联接。

If you're using hundreds of values I really suggest joining over a VALUES clause. For many thousands of values, COPY to a temp table and index it then join on it.

加入值子句。给出以下 IN 查询:

An example of joining on a values clause. Given this IN query:

SELECT *
FROM mytable
WHERE somevalue IN (1, 2, 3, 4, 5);

等价于 VALUES 的是:

SELECT *
FROM mytable
INNER JOIN (
  VALUES (1), (2), (3), (4), (5)
) vals(v)
ON (somevalue = v);

但是请注意,使用 VALUES 方式是PostgreSQL扩展,wheras IN 或使用临时表是SQL标准。

Note, however, that using VALUES this way is a PostgreSQL extension, wheras IN, or using a temporary table, is SQL standard.

问题:

  • Postgres NOT IN performance

这篇关于更新查询中的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆