Note that there are some explanatory texts on larger screens.

plurals
  1. POMore efficient way to do SQL queries
    primarykey
    data
    text
    <p>I've been using the below php and sql for loading schedule information and real time information for passenger trains in the UK. Essentially you have to find the relevant schedules, and then load the realtime information for each schedule which is in a different table relating to todays trains.</p> <p>The query is taking a little longer than is really idea and using lots of CPU% which again isn''t ideal. I'm pretty weak when it comes to sql programming so any pointers as to what is inefficient would be great.</p> <p>This is for an android app and so i've tried to all with one call over http. The prints(*) and > is for splitting the string at the other end. </p> <p>Here is the code:</p> <pre><code>&lt;? //Connect to the database mysql_connect("localhost","XXXX","XXXX") or die ("No connection could be made to the OpenRail Database"); mysql_select_db("autotrain"); //Set todays date from system and get HTTP parameters for the station,time to find trains and todays locations table. $date = date('Y-m-d'); $test = $_GET['station']; $time = $_GET['time']; $table = $_GET['table']; //Find the tiploc associated with the station being searched. $tiplocQuery = "SELECT tiploc_code FROM allstations WHERE c LIKE '$test';"; $tiplocResult =mysql_query($tiplocQuery); $tiplocRow = mysql_fetch_assoc($tiplocResult); $tiploc=$tiplocRow['tiploc_code']; //Now find the timetabled trains for the station where there exists no departure information. Goes back two hours to account for any late running. $timeTableQuery = "SELECT tiplocs.tps_description AS 'C', locations$table.public_departure, locations$table.id,schedules.stp_indicator ,schedules.train_uid FROM locations$table, tiplocs, schedules_cache, schedules,activations WHERE locations$table.id = schedules_cache.id AND schedules_cache.id = schedules.id AND schedules.id =activations.id AND '$date' BETWEEN schedules.date_from AND schedules.date_to AND locations$table.tiploc_code = '$tiploc' AND locations$table.real_departure LIKE '0' AND locations$table.public_departure NOT LIKE '0' AND locations$table.public_departure &gt;='$time'-300 AND locations$table.public_departure &lt;='$time'+300 AND schedules.runs_th LIKE '1' AND schedules_cache.destination = tiplocs.tiploc ORDER BY locations$table.public_departure ASC LIMIT 0,30;"; $timeTableResult=mysql_query($timeTableQuery); while($timeTablerow = mysql_fetch_assoc($timeTableResult)){ $output[] = $timeTablerow; } //Now for each id returned in the timetable, get the locations and departure times so the app may calculate expected arrival times. foreach ($output as $value) { $id = $value['id']; $realTimeQuery ="SELECT locations$table.id,locations$table.location_order,locations$table.arrival,locations$table.public_arrival, locations$table.real_arrival,locations$table.pass,locations$table.departure,locations$ table.public_departure,locations$table.real_departure,locations$table.location_cancelled, tiplocs.tps_description FROM locations$table,tiplocs WHERE id =$id AND locations$table.tiploc_code=tiplocs.tiploc;"; $realTimeResult =mysql_query($realTimeQuery); while($row3 = mysql_fetch_assoc($realTimeResult)){ $output3[] = $row3; } print json_encode($output3); print("*"); unset($output3); unset($id); } print('&gt;'); print json_encode($output); ?&gt; </code></pre> <p>Many Thanks Matt</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.
    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