我正在开发一个简单的社交网络,其中用户及其对 friend 的请求位于同一个 MySQL 数据库中
我需要组织对用户的快速搜索。我需要查找尚未向 friend 发送请求的用户。
目前我有这样的结构:
mysql> SELECT * FROM profiles;
+----+---------+-----+---------+------------+
| id | name | age | city_id | country_id |
+----+---------+-----+---------+------------+
| 1 | WILLIAM | 20 | 1 | 1 |
| 2 | JOHN | 24 | 1 | 1 |
| 3 | ROBERT | 21 | 3 | 2 |
| 4 | MICHAEL | 33 | 4 | 2 |
| 5 | JAMES | 27 | 16 | 1 |
| 6 | DAVID | 21 | 13 | 666 |
| 7 | RICHARD | 18 | 4 | 2 |
| 8 | CHARLES | 32 | 88 | 5 |
| 9 | JOSEPH | 29 | 5 | 1 |
| 10 | THOMAS | 19 | 1 | 1 |
+----+---------+-----+---------+------------+
mysql> SELECT * FROM request_for_friendship;
+----+---------+-------+
| id | from_id | to_id |
+----+---------+-------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 8 |
| 5 | 4 | 1 |
| 6 | 9 | 1 |
+----+---------+-------+
当id = 1
的用户发送请求“show me users”时,服务器必须返回1个用户,该用户在request_for_friendship
中没有请求,并且结果应该被过滤按 city_id
、county_id
和 age
我的第一个 SQL 是 NOT EXIST
( select 1 random row with complex filtering ):
SELECT *
FROM
(
SELECT *, ABS(profiles.age - 21) AS nearest_age
FROM profiles
WHERE profiles.id != 1
ORDER BY profiles.city_id <> 1, profiles.country_id <> 1, nearest_age
) AS users
WHERE
NOT EXISTS (
SELECT *
FROM request_for_friendship
WHERE
(
request_for_friendship.from_id = 1
AND
request_for_friendship.to_id = users.id
)
OR
(
request_for_friendship.from_id = users.id
AND
request_for_friendship.to_id = 1
)
)
LIMIT 0 , 1;
无限制的结果:
+----+---------+-----+---------+------------+-------------+
| id | name | age | city_id | country_id | nearest_age |
+----+---------+-----+---------+------------+-------------+
| 10 | THOMAS | 19 | 1 | 1 | 2 |
| 5 | JAMES | 27 | 16 | 1 | 6 |
| 6 | DAVID | 21 | 13 | 666 | 0 |
| 7 | RICHARD | 18 | 4 | 2 | 3 |
+----+---------+-----+---------+------------+-------------+
一切都很好,直到 10,000 个用户注册并发送了 500,000 个友谊请求。
此后,每个通过 NOT EXISTS
进行过滤的用户花费了 ~0.05 秒
因此,如果用户发送 100 个请求,则过滤 1 个用户将花费 0.05 * 100 = 5 秒
。
很明显,您不能使用 NOT EXISTS
进行过滤,因为它每次都会为每个用户运行。
我的第二个 SQL 是使用 LEFT JOIN
( mysql: how to save ORDER BY after LEFT JOIN without reorder? ):
SELECT * FROM
(
SELECT *, ABS(profiles.age - 21) AS nearest_age
FROM profiles
WHERE profiles.id != 1
ORDER BY profiles.city_id <> 1, profiles.country_id <> 1, nearest_age
) as users
LEFT JOIN request_for_friendship
AS request_for_friendship_copy
ON
(
request_for_friendship_copy.from_id = 1
AND
request_for_friendship_copy.to_id = users.id
)
OR
(
request_for_friendship_copy.from_id = users.id
AND
request_for_friendship_copy.to_id = 1
);
LIMIT 1;
无限制的结果:
+----+---------+-----+---------+------------+-------------+------+---------+-------+
| id | name | age | city_id | country_id | nearest_age | id | from_id | to_id |
+----+---------+-----+---------+------------+-------------+------+---------+-------+
| 2 | JOHN | 24 | 1 | 1 | 3 | 1 | 1 | 2 |
| 3 | ROBERT | 21 | 3 | 2 | 0 | 2 | 1 | 3 |
| 8 | CHARLES | 32 | 88 | 5 | 11 | 3 | 1 | 8 |
| 4 | MICHAEL | 33 | 4 | 2 | 12 | 5 | 4 | 1 |
| 9 | JOSEPH | 29 | 5 | 1 | 8 | 6 | 9 | 1 |
| 5 | JAMES | 27 | 16 | 1 | 6 | NULL | NULL | NULL |
| 6 | DAVID | 21 | 13 | 666 | 0 | NULL | NULL | NULL |
| 7 | RICHARD | 18 | 4 | 2 | 3 | NULL | NULL | NULL |
| 10 | THOMAS | 19 | 1 | 1 | 2 | NULL | NULL | NULL |
+----+---------+-----+---------+------------+-------------+------+---------+-------+
此 SQL 非常快(~0.02s
),但如您所见,ORDER BY
已损坏。当我将 ORDER BY 移动到底部(在 JOIN 之后)时,花费了 ~3.2s
。
效果更好,但当用户数量达到 1 000 000 左右时,就会花费很多时间。我没有找到用 LEFT JOIN
保持排序的方法。
现在我正在考虑为每个用户创建一个个性化表格,其中仅存储他们对 friend 的请求
因此,我们可以像在我的 SQL 的第一个版本中那样使用 NOT EXISTS 排除用户 但现在所有用户都将根据他们对 friend 的个人请求进行过滤
例如,在第一个变体中,为了过滤 1,用户 NOT EXISTS
在 500,000 个其他请求中搜索他的 friend 请求。
现在,对于 1 个用户的过滤,NOT EXISTS
将仅检查该用户向好友发出的 100 - 1000 个请求。
但这种方法需要在数据库中创建数百万张表。
这个主意有多好?您还能为这项任务提供哪些其他好的解决方案?
附注抱歉我的英语不好
最佳答案
你真的认为制作数万张 table 是个好主意吗?
NOT EXISTS
很好,您可能只是缺少索引。您需要两个索引,on (from_id, to_id) 和on (to_id, from_id)。你需要他们两个。您还可以尝试将 NOT EXISTS (A OR B)
重写为 NOT EXISTS A AND NOT EXISTS B
,但可能是相同的。
关于mysql:为每个用户创建单独的表是个好主意吗?哪种结构更适合寻找用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705679/