I need to run below in two different modes hence the "if " ( second builds a large csv later on)
Below runs Ok for the single instance but falls over on load time for the second (*) as runs thru on each of the first 7k rows.
I would like to avoid the dreaded
set_time_limit(0);
I am also picking bits off at each stage as these are used later as well.
So The question is what is the fastest way to achieve below ?
May be one SQL statement and pull of defined values as in
table_c.tree , $row['apple']
Table structure
Table A has 7k rows where $row['Code'] can be repeated may be 10 times
Table B has 10k rows where $row['Group'] can be repeated may be 5 times
Table c has 2k rows where $row['Group2'] can be repeated may be 5 times
if (isset($_POST["go"])) { $where = 'WHERE xxxxx = 1';} else{$where = 'WHERE xxxxx = "'.$selected_val[1].'"';}
$stmt = $conn->prepare(" SELECT * FROM table_A $where "); $stmt->execute();
foreach ($stmt as $row) {
$Code = $row['Code'];
$stmt1 = $conn->prepare(" SELECT * FROM table_B WHERE Code = '$Code' "); $stmt1->execute();
foreach ( $stmt1 as $row1 ) {
if ($row['apple']) {$apple = $row['apple'];} // used to stop running rest all the time and also set $ for use later
else {$group1 = $row1['Group'];
$stmt2 = $conn->prepare(" SELECT * FROM table_c WHERE group2 = '$group1' "); $stmt2->execute();
foreach ( $stmt2 as $row2 ) {$group2 = $row2['group2'];}
$stmt3 = $conn->prepare(" SELECT * FROM table_d WHERE group3 = '$group2' "); $stmt3->execute();
foreach ( $stmt3 as $row3 ) {
$result [] = $row3['result']
}
}
}
/// do things with
$result
$Code
$group1
$row2['group2']
$row['apple']
}
The tables bar the first are not mine so I have no control over them.
An alternative may be to load some new fields into "Table_a" but that would be just splitting the time to other functions.
Following on from @yoshiwaan
Am I missing a speed trick here? if i create "my_result_chain" once (as all data can be dumped into at a good speed) at the beginning then just call in the foreach it runs very quick. BUT as there are some tables which do not include all references from the table one level up it returns null rather than the trail as far as possible. This is corrected by using LEFT JOIN but obviously creates multiple rows where filter key is the same etc is there a way to pick which row / field, DISTINCT Grabs only the top level , and there is not unique key to each table As I need to get various data from different tables ? for ref 13 sec v .03sec if created once. Sort of works if I add a key number to each row of the foreach but not a nice solution.