mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: avoid regex on uploads table (#12485)
In extreme circumstances when the uploads table is huge, the old version of this migration could take a very long time. The rewrite extracts the sha1 directly from the badges table and does an index based match on the uploads table
This commit is contained in:
parent
d453d74ca7
commit
e45bca7298
@ -8,10 +8,16 @@ class AddImageUploadIdToBadges < ActiveRecord::Migration[6.0]
|
|||||||
DB.exec <<~SQL
|
DB.exec <<~SQL
|
||||||
UPDATE badges b1
|
UPDATE badges b1
|
||||||
SET image_upload_id = u.id
|
SET image_upload_id = u.id
|
||||||
FROM badges b2
|
FROM (
|
||||||
INNER JOIN uploads u
|
SELECT id, (regexp_matches(b.image, '[a-f0-9]{40}'))[1] as sha1
|
||||||
ON b2.image ~ CONCAT('/', u.sha1, '\\.\\w')
|
FROM badges b
|
||||||
WHERE b1.id = b2.id
|
WHERE
|
||||||
|
b.image IS NOT NULL AND
|
||||||
|
b.image ~ '[a-f0-9]{40}'
|
||||||
|
) b2
|
||||||
|
JOIN uploads u ON u.sha1 = b2.sha1
|
||||||
|
WHERE
|
||||||
|
b1.id = b2.id
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user