简体   繁体   English

数据库模式用户匹配

[英]Database schema User Matching

Among these two schemas solutions about user matching which could be the best with big data? 在这两种方案中,关于用户匹配的解决方案对于大数据而言可能是最好的?

Solution 1: 解决方案1:

CREATE TABLE `user_matches` (
  `user_id_1` int(11) NOT NULL,
  `user_id_2` int(11) NOT NULL,
  `like_user_1` tinyint(1) DEFAULT '0',
  `like_user_2` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`user_id_1`,`user_id_2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

To select the matching among two users I should write this query: 要在两个用户之间选择匹配项,我应该编写以下查询:

SELECT * 
FROM user_matches
WHERE (user_id_1 = 123 OR user_id_2 = 123) AND (like_user_1 = 1 AND like_user_2 = 1)

PS: Imagine that like_user_1 and like_user_2 are both indexed PS:想象一下,like_user_1和like_user_2都已建立索引

Solution 2: 解决方案2:

CREATE TABLE `user_matches` (
  `user_id` int(11) NOT NULL,
  `user_id_liked` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`user_id_liked`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

To select the matching among users I should write this query: 要选择用户之间的匹配,我应该编写以下查询:

    SELECT me.user_id_liked
    FROM user_matches me
    INNER JOIN user_matches you ON me.user_id = you.user_id_liked 
                                AND you.user_id = me.user_id_liked 
                                AND me.user_id = 123

I think that the 2nd solution is the best for the schema and for querying because from and joins clauses are executed before where clause, but on the same times in the first solution I don't need to join tables. 我认为第二种解决方案最适合架构和查询,因为from和joins子句在where子句之前执行,但是在第一种解决方案中,我不需要同时连接表。

I you index like_user_1 and like_user_2 on solution 1 queries should be fast. 我在解决方案1查询中索引like_user_1和like_user_2应该很快。

I would test both solutions and compare execution plans. 我将测试两个解决方案并比较执行计划。 EXPLAIN and EXPLAIN EXTENDED will be useful. EXPLAIN和EXPLAIN EXTENDED将很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM