我有一张有 200,000 行的表。
当我通过 ActiveRecord 模型进行插入/更新时,我看到接近 20 个相同的 Exist 查询,每个查询花费近 100 毫秒!
Domain Load (0.5ms) SELECT "domains".* FROM "domains" WHERE "domains"."name" = 'sgsgroup.in' LIMIT 1
(0.1ms) BEGIN
Domain Exists (90.7ms) SELECT 1 AS one FROM "domains" WHERE LOWER("domains"."name") = LOWER('sgsgroup.in') LIMIT 1
Domain Exists (89.4ms) SELECT 1 AS one FROM "domains" WHERE LOWER("domains"."name") = LOWER('sgsgroup.in') LIMIT 1
Domain Exists (91.6ms) SELECT 1 AS one FROM "domains" WHERE LOWER("domains"."name") = LOWER('sgsgroup.in') LIMIT 1
[...]
Domain Exists (89.7ms) SELECT 1 AS one FROM "domains" WHERE LOWER("domains"."name") = LOWER('sgsgroup.in') LIMIT 1
Domain Exists (89.2ms) SELECT 1 AS one FROM "domains" WHERE LOWER("domains"."name") = LOWER('sgsgroup.in') LIMIT 1
SQL (0.6ms) INSERT INTO "domains" (....
我已经在域表上有“名称”索引。任何想法这里发生了什么以及如何优化这些记录更新?
此外,在记录更新中出现类似这样的相同查询是否正常?