0

我们有 3 张桌子。

第一行 10,000 行,第二行 80,000 行,第三行 400 行。

代码运行良好,但最近我们遇到了性能问题。

EXPLAIN ANALYZE SELECT "users_users"."id", "users_users"."email" 
FROM "users_users" WHERE (NOT ("users_users"."email" IN 
     (SELECT U0."email" FROM "users_blacklist" U0)) 
     AND NOT ("users_users"."id" IN (SELECT U0."user_id" 
FROM "games_user2game" U0))) ORDER BY "users_users"."id" DESC;
                                                                       QUERY PLAN                                                                        
---------------------------------------------------------------------------------------------------------------------------------------------------------
 Index Scan Backward using users_user_pkey on users_users  (cost=9.25..12534132.45 rows=2558 width=26) (actual time=46.101..77158.318 rows=2510 loops=1)
   Filter: ((NOT (hashed SubPlan 1)) AND (NOT (SubPlan 2)))
   Rows Removed by Filter: 7723
   SubPlan 1
     ->  Seq Scan on users_blacklist u0  (cost=0.00..8.20 rows=420 width=22) (actual time=0.032..0.318 rows=420 loops=1)
   SubPlan 2
     ->  Materialize  (cost=0.00..2256.20 rows=77213 width=4) (actual time=0.003..4.042 rows=35774 loops=9946)
           ->  Seq Scan on games_user2game u0  (cost=0.00..1568.13 rows=77213 width=4) (actual time=0.011..17.159 rows=77213 loops=1)
 Total runtime: 77159.689 ms
(9 rows)

主要问题:可以吗,我们在加入 2 个少于 100,000 行的表时遇到性能问题?

在哪里挖?我们应该更改查询还是深入研究数据库设置?

UPD临时解决方案是通过在代码中预取子查询来摆脱子查询。

4

2 回答 2

1

我在 SQL Server 上遇到过类似的问题,并用存在重写了查询,正如@Scotch 建议的那样,效果很好。

SELECT 
    "users_users"."id", 
    "users_users"."email" 
FROM "users_users" 
WHERE 
NOT EXISTS 
(
    SELECT NULL FROM "users_blacklist" WHERE "users_blacklist"."email" = "users_users"."email"
)
AND NOT EXISTS
(
    SELECT NULL FROM "games_user2game" WHERE "games_user2game"."user_id" = "users_users"."user_id"
)
ORDER BY "users_users"."id" DESC;

此查询将为您提供所有未列入黑名单且不在游戏中的用户。根据 postgres 计划查询的方式,它可能比外部连接选项更快。

于 2013-06-24T22:22:09.793 回答
1

我不知道 SQL 的 postgres 方言,但可能值得尝试外部连接。在许多其他 dbms 中,它们可以提供比子选择更好的性能。

类似的东西

SELECT "users_users"."id", "users_users"."email"
FROM "users_users" us left join  "users_blacklist" uo on uo.email = us.email
                      left join "games_user2game" ug on us.id = ug.user_id
where uo.email is null
AND   ug.id is null

认为这与您的原始查询做同样的事情,但您必须进行测试以确保。

于 2013-06-24T22:08:27.503 回答