spleinmuncher Posted March 31, 2010 Share Posted March 31, 2010 So, I have recently switched to MySQLi for security reasons. This has been great, but it seems to have come at the cost of performance. To be more specific, the action of taking the results from a mysql query and converting them into a multi-dimensional array for PHP use seems to have become much slower. Whereas before I might use: $result = mysql_query(...); while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray)); I now have (disregard the $type deal; that is application specific): function results($type=0) { global $mysqli, $stmt, $result, $param; if ($type == 0) { $meta = $stmt->result_metadata(); $result = array(); while ($field = $meta->fetch_field()) { $fields[] = &$row[$field->name]; } call_user_func_array(array($stmt, 'bind_result'), $fields); while ($stmt->fetch()) { foreach($row as $key => $val) { $c[$key] = $val; } $result[] = $c; } } Now, I am currently designing a search protocol that parses a VERY large database (around 50,000 rows, with each row having something like 60 fields). For large queries, this can be very slow. In particular, the line "$c[$key] = $val;" could be run over a million times on some queries! Some queries have resulted in the system running out of memory, or taking close to 25 seconds to complete. I have confirmed that the hang up is in this area - the amount time every other part of the process is negligible in comparison to this behemoth. So, what I am looking for are clever ways to re-write the code to use less resources and execute faster. For example, I have been considering whether to use "for" or "foreach," though I have heard conflicting reports as to which is faster. Anyway, any help would be greatly appreciated. Thanks! -Alex (To Admin: sorry about double posting; I accidentally put it in the PHP Regex forum as well) Link to comment https://forums.phpfreaks.com/topic/197059-mysqli-and-returning-multi-dimensional-arrays-speed-optimization/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.