简体   繁体   English

如何更新 jsonb 类型值的大型(100 万行以上)postgres 列

[英]How to update a large (1 million+ rows) postgres column of jsonb type values

Trying to update a specific array inside of a jsonb type in a column called params, and having issues with how long it's taking.尝试在名为 params 的列中更新 jsonb 类型内的特定数组,并且在花费多长时间时遇到问题。 For example, there is a table with a row that contains an array owners例如,有一个表,其中一行包含一个数组所有者

{
  "hidden": false,
  "org_id": "34692",
  "owners": [
    "tim@facebuk.com"
  ],
  "deleted": false
}

And another example另一个例子

{
  "hidden": false,
  "org_id": "34692",
  "owners": [
    "tim@google.com"
  ],
  "deleted": false
}

And there's essentially a million of these rows (all with different email domains as owners . I have this query which I want to execute across all of these rows:而且基本上有一百万行(所有这些行都具有不同的 email 域作为owner 。我有这个查询,我想在所有这些行中执行:

UPDATE table set params = CASE WHEN params->>'owners' NOT LIKE '%google.com%' THEN jsonb_set(params, '{owners}', concat('"', substr(md5(random()::text), 0, 25), '@googlefake.com"')::jsonb) ELSE params END

I've tested with a dataset of 100, and it executes perfectly time, but doing this with a 1000x multiple, makes the query forever execute, and I've no clue if it will actually successfully complete.我已经使用 100 个数据集进行了测试,它的执行时间很完美,但是使用 1000 倍的倍数执行此操作会使查询永远执行,而且我不知道它是否真的会成功完成。 Not entirely sure how to speed up this process or utilize this in a better fashion.不完全确定如何加快此过程或以更好的方式利用它。 I did try indexing eg CREATE INDEX ON table((params->>'owners'));我确实尝试过索引,例如 CREATE INDEX ON table((params->>'owners')); to no avail.无济于事。 Query has run >1 hour, and there are multiple rows similar to this.查询已运行 >1 小时,并且有多行与此类似。

Am i indexing incorrectly?我索引不正确吗? Also, I've looked into the gin operator and @> won't help since each owner field differs另外,我研究了 gin 运算符,@> 无济于事,因为每个所有者字段都不同

Avoid unnecessary updates with a WHERE clause that filters out the rows that don't need to be modified.使用过滤掉不需要修改的行的WHERE子句避免不必要的更新。 Perhaps supporting that condition with an index can help.也许用索引来支持这种情况会有所帮助。

You may want to run run VACUUM (FULL) once the update is done.更新完成后,您可能希望运行VACUUM (FULL)

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

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