2010-10-31 74 views
0

Web應用PHP,Mysql。用戶可以撰寫文章。如何構建允許用戶訂閱(跟隨)其他用戶文章的算法,然後查看訂閱用戶添加的最後一篇文章的列表?算法必須擴展,以便一個用戶可以訂閱10 000個用戶,並且10 000個用戶可以訂閱一個用戶,並且所有部分都可以快速工作 - 添加新文章並且用戶從訂閱用戶查找最後一篇文章時。訂閱算法,比例尺

回答

1
create table `user`(
    `id` INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT 
); 

create table `subscribes_to` (
    `subscriber_user_id` INT(10) NOT NULL, 
    `subscribed_to_user_id` INT(10) NOT NULL, # Receiver of the subscription 
    PRIMARY KEY(`subscribe_user_id`, `subribed_to_user_id`), 
    KEY `subscriber(`subscribe_user_id`), 
    KEY `subscribed(`subscribed_to_user_id`); 
); 


# Users subscribed to role 100 
SELECT distinct u.* FROM user u 
    JOIN subscribes_to st ON st.subscriber_user_id = u.id 
WHERE 
    st.subscribded_to_user_id = 100; 

# User 100's subsriptions 
SELECT distinct u.* FROM user u 
    JOIN subscribes_to st ON st.subscribded_to_user_id = u.id 
WHERE 
    st.subscriber_user_id = 100; 

附加綱要,以顯示與文章關係:

文章( id INT(10)UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255), body TEXT, date_created DATETIME, date_updated DATETIME, author_user_id int(10) );

# Create new article 
INSERT INTO `article` VALUES (NULL, "Hello", "This is the body", NOW(), NOW(), 1); 

# Find the last 10 articles posted that user 15 suscribes to 
# the author of 
SELECT a.* FROM article a 
    JOIN user ON u.id = a.author_user_id 
    JOIN subscribes_to st ON st.subscribed_to_user_id = u.id 
WHERE st.subscriber_user_id = 15 ORDER BY a.date_created DESC LIMIT 10; 
+0

這並不能解決我的任何問題:1)看到用戶添加的最後一篇文章,一個訂閱; 2)快速添加新文章 - 插入10000行(針對每個訂閱者)。 – codez 2010-10-31 10:21:36