简体   繁体   English

如何在php中显示运行的mysql查询

[英]How to show running mysql queries in php

I have an overflow memory usage problem and I need to scan all my scripts. 我有溢出内存使用问题,我需要扫描所有脚本。

My question is how can I show a list of running mysql queries in php? 我的问题是如何在php中显示运行mysql查询的列表?

If the problem is on MySQL site this could help a lot: 如果问题出在MySQL网站上,这可能会有很大帮助:

mysql_query('show processlist');

It should return all queries waiting for processing. 它应该返回所有等待处理的查询。 I usually use it directly from mysql console where I don't have to format the output. 我通常直接从mysql控制台使用它,我没有格式化输出。

You can rename the original mysql_query function and write your own containing some additional logging/inspecting code to see what is going wrong with your queries: 您可以重命名原始的mysql_query函数并编写自己的函数,其中包含一些额外的日志记录/检查代码,以查看查询出了什么问题:

rename_function('mysql_query', 'mysql_query_original');
override_function('mysql_query', '$query', 'return mysql_query_override($query);');

function mysql_query_override($query){
    echo "Query started";         // Add some more sensible information here
    $result = mysql_query_original($query);  
    echo "Query ended";           // Add some more sensible information here
    return $result;
}

Unless you are running a site with massive levels of traffic, then any snapshot of the queries running / enqueued is not likely to be representative. 除非您运行具有大量流量的站点,否则运行/入队的查询的任何快照都不可能具有代表性。 There are also problems in terms of correlating the query with the script which spawned it. 在将查询与产生它的脚本相关联方面也存在问题。

I'd recommend instrumenting your own code - use an auto-prepend to define a wrapper around mysql_query() then you can implement your own logging which: 我建议使用自己的代码 - 使用自动前置来定义mysql_query()的包装,然后你可以实现自己的日志记录:

  1. creates a log entry before the script is fired (most log entries are created afterwards - not very handy if the code which writes the log crashes) 在触发脚本之前创建一个日志条目(之后创建大多数日志条目 - 如果写入日志的代码崩溃,则不是很方便)
  2. records the script which triggered the query and the query itself 记录触发查询的脚本和查询本身
  3. records the memory usage before 记录以前的内存使用情况
  4. records the same facts after along with the time taken 记录相同的事实以及所花费的时间

Then do a recursive search and replace on your source code to replace calls to mysql_query() with your wrapper function. 然后执行递归搜索并替换源代码,用您的包装函数替换对mysql_query()的调用。

I would suggest you connect to your database server and run show full processlist; 我建议你连接到你的数据库服务器并运行show full processlist; this will show you all the active queries and you can debug from there. 这将显示所有活动查询,您可以从那里进行调试。 Also, adding some instrumentation to your project to log slow queries would be beneficial. 此外,向项目添加一些检测以记录慢速查询将是有益的。

There is only one query running at any given time, since this is how PHP works, it doesn't do stuff asynchronous. 在任何给定时间只运行一个查询,因为这是PHP的工作方式,它不会异步运行。 Unless you use some kind of 3rd party library to do it different. 除非你使用某种第三方库来做不同的事情。

*EDIT: Seems it is possible, maybe take a look at: Asynchronous PHP calls? *编辑:似乎有可能,也许看看: 异步PHP调用? . However when you get PHP to run async there is still no way to see how many queries are running. 但是,当您让PHP运行异步时,仍然无法查看正在运行的查询数。 Maybe you could try and check some mysql stats while running your code. 也许您可以在运行代码时尝试检查一些mysql统计信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM