Answer the question
In order to leave comments, you need to log in
MongoDB fast base? Why is everything not so rosy for me?
So! Installed MongoDB. Before rushing into battle and adding real data there, I decided to play a little with the database and what was my surprise when the fetch request took as much as 4 seconds, despite the fact that there were only 3kk (million) test small documents. Maybe I'm doing something wrong? Actually the question is obvious. How to achieve a normal data sampling rate? Below is the code and data with which I tested.
$connection = new MongoClient('192.168.1.102:27017');
$db = $connection->mongo;
$collection = $db->user;
$i = 0;
$this->timer_start();
while($i < 3000000){
$doc = array(
"user_id"=>$i,
"user_name"=>"john_smith",
"first_name"=>"John",
"last_name"=>"Smith",
"phone"=>array(
"+48576938575773732",
"+23423429384239489",
),
);
$i++;
$collection->insert($doc);
}
echo "Insert in database: ".$this->timer_stop()."<hr>";
$filter = array("user_id"=>999999);
$this->timer_start();
$docs = $collection->find($filter);
echo $docs->count()."<hr>";
print_r($docs->getNext());
echo "<hr>";
echo "Search in database: ".$this->timer_stop()."<hr>"; //4,0992350578308
Answer the question
In order to leave comments, you need to log in
You are prompted that there are indexes in the monge. One of them is even generated automatically - by the "_id" field. Drop this base, create the same one, but with the replacement "user_id" -> "_id" (initialization of this field is allowed, modification is not), you should like it.
Well, read about indexes - what I will retell you)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question