[mnesia]mnesia内存备份表存储数据总结
今天群里有一朋友(随波逐流磨剑)提了这么一个问题说并行写入时数据丢失,当然最后的原因是他没等所有写入进程执行完就进行查询,所以数据自然少了,不过这不是我要说的重点,重点是我在帮他测试程序的时候遇到了一个警告和一个错误:
警告:** WARNING ** Mnesia is overloaded: {mnesia_tm, message_queue_len, [2107,1441]}
错误:** Too many db tables **
代码:
-module(test_mnesia). %%-include("test_mnesia.hrl"). -export([init/0,insert_recodes/2,select/0,select2/0,clear/0]). -record(hdjsw, {id, bei_bao, ci_shu, fu_jian, jie_shu_date, sheng_cheng_date, shi_yong_date, bian_hao}). -record(hdjoin, {id, hdjsw, name, type}). init() -> %spawn(fun()-> randoms:start() end), case mnesia:create_schema([node()]) of ok -> ok; _ -> mnesia:delete_schema([node()]), mnesia:create_schema([node()]) end, mnesia:start(), case mnesia:create_table(hdjsw,[{ram_copies, []},{type, bag},{attributes, record_info(fields,hdjsw)}]) of {atomic, ok} -> ok; _Any1 -> mnesia:delete_table(hdjsw), mnesia:create_table(hdjsw,[{ram_copies, []},{type, bag},{attributes, record_info(fields,hdjsw)}]) end, case mnesia:create_table(hdjoin,[{ram_copies, []},{type, bag},{attributes, record_info(fields,hdjoin)}]) of {atomic, ok} -> ok; _Any2 -> mnesia:delete_table(hdjoin), mnesia:create_table(hdjoin,[{ram_copies, []},{type, bag},{attributes, record_info(fields,hdjoin)}]) end. % mnesia:create_table(hdjsw,[{ram_copies, []},{attributes, record_info(fields,hdjsw)}]), % mnesia:create_table(hdjoin,[{ram_copies, []},{attributes, record_info(fields,hdjoin)}]). insert_recodes(N,T) ->%200000,2000 C = trunc(N/T),%100 M = N rem T,%0 case C of 0 -> spawn_insert_recode(N,1); _ -> spawn_insert_recode(T,C), spawn_insert_recode(M,1) end. spawn_insert_recode(0,_) -> ok; spawn_insert_recode(N,M) ->%{2000, 100},{500,1} spawn(fun()-> insert_recode(M) end), spawn_insert_recode(N-1,M). insert_recode(0) -> ok; insert_recode(N) ->%10 Hdjsw = #hdjsw{id=get_string(), bei_bao = "dsg", ci_shu = "asdhg", fu_jian = "adfsh", jie_shu_date = "sadhged", sheng_cheng_date = 44, shi_yong_date = "dsfh", bian_hao = 54}, Fun = fun() -> mnesia:write(Hdjsw), insert(Hdjsw,10) end, %spawn(fun()-> mnesia:transaction(Fun) end), mnesia:transaction(Fun), insert_recode(N-1). insert(_Hdjsw,0) -> ok; insert(Hdjsw,N) -> Hdjoin = #hdjoin{ id = get_string(), hdjsw = Hdjsw, name = hdjswname, type = 2 }, mnesia:write(Hdjoin), insert(Hdjsw,N-1). select() -> F = fun() -> Q = #hdjsw{id = '$1', _ = '_'}, mnesia:select(hdjsw, [{Q, [], ['$1']}]) end, {atomic, L} = mnesia:transaction(F), length(L). select2() -> F = fun() -> Q = #hdjoin{id = '$1', _ = '_'}, mnesia:select(hdjoin, [{Q, [], ['$1']}]) end, {atomic, L} = mnesia:transaction(F), length(L). clear() -> mnesia:clear_table(hdjsw), mnesia:clear_table(hdjoin). get_string() -> {M, S, L} = now(), integer_to_list(M*1000000000000+S*1000000+L).