使用 PHP 在 MySQL 中查询时间结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5267890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:49:19  来源:igfitidea点击:

Query time result in MySQL w/ PHP

phpmysqltimemyisam

提问by bob_cobb

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 表引擎的全文搜索。任何帮助都是不可思议的。谢谢你。

回答by rayman86

$starttime = microtime(true);

//Do your query and stuff here

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

NOTEthat this will give you the run time in seconds(not microseconds) to the nearest microsecond due to get_as_floatparameter being true. See this

请注意,由于参数为真,这将为您提供以(而不是微秒)为单位的运行时间到最接近的微秒get_as_float。看到这个

回答by Patricio Jofre

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

你好

回答by Tomasz Kowalczyk

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
  • 用 microtime() 包装 ->execute() 并自己测量它,可能将整个“查询”代码片段包装在一个类/函数中
  • 运行该查询的 EXPLAIN 查询,看看是否可以从返回的数据中读取一些值

Hope that helps.

希望有帮助。

回答by Jinxmcg

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 等