当执行.php文件时,mysql_query命令是以top-> bottom方式执行的吗?

I am attempting to build a progress bar loaded for a long running page (yes indexes are obvious solution but currently the dataset/schema prohibits partitioning/proper indexing) so I plan to use a GUID/uniqueID within the query comment to track progess with SHOW_FULL_PROCESSLIST via ajax - but the key to this rests on if the sequential queries are executed in order by php, does anyone know?

MySQL as a database server uses multiple threads to handle multiple running queries. It allocates a thread as and when it received a query from a client i.e. a PHP connection or ODBC connection etc.

However, since you mentioned mysql_query I think there can be 2 things in your mind:

  1. if you call mysql_query() multiple times to pass various commands, then MySQL will be passed each query after the previous query is completely executed and the result returned to PHP. So, of course MySQL will seem to then work sequentially although it is actually PHP that is waiting to send MySQL a query till one query is finished.

  2. In MySQL5 and PHP5 there is a function called (mysqli_multi_query()) using which you can pass multiple queries at once to MySQL without PHP waiting for 1 query to end. The function will return all results at once in the same result object. MySQL will actually run all the queries at once using multiple threads and the wait time in this case is substantially less and you also tend to use the server resources available much better as all the queries will run as separate threads.

As Babbage once said, "I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." Calls to mysql_query, like calls to any other function, execute in the order that they are reached in program flow.

Order in the file is mostly irrelevant. It's order of execution that matters.

<?php

    myCmdFour();
    myCmdTwo();
    myCmdThree();
    myCmdTwo();

    function myCmdTwo() {
        mysql_query(...);
    }

    function myCmdThree() {
        mysql_query(...);
    }

    function myCmdFour() {
        mysql_query(...);
    }

    myCmdFour();
    myCmdThree();
    myCmdTwo();
    myCmdTwo();
?>

Although anyone that has PHP files that look like that, needs to seriously rethink things.