Start of database benchmarks

This commit is contained in:
GRMrGecko 2013-05-19 19:26:16 -05:00
commit 58b93456d4
7 changed files with 1841 additions and 0 deletions

172
DB-Benchmarks.txt Normal file
View File

@ -0,0 +1,172 @@
MongoDB 2.4.3
Insert 127608 rows: 17.86667
Create index: 14.66951
Search Index: 0.01768
Find by string contains: 0.03721
Find by id: 0.11522
1000 INSERTs: 0.09509
25000 INSERTs in a transaction: 2.47788
25000 INSERTs into an indexed table: 2.50635
100 SELECTs without an index: 1.70445
100 SELECTs on a string comparison: 2.88064
Creating an index: 0.00027
5000 SELECTs with an index: 4.46845
1000 UPDATEs without an index: 1.51740
25000 UPDATEs with an index: 1.86943
25000 text UPDATEs with an index: 3.96473
INSERTs from a SELECT: 8.94888
DELETE without an index: 0.00009
DELETE with an index: 0.00005
A big INSERT after a big DELETE: 5.65973
A big DELETE followed by many small INSERTs: 1.27760
DROP TABLE: 0.01578
Binary range seems to work.
Insert large amount of test IP Addreses: 824.06385
$VAR1 = {
'ok' => '1',
'n' => '10000000'
};
$VAR1 = {
'a' => "\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}_\x{8f}?\x{9f}o\x{f0}",
'b' => "\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}\x{ff}_\x{8f}?\x{9f}o\x{ff}",
'_id' => bless( {
'value' => '517f2b8cffbcb610025b29fb'
}, 'MongoDB::OID' )
};
Find one IP Address: 5.92847
SQLite3 3.7.12.1
Insert 127608 rows: 23.67114
Create index: 3.72403
Search Index: 0.00331
Find by string contains: 0.03072
Find by id: 0.16024
1000 INSERTs: 1.81813
25000 INSERTs in a transaction: 2.04020
25000 INSERTs into an indexed table: 2.33659
100 SELECTs without an index: 0.67285
100 SELECTs on a string comparison: 1.24578
Creating an index: 0.09293
5000 SELECTs with an index: 0.47482
1000 UPDATEs without an index: 0.25084
25000 UPDATEs with an index: 1.43273
25000 text UPDATEs with an index: 2.34430
INSERTs from a SELECT: 0.20208
DELETE without an index: 0.15024
DELETE with an index: 2.04350
A big INSERT after a big DELETE: 0.14055
A big DELETE followed by many small INSERTs: 0.99803
DROP TABLE: 0.02864
Binary range seems to work.
Insert large amount of test IP Addreses: 474.88479
$VAR1 = {
'COUNT(*)' => 10000000
};
$VAR1 = {
'c' => undef,
'a' => '????????_???o?',
'b' => '????????_???o?'
};
Find one IP Address: 3.16895
MySQL 5.6.10 InnoDB
Insert 127608 rows: 166.73900
Create index: 4.38940
Search Index: 0.00782
Find by string contains: 0.05098
Find by id: 0.14827
1000 INSERTs: 1.19199
25000 INSERTs in a transaction: 18.25592
25000 INSERTs into an indexed table: 18.30708
100 SELECTs without an index: 1.15091
100 SELECTs on a string comparison: 3.71783
Creating an index: 0.29839
5000 SELECTs with an index: 3.77709
1000 UPDATEs without an index: 1.47334
25000 UPDATEs with an index: 17.67512
25000 text UPDATEs with an index: 18.79318
INSERTs from a SELECT: 0.54077
DELETE without an index: 0.16496
DELETE with an index: 0.33108
A big INSERT after a big DELETE: 0.59019
A big DELETE followed by many small INSERTs: 8.94354
DROP TABLE: 0.02368
Binary range seems to work.
Insert large amount of test IP Addreses: 6731.02201
$VAR1 = {
'COUNT(*)' => '10000000'
};
$VAR1 = {
'c' => undef,
'a' => '????????_???o?',
'b' => '????????_???o?'
};
Find one IP Address: 9.34118
MySQL 5.6.10 MyISAM
Insert 127608 rows: 119.67051
Create index: 5.90174
Search Index: 0.17314
Find by string contains: 0.04022
Find by id: 0.05751
1000 INSERTs: 1.13861
25000 INSERTs in a transaction: 24.48944
25000 INSERTs into an indexed table: 20.97616
100 SELECTs without an index: 0.79451
100 SELECTs on a string comparison: 2.84247
Creating an index: 0.46834
5000 SELECTs with an index: 3.90304
1000 UPDATEs without an index: 3.07162
25000 UPDATEs with an index: 17.56033
25000 text UPDATEs with an index: 20.98868
INSERTs from a SELECT: 0.35422
DELETE without an index: 0.24055
DELETE with an index: 0.55815
A big INSERT after a big DELETE: 0.50878
A big DELETE followed by many small INSERTs: 9.99112
DROP TABLE: 0.00924
Binary range seems to work.
Insert large amount of test IP Addreses: 8020.03561
$VAR1 = {
'COUNT(*)' => '10000000'
};
$VAR1 = {
'c' => undef,
'a' => '????????_???o?',
'b' => '????????_???o?'
};
Find one IP Address: 2.88980
PostgreSQL 9.1.4
Insert 127608 rows: 130.77080
Create index: 6.60678
Search Index: 0.00550
Find by string contains: 0.01231
Find by id: 0.02360
1000 INSERTs: 1.39907
25000 INSERTs in a transaction: 21.18136
25000 INSERTs into an indexed table: 25.01201
100 SELECTs without an index: 0.60641
100 SELECTs on a string comparison: 0.89370
Creating an index: 0.03078
5000 SELECTs with an index: 4.54719
1000 UPDATEs without an index: 0.99736
25000 UPDATEs with an index: 23.16692
25000 text UPDATEs with an index: 24.44646
INSERTs from a SELECT: 0.75400
DELETE without an index: 0.01990
DELETE with an index: 0.28124
A big INSERT after a big DELETE: 0.60484
A big DELETE followed by many small INSERTs: 10.28418
DROP TABLE: 0.00601
Binary range seems to work.
Insert large amount of test IP Addreses: 8811.93481
$VAR1 = {
'count' => '10000000'
};
$VAR1 = {
'c' => undef,
'a' => '????????_???o?',
'b' => '????????_???o?'
};
Find one IP Address: 2.26947

BIN
data.7z Normal file

Binary file not shown.

278
db-test-mongodb.pl Executable file
View File

@ -0,0 +1,278 @@
#!/usr/bin/perl
use MongoDB;
use Text::CSV;
use Data::Dumper;
use Lingua::EN::Nums2Words;
use Time::HiRes qw/ time /;
use POSIX;
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag();
my $client = MongoDB::MongoClient->new(host => 'localhost', port => 27017);
my $database = $client->get_database('test');
my $collection = $database->get_collection('posts');
my $start = time();
my $rowCount = 0;
my $columnCount = 0;
my $columnTitles = undef;
open(my $fh, "<".$ENV{"HOME"}."/Desktop/data.csv");
while (my $row = $csv->getline($fh)) {
$rowCount++;
if ($rowCount==1) {
$columnCount = scalar(@$row);
$columnTitles = $row;
next;
}
my $data = {};
for (my $i=0; $i<$columnCount; $i++) {
$data->{$columnTitles->[$i]} = $row->[$i];
}
$data->{'id'} = $rowCount-1;
$collection->insert($data);
}
$csv->eof or $csv->error_diag();
close($fh);
my $runTime = sprintf("%.5f", time()-$start);
print "Insert ".$rowCount." rows: ".$runTime."\n";
my $start = time();
$collection->ensure_index({"data" => "text"}, {"name" => "data_index"});
while (1) {
eval {
my $result = $database->eval("db.currentOp()");
};
unless ($@) {
last;
}
}
my $runTime = sprintf("%.5f", time()-$start);
print "Create index: ".$runTime."\n";
my $start = time();
my $result = $database->eval("db.posts.runCommand('text', {search: 'feet'});");
my $runTime = sprintf("%.5f", time()-$start);
print "Search Index: ".$runTime."\n";
my $start = time();
my $result = $collection->find({"data" => {"\$regex" => "feet"}})->limit(100);
while (my $row = $result->next) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by string contains: ".$runTime."\n";
my $start = time();
my $result = $collection->find({"id" => 584});
while (my $row = $result->next) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by id: ".$runTime."\n";
$collection->drop();
my $collection = $database->get_collection('t1');
my $start = time();
for (my $i=0; $i<1000; $i++) {
my $number = $i*135;
$collection->insert({"a" => $i, "b" => $number, "c" => num2word($number)});
}
my $runTime = sprintf("%.5f", time()-$start);
print "1000 INSERTs: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
$collection->insert({"a" => $i, "b" => $number, "c" => num2word($number)});
}
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs in a transaction: ".$runTime."\n";
my $collection = $database->get_collection('t3');
$collection->ensure_index({"c" => 1}, {"name" => "c_index"});
my $start = time();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
$collection->insert({"a" => $i, "b" => $number, "c" => num2word($number)});
}
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs into an indexed table: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i*100;
my $result = $collection->aggregate([{"\$match" => {"b" => {"\$gte" => $number, "\$lt" => $number+1000}}}, {"\$group" => {"_id" => 0, "average" => {"\$avg" => "\$b"}, "count" => {"\$sum" => 1}}}]);
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs without an index: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i;
my $result = $collection->aggregate([{"\$match" => {"c" => {"\$regex" => num2word($number)}}}, {"\$group" => {"_id" => 0, "average" => {"\$avg" => "\$b"}, "count" => {"\$sum" => 1}}}]);
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs on a string comparison: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
$collection->ensure_index({"a" => 1}, {"name" => "a_index"});
$collection->ensure_index({"b" => 1}, {"name" => "b_index"});
my $runTime = sprintf("%.5f", time()-$start);
print "Creating an index: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
for (my $i=0; $i<5000; $i++) {
my $number = $i*100;
my $result = $collection->aggregate([{"\$match" => {"b" => {"\$gte" => $number, "\$lt" => $number+100}}}, {"\$group" => {"_id" => 0, "average" => {"\$avg" => "\$b"}, "count" => {"\$sum" => 1}}}]);
}
my $runTime = sprintf("%.5f", time()-$start);
print "5000 SELECTs with an index: ".$runTime."\n";
my $collection = $database->get_collection('t1');
my $start = time();
for (my $i=0; $i<1000; $i++) {
my $number = $i*10;
my $result = $collection->find({"b" => {"\$gte" => $number, "\$lt" => $number+10}});
while (my $row = $result->next) {
$collection->update({"_id" => $row->{'_id'}}, {'$set' => {'b' => $row->{'b'}*2}});
}
}
my $runTime = sprintf("%.5f", time()-$start);
print "1000 UPDATEs without an index: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
$collection->update({"a" => $i}, {'$set' => {'b' => $number}});
}
my $runTime = sprintf("%.5f", time()-$start);
print "25000 UPDATEs with an index: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
$collection->update({"a" => $i}, {'$set' => {'c' => num2word($number)}});
}
my $runTime = sprintf("%.5f", time()-$start);
print "25000 text UPDATEs with an index: ".$runTime."\n";
my $start = time();
$database->eval("db.t2.find().forEach(function(x){db.t1.insert({a: x['b'],b: x['a'],c: x['c']})});");
$database->eval("db.t1.find().forEach(function(x){db.t2.insert({a: x['b'],b: x['a'],c: x['c']})});");
my $runTime = sprintf("%.5f", time()-$start);
print "INSERTs from a SELECT: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
$collection->remove({"c" => {"\$regex" => "FIFTY"}});
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE without an index: ".$runTime."\n";
my $collection = $database->get_collection('t2');
my $start = time();
$collection->remove({"a" => {"\$gt" => 10, "\$lt" => 20000}});
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE with an index: ".$runTime."\n";
my $start = time();
$database->eval("db.t1.find().forEach(function(x){db.t2.insert(x)});");
my $runTime = sprintf("%.5f", time()-$start);
print "A big INSERT after a big DELETE: ".$runTime."\n";
my $collection = $database->get_collection('t1');
my $start = time();
$collection->remove();
for (my $i=0; $i<12000; $i++) {
my $number = $i*165;
$collection->insert({"a" => $i, "b" => $number, "c" => num2word($number)});
}
my $runTime = sprintf("%.5f", time()-$start);
print "A big DELETE followed by many small INSERTs: ".$runTime."\n";
my $start = time();
my $collection = $database->get_collection('t1');
$collection->drop();
my $collection = $database->get_collection('t2');
$collection->drop();
my $collection = $database->get_collection('t3');
$collection->drop();
my $runTime = sprintf("%.5f", time()-$start);
print "DROP TABLE: ".$runTime."\n";
sub hex2bin {
my $h = shift;
my $hlen = length($h);
return pack("H$hlen", $h);
}
my $collection = $database->get_collection('ips');
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."F");
$collection->insert({"a" => $binary, "b" => $binaryEnd, "c" => $i})
}
my $correct = 1;
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."5");
my $result = $collection->find({"a" => {"\$lte" => $binary}, "b" => {"\$gte" => $binary}});
my $row = $result->next;
unless ($row->{'c'}==$i) {
$correct = 0;
print "Unable to find ".$i." ".$binary." in the database.\n";
}
}
if ($correct) {
print "Binary range seems to work.\n";
}
$collection->remove();
my $start = time();
for (my $a=0; $a<10; $a++) {
for (my $b=0; $b<10; $b++) {
for (my $c=0; $c<10; $c++) {
for (my $d=0; $d<10; $d++) {
for (my $e=0; $e<10; $e++) {
for (my $f=0; $f<10; $f++) {
for (my $g=0; $g<10; $g++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FF0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FFF");
$collection->insert({"a" => $binary, "b" => $binaryEnd})
}
}
}
}
}
}
}
my $runTime = sprintf("%.5f", time()-$start);
print "Insert large amount of test IP Addreses: ".$runTime."\n";
my $result = $database->run_command({"count" => "ips"});
print Dumper($result);
my $start = time();
my $binary = hex2bin("FFFFFFFFFFFFFFFF5F7F8F3F9F0F6FF4");
my $result = $collection->find({"a" => {"\$lte" => $binary}, "b" => {"\$gte" => $binary}});
my $row = $result->next;
print Dumper($row);
my $runTime = sprintf("%.5f", time()-$start);
print "Find one IP Address: ".$runTime."\n";
$collection->drop();

342
db-test-mysql-InnoDB.pl Executable file
View File

@ -0,0 +1,342 @@
#!/usr/bin/perl
use DBI;
use Text::CSV;
use Data::Dumper;
use Lingua::EN::Nums2Words;
use Time::HiRes qw/ time /;
use POSIX;
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag();
my $dbh = DBI->connect("DBI:mysql:database=test;host=127.0.0.1","root","password");
my $start = time();
my $columnStructure = "id INTEGER";
my $rowCount = 0;
my $columnCount = 0;
my $columnTitles = undef;
open(my $fh, "<".$ENV{"HOME"}."/Desktop/data.csv");
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
while (my $row = $csv->getline($fh)) {
$rowCount++;
if ($rowCount==1) {
$columnCount = scalar(@$row);
$columnTitles = $row;
for (my $i=0; $i<scalar(@$row); $i++) {
$columnStructure .= ",".$row->[$i]." TEXT";
}
my $sth = $dbh->prepare("CREATE TABLE posts(".$columnStructure.") ENGINE = INNODB;");
$sth->execute();
next;
}
my @data = ();
push(@data,$rowCount-1);
my $columns = "id";
my $values = "?";
for (my $i=0; $i<$columnCount; $i++) {
$columns .= ",".$columnTitles->[$i];
$values .= ",?";
push(@data,$row->[$i]);
}
my $sth = $dbh->prepare("INSERT INTO posts (".$columns.")VALUES(".$values.");");
$sth->execute(@data);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
$csv->eof or $csv->error_diag();
close($fh);
my $runTime = sprintf("%.5f", time()-$start);
print "Insert ".$rowCount." rows: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("ALTER TABLE posts ADD FULLTEXT(data);");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Create index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE MATCH(data) AGAINST(?) LIMIT 100;");
$sth->execute("feet");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Search Index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE data LIKE ? LIMIT 100;");
$sth->execute("\%feet\%");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by string contains: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE id=584;");
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by id: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE posts;");
$sth->execute();
my $sth = $dbh->prepare("CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100)) ENGINE = INNODB;");
$sth->execute();
my $start = time();
for (my $i=0; $i<1000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $runTime = sprintf("%.5f", time()-$start);
print "1000 INSERTs: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100)) ENGINE = INNODB;");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t2 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs in a transaction: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t3(a INTEGER, b INTEGER, c VARCHAR(100)) ENGINE = INNODB;");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i3 ON t3(c);");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t3 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs into an indexed table: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs without an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE c LIKE ?;");
$sth->execute("%".num2word($number)."%");
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs on a string comparison: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("CREATE INDEX i2a ON t2(a);");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i2b ON t2(b);");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Creating an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<5000; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "5000 SELECTs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<1000; $i++) {
my $number = $i*10;
my $sth = $dbh->prepare("UPDATE t1 SET b=b*2 WHERE a>=? AND a<?;");
$sth->execute($number,$number+10);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "1000 UPDATEs without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET b=? WHERE a=?;");
$sth->execute($number,$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET c=? WHERE a=?;");
$sth->execute(num2word($number),$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 text UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t1 SELECT b,a,c FROM t2;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT b,a,c FROM t1;");
$sth->execute();
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "INSERTs from a SELECT: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE c LIKE '\%fifty\%';");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE a>10 AND a<20000;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT * FROM t1;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big INSERT after a big DELETE: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
my $sth = $dbh->prepare("DELETE FROM t1;");
$sth->execute();
for (my $i=0; $i<12000; $i++) {
my $number = $i*165;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big DELETE followed by many small INSERTs: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DROP TABLE t1;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t2;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t3;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DROP TABLE: ".$runTime."\n";
sub hex2bin {
my $h = shift;
my $hlen = length($h);
return pack("H$hlen", $h);
}
my $sth = $dbh->prepare("CREATE TABLE ips(a BLOB(16), b BLOB(16), c INTEGER) ENGINE = INNODB;");
$sth->execute();
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."F");
my $sth = $dbh->prepare("INSERT INTO ips (a,b,c)VALUES(?,?,?);");
$sth->execute($binary,$binaryEnd,$i);
}
my $correct = 1;
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."5");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
unless ($row->{'c'}==$i) {
$correct = 0;
print "Unable to find ".$i." ".$binary." in the database.\n";
}
}
if ($correct) {
print "Binary range seems to work.\n";
}
my $sth = $dbh->prepare("DELETE FROM ips;");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $a=0; $a<10; $a++) {
for (my $b=0; $b<10; $b++) {
for (my $c=0; $c<10; $c++) {
for (my $d=0; $d<10; $d++) {
for (my $e=0; $e<10; $e++) {
for (my $f=0; $f<10; $f++) {
for (my $g=0; $g<10; $g++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FF0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FFF");
my $sth = $dbh->prepare("INSERT INTO ips (a,b)VALUES(?,?);");
$sth->execute($binary,$binaryEnd);
}
}
}
}
}
}
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Insert large amount of test IP Addreses: ".$runTime."\n";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM ips;");
$sth->execute();
my $result = $sth->fetchrow_hashref;
print Dumper($result);
my $start = time();
my $binary = hex2bin("FFFFFFFFFFFFFFFF5F7F8F3F9F0F6FF4");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
print Dumper($row);
my $runTime = sprintf("%.5f", time()-$start);
print "Find one IP Address: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE ips;");
$sth->execute();

342
db-test-mysql-MyISAM.pl Executable file
View File

@ -0,0 +1,342 @@
#!/usr/bin/perl
use DBI;
use Text::CSV;
use Data::Dumper;
use Lingua::EN::Nums2Words;
use Time::HiRes qw/ time /;
use POSIX;
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag();
my $dbh = DBI->connect("DBI:mysql:database=test;host=127.0.0.1","root","password");
my $start = time();
my $columnStructure = "id INTEGER";
my $rowCount = 0;
my $columnCount = 0;
my $columnTitles = undef;
open(my $fh, "<".$ENV{"HOME"}."/Desktop/data.csv");
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
while (my $row = $csv->getline($fh)) {
$rowCount++;
if ($rowCount==1) {
$columnCount = scalar(@$row);
$columnTitles = $row;
for (my $i=0; $i<scalar(@$row); $i++) {
$columnStructure .= ",".$row->[$i]." TEXT";
}
my $sth = $dbh->prepare("CREATE TABLE posts(".$columnStructure.") ENGINE = MYISAM;");
$sth->execute();
next;
}
my @data = ();
push(@data,$rowCount-1);
my $columns = "id";
my $values = "?";
for (my $i=0; $i<$columnCount; $i++) {
$columns .= ",".$columnTitles->[$i];
$values .= ",?";
push(@data,$row->[$i]);
}
my $sth = $dbh->prepare("INSERT INTO posts (".$columns.")VALUES(".$values.");");
$sth->execute(@data);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
$csv->eof or $csv->error_diag();
close($fh);
my $runTime = sprintf("%.5f", time()-$start);
print "Insert ".$rowCount." rows: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("ALTER TABLE posts ADD FULLTEXT(data);");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Create index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE MATCH(data) AGAINST(?) LIMIT 100;");
$sth->execute("feet");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Search Index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE data LIKE ? LIMIT 100;");
$sth->execute("\%feet\%");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by string contains: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE id=584;");
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by id: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE posts;");
$sth->execute();
my $sth = $dbh->prepare("CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100)) ENGINE = MYISAM;");
$sth->execute();
my $start = time();
for (my $i=0; $i<1000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $runTime = sprintf("%.5f", time()-$start);
print "1000 INSERTs: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100)) ENGINE = MYISAM;");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t2 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs in a transaction: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t3(a INTEGER, b INTEGER, c VARCHAR(100)) ENGINE = MYISAM;");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i3 ON t3(c);");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t3 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs into an indexed table: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs without an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE c LIKE ?;");
$sth->execute("%".num2word($number)."%");
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs on a string comparison: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("CREATE INDEX i2a ON t2(a);");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i2b ON t2(b);");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Creating an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<5000; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "5000 SELECTs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<1000; $i++) {
my $number = $i*10;
my $sth = $dbh->prepare("UPDATE t1 SET b=b*2 WHERE a>=? AND a<?;");
$sth->execute($number,$number+10);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "1000 UPDATEs without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET b=? WHERE a=?;");
$sth->execute($number,$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET c=? WHERE a=?;");
$sth->execute(num2word($number),$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 text UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t1 SELECT b,a,c FROM t2;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT b,a,c FROM t1;");
$sth->execute();
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "INSERTs from a SELECT: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE c LIKE '\%fifty\%';");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE a>10 AND a<20000;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT * FROM t1;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big INSERT after a big DELETE: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
my $sth = $dbh->prepare("DELETE FROM t1;");
$sth->execute();
for (my $i=0; $i<12000; $i++) {
my $number = $i*165;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big DELETE followed by many small INSERTs: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DROP TABLE t1;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t2;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t3;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DROP TABLE: ".$runTime."\n";
sub hex2bin {
my $h = shift;
my $hlen = length($h);
return pack("H$hlen", $h);
}
my $sth = $dbh->prepare("CREATE TABLE ips(a BLOB(16), b BLOB(16), c INTEGER) ENGINE = MYISAM;");
$sth->execute();
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."F");
my $sth = $dbh->prepare("INSERT INTO ips (a,b,c)VALUES(?,?,?);");
$sth->execute($binary,$binaryEnd,$i);
}
my $correct = 1;
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."5");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
unless ($row->{'c'}==$i) {
$correct = 0;
print "Unable to find ".$i." ".$binary." in the database.\n";
}
}
if ($correct) {
print "Binary range seems to work.\n";
}
my $sth = $dbh->prepare("DELETE FROM ips;");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $a=0; $a<10; $a++) {
for (my $b=0; $b<10; $b++) {
for (my $c=0; $c<10; $c++) {
for (my $d=0; $d<10; $d++) {
for (my $e=0; $e<10; $e++) {
for (my $f=0; $f<10; $f++) {
for (my $g=0; $g<10; $g++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FF0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FFF");
my $sth = $dbh->prepare("INSERT INTO ips (a,b)VALUES(?,?);");
$sth->execute($binary,$binaryEnd);
}
}
}
}
}
}
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Insert large amount of test IP Addreses: ".$runTime."\n";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM ips;");
$sth->execute();
my $result = $sth->fetchrow_hashref;
print Dumper($result);
my $start = time();
my $binary = hex2bin("FFFFFFFFFFFFFFFF5F7F8F3F9F0F6FF4");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
print Dumper($row);
my $runTime = sprintf("%.5f", time()-$start);
print "Find one IP Address: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE ips;");
$sth->execute();

353
db-test-postgresql.pl Executable file
View File

@ -0,0 +1,353 @@
#!/usr/bin/perl
use DBI;
use DBD::Pg qw(:pg_types);
use Text::CSV;
use Data::Dumper;
use Lingua::EN::Nums2Words;
use Time::HiRes qw/ time /;
use POSIX;
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag();
my $dbh = DBI->connect("dbi:Pg:dbname=test","","");
my $start = time();
my $columnStructure = "id INTEGER";
my $rowCount = 0;
my $columnCount = 0;
my $columnTitles = undef;
open(my $fh, "<".$ENV{"HOME"}."/Desktop/data.csv");
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
while (my $row = $csv->getline($fh)) {
$rowCount++;
if ($rowCount==1) {
$columnCount = scalar(@$row);
$columnTitles = $row;
for (my $i=0; $i<scalar(@$row); $i++) {
$columnStructure .= ",".$row->[$i]." TEXT";
}
my $sth = $dbh->prepare("CREATE TABLE posts(".$columnStructure.");");
$sth->execute();
next;
}
my @data = ();
push(@data,$rowCount-1);
my $columns = "id";
my $values = "?";
for (my $i=0; $i<$columnCount; $i++) {
$columns .= ",".$columnTitles->[$i];
$values .= ",?";
push(@data,$row->[$i]);
}
my $sth = $dbh->prepare("INSERT INTO posts (".$columns.")VALUES(".$values.");");
$sth->execute(@data);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
$csv->eof or $csv->error_diag();
close($fh);
my $runTime = sprintf("%.5f", time()-$start);
print "Insert ".$rowCount." rows: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("CREATE INDEX data_index ON posts USING gin(to_tsvector('english', data));");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Create index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE to_tsvector('english', data) @@ to_tsquery('english', ?) LIMIT 100;");
$sth->execute("feet");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Search Index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE data LIKE ? LIMIT 100;");
$sth->execute("\%feet\%");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by string contains: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE id=584;");
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by id: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE posts;");
$sth->execute();
my $sth = $dbh->prepare("CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));");
$sth->execute();
my $start = time();
for (my $i=0; $i<1000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $runTime = sprintf("%.5f", time()-$start);
print "1000 INSERTs: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t2 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs in a transaction: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t3(a INTEGER, b INTEGER, c VARCHAR(100));");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i3 ON t3(c);");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t3 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs into an indexed table: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs without an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE c LIKE ?;");
$sth->execute("%".num2word($number)."%");
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs on a string comparison: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("CREATE INDEX i2a ON t2(a);");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i2b ON t2(b);");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Creating an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<5000; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "5000 SELECTs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<1000; $i++) {
my $number = $i*10;
my $sth = $dbh->prepare("UPDATE t1 SET b=b*2 WHERE a>=? AND a<?;");
$sth->execute($number,$number+10);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "1000 UPDATEs without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET b=? WHERE a=?;");
$sth->execute($number,$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET c=? WHERE a=?;");
$sth->execute(num2word($number),$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 text UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t1 SELECT b,a,c FROM t2;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT b,a,c FROM t1;");
$sth->execute();
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "INSERTs from a SELECT: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE c LIKE '\%fifty\%';");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE a>10 AND a<20000;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT * FROM t1;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big INSERT after a big DELETE: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
my $sth = $dbh->prepare("DELETE FROM t1;");
$sth->execute();
for (my $i=0; $i<12000; $i++) {
my $number = $i*165;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big DELETE followed by many small INSERTs: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DROP TABLE t1;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t2;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t3;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DROP TABLE: ".$runTime."\n";
sub hex2bin {
my $h = shift;
my $hlen = length($h);
return pack("H$hlen", $h);
}
my $sth = $dbh->prepare("SET CLIENT_ENCODING TO 'UTF8';");
$sth->execute();
my $sth = $dbh->prepare("CREATE TABLE ips(a BYTEA, b BYTEA, c INTEGER);");
$sth->execute();
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."F");
my $sth = $dbh->prepare("INSERT INTO ips (a,b,c)VALUES(?,?,?);");
$sth->bind_param(1, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->bind_param(2, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->execute($binary,$binaryEnd,$i);
}
my $correct = 1;
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."5");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->bind_param(1, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->bind_param(2, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
unless ($row->{'c'}==$i) {
$correct = 0;
print "Unable to find ".$i." ".$binary." in the database.\n";
}
}
if ($correct) {
print "Binary range seems to work.\n";
}
my $sth = $dbh->prepare("DELETE FROM ips;");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("START TRANSACTION;");
$sth->execute();
for (my $a=0; $a<10; $a++) {
for (my $b=0; $b<10; $b++) {
for (my $c=0; $c<10; $c++) {
for (my $d=0; $d<10; $d++) {
for (my $e=0; $e<10; $e++) {
for (my $f=0; $f<10; $f++) {
for (my $g=0; $g<10; $g++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FF0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FFF");
my $sth = $dbh->prepare("INSERT INTO ips (a,b)VALUES(?,?);");
$sth->bind_param(1, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->bind_param(2, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->execute($binary,$binaryEnd);
}
}
}
}
}
}
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Insert large amount of test IP Addreses: ".$runTime."\n";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM ips;");
$sth->execute();
my $result = $sth->fetchrow_hashref;
print Dumper($result);
my $start = time();
my $binary = hex2bin("FFFFFFFFFFFFFFFF5F7F8F3F9F0F6FF4");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->bind_param(1, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->bind_param(2, undef, {pg_type => DBD::Pg::PG_BYTEA});
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
print Dumper($row);
my $runTime = sprintf("%.5f", time()-$start);
print "Find one IP Address: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE ips;");
$sth->execute();

354
db-test-sqlite3.pl Executable file
View File

@ -0,0 +1,354 @@
#!/usr/bin/perl
use DBI;
use Text::CSV;
use Data::Dumper;
use Lingua::EN::Nums2Words;
use Time::HiRes qw/ time /;
use POSIX;
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag();
my $dbh = DBI->connect("dbi:SQLite:dbname=".$ENV{"HOME"}."/Desktop/db.sqlite","","");
my $start = time();
my $columnStructure = "id INTEGER";
my $rowCount = 0;
my $columnCount = 0;
my $columnTitles = undef;
open(my $fh, "<".$ENV{"HOME"}."/Desktop/data.csv");
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
while (my $row = $csv->getline($fh)) {
$rowCount++;
if ($rowCount==1) {
$columnCount = scalar(@$row);
$columnTitles = $row;
for (my $i=0; $i<scalar(@$row); $i++) {
$columnStructure .= ",".$row->[$i]." TEXT";
}
my $sth = $dbh->prepare("CREATE TABLE posts(".$columnStructure.");");
$sth->execute();
next;
}
my @data = ();
push(@data,$rowCount-1);
my $columns = "id";
my $values = "?";
for (my $i=0; $i<$columnCount; $i++) {
$columns .= ",".$columnTitles->[$i];
$values .= ",?";
push(@data,$row->[$i]);
}
my $sth = $dbh->prepare("INSERT INTO posts (".$columns.")VALUES(".$values.");");
$sth->execute(@data);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
$csv->eof or $csv->error_diag();
close($fh);
my $runTime = sprintf("%.5f", time()-$start);
print "Insert ".$rowCount." rows: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
my $sth = $dbh->prepare("CREATE TABLE posts_copy AS SELECT * FROM posts;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE posts;");
$sth->execute();
my $sth = $dbh->prepare("CREATE VIRTUAL TABLE posts USING fts3(".$columnStructure.");");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO posts SELECT * FROM posts_copy;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE posts_copy;");
$sth->execute();
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Create index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE data MATCH ? LIMIT 100;");
$sth->execute("feet");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Search Index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE data LIKE ? LIMIT 100;");
$sth->execute("\%feet\%");
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by string contains: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("SELECT * FROM posts WHERE id=584;");
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
#Do nothing
}
my $runTime = sprintf("%.5f", time()-$start);
print "Find by id: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE posts;");
$sth->execute();
my $sth = $dbh->prepare("CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));");
$sth->execute();
my $start = time();
for (my $i=0; $i<1000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $runTime = sprintf("%.5f", time()-$start);
print "1000 INSERTs: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t2 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs in a transaction: ".$runTime."\n";
my $sth = $dbh->prepare("CREATE TABLE t3(a INTEGER, b INTEGER, c VARCHAR(100));");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i3 ON t3(c);");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*135;
my $sth = $dbh->prepare("INSERT INTO t3 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 INSERTs into an indexed table: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs without an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<100; $i++) {
my $number = $i;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE c LIKE ?;");
$sth->execute("%".num2word($number)."%");
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "100 SELECTs on a string comparison: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("CREATE INDEX i2a ON t2(a);");
$sth->execute();
my $sth = $dbh->prepare("CREATE INDEX i2b ON t2(b);");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Creating an index: ".$runTime."\n";
my $start = time();
for (my $i=0; $i<5000; $i++) {
my $number = $i*100;
my $sth = $dbh->prepare("SELECT count(*), avg(b) FROM t2 WHERE b>=? AND b<?;");
$sth->execute($number,$number+1000);
my $row = $sth->fetchrow_hashref;
}
my $runTime = sprintf("%.5f", time()-$start);
print "5000 SELECTs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
for (my $i=0; $i<1000; $i++) {
my $number = $i*10;
my $sth = $dbh->prepare("UPDATE t1 SET b=b*2 WHERE a>=? AND a<?;");
$sth->execute($number,$number+10);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "1000 UPDATEs without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET b=? WHERE a=?;");
$sth->execute($number,$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
for (my $i=0; $i<25000; $i++) {
my $number = $i*123;
my $sth = $dbh->prepare("UPDATE t2 SET c=? WHERE a=?;");
$sth->execute(num2word($number),$i);
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "25000 text UPDATEs with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t1 SELECT b,a,c FROM t2;");
$sth->execute();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT b,a,c FROM t1;");
$sth->execute();
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "INSERTs from a SELECT: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE c LIKE '\%fifty\%';");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE without an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DELETE FROM t2 WHERE a>10 AND a<20000;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DELETE with an index: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("INSERT INTO t2 SELECT * FROM t1;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big INSERT after a big DELETE: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
my $sth = $dbh->prepare("DELETE FROM t1;");
$sth->execute();
for (my $i=0; $i<12000; $i++) {
my $number = $i*165;
my $sth = $dbh->prepare("INSERT INTO t1 (a,b,c)VALUES(?,?,?);");
$sth->execute($i,$number,num2word($number));
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "A big DELETE followed by many small INSERTs: ".$runTime."\n";
my $start = time();
my $sth = $dbh->prepare("DROP TABLE t1;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t2;");
$sth->execute();
my $sth = $dbh->prepare("DROP TABLE t3;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "DROP TABLE: ".$runTime."\n";
sub hex2bin {
my $h = shift;
my $hlen = length($h);
return pack("H$hlen", $h);
}
my $sth = $dbh->prepare("CREATE TABLE ips(a VARCHAR(16), b VARCHAR(16), c INTEGER);");
$sth->execute();
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."F");
my $sth = $dbh->prepare("INSERT INTO ips (a,b,c)VALUES(?,?,?);");
$sth->execute($binary,$binaryEnd,$i);
}
my $correct = 1;
for (my $i=0; $i<10; $i++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".$i."5");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
unless ($row->{'c'}==$i) {
$correct = 0;
print "Unable to find ".$i." ".$binary." in the database.\n";
}
}
if ($correct) {
print "Binary range seems to work.\n";
}
my $sth = $dbh->prepare("DELETE FROM ips;");
$sth->execute();
my $start = time();
my $sth = $dbh->prepare("BEGIN;");
$sth->execute();
for (my $a=0; $a<10; $a++) {
for (my $b=0; $b<10; $b++) {
for (my $c=0; $c<10; $c++) {
for (my $d=0; $d<10; $d++) {
for (my $e=0; $e<10; $e++) {
for (my $f=0; $f<10; $f++) {
for (my $g=0; $g<10; $g++) {
my $binary = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FF0");
my $binaryEnd = hex2bin("FFFFFFFFFFFFFFFF".$a."F".$b."F".$c."F".$d."F".$e."F".$f."F".$g."FFF");
my $sth = $dbh->prepare("INSERT INTO ips (a,b)VALUES(?,?);");
$sth->execute($binary,$binaryEnd);
}
}
}
}
}
}
}
my $sth = $dbh->prepare("COMMIT;");
$sth->execute();
my $runTime = sprintf("%.5f", time()-$start);
print "Insert large amount of test IP Addreses: ".$runTime."\n";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM ips;");
$sth->execute();
my $result = $sth->fetchrow_hashref;
print Dumper($result);
my $start = time();
my $binary = hex2bin("FFFFFFFFFFFFFFFF5F7F8F3F9F0F6FF4");
my $sth = $dbh->prepare("SELECT * FROM ips WHERE a<=? AND b>?;");
$sth->execute($binary,$binary);
my $row = $sth->fetchrow_hashref;
print Dumper($row);
my $runTime = sprintf("%.5f", time()-$start);
print "Find one IP Address: ".$runTime."\n";
my $sth = $dbh->prepare("DROP TABLE ips;");
$sth->execute();