6

I am using a MySQL database with phpMyAdmin as the frontend (I am not sure I have remote/client access yet). I have a script that queries the database and would like to see how long each query takes? What is the easiest way to do this? Could I install another PHP app on the server?

3 Answers 3

2

If you have access to MySQL config files, you can enable general query log and slow query log.

See here for details.

Since, as I think, 5.1.6, you can also do it in runtime:

SET GLOBAL long_query_time = 10  /* which queries are considered long */
SET GLOBAL slow_query_log = 1

, but try it anyway, I don't rememeber exact version when it appeared.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think I have access to those. I am using a cPanel backend/config management system for the website.
2

For most applications that I work on, I include query profiling output that can easily be turned on in the development environment. This outputs the SQL, execution time, stack trace and a link to display explain output. It also highlights queries running longer than 1 second.

Although you probably don't need something as sophisticated, you can get a fairly good sense of run time of queries by writing a function in PHP that wraps the query execution, and store debug information on the session (or simply output it). For example:

function run_query($sql, $debug=false, $output=false) {
    $start = microtime(true);
    $q = mysql_query($sql);
    $time = microtime(true) - $start;

    if($debug) {
        $debug = "$sql<br/>$time<br/><br/>";
        if($output) {
            print $debug;
        } else {
            $_SESSION['sql_debug'] .= $debug;
        }
    }

    return $q;

 }

That's just kind of a rough idea. You can tweak it however you want.

Hope that helps -

Comments

1

You should set long_query_time to 1 since setting it to 10 will exclude most if not all of your queries. In the mysql prompt type

set @@long_query_time=1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.