外键与分区

时间:2010-03-24 23:31:23

标签: mysql foreign-keys innodb partitioning myisam

由于目前分区的mySQL数据库不支持外键,我希望听到一些读取繁重的应用程序的专业版和内容,它将处理每个表大约1-400 000行。不幸的是,我在这方面还没有足够的经验来自己做出结论......

非常感谢!

参考文献:

How to handle foreign key while partitioning

Partitioning mySQL tables that has foreign keys?

2 个答案:

答案 0 :(得分:4)

好吧,如果你需要为一个只有400.000行的表进行分区,那么获取另一个数据库而不是MySQL。认真。根据现代标准,任何低于1.000.000行的表通常都是可以忽略不计的(甚至不是很小),除非你也没有任何索引等。现代标准在这方面大约有10年的历史。

答案 1 :(得分:1)

好吧,对于复杂的数据模型,分区不是一个好的解决方案。如果您只有2到3个表彼此依赖,您可能可以这样做,但它不漂亮。每个表必须有一个确定分区的列。然后,每个表必须有一个触发器来创建新表,设置外键和唯一约束。

例如, audittransaction< - auditentry

每个audittransaction都有0到n auditentry。 table auditentry包含事务的外键。两个表都必须具有列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();