emysql默认每次执行sql前,会ping一次数据库, 很奇怪! 不知道为什么。
下面是粗略做了一下效率测试:
表如下:
mysql> show create table just_test\G;
*************************** 1. row ***************************
Table: just_test
Create 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_T
IMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.02 sec)
在64位win7系统下(CPU: Intel(R) Core(TM) i3 cpu [email protected] 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: 63ms
ok
2> fg_sql_inf:prof(100).
100 loops, using time: 94ms
ok
3> fg_sql_inf:prof(100).
100 loops, using time: 125ms
ok
4> fg_sql_inf:prof(100).
100 loops, using time: 78ms
ok
5> fg_sql_inf:prof(10000).
10000 loops, using time: 4010ms
ok
6> fg_sql_inf:prof(10000).
10000 loops, using time: 3978ms
ok
7> fg_sql_inf:prof(10000).
10000 loops, using time: 3838ms
ok
8> fg_sql_inf:prof(10000).
10000 loops, using time: 3994ms
ok
9> fg_sql_inf:prof(10000).
10000 loops, using time: 4009ms
ok
10> fg_sql_inf:prof(10000).
10000 loops, using time: 3853ms
ok
11> fg_sql_inf:prof(10000).
10000 loops, using time: 3838ms
ok
12> fg_sql_inf:prof(10000).
10000 loops, using time: 3947ms
ok
13> fg_sql_inf:prof(10000).
10000 loops, using time: 4025ms
ok
14> fg_sql_inf:prof(10000).
10000 loops, using time: 4025ms
ok
15> fg_sql_inf:prof(10000).
10000 loops, using time: 3869ms
ok
16> fg_sql_inf:prof(10000).
10000 loops, using time: 3993ms
ok
17> fg_sql_inf:prof(10000).
10000 loops, using time: 3853ms
ok
18> fg_sql_inf:prof(10000).
10000 loops, using time: 3947ms
ok
19> fg_sql_inf:prof(100000).
100000 loops, using time: 39359ms
ok
20> fg_sql_inf:prof(100000).
100000 loops, using time: 39328ms
ok
21> fg_sql_inf:prof(100000).
100000 loops, using time: 39187ms
ok
22> fg_sql_inf:prof(100000).
100000 loops, using time: 39406ms
ok
23> fg_sql_inf:prof(100000).
100000 loops, using time: 39265ms
ok
24> fg_sql_inf:prof(100000).
100000 loops, using time: 39515ms
ok
======== without ping(不包含ping操作)的测试结果========
2> fg_sql_inf:prof(100).
100 loops, using time: 46ms
ok
3> fg_sql_inf:prof(10000).
10000 loops, using time: 2308ms
ok
4> fg_sql_inf:prof(10000).
10000 loops, using time: 2402ms
ok
5> fg_sql_inf:prof(10000).
10000 loops, using time: 2418ms
ok
6> fg_sql_inf:prof(10000).
10000 loops, using time: 2419ms
ok
7> fg_sql_inf:prof(10000).
10000 loops, using time: 2417ms
ok
8> fg_sql_inf:prof(10000).
10000 loops, using time: 2419ms
ok
9> fg_sql_inf:prof(10000).
10000 loops, using time: 2294ms
ok
10> fg_sql_inf:prof(10000).
10000 loops, using time: 2417ms
ok
11> fg_sql_inf:prof(10000).
10000 loops, using time: 2479ms
ok
12> fg_sql_inf:prof(10000).
10000 loops, using time: 2433ms
ok
13> fg_sql_inf:prof(10000).
10000 loops, using time: 2403ms
ok
14> fg_sql_inf:prof(100000).
100000 loops, using time: 23540ms
ok
15> fg_sql_inf:prof(100000).
100000 loops, using time: 23556ms
ok
16> fg_sql_inf:prof(100000).
100000 loops, using time: 23759ms
ok
17> fg_sql_inf:prof(100000).
100000 loops, using time: 23510ms
ok
18> fg_sql_inf:prof(100000).
100000 loops, using time: 23667ms
ok
19> fg_sql_inf:prof(100000).
100000 loops, using time: 26786ms
ok
20> fg_sql_inf:prof(100000).
100000 loops, using time: 23618ms
ok
21> fg_sql_inf:prof(100000).
100000 loops, using time: 23525ms
ok
可见,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).