问题
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.
回答1:
$starttime = microtime(true);
//Do your query and stuff here
$endtime = microtime(true);
$duration = $endtime - $starttime; //calculates total time taken
This will give you the run time in microseconds.
回答2:
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
回答3:
There are two possibilities I can tell you now:
- wrap ->execute() with microtime() and measure it yourself, possibly wrapping whole "querying" code snippet within a class / function
- run EXPLAIN query of that query and see if you can read some values from the returned data
Hope that helps.
回答4:
If you are using MYSQL 5, you should better check SHOW PROFILE
http://dev.mysql.com/doc/refman/5.0/en/show-profile.html
and print the timings in php...or EXPLAIN the SQL statement which took longer or detail each query...by CPU etc
来源:https://stackoverflow.com/questions/5267890/query-time-result-in-mysql-w-php