簡體   English   中英

如何將mysql表拆分為多個表並查詢正確的區域?

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

下面是我的朋友表,
我提供了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');
?>  

我想要做的是根據用戶ID號將friend_friend表拆分為多個表
就像所有在1-20,000之間的用戶ID轉到一張表一樣,所有用戶ID 20,001-40,000、40,001-60,000都進入另一張表

我不確定如何做到最好,我需要在添加新朋友以及檢索用戶的朋友列表時檢測用戶應查詢哪個表
我假設在頂部的代碼中,添加用戶的2個條目將必須分為2個查詢並更新不同的表?

假設您使用的是MySQL 5.1或更高版本,則可以使用分區來執行所需的操作。 請參閱以下鏈接:

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

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

藝術術語是“分片”(以幫助您進行文獻搜索,網絡搜索等)-或至少是一種流行的藝術術語(不幸的是,詞匯在這個領域還沒有完全解決)。 研究完成后,您將發現,伴隨而來的問題是,當您不知道在哪里(部分或全部)時,必須查詢所有分片(通常是UNION ALL,或者有時以不同的方式聚合它們)。答案可能是。

因此,應該以特定於應用程序的方式建議分片(特別是“水平分片”,這就是您在此處執行的操作),以嘗試將“一起”的條目進行分組,以便盡可能多地檢查單個分片就足夠了。 垂直分片(在不同的表中放入不同的列,而不是行)在設計上更容易,因為您只需要檢查最頻繁的查詢,以確保幾乎每個(理想的是一個)分片都能完全滿足它們。

哦,而且,當然,在確實需要這樣做之前,真的不值得做大量的微妙的高級工作-然后,將確保數據庫后端的工作分配給許多服務器,因為單台服務器再也無法削減了。 您似乎只是在嘗試學習分片的基本原理(如果我讀錯了,我深表歉意!-),而問題的一部分(例如系統體系結構的其他重要部分)就是沒有真正的直到系統大小超出“玩具應用程序”中合理顯示的范圍...!-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM