Skip to content

Instantly share code, notes, and snippets.

@riywo
Created March 17, 2011 04:54
Show Gist options
  • Save riywo/873865 to your computer and use it in GitHub Desktop.
Save riywo/873865 to your computer and use it in GitHub Desktop.
DBD::mysqlでのinsertの処理時間(localhost接続)
mysql> select * from microsec limit 10;
+------------+--------+
| epoch | us |
+------------+--------+
| 1300337288 | 938208 |
| 1300337288 | 965069 |
| 1300337288 | 976498 |
| 1300337288 | 987483 |
| 1300337288 | 998476 |
| 1300337289 | 9421 |
| 1300337289 | 20474 |
| 1300337289 | 31551 |
| 1300337289 | 42421 |
| 1300337289 | 53417 |
+------------+--------+
auto_commit = 0
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.9695
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.1182
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.0386
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.9484
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.7235
auto_commit = 1
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.0103
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.2025
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.7875
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.3994
$ perl microsec.pl| awk '{sum+=$1}END{print sum/NR}'
11.0079
use strict;
use warnings;
use DBI;
use Time::HiRes qw(gettimeofday usleep);
my $user = 'root';
my $pass = '';
my $db = 'test';
my $table = 'microsec';
my $auto_commit = 0;
#my $auto_commit = 1;
my $DDL = <<SQL;
create table `$table` (
`epoch` int unsigned not null,
`us` int unsigned not null
)
SQL
my $dbh = DBI->connect("dbi:mysql:database=$db;", $user, $pass,
{ RaiseError => 1, AutoCommit => $auto_commit });
$dbh->do("drop table if exists `$table`");
$dbh->do($DDL);
$dbh->commit if($auto_commit == 0);
my $sth = $dbh->prepare("insert into `$table` values (?, ?)");
my ($epoch, $us);
for (1..300) {
($epoch, $us) = gettimeofday;
$sth->execute($epoch, $us);
$dbh->commit if($auto_commit == 0);
usleep 10000; # 10ms
}
my $ref = $dbh->selectall_arrayref("select `us` from `$table`");
for (my $i=1; $i<=$#{$ref}; $i++ ) {
my $ms_diff = ($ref->[$i][0] - $ref->[$i-1][0]);
$ms_diff += $ms_diff < 0 ? 1000000 : 0;
print $ms_diff/1000 ."\n";
}
$dbh->disconnect;
@riywo
Copy link
Author

riywo commented Mar 17, 2011

約1ms強かかっている印象。usleepを8500くらいにすれば丁度10msくらいの感覚になりそう。

auto_commitの有無はほとんど影響なさそう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment