简体   繁体   English

如何将mysql表拆分为多个表并查询正确的区域?

[英]How can I split a mysql table into multiple tables and query the correct arrea?

Below is my friend table, 下面是我的朋友表,
I included 2 entries to show how it works, When a user adds a person as a friend it inserts 2 entries into the DB with this code; 我提供了2个条目来显示其工作原理。当用户将某人添加为朋友时,它将使用此代码在DB中插入2个条目。

<?PHP

 //status 0=approved 1=declined approval 3=pending approval
$sql = "insert into friend_friend (userid,friendid,status,submit_date) 
    values
    ('$_SESSION[auto_id]','$friendid','0',now()), 
    ('$friendid','$_SESSION[auto_id]','3',now())"; //Line above is my user ID, the other users ID, status 0 for approved on my side, date
                                                    //next entry is the receiving users entry, there ID, my ID, 3 for not approved yet, date
executeQuery($sql);

//So that code above is my php that adds a friend

//Below is my table scheme for the friends table
CREATE TABLE IF NOT EXISTS `friend_friend` (
  `autoid` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(10) DEFAULT NULL,
  `friendid` int(10) DEFAULT NULL,
  `status` enum('1','0','3') NOT NULL DEFAULT '0',
  `submit_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `alert_message` enum('yes','no') NOT NULL DEFAULT 'yes',
  PRIMARY KEY (`autoid`),
  KEY `userid` (`userid`),
  KEY `friendid` (`friendid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1756421 ;

--
-- Dumping data for table `friend_friend`
--
INSERT INTO `friend_friend` (`autoid`, `userid`, `friendid`, `status`, `submit_date`, `alert_message`) VALUES
(637229, 2, 1, '1', '2007-10-18 01:02:00', 'no');
INSERT INTO `friend_friend` (`autoid`, `userid`, `friendid`, `status`, `submit_date`, `alert_message`) VALUES
(637230, 1, 2, '1', '2007-10-18 01:02:00', 'no');

INSERT INTO `friend_friend` (`autoid`, `userid`, `friendid`, `status`, `submit_date`, `alert_message`) VALUES
(637231, 22901, 1, '1', '2007-10-18 02:24:05', 'no');
INSERT INTO `friend_friend` (`autoid`, `userid`, `friendid`, `status`, `submit_date`, `alert_message`) VALUES
(637232, 1, 22901, '1', '2007-10-18 02:24:05', 'no');
?>  

What I am wanting to do is split the friend_friend table up into multiple tables based on user ID number 我想要做的是根据用户ID号将friend_friend表拆分为多个表
Like all user ID's between 1-20,000 go to one table, all userIDs 20,001-40,000, 40,001-60,000 all go to a different table 就像所有在1-20,000之间的用户ID转到一张表一样,所有用户ID 20,001-40,000、40,001-60,000都进入另一张表

I am not sure how to do this best, I would need to detect which table a user should query when adding a new friend and well as when retrieving friend list of users 我不确定如何做到最好,我需要在添加新朋友以及检索用户的朋友列表时检测用户应查询哪个表
I assume in my code at the top, the 2 entries to add a user would have to be broken into 2 queries and update different tables probably? 我假设在顶部的代码中,添加用户的2个条目将必须分为2个查询并更新不同的表?

Assuming you are using MySQL 5.1 or above, then you can use partitioning to do what you want. 假设您使用的是MySQL 5.1或更高版本,则可以使用分区来执行所需的操作。 See the following links: 请参阅以下链接:

http://dev.mysql.com/doc/refman/5.1/en/partitioning.html http://dev.mysql.com/doc/refman/5.1/en/partitioning.html

http://dev.mysql.com/tech-resources/articles/performance-partitioning.html http://dev.mysql.com/tech-resources/articles/performance-partitioning.html

The term of art is "sharding" (to help you in literature searches, web searches, etc) -- or at least, one popular term of art (vocabulary is unfortunately not fully settled in this area). 艺术术语是“分片”(以帮助您进行文献搜索,网络搜索等)-或至少是一种流行的艺术术语(不幸的是,词汇在这个领域还没有完全解决)。 Once you do research it you'll learn that the concomitant problem is having to query all shards (and UNION ALL them, typically -- or sometimes aggregate them back in different ways) when you don't know where (part or all of) the answer(s) may be. 研究完成后,您将发现,伴随而来的问题是,当您不知道在哪里(部分或全部)时,必须查询所有分片(通常是UNION ALL,或者有时以不同的方式聚合它们)。答案可能是。

So, sharding (in particular "horizontal sharding", which is what you're doing here) should be done advisedly in application-specific ways, to try and group entries that are "together" so that as often as feasible checking a single shard will suffice. 因此,应该以特定于应用程序的方式建议分片(特别是“水平分片”,这就是您在此处执行的操作),以尝试将“一起”的条目进行分组,以便尽可能多地检查单个分片就足够了。 Vertical sharding (putting different columns, rather than rows, in different tables) is easier to design, as you need only examine the most frequent queries to ensure each of them can be entirely satisfied by very few (ideally only one) shard. 垂直分片(在不同的表中放入不同的列,而不是行)在设计上更容易,因为您只需要检查最频繁的查询,以确保几乎每个(理想的是一个)分片都能完全满足它们。

Oh, and, of course, this huge amount of delicate, advanced work is NOT really worth doing until it does get proven to be needed -- and then, it will be to ensure the database backend's work is split among many servers, because a single server just can't cut it any longer. 哦,而且,当然,在确实需要这样做之前,真的不值得做大量的微妙的高级工作-然后,将确保数据库后端的工作分配给许多服务器,因为单台服务器再也无法削减了。 You appear to be just trying to learn the very fundamentals of sharding (my apologies if I'm reading this wrong!-) and part of the problem -- like for other hard and important parts of system architecture -- is that there's no real motivation until the system size goes WELL above what's reasonable to present in a "toy application"...!-) 您似乎只是在尝试学习分片的基本原理(如果我读错了,我深表歉意!-),而问题的一部分(例如系统体系结构的其他重要部分)就是没有真正的直到系统大小超出“玩具应用程序”中合理显示的范围...!-)

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

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