Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query time result in MySQL w/ PHP

Is there a way that I can get the time of a MySQL query (specifically with PHP)? The actual time it took to complete the query, that is.

Something such as: Results 1 - 10 for brown. (0.11 seconds)

I tried to look for an example, to no avail. Here is an example of my code:

                    // prepare sql statement
                $stmt = $dbh->prepare("SELECT ijl, description, source, user_id, timestamp FROM Submissions WHERE MATCH (ijl, description) AGAINST (?)");

                // bind parameters
                $stmt->bindParam(1, $search, PDO::PARAM_STR);

                // execute prepared statement
                $stmt->execute();

For my current full text search using a MyISAM table engine. Any help would be incredible. Thank you.

like image 259
bob_cobb Avatar asked Mar 11 '11 01:03

bob_cobb


People also ask

How does MySQL calculate query execution time?

Now, go to the Scalyr dashboard menu and select MySQL. You will be able to see the log details of your MySQL, which includes the query time. This is a very simple and easy way to measure query time for a large number of MySQL queries.

What is the use of $result in PHP?

mysqli_num_rows ( $result ); Parameters: This function accepts single parameter $result (where $result is a MySQL query set up using mysqli_query()). It is a mandatory parameter and represents the result set returned by a fetch query in MySQL. Return Value: It returns the number of rows present in a result set.

What is $SQL in PHP?

PHP is the most popular scripting language for web development. It is free, open source and server-side (the code is executed on the server). MySQL is a Relational Database Management System (RDBMS) that uses Structured Query Language (SQL).


2 Answers

$starttime = microtime(true);

//Do your query and stuff here

$endtime = microtime(true);
$duration = $endtime - $starttime; //calculates total time taken

NOTE that this will give you the run time in seconds(not microseconds) to the nearest microsecond due to get_as_float parameter being true. See this

like image 166
rayman86 Avatar answered Sep 19 '22 13:09

rayman86


this may be help you

http://dev.mysql.com/doc/refman/5.0/en/show-profile.html

mysql> SET profiling = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE T1 (id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW PROFILES;
+----------+----------+--------------------------+
| Query_ID | Duration | Query                    |
+----------+----------+--------------------------+
|        0 | 0.000088 | SET PROFILING = 1        |
|        1 | 0.000136 | DROP TABLE IF EXISTS t1  |
|        2 | 0.011947 | CREATE TABLE t1 (id INT) |
+----------+----------+--------------------------+

greetings

like image 41
Patricio Jofre Avatar answered Sep 20 '22 13:09

Patricio Jofre