我正在尝试每秒最大化插入次数.我目前获得大约20k插入/秒.我的性能实际上降低了我使用的线程和CPU的数量(我有16个核心可用).目前,2个线程比16核双处理器机器上的16个线程每秒更多.关于问题是什么的任何想法?是因为我只使用一个mongod吗?索引可能会减慢速度吗?我需要使用分片吗?我想知道是否有办法分片,但也保持数据库上限......
约束:必须处理大约300k插入/秒,必须是自限制(上限),必须能够相对快速地查询
问题空间:必须处理主要手机公司的通话记录(大约300k插入/秒),并尽可能长时间地查询这些通话记录(例如,一周)
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
use MongoDB;
use Time::HiRes;
my $conn = MongoDB::Connection->new;
my $db = $conn->tutorial;
my $users = $db->users;
my $cmd = Tie::IxHash->new(
"create" => "users",
"capped" => "boolean::true",
"max" => 10000000,
);
$db->run_command($cmd);
my $idx = Tie::IxHash->new(
"background"=> "boolean::true",
);
$users->ensure_index($idx);
my $myhash =
{
"name" => "James",
"age" => 31,
# "likes" => [qw/Danielle biking food games/]
};
my $j : shared = 0;
my $numthread = 2; # how many threads to run
my @array;
for (1..100000) {
push (@array, $myhash);
$j++;
}
sub thInsert {
#my @ids = $users->batch_insert(\@array);
#$users->bulk_insert(\@array);
$users->batch_insert(\@array);
}
my @threads;
my $timestart = Time::HiRes::time();
push @threads, threads->new(\&thInsert) for 1..$numthread;
$_->join foreach @threads; # wait for all threads to finish
print (($j*$numthread) . "\n");
my $timeend = Time::HiRes::time();
print( (($j*$numthread)/($timeend - $timestart)) . "\n");
$users->drop();
$db->drop();
Run Code Online (Sandbox Code Playgroud)
Thi*_*ilo 15
目前,2个线程比16核双处理器机器上的16个线程每秒更多.
MongoDB插入不能同时完成.每个插入都需要获取写锁.不确定这是全局锁定还是每个集合锁定,但在您的情况下不会产生任何影响.
因此,一旦Mongo成为瓶颈,使这个程序成为多线程并没有多大意义.
我需要使用分片吗?
你不能破坏上限集合.