2010-03-24 49 views

回答

4

好吧,如果你需要分區的小400.000行表中獲取其他數據庫比MySQL。認真。按照現代標準,任何低於1.000.000行的表格通常都會忽略大小(甚至不是很小),除非你還沒有任何索引等。現代標準在這方面大約有10年的歷史。

+0

好,號我不能找不到分區與否的建議。我認爲它也會對小桌面產生明顯的影響。 – Industrial 2010-03-25 07:34:43

+1

不是。分區基本上是當你遇到有一個表的問題時。理論上,例如,因爲你每年保持銷售額 - 刪除一年的所有銷售額真的很難,但通過分區,你可以在一年內刪除該表。小事情就是這樣的零感覺。 – TomTom 2010-03-25 08:52:47

+0

非常感謝TomTom。欣賞你的答案。使用分區的刪除部分是顯而易見的,但從我以前聽說過的情況來看,分區表總是比非分區的表現更好,即使使用較小的數據集也是如此。然而,我不知道在外鍵上的性能提高的比較。看起來像一個很少有關於信息的主題。也許我將不得不艱難地找出基準和樣本數據...... – Industrial 2010-03-25 13:35:45

1

那麼,對於複雜的數據模型,分區並不是一個好的解決方案。如果你只有2到3張桌子,你可能可以做到,但不是很漂亮。每個表必須有一列確定分區。然後,每個表必須有一個觸發器來創建新表,設置外鍵和唯一約束。

例如, audittransaction < - auditentry

每個audittransactionhas 0到n auditentry。表auditistry包含事務的外鍵。這兩個表都必須具有列creationDate,因爲它用於對兩個表進行分區。

------創建觸發器觸發中插入audittransaction

create or replace function audittransaction_insert_function() 
returns trigger as $$ 
DECLARE 

    tablepartition varchar; 
    tablename varchar; 
    startbounds timestamp; 
    endbounds timestamp;     


BEGIN 
    tablepartition := to_char(date_trunc('month', NEW.whendone), 'YYYYMMDD'); 
    tablename := 'audittransaction_' || tablepartition ;   

    if not exists(select * from information_schema.tables where table_name = tablename) then 
     startbounds := date_trunc('month', NEW.whendone); 
     endbounds := startbounds + cast('1 months' as interval); 
     execute 'create table ' || tablename || ' (CHECK (whendone >= ' || quote_literal(startbounds) || ' and whendone < ' || quote_literal(endbounds)|| ')) inherits (audittransaction)'; 
     execute 'ALTER TABLE '|| tablename ||' ADD CONSTRAINT '||tablename||'_unique_id UNIQUE (id)';   
    end if;  
    execute 'insert into ' || tablename || ' (id, operationid, whendone, "comment", ticketid ,transactionid, userid) values (' || quote_literal(NEW.id) || ',' || quote_literal(NEW.operationid) || ',' || quote_literal(NEW.whendone) || ')';     
    return null; 
END; $$ 
LANGUAGE plpgsql; 

create trigger insert_audittrans 

-----然後,創建一個觸發autientry

create or replace function auditentry_insert_function() 
returns trigger as $$ 
DECLARE 
    tablepartition varchar; 
    tablename varchar; 
    startbounds timestamp; 
    endbounds timestamp;     


BEGIN 
    tablepartition := to_char(date_trunc('month', NEW.transactiontimestampgmt), 'YYYYMMDD'); 
    tablename := 'auditentry_' || tablepartition ; 

    if not exists(select * from information_schema.tables where table_name = tablename) then 
     startbounds := date_trunc('month', NEW.transactiontimestampgmt); 
     endbounds := startbounds + cast('1 months' as interval); 
     execute 'create table ' || tablename || ' (CHECK (transactiontimestampgmt >= ' || quote_literal(startbounds) || ' and transactiontimestampgmt < ' || quote_literal(endbounds)|| ')) inherits (auditentry)'; 
     execute 'ALTER TABLE '|| tablename ||' ADD CONSTRAINT '||tablename||'_unique_id UNIQUE (id)'; 
     execute 'ALTER TABLE ' || tablename ||' ADD CONSTRAINT auditentry FOREIGN KEY (audit_transaction_id) REFERENCES audittransaction_'||tablepartition ||'(id)';     
    end if;  
    execute 'insert into ' || tablename || ' (id, audit_transaction_id, eventid, transactiontimestampgmt,timestampgmt, acknowledged, resolved, acknowledgedbyusername, acknowledgeddate, notificationlevel, resolvedbyusername, resolveddate, severity, parentauditentry_id) values (' || quote_literal(NEW.id) || ',' || quote_literal(NEW.audit_transaction_id) || ',' || quote_literal(NEW.eventid) || ','||quote_literal(NEW.transactiontimestampgmt)||')';    
    return null; 
END; $$ 
LANGUAGE plpgsql; 

create trigger insert_auditentry before insert on auditentry for each row execute procedure auditentry_insert_function();