2010-09-15 27 views
0

我有一個postgress中的用戶表。 每個用戶可以有0到多個網站。POSTGRESQL有效的方式來拆分表性能明智

我知道,每次從數據庫獲取用戶對象時,都會浪費內存,當然我不知道用戶將擁有多少個網站。

我可以有一個表格稱爲網站,但我認爲這可能會再次發生其他類型的列表,我想添加在用戶配置文件下。

這個問題的最佳解決方案是什麼?

注意:最好的解決方案,不會影響網站的性能。 FYI:該網站將在Ruby on Rails的運行3

+0

對不起,我還不夠清楚。 我不是在談論分頁,而是在用戶表中存儲數據(在本例中是網站)。 可以說,用戶可以添加到他的個人資料幾個選項,如: 興趣,網站,聯繫號碼等 我不能只有一個表。我正在尋找理想的解決方案。 – Sharethefun 2010-09-15 21:43:27

回答

1

你可以有這樣的事情:

create table users (
    user_id serial primary key, 
    username text not null unique 
); 
create table datatypes (
    datatype_id serial primary key, 
    datatype text not null unique 
); 
create table data (
    user_id int not null references users(user_id), 
    datatype_id int not null references datatypes(datatype_id), 
    data text not null 
); 
insert into datatypes (datatype) 
    values ('website','interest','contact_number'); 

然後網站地址 'example.com' 用戶 'testuser的' 添加:

insert into data (user_id, datatype_id, data) 
    select user_id, datatype_id, 'example.com'::text as data 
    from users, datatypes 
    where username='testuser' and datatype='website'; 
+0

我希望我可以投票:) 4更多去。謝謝 – Sharethefun 2010-09-15 22:19:36