sequalit Posted March 30, 2008 Share Posted March 30, 2008 Say you have a table filled with transactions, both deposits and withdrawals are located in the table You have to split the data into two arrays, one for deposits and one for withdrawals Which in your opinion uses less resources to execute, and runs more effeciantly. Why do you think that? Two MySQL Queries $deposits = mysql_query(SELECT * FROM transactions WHERE type="deposit"); $withdrawals = mysql_query(SELECT * FROM transactions WHERE type="withdrawal"); $deposits = fetchTable($deposits); $withdrawals = fetchTable($withdrawals); or PHP parsed $transactions = mysql_query(SELECT * FROM transactions); $transactions = fetchTable($transactions); $expenses = array(); $deposits = array(); while($row = mysql_fetch_array($transactions, MYSQL_BOTH)){ if($row['amt'] >= 0) array_push($deposits, $row); else array_push($expenses, $row); } function fetchTable($data){ $table = array(); if(mysql_num_rows($data) == 0) return $table; mysql_data_seek($data, 0); while($row = mysql_fetch_array($data, MYSQL_BOTH)){ array_push($table, $row); } return $table; } Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/ Share on other sites More sharing options...
keeB Posted March 30, 2008 Share Posted March 30, 2008 I think I would have a Withdrawl (FK Trs.id), Deposit (Same FK), and Transaction tables. As for efficiency, always have the database do it. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504567 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 If you want to know which is faster, time them. www.php.net/microtime Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504623 Share on other sites More sharing options...
448191 Posted March 30, 2008 Share Posted March 30, 2008 Second example won't work, you need to lose the fetchTable thingy. Then, consider, example 1 queries the database server twice, and loops though the results twice. The second example does both only once. Both store the same amount of data in memory. Which one do YOU think is faster? Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504627 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 But the number of results is less each time in method 1, so you arn't comparing like with like. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504631 Share on other sites More sharing options...
448191 Posted March 30, 2008 Share Posted March 30, 2008 True, the total number of iterations is the same. I still find it hard to believe option 1 would be faster. In any case, the difference is probably marginal. As Barand said, test it if you want to be sure. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504644 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 If you're using the same where clause for both, and know you need to iterate over all results... use a single query. If you've got 2 different where clauses on the same table, use 2 queries. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504649 Share on other sites More sharing options...
keeB Posted March 30, 2008 Share Posted March 30, 2008 Still think 2 queries are more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504830 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 If that comment was in response to me, it's absurd. Consider the following: $query = "SELECT pk_field, name FROM sometable WHERE fk_field = 99"; $result = mysql_query($query); $rows = mysql_num_rows($result); for( $i = 0; $i < $rows; ++$i ) { $row = mysql_fetch_assoc($result); //do some work } By using another query (same where clause) we do this: $query = "SELECT age, gender FROM sometable WHERE fk_field = 99"; $result = mysql_query($query); $rows = mysql_num_rows($result); for( $i = 0; $i < $rows; ++$i ) { $row = mysql_fetch_assoc($result); //do some work } Now with a single query we could eliminate an extra call to the DBMS... and we eliminate a loop. $query = "SELECT pk_field, name, age, gender FROM sometable WHERE fk_field = 99"; $result = mysql_query($query); $rows = mysql_num_rows($result); for( $i = 0; $i < $rows; ++$i ) { $row = mysql_fetch_assoc($result); //do some work } Now, if you've got two different where clauses, it's going to be more efficient to use the DB, as the DB is indexed in such a way that it's much more efficient at finding it than you could do parsing it manually. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-504873 Share on other sites More sharing options...
sequalit Posted March 30, 2008 Author Share Posted March 30, 2008 Correction on the example on PHP parsing, you do not have to do the fetchTable() thing I called, which returns a two-deminsional array, you just need the mysql results output Took 0.000418901443481 to run one big query and parse with PHP Took 0.00118207931519 to run two queries Took 0.000531911849976 to run one big query and parse with PHP Took 0.000844955444336 to run two queries Took 0.000542163848877 to run one big query and parse with PHP Took 0.000877141952515 to run two queries Took 0.000545978546143 to run one big query and parse with PHP Took 0.00101900100708 to run two queries Took 0.000496864318848 to run one big query and parse with PHP Took 0.000808000564575 to run two queries function testPHPparsed(){ $time_start = microtime(true); $sql = "". "SELECT userID, date, transactions.name, amt, transactionTypes.transType, transactionCategories.transCat". " FROM transactions, transactionTypes, transactionCategories". " WHERE transactions.typeID = transactionTypes.typeID". " AND transactions.catID = transactionCategories.catID". " AND userID = 1"; $transactions = $this->mysql->queryGetResults($sql);//from my database class $transactions = $transactions->getResults();//gets the actual data returned from mysql_query $expenses = array(); $deposits = array(); while($row = mysql_fetch_array($transactions, MYSQL_BOTH)){ if($row['amt'] >= 0) array_push($deposits, $row); else array_push($expenses, $row); } mysql_free_result($transactions); $time_end = microtime(true); $time = $time_end - $time_start; echo "<br>Took $time to run one big query and parse with PHP"; } function testTwoQuery(){ $time_start = microtime(true); $depositsQry = "". "SELECT userID, date, transactions.name, amt, transactionTypes.transType, transactionCategories.transCat". " FROM transactions, transactionTypes, transactionCategories". " WHERE transactions.typeID = 1". " AND transactions.catID = transactionCategories.catID". " AND userID = 1"; $expensesQry = "". "SELECT userID, date, transactions.name, amt, transactionTypes.transType, transactionCategories.transCat". " FROM transactions, transactionTypes, transactionCategories". " WHERE transactions.typeID = 2". " AND transactions.catID = transactionCategories.catID". " AND userID = 1"; $deposits = $this->mysql->queryGetResults($depositsQry);//gets results $expenses = $this->mysql->queryGetResults($expensesQry);//gets results $deposits = $deposits->fetchTableAndFree();//turns results into a two-dimensional array to be passed to Template Engine and free's the results $expenses = $expenses->fetchTableAndFree();//turns results into two-dimensional array to be passed to Template Engine and free's the results $time_end = microtime(true); $time = $time_end - $time_start; echo "<br>Took $time to run two queries"; } Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505083 Share on other sites More sharing options...
sequalit Posted March 30, 2008 Author Share Posted March 30, 2008 more results: Looking at it, it seems that running two queries generally runs around .0008 no matter what, sometimes it shot up to .0010 Parsing with PHP was sometimes twice as fast as running two queries, but in some cases ended up being very slow. All these calls were made in a for loop one right after another... Generally parsing in PHP is faster, but if you are making a bunch of database calls at the same time, two queries is more stable. Took 0.000423908233643 to run one big query and parse with PHP Took 0.00081992149353 to run two queries Took 0.00131893157959 to run one big query and parse with PHP Took 0.00081205368042 to run two queries Took 0.00046706199646 to run one big query and parse with PHP Took 0.000813961029053 to run two queries Took 0.000460863113403 to run one big query and parse with PHP Took 0.00080394744873 to run two queries Took 0.00125288963318 to run one big query and parse with PHP Took 0.000875949859619 to run two queries Took 0.000470161437988 to run one big query and parse with PHP Took 0.000808000564575 to run two queries Took 0.00047492980957 to run one big query and parse with PHP Took 0.000827074050903 to run two queries Took 0.000459909439087 to run one big query and parse with PHP Took 0.000782012939453 to run two queries Took 0.000473976135254 to run one big query and parse with PHP Took 0.00080394744873 to run two queries Took 0.00162196159363 to run one big query and parse with PHP Took 0.000848054885864 to run two queries Took 0.000874996185303 to run one big query and parse with PHP Took 0.000823020935059 to run two queries Took 0.000463008880615 to run one big query and parse with PHP Took 0.000833988189697 to run two queries Took 0.000469923019409 to run one big query and parse with PHP Took 0.000808954238892 to run two queries Took 0.000465154647827 to run one big query and parse with PHP Took 0.000816822052002 to run two queries Took 0.000470161437988 to run one big query and parse with PHP Took 0.000808954238892 to run two queries Took 0.000465154647827 to run one big query and parse with PHP Took 0.00120115280151 to run two queries Took 0.000458955764771 to run one big query and parse with PHP Took 0.000802040100098 to run two queries Took 0.000505924224854 to run one big query and parse with PHP Took 0.000778913497925 to run two queries Took 0.000575065612793 to run one big query and parse with PHP Took 0.000818014144897 to run two queries Took 0.000460863113403 to run one big query and parse with PHP Took 0.000833034515381 to run two queries Took 0.000464916229248 to run one big query and parse with PHP Took 0.000817775726318 to run two queries Took 0.000471115112305 to run one big query and parse with PHP Took 0.00116610527039 to run two queries Took 0.000472068786621 to run one big query and parse with PHP Took 0.000845193862915 to run two queries Took 0.000591039657593 to run one big query and parse with PHP Took 0.00122308731079 to run two queries Took 0.000942945480347 to run one big query and parse with PHP Took 0.000870227813721 to run two queries Took 0.000476121902466 to run one big query and parse with PHP Took 0.000828981399536 to run two queries Took 0.000488042831421 to run one big query and parse with PHP Took 0.000829935073853 to run two queries Took 0.000472784042358 to run one big query and parse with PHP Took 0.00139999389648 to run two queries Took 0.00048303604126 to run one big query and parse with PHP Took 0.000809907913208 to run two queries Took 0.000481843948364 to run one big query and parse with PHP Took 0.000984907150269 to run two queries Took 0.000528812408447 to run one big query and parse with PHP Took 0.000830888748169 to run two queries Took 0.000534057617188 to run one big query and parse with PHP Took 0.00177597999573 to run two queries Took 0.000501871109009 to run one big query and parse with PHP Took 0.00089693069458 to run two queries Took 0.000468969345093 to run one big query and parse with PHP Took 0.000833034515381 to run two queries Took 0.000463962554932 to run one big query and parse with PHP Took 0.000808000564575 to run two queries Took 0.000488996505737 to run one big query and parse with PHP Took 0.000815153121948 to run two queries Took 0.000463008880615 to run one big query and parse with PHP Took 0.000815868377686 to run two queries Took 0.000468969345093 to run one big query and parse with PHP Took 0.000823974609375 to run two queries Took 0.000459909439087 to run one big query and parse with PHP Took 0.000813961029053 to run two queries Took 0.000459909439087 to run one big query and parse with PHP Took 0.00103998184204 to run two queries Took 0.000488996505737 to run one big query and parse with PHP Took 0.000797986984253 to run two queries Took 0.000468969345093 to run one big query and parse with PHP Took 0.0010769367218 to run two queries Took 0.000463962554932 to run one big query and parse with PHP Took 0.000801086425781 to run two queries Took 0.00112199783325 to run one big query and parse with PHP Took 0.000917196273804 to run two queries Took 0.000474214553833 to run one big query and parse with PHP Took 0.00084114074707 to run two queries Took 0.00047492980957 to run one big query and parse with PHP Took 0.0014021396637 to run two queries Took 0.000473022460938 to run one big query and parse with PHP Took 0.000827074050903 to run two queries Took 0.000460147857666 to run one big query and parse with PHP Took 0.00081992149353 to run two queries Took 0.00082802772522 to run one big query and parse with PHP Took 0.000808954238892 to run two queries Took 0.000445127487183 to run one big query and parse with PHP Took 0.000785827636719 to run two queries Took 0.000448942184448 to run one big query and parse with PHP Took 0.000777959823608 to run two queries Took 0.000454902648926 to run one big query and parse with PHP Took 0.000964164733887 to run two queries Took 0.00047492980957 to run one big query and parse with PHP Took 0.000805854797363 to run two queries Took 0.000469923019409 to run one big query and parse with PHP Took 0.000805854797363 to run two queries Took 0.000472068786621 to run one big query and parse with PHP Took 0.00081205368042 to run two queries Took 0.000466108322144 to run one big query and parse with PHP Took 0.000806093215942 to run two queries Took 0.000468015670776 to run one big query and parse with PHP Took 0.00081205368042 to run two queries Took 0.000463008880615 to run one big query and parse with PHP Took 0.00081205368042 to run two queries Took 0.000465869903564 to run one big query and parse with PHP Took 0.000813961029053 to run two queries Took 0.000465154647827 to run one big query and parse with PHP Took 0.00219416618347 to run two queries Took 0.000470161437988 to run one big query and parse with PHP Took 0.000815868377686 to run two queries Took 0.000478029251099 to run one big query and parse with PHP Took 0.000789880752563 to run two queries Took 0.000449895858765 to run one big query and parse with PHP Took 0.000792980194092 to run two queries Took 0.000457048416138 to run one big query and parse with PHP Took 0.000790119171143 to run two queries Took 0.000483989715576 to run one big query and parse with PHP Took 0.00081205368042 to run two queries Took 0.000463962554932 to run one big query and parse with PHP Took 0.000805854797363 to run two queries Took 0.000761985778809 to run one big query and parse with PHP Took 0.000808954238892 to run two queries Took 0.000484943389893 to run one big query and parse with PHP Took 0.000807046890259 to run two queries Took 0.000473022460938 to run one big query and parse with PHP Took 0.000809907913208 to run two queries Took 0.000472068786621 to run one big query and parse with PHP Took 0.000814914703369 to run two queries Took 0.000465869903564 to run one big query and parse with PHP Took 0.000785112380981 to run two queries Took 0.000453948974609 to run one big query and parse with PHP Took 0.000777006149292 to run two queries Took 0.000452995300293 to run one big query and parse with PHP Took 0.000769853591919 to run two queries Took 0.000452041625977 to run one big query and parse with PHP Took 0.000771999359131 to run two queries Took 0.000447034835815 to run one big query and parse with PHP Took 0.000779867172241 to run two queries Took 0.000449895858765 to run one big query and parse with PHP Took 0.00078010559082 to run two queries Took 0.000447034835815 to run one big query and parse with PHP Took 0.000771999359131 to run two queries Took 0.00044584274292 to run one big query and parse with PHP Took 0.000773906707764 to run two queries Took 0.000443935394287 to run one big query and parse with PHP Took 0.000808000564575 to run two queries Took 0.000463962554932 to run one big query and parse with PHP Took 0.000809907913208 to run two queries Took 0.00139999389648 to run one big query and parse with PHP Took 0.000861167907715 to run two queries Took 0.000831842422485 to run one big query and parse with PHP Took 0.00134301185608 to run two queries Took 0.00045108795166 to run one big query and parse with PHP Took 0.000817060470581 to run two queries Took 0.000458955764771 to run one big query and parse with PHP Took 0.00078296661377 to run two queries Took 0.00045108795166 to run one big query and parse with PHP Took 0.00078010559082 to run two queries Took 0.000455856323242 to run one big query and parse with PHP Took 0.000787973403931 to run two queries Took 0.000458002090454 to run one big query and parse with PHP Took 0.000782012939453 to run two queries Took 0.000454902648926 to run one big query and parse with PHP Took 0.00083589553833 to run two queries Took 0.000458955764771 to run one big query and parse with PHP Took 0.000782012939453 to run two queries Took 0.000453948974609 to run one big query and parse with PHP Took 0.000787019729614 to run two queries Took 0.000457048416138 to run one big query and parse with PHP Took 0.000786066055298 to run two queries Took 0.000452041625977 to run one big query and parse with PHP Took 0.000794887542725 to run two queries Took 0.000455856323242 to run one big query and parse with PHP Took 0.000786066055298 to run two queries Took 0.000452995300293 to run one big query and parse with PHP Took 0.00078821182251 to run two queries Took 0.000463008880615 to run one big query and parse with PHP Took 0.000787019729614 to run two queries Took 0.000452041625977 to run one big query and parse with PHP Took 0.000787019729614 to run two queries Took 0.000468969345093 to run one big query and parse with PHP Took 0.00447082519531 to run two queries Took 0.00273609161377 to run one big query and parse with PHP Took 0.00416684150696 to run two queries Took 0.00421690940857 to run one big query and parse with PHP Took 0.00127410888672 to run two queries Took 0.00244998931885 to run one big query and parse with PHP Took 0.00440096855164 to run two queries Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505091 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 How many results were returned from your queries? How does your DB abstraction layer work? Were you connecting each time you executed a query? Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505101 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 Assuming the objective is either A ) 2 results sets, one of withdrawals and one of deposits or B ) 2 arrays, one of withdrawals and one of deposits (from single query) In my `sales` table there are just under 11,000 rows and the median value is approx 40.00, so each "small" query retrieves 5000+ rows. <?php include 'db.php'; $lo = $hi = array(); /********* * Method A */ $t1 = microtime(1); $reslo = mysql_query ("SELECT * FROM sales WHERE salesvalue < 40"); $reshi = mysql_query ("SELECT * FROM sales WHERE salesvalue >= 40"); /********* * Method B */ $t2 = microtime(1); $res = mysql_query ("SELECT * FROM sales"); while ($r = mysql_fetch_assoc($res)) { if ($r['salesvalue'] < 40) $lo[] = $r; else $hi[] = $r; } $t3 = microtime(1); /********* * Results */ echo '<pre>'; printf ('Method A : %10.5f<br>', $t2-$t1); // Method A : 0.20248 (typical) printf ('Method B : %10.5f', $t3-$t2); // Method B : 0.30840 (typical) echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505117 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 The results become more and more evident when you start running against large databases, with hundreds of thousands of rows. A big part of why it's faster also has to do with an array having to resize itself, which requires it to create a bigger memory block and then copy over all of the old data into the new array. I don't know the details of how this is implemented in PHP, what the default size of an array is, or when it resizes, but this is something we had a fair amount of discussions on in CS classes. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505127 Share on other sites More sharing options...
sequalit Posted March 30, 2008 Author Share Posted March 30, 2008 How many results were returned from your queries? How does your DB abstraction layer work? Were you connecting each time you executed a query? the database returns ~1000 results each query. The connect to the database was made once, then queries ran each time, then the database was closed in the end. The objective is to get two arrays, to be passed into a template engine for display into two tables, one for each kind of data. my DB abstraction layer, so you can see it ;p, though please explain what it has to do with it? <?php class MySQL{ protected $con; protected $res; protected $query; protected $connected; protected $server; protected $user; protected $pass; protected $database; function MySQL($server, $user, $pass, $database){ $this->server = $server; $this->user = $user; $this->pass = $pass; $this->database = $database; $this->query = ""; $this->connect(); } function selectDB($db){ mysql_select_db($db, $this->con); } function connect(){ $this->con = mysql_pconnect($this->server, $this->user, $this->pass) or trigger_error(mysql_error(), E_USER_ERROR); $this->selectDB($this->database); $this->connected = true; } function query($sql){ if(!$this->connected) $this->connect(); $this->query = $sql; $this->res = mysql_query($sql, $this->con); } function queryGetResults($sql){ if(!$this->connected) $this->connect(); $this->query = $sql; $this->res = mysql_query($sql, $this->con); return new results($this->res); } function queryError(){ echo "Unable to process query<br />"; $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $this->query; die($message); } function getResults(){ return new results($this->res); } function fetch(){ return mysql_fetch_row($this->res); } function close(){ if(!$this->connected) return; mysql_close($this->con); $this->connected = false; } } class results{ protected $data; function results($res){ $this->data = $res; } function getResults(){ return $this->data; } function free(){ mysql_free_result($this->data); } function fetch(){ return mysql_fetch_assoc($this->data); } function fetchTable(){ $table = array(); if(mysql_num_rows($this->data) == 0) return $table; mysql_data_seek($this->data, 0); while($row = mysql_fetch_array($this->data, MYSQL_BOTH)){ array_push($table, $row); } return $table; } function fetchTableAndFree(){ $table = $this->fetchTable(); $this->free(); return $table; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505128 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 Biggest reason I was curious was the connection handling. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505145 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 in that case there is very little between them <?php include 'db.php'; /********* * Method A */ $t1 = microtime(1); $lo = $hi = array(); $reslo = mysql_query ("SELECT * FROM sales WHERE salesvalue < 40"); $reshi = mysql_query ("SELECT * FROM sales WHERE salesvalue >= 40"); while ($r = mysql_fetch_row($reslo)) $lo[] = $r; while ($r = mysql_fetch_row($reshi)) $hi[] = $r; /********* * Method B */ $t2 = microtime(1); $lo2 = $hi2 = array(); $res = mysql_query ("SELECT * FROM sales"); while ($r = mysql_fetch_assoc($res)) { if ($r['salesvalue'] < 40) $lo2[] = $r; else $hi2[] = $r; } $t3 = microtime(1); /********* * Results */ echo '<pre>'; printf ('Method A : %10.5f<br>', $t2-$t1); // Method A : 0.29025 (typical) printf ('Method B : %10.5f', $t3-$t2); // Method B : 0.31534 (typical) echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505160 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 Which case? Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505167 Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 Which case? That "The objective is to get two arrays" Method A - create arrays from 2 queries Method B - create arrays from 1 query Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505169 Share on other sites More sharing options...
dbo Posted March 30, 2008 Share Posted March 30, 2008 Ah, gotcha. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505171 Share on other sites More sharing options...
sequalit Posted March 31, 2008 Author Share Posted March 31, 2008 well i tried to do it again, but under very intense testing... looping each method 10000 times, while keeping a average time and a count on which was faster... Needless to say, I froze my VPS and it has been off-line for the past 6 hours HAHA!~ It's safe to assume then, that it is less intensive and quicker to go with one db query and split the data as needed into the arrays instead of two queries. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505311 Share on other sites More sharing options...
dbo Posted March 31, 2008 Share Posted March 31, 2008 So so wrong, but do what you will Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505320 Share on other sites More sharing options...
keeB Posted March 31, 2008 Share Posted March 31, 2008 Question: Who displays > 200 rows of anything? Isn't this why paging was invented? If you are exporting this information to pdf, xls, csv -- doing it in chunks is most likely safer and less intensive on the server. I'm going to assume the database server has better ways of extracting the data and parsing it than you do. If not, contact said database and get a job there. As for starting with 2 arrays and parsing them, I have no idea but starting with 2 sounds faster than having a huge one and breaking it in to 2. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505394 Share on other sites More sharing options...
sequalit Posted March 31, 2008 Author Share Posted March 31, 2008 Average Time of PHP Completion (100 results) :0.00052145242691 Average Time of QRY Completion (100 results) :0.000961785316467 PHP faster by :0.000333070755005 0.00061297416687 - 0.000946044921875 PHP faster by :0.000367879867554 0.000465154647827 - 0.000833034515381 PHP faster by :0.00034499168396 0.000468015670776 - 0.000813007354736 PHP faster by :0.000280857086182 0.000524997711182 - 0.000805854797363 PHP faster by :0.000319004058838 0.000500917434692 - 0.00081992149353 PHP faster by :0.000659942626953 0.00047492980957 - 0.00113487243652 QRY faster by :0.000424146652222 0.0012640953064 - 0.000839948654175 PHP faster by :0.000411987304688 0.000391960144043 - 0.00080394744873 PHP faster by :0.000409841537476 0.000393152236938 - 0.000802993774414 PHP faster by :0.00040078163147 0.000395059585571 - 0.000795841217041 PHP faster by :0.000392913818359 0.000403165817261 - 0.00079607963562 PHP faster by :0.000401020050049 0.000396013259888 - 0.000797033309937 PHP faster by :0.000380992889404 0.000416994094849 - 0.000797986984253 PHP faster by :0.000495910644531 0.000389099121094 - 0.000885009765625 PHP faster by :0.000411987304688 0.000391960144043 - 0.00080394744873 PHP faster by :0.000400066375732 0.000405073165894 - 0.000805139541626 PHP faster by :0.000410079956055 0.000391960144043 - 0.000802040100098 PHP faster by :0.000412940979004 0.000388145446777 - 0.000801086425781 PHP faster by :0.000998973846436 0.000396013259888 - 0.00139498710632 PHP faster by :0.000407934188843 0.000395059585571 - 0.000802993774414 QRY faster by :0.000292062759399 0.00124311447144 - 0.000951051712036 PHP faster by :0.000439167022705 0.000391960144043 - 0.000831127166748 PHP faster by :0.000866889953613 0.000389099121094 - 0.00125598907471 PHP faster by :0.000408887863159 0.000400066375732 - 0.000808954238892 PHP faster by :0.000417232513428 0.000392913818359 - 0.000810146331787 PHP faster by :0.000415086746216 0.000393867492676 - 0.000808954238892 PHP faster by :0.000421047210693 0.000391006469727 - 0.00081205368042 PHP faster by :0.00041389465332 0.000395059585571 - 0.000808954238892 PHP faster by :0.000414848327637 0.000389099121094 - 0.00080394744873 PHP faster by :0.000419855117798 0.000393152236938 - 0.000813007354736 PHP faster by :0.000417232513428 0.000512838363647 - 0.000930070877075 PHP faster by :0.000409126281738 0.000388860702515 - 0.000797986984253 PHP faster by :0.000411987304688 0.000388860702515 - 0.000800848007202 PHP faster by :0.00041127204895 0.00038480758667 - 0.00079607963562 PHP faster by :0.000415802001953 0.000388145446777 - 0.00080394744873 PHP faster by :0.000412940979004 0.000389099121094 - 0.000802040100098 PHP faster by :0.000412940979004 0.00039005279541 - 0.000802993774414 PHP faster by :0.000915050506592 0.000385046005249 - 0.00130009651184 PHP faster by :0.000320911407471 0.000483989715576 - 0.000804901123047 PHP faster by :0.000415086746216 0.000391006469727 - 0.000806093215942 PHP faster by :0.000419855117798 0.000396013259888 - 0.000815868377686 PHP faster by :0.000414848327637 0.000388145446777 - 0.000802993774414 PHP faster by :0.000408172607422 0.000391960144043 - 0.000800132751465 PHP faster by :0.000945091247559 0.000388860702515 - 0.00133395195007 PHP faster by :0.000409841537476 0.000394105911255 - 0.00080394744873 PHP faster by :0.000416994094849 0.000391006469727 - 0.000808000564575 PHP faster by :0.000655651092529 0.000393152236938 - 0.00104880332947 PHP faster by :0.000524044036865 0.000395059585571 - 0.000919103622437 PHP faster by :0.000411987304688 0.000385999679565 - 0.000797986984253 PHP faster by :0.000810861587524 0.000389099121094 - 0.00119996070862 PHP faster by :0.000418901443481 0.000391006469727 - 0.000809907913208 PHP faster by :0.000416994094849 0.000389099121094 - 0.000806093215942 PHP faster by :0.000427961349487 0.000399112701416 - 0.000827074050903 PHP faster by :0.000445127487183 0.000395059585571 - 0.000840187072754 PHP faster by :0.000355958938599 0.000424146652222 - 0.00078010559082 PHP faster by :0.00093412399292 0.000375986099243 - 0.00131011009216 PHP faster by :0.000789880752563 0.000403165817261 - 0.00119304656982 PHP faster by :0.000420093536377 0.000396966934204 - 0.000817060470581 PHP faster by :0.000411033630371 0.000391006469727 - 0.000802040100098 PHP faster by :0.000423908233643 0.000386953353882 - 0.000810861587524 PHP faster by :0.000442028045654 0.000391960144043 - 0.000833988189697 PHP faster by :0.00114011764526 0.000401973724365 - 0.00154209136963 PHP faster by :0.000420808792114 0.000392198562622 - 0.000813007354736 PHP faster by :0.000306129455566 0.000504970550537 - 0.000811100006104 PHP faster by :0.000336885452271 0.000473022460938 - 0.000809907913208 PHP faster by :0.000412940979004 0.0003981590271 - 0.000811100006104 PHP faster by :0.000419139862061 0.000386953353882 - 0.000806093215942 PHP faster by :0.000409126281738 0.000396013259888 - 0.000805139541626 PHP faster by :0.000429153442383 0.000391960144043 - 0.000821113586426 PHP faster by :0.00040602684021 0.000393867492676 - 0.000799894332886 PHP faster by :0.00041127204895 0.000387907028198 - 0.000799179077148 PHP faster by :0.00040602684021 0.000396013259888 - 0.000802040100098 PHP faster by :0.000472068786621 0.00048303604126 - 0.000955104827881 PHP faster by :0.000577926635742 0.000468969345093 - 0.00104689598083 PHP faster by :0.00032901763916 0.000484943389893 - 0.000813961029053 PHP faster by :0.000324726104736 0.000477075576782 - 0.000801801681519 PHP faster by :0.000331878662109 0.000469923019409 - 0.000801801681519 PHP faster by :0.000359058380127 0.000468969345093 - 0.00082802772522 PHP faster by :0.000334024429321 0.00046706199646 - 0.000801086425781 PHP faster by :0.000325202941895 0.00047492980957 - 0.000800132751465 PHP faster by :0.000371217727661 0.000470876693726 - 0.000842094421387 PHP faster by :4.29153442383E-05 0.000797033309937 - 0.000839948654175 PHP faster by :0.000309944152832 0.000498056411743 - 0.000808000564575 PHP faster by :0.000505208969116 0.000476837158203 - 0.000982046127319 PHP faster by :0.00117111206055 0.00120401382446 - 0.00237512588501 PHP faster by :0.000298976898193 0.000488996505737 - 0.000787973403931 PHP faster by :0.000324010848999 0.000458955764771 - 0.00078296661377 PHP faster by :0.000342130661011 0.000458955764771 - 0.000801086425781 PHP faster by :0.000323057174683 0.000463008880615 - 0.000786066055298 PHP faster by :0.000323057174683 0.000460863113403 - 0.000783920288086 PHP faster by :0.000317096710205 0.000464916229248 - 0.000782012939453 PHP faster by :0.000832796096802 0.00045919418335 - 0.00129199028015 QRY faster by :0.000553131103516 0.00143814086914 - 0.000885009765625 QRY faster by :5.79357147217E-05 0.00163888931274 - 0.00158095359802 PHP faster by :0.000288009643555 0.000504016876221 - 0.000792026519775 PHP faster by :0.00089168548584 0.00045919418335 - 0.00135087966919 PHP faster by :0.000316143035889 0.000465869903564 - 0.000782012939453 PHP faster by :0.000323057174683 0.000464916229248 - 0.000787973403931 PHP faster by :0.00107789039612 0.00274300575256 - 0.00382089614868 PHP faster by :0.0012469291687 0.00280690193176 - 0.00405383110046 Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505888 Share on other sites More sharing options...
Barand Posted March 31, 2008 Share Posted March 31, 2008 if the differences are measured in tenths of thousandths of second, who cares? Use a method that is easier to understand. easier to write and easier to maintain. Because ultimately, that's where the true cost will be. Quote Link to comment https://forums.phpfreaks.com/topic/98586-faster-way-of-getting-two-sets-of-data-from-same-table/#findComment-505898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.