【问题标题】:mysql: make individual tables for each users is good idea? Which structure is better for finding users?mysql:为每个用户制作单独的表是个好主意吗?哪种结构更适合寻找用户?
【发布时间】:2017-04-30 11:04:17
【问题描述】:

我正在开发一个简单的社交网络,其中在同一个 MySQL 数据库中有用户和他们对朋友的请求

我需要为用户组织一次快速搜索。我需要找到尚未向朋友发送请求的用户。

目前我有这个结构:

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_idcounty_idage 过滤

我的第一个 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 sec 因此,如果用户发送了 100 个请求,那么 0.05 * 100 = 5 sec 将用于过滤 1 个用户。

很明显,您不能使用NOT EXISTS 进行过滤,因为它每次都会为每个用户运行。

我的第二个 SQLLEFT 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 保持排序的方法。

现在我正在考虑为每个用户创建一个个性化的表格,其中只会存储他们对朋友的请求

因此,我们可以在我的 SQL 的第一个版本中使用 NOT EXISTS 排除用户 但是现在所有用户都将根据他们对朋友的个人请求进行过滤

例如,在第一个变体中,为了过滤 1,用户 NOT EXISTS 在 500,000 个其他请求中搜索了他的好友请求。 现在,对于 1 个用户的过滤,NOT EXISTS 将只检查 100 - 1000 个此用户个人对朋友的请求。 但是这种方法需要在数据库中创建数百万个表。

这个想法有多好?对于这项任务,您还能提供哪些其他好的解决方案?

附:对不起我的英语

【问题讨论】:

    标签: mysql performance data-structures database-performance


    【解决方案1】:

    你真的认为做几万张表是个好主意吗?

    NOT EXISTS 很好,你可能只是缺少索引。您需要两个索引,在 (from_id, to_id) 和 (to_id, from_id) 上。你需要他们两个。您也可以尝试将NOT EXISTS (A OR B) 重写为NOT EXISTS A AND NOT EXISTS B,但可能是一样的。

    【讨论】:

    • 我在他们的展位上做了索引,但我很伤心:性能很低。你有更好的主意吗?
    • 查询是否使用它们?一般来说,执行计划是一个很好的信息来源。
    • 什么意思?
    猜你喜欢
    • 2015-08-13
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多