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.
有没有一种方法可以让我获得MySQL查询的时间(特别是PHP)?即完成查询所需的实际时间。
Something such as: Results 1 - 10 for brown. (0.11 seconds)
比如:布朗的结果是1 - 10。(0.11秒)
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.
对于当前使用MyISAM表引擎的全文搜索。任何帮助都是不可思议的。谢谢你!
4 个解决方案
#1
37
$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
4
this may be help you
这可能对你有帮助
http://dev.mysql.com/doc/refman/5.0/en/show-profile.html
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
2
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
- wrap ->execute()使用microtime()并自己度量它,可能在类/函数中包装整个“查询”代码片段
- run EXPLAIN query of that query and see if you can read some values from the returned data
- 运行该查询的EXPLAIN查询,看看是否可以从返回的数据中读取一些值
Hope that helps.
希望有帮助。
#4
0
If you are using MYSQL 5, you should better check SHOW PROFILE
如果您正在使用MYSQL 5,那么最好检查一下SHOW PROFILE
http://dev.mysql.com/doc/refman/5.0/en/show-profile.html
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
用php打印时间……或者解释SQL语句,它花费了更长的时间,或者详细描述每个查询……由CPU等
#1
37
$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
4
this may be help you
这可能对你有帮助
http://dev.mysql.com/doc/refman/5.0/en/show-profile.html
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
2
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
- wrap ->execute()使用microtime()并自己度量它,可能在类/函数中包装整个“查询”代码片段
- run EXPLAIN query of that query and see if you can read some values from the returned data
- 运行该查询的EXPLAIN查询,看看是否可以从返回的数据中读取一些值
Hope that helps.
希望有帮助。
#4
0
If you are using MYSQL 5, you should better check SHOW PROFILE
如果您正在使用MYSQL 5,那么最好检查一下SHOW PROFILE
http://dev.mysql.com/doc/refman/5.0/en/show-profile.html
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
用php打印时间……或者解释SQL语句,它花费了更长的时间,或者详细描述每个查询……由CPU等