Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I must confess Im still a novice PHP/MySQL coder, but with many ideas; so my code is probably "dirty".</p> <p>So I solved the problem this way in order to move forward, but please better solutions are welcome. As for any strange syntax, I am using a database class built upon the PHP PDO because I am using many different RBDMS types on this project.</p> <p>For the $myQuery_start variable, I added the names of the other columns as well as sensors 1 to 3. <code><br /> $myQuery_start = "(SELECT sensor1, sensor2, sensor3 FROM ";<br/> $myQueryCount_start = "(SELECT COUNT(*) FROM ";<br/> $myQuery_stop = " WHERE robotID=".$robotID." AND timestamp BETWEEN ".$timeStampStart." AND ".$timeStampStop.")";<br /> </code> <br /> <code> $count_0 = DB::getDB("mysql", $myDB)->query($myQueryCount_start."table_0".$myQuery_stop)->fetchColumn();<br /> $count_1 = DB::getDB("mysql", $myDB)->query($myQueryCount_start."table_1".$myQuery_stop)->fetchColumn();<br /> $count_2 = DB::getDB("mysql", $myDB)->query($myQueryCount_start."table_2".$myQuery_stop)->fetchColumn();<br /> $count_3 = DB::getDB("mysql", $myDB)->query($myQueryCount_start."table_3".$myQuery_stop)->fetchColumn();<br /> $count_4 = DB::getDB("mysql", $myDB)->query($myQueryCount_start."table_4".$myQuery_stop)->fetchColumn();<br /> $count_5 = DB::getDB("mysql", $myDB)->query($myQueryCount_start."table_5".$myQuery_stop)->fetchColumn();<br /> </code> <br /> And now I check to see if UNION ALL needs to be appended to each table's query or not. No need to have a UNION ALL if there is no data record to attach in the next table. <br /> <code> $union_0 = (($count_1 + $count_2 + $count_3 + $count_4 + $count_5) > 0)?" UNION ALL ":"";<br /> $union_1 = (($count_2 + $count_3 + $count_4 + $count_5) > 0)?" UNION ALL ":"";<br /> $union_2 = (($count_3 + $count_4 + $count_5) > 0)?" UNION ALL ":"";<br /> $union_3 = (($count_4 + $count_5) > 0)?" UNION ALL ":"";<br /> $union_4 = (($count_5) > 0)?" UNION ALL ":"";<br /> </code> <br /> and now we build up the table queries and combine to form the full query<br /> <br /> <code> $query_0 = ($count_0 > 0)?$myQuery_start."ip_minute_stats_0".$myQuery_stop.$union_0:"";<br /> $query_1 = ($count_1 > 0)?$myQuery_start."ip_minute_stats_1".$myQuery_stop.$union_1:"";<br /> $query_2 = ($count_2 > 0)?$myQuery_start."ip_minute_stats_2".$myQuery_stop.$union_2:"";<br /> $query_3 = ($count_3 > 0)?$myQuery_start."ip_minute_stats_3".$myQuery_stop.$union_3:"";<br /> $query_4 = ($count_4 > 0)?$myQuery_start."ip_minute_stats_4".$myQuery_stop.$union_4:"";<br /> $query_5 = ($count_5 > 0)?$myQuery_start."ip_minute_stats_5".$myQuery_stop:""; </code> <br /> Then concatenated: <br /> <code> $myQuery = $query_0.$query_1.$query_2.$query_3.$query_4.$query_5; </code> <br /> And finally $myQuery is executed to produce all the data as required.</p> <p>At least this is roughly 8 times faster than the previous way I used UNION ALL, so I think this is valid. Any suggested further optimization?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload