简体   繁体   中英

Database schema User Matching

Among these two schemas solutions about user matching which could be the best with big data?

Solution 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

Solution 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.

I you index like_user_1 and like_user_2 on solution 1 queries should be fast.

I would test both solutions and compare execution plans. EXPLAIN and EXPLAIN EXTENDED will be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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