views:

281

answers:

4

i have a overflow memory usage problem and i need badly to scan all my scripts . so my question is how to show a list of running mysql queries in php

A: 

There is only one query running at any given time, since this is how PHP works, it doesn't do stuff asynchronous. Unless you use some kind of 3rd party library to do it different.

*EDIT: Seems it is possible, maybe take a look at: http://stackoverflow.com/questions/124462/asynchronous-php-calls. However when you get PHP to run async there is still no way to see how many queries are running. Maybe you could try and check some mysql stats while running your code.

Kolky
hey i know how to find the number of running queries : simple sql query statement : $con->sql_num_queries$con is my db class
farshad Ghazanfari
However it probably only lists all the active queries, from multiple active pages and not from only the current page. Or does it? :)
Kolky
if u consider that my page including other functions from other files then the number of queries the sql statement shows is the amount of queries ruining right now .
farshad Ghazanfari
+1  A: 

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:

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;
}
Veger
+2  A: 

If the problem is on MySQL site this could help a lot:

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.

Petr Peller
It shows only the processes according to http://dev.mysql.com/doc/refman/5.1/en/show-processlist.html not the amount of queries (or other query information)
Veger
It shows all processes. Queries are subset of the processes.
Petr Peller
A: 

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:

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 fn.

C.

symcbean
i got u , i find the problem by setting some break point in my script , but problem was while() statement for showing my latest stories , forexample if i want to show 5 stories in a page then num queries will be 60 and for showing one story it would be 13 question is how to avoid this problem ?
farshad Ghazanfari