2009-08-08 112 views
2

下面是我的朋友表,
我包括2个条目以显示它是如何工作的,当用户添加一个人作为朋友时,它将2个条目插入到具有此代码的数据库中;如何将一个mysql表分成多个表并查询正确的区域?

<?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'); 
?> 

我所想要做的是最多分裂friend_friend表为根据用户ID号码的多个表
像1-20,000之间的所有用户ID的去一个表,所有用户ID 20,001-40,000,40,001- 60000都去不同的表

我不知道如何做到这一点最好的,我需要检测添加了新的朋友,当以及用户
我假设的检索时,朋友列表中的用户应查询该表在我的代码顶部,添加用户的2个条目将不得不被分成2个查询并更新不同的表格?

+0

为什么你想分裂用户ID的? – Milhous 2009-08-08 03:06:44

+0

由于这个表格变大了,我认为在一个较小的表上运行查询会更好,它在一年内已​​经有1,700,000行,这个表的数量可能是几百万行,它是我网站上访问量最大的表,通常每天都会有数以千计的查询与之对抗,并且@当交通繁忙时 – JasonDavis 2009-08-08 06:11:30

+0

嗨Jasondavis,您是否找到适合您的问题的正确解决方案?分区或分片之后,最佳方法是什么?现在你的桌子尺寸是多少?感谢您对此的帮助?这是你的问题的评论在http://stackoverflow.com/questions/1247841 – Bujji 2012-05-21 18:35:26

回答

0

艺术的术语是“分片”(以帮助您在文献检索,网络搜索等) - 或者至少,一个流行的名词艺术(词汇不幸在这个领域没有完全解决)。一旦你做了研究,你会发现伴随的问题是不得不查询所有碎片(和UNION ALL,通常是 - 或者有时以不同的方式将它们聚合),当你不知道在哪里(部分或全部)答案可能是。

因此,分片(特别是“水平分片”,这就是你在这里所做的)应该以特定于应用的方式进行建议,尝试对“合在一起”的条目进行分组,以便尽可能检查一个分片就足够了。垂直分片(在不同的表中放置不同的列,而不是行)更容易设计,因为您只需检查最频繁的查询,以确保其中几个(理想情况下只有一个)分片完全满足它们。

哦,当然,如此巨大数量的微妙的高级工作在证明是需要的之前是不值得的 - 然后,它将确保数据库后端的工作被分割服务器,因为单个服务器无法再削减它。你似乎只是试图学习分片的基本原理(如果我正在读这个错误,我很抱歉) - 而且问题的一部分 - 就像系统架构中其他重要的部分一样 - 是没有真正的动机,直到系统大小超过合理的“玩具应用程序”... - -