MongoDB + PHP = execution stat results


#1

Hi All,

I am a university student and require some help, I have been trying to find out how to show the execution time of simple SQL queries such as insert select delete etc

I have already understood how to insert data into MongoDB but what I need to find now is how to code it so once the code has inserted the data in the DB it shows the time it had taken to perform the insert ,, the preferred way of showing this would be on a php window once the insert button has been clicked,

I hope t hear from you soon

This is the code I have...

$m = new MongoDB\Client("");
$db=$m->PHP;
$collection=$db->Users;

if($_POST)
{

$insert= array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'qebsite' => $_POST['website'],
);

if($collection->insertOne($insert))

{
echo "data is Inserted";
}

else{
echo "some issue";
}

}
?>

#2

Have a look at the microtime() function: http://php.net/manual/en/function.microtime.php

All you need to do is assign the return from that function to a variable immediately before you execute the query, and then do it again immediately after the query has executed. Difference between the two is the number of microseconds to execute.

I'm sure there are probably plenty of other ways to do it.


#3

Ok I've managed to figure that out .. also do you know how we would be able to search data from a php text box into both MySQL and mongodb if they have the same data in them.

The output would be something like this

In the text box I would search 'London' and then I would wait for both the Databases to select all attributes with London in them and display how long it had taken to retrieve the data and show it on the page like;

MongoDB = 34 seconds
MySQL = 20 seconds


#4

I'd imagine you would just have a separate set of code to access the MySQL database and run the query, which itself is pretty straightforward. Get the text from the form as you did in the code above, sanitise it, build the query and run it on each database. Plenty of sample code if you scroll down a page or two showing how to run a query in MySQL if you're not familiar with it. Use PDO to run the query on MySQL, or mysqli, but not mysql() calls.


#5

Hi Again,

This is my latest bit of code, I am trying to include the following https://docs.mongodb.com/manual/reference/command/explain/ would anyone know how to add this into my code below? so it shows the executionstats of the query?

<?php

//------------------------------MONGODB CODE -----------------------------

// Connecting to server
$m = new MongoDB\Client("mongodb://localhost:27017");
$db=$m->CSV;
$mObj=$db->Schools;

if($mObj->find())
{
	
echo "<br>";
      echo "The search time for all school data is ";
      echo "</br>";
}

else{
	echo "some issue";
}

#6

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.