Performance of PHP client > fetching rows
Hi, I querying my database using a script in PHP and returning the result as a JSON (so that it can be retrieved in Javascript). However I notice that querying the database (sending the query to the database) takes about half the time if takes PHP to retrieve the rows (row by row using monetdb_fetch_object). In the documentation I see that the MAPI interface has a fetch_all_rows function, however I can't find this function in the PHP library. In the Python example I see that you are also using a cursor.fetchall() procedure. What is the best way to approach this problem > querying the database and returning all rows as a JSON object Is PHP the way to go (it is the easiest for me, since I know that), if it is, what is the best way or should I consider another language like Python or maybe Java to implement this function? Below I list the PHP code I see that send the query ($q_statement) to the database and formulating the JSON: // Measure time $time_end = microtime(true); $execution_time = ($time_end - $time_start); //dividing with 60 will give the execution time in minutes other wise seconds error_log($execution_time.'sec - Formulate query'); $time_start = microtime(true); $db = monetdb_connect($lang = "sql", $host = "127.0.0.1", $port = "50000" , $username = "monetdb", $password = "monetdb", $database = "social" ) or die(monetdb_last_error()); $res = monetdb_query($db, monetdb_escape_string($q_statement)); // Measure time $time_end = microtime(true); $execution_time = ($time_end - $time_start); //dividing with 60 will give the execution time in minutes other wise seconds error_log($execution_time.'sec - Query database'); $time_start = microtime(true); /* Iterate over the result set returning rows as objects */ while ( $row = monetdb_fetch_object($res) ) { $rows[] = $row; } echo json_encode($rows); // Measure time $time_end = microtime(true); $execution_time = ($time_end - $time_start); //dividing with 60 will give the execution time in minutes other wise seconds $total_execution_time = ($time_end - $initial_start); error_log($execution_time.'sec - Return records'); error_log($total_execution_time.'sec - Total time'); $time_start = microtime(true); thanks in advance, Richard
Hello Richard, On 16/03/13 13:50, Richard Siebeling wrote:
I querying my database using a script in PHP and returning the result as a JSON (so that it can be retrieved in Javascript).
However I notice that querying the database (sending the query to the database) takes about half the time if takes PHP to retrieve the rows (row by row using monetdb_fetch_object). Interestingly, the PHP connector already reads back the entire query response in the call to mapi_execute. Hence, the timings you observed seem to be only caused by calls to monetdb_fetch_object and json_encode, which do not talk to the database. There is no need for a fetchall()-method here.
How could you speed this up? Well, first it would be interesting which of the two calls actually take the time. If it is json_encode, you could try another method of encoding JSON, possibly with another library. If it is monetdb_fetch_object() the first, you could use another connector, e.g. JDBC or the C MAPI library. Best, Hannes
participants (2)
-
Hannes Mühleisen
-
Richard Siebeling