emysql默认每次执行sql前,会ping一次数据库, 很奇怪! 不知道为什么。
下面是粗略做了一下效率测试:
表如下:
mysql> show create table just_test\G;*************************** 1. row *************************** Table: just_testCreate Table: CREATE TABLE `just_test` ( `id` bigint(20) unsigned NOT NULL, `data` int(11) NOT NULL, `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin1 row in set (0.02 sec)
在64位win7系统下(CPU: Intel(R) Core(TM) i3 cpu 530@2.93GHz 2.93GHz, 内存:8G),
创建该表,然后插入一条记录, 然后重复执行update该表的data字段为某个值(sql如: update just_test set data = 456),
结果:
========with ping(每次update前都ping一次)的测试结果:========
fg_sql_inf:prof(100).
100 loops, using time: 63msok2> fg_sql_inf:prof(100).100 loops, using time: 94msok3> fg_sql_inf:prof(100).100 loops, using time: 125msok4> fg_sql_inf:prof(100).100 loops, using time: 78msok5> fg_sql_inf:prof(10000).10000 loops, using time: 4010msok6> fg_sql_inf:prof(10000).10000 loops, using time: 3978msok7> fg_sql_inf:prof(10000).10000 loops, using time: 3838msok8> fg_sql_inf:prof(10000).10000 loops, using time: 3994msok9> fg_sql_inf:prof(10000).10000 loops, using time: 4009msok10> fg_sql_inf:prof(10000).10000 loops, using time: 3853msok11> fg_sql_inf:prof(10000).10000 loops, using time: 3838msok12> fg_sql_inf:prof(10000).10000 loops, using time: 3947msok13> fg_sql_inf:prof(10000).10000 loops, using time: 4025msok14> fg_sql_inf:prof(10000).10000 loops, using time: 4025msok15> fg_sql_inf:prof(10000).10000 loops, using time: 3869msok16> fg_sql_inf:prof(10000).10000 loops, using time: 3993msok17> fg_sql_inf:prof(10000).10000 loops, using time: 3853msok18> fg_sql_inf:prof(10000).10000 loops, using time: 3947msok19> fg_sql_inf:prof(100000).100000 loops, using time: 39359msok20> fg_sql_inf:prof(100000).100000 loops, using time: 39328msok21> fg_sql_inf:prof(100000).100000 loops, using time: 39187msok22> fg_sql_inf:prof(100000).100000 loops, using time: 39406msok23> fg_sql_inf:prof(100000).100000 loops, using time: 39265msok24> fg_sql_inf:prof(100000).100000 loops, using time: 39515msok
======== without ping(不包含ping操作)的测试结果========
2> fg_sql_inf:prof(100).
100 loops, using time: 46msok3> fg_sql_inf:prof(10000).10000 loops, using time: 2308msok4> fg_sql_inf:prof(10000).10000 loops, using time: 2402msok5> fg_sql_inf:prof(10000).10000 loops, using time: 2418msok6> fg_sql_inf:prof(10000).10000 loops, using time: 2419msok7> fg_sql_inf:prof(10000).10000 loops, using time: 2417msok8> fg_sql_inf:prof(10000).10000 loops, using time: 2419msok9> fg_sql_inf:prof(10000).10000 loops, using time: 2294msok10> fg_sql_inf:prof(10000).10000 loops, using time: 2417msok11> fg_sql_inf:prof(10000).10000 loops, using time: 2479msok12> fg_sql_inf:prof(10000).10000 loops, using time: 2433msok13> fg_sql_inf:prof(10000).10000 loops, using time: 2403msok14> fg_sql_inf:prof(100000).100000 loops, using time: 23540msok15> fg_sql_inf:prof(100000).100000 loops, using time: 23556msok16> fg_sql_inf:prof(100000).100000 loops, using time: 23759msok17> fg_sql_inf:prof(100000).100000 loops, using time: 23510msok18> fg_sql_inf:prof(100000).100000 loops, using time: 23667msok19> fg_sql_inf:prof(100000).100000 loops, using time: 26786msok20> fg_sql_inf:prof(100000).100000 loops, using time: 23618msok21> fg_sql_inf:prof(100000).100000 loops, using time: 23525msok
可见,ping的操作还是有一定影响, 去掉ping之后, 大概会加快80%
附:
update_test_tbl(_NewVal) ->
Table = just_test, TableBin = atom_to_binary(Table, latin1),Cmd = <<"update ", TableBin/binary, " set data = 456">>,
execute(Cmd). % 会调用emysql:execute(?POOL, Cmd)
prof(Times) ->
F = fun() -> update_test_tbl(1) end, run(F, Times). run(Fun, Loop) -> statistics(wall_clock), for(1, Loop, Fun), {_, T1} = statistics(wall_clock), io:format("~p loops, using time: ~pms~n", [Loop, T1]), ok.
for(Max, Max , Fun) ->
Fun();for(I, Max, Fun) -> Fun(),for(I + 1, Max, Fun).