php - Why does my mysql query takes way too long to produce results -


hi wrote following code retrieve data database, slow on execution, can have on , give few optimizing tips better performance.

what happens im creating new table binding existing current data few other tables.

$table_count = mysql_query("select table_name information_schema.tables  table_schema = 'datamatrix'  , table_name  'tracking_%' ");  while($row = mysql_fetch_array($table_count)){  $trackingtable = $row["table_name"];  $update = mysql_query("select id unp $trackingtable"); $col = mysql_fetch_assoc($update); $col_id = ($col["unp"]);  $var1 = mysql_query("select useragent ua  trackpanel  id = $col_id"); $col_ua = ($var1["ua"]); $browser = get_browser($col_ua, true);   $new_timestamp = mysql_query("select timestamp $trackingtable  order timestamp  desc limit 1");         $col_new = mysql_fetch_assoc($new_timestamp);  $new_timestamp1 = mysql_query("select timestamp $trackingtable  order timestamp  asc limit 1"); $col_old = mysql_fetch_assoc($new_timestamp1);  mysql_query(" insert report (`trackingid`,`trackname`,`accountname`,`accountid`, `status`, `clickcount`, `earliest_click`,`recent_click`,`platform`,`device`,`browser`,`browser_version`) select b.trackingid, `trackname`, accountname, c.accountid, status, total_clicks,'".$col_new['timestamp']."','".$col_old['timestamp']."','".$browser['platform']."','".$browser['device_name']."','".$browser['browser']."','".$browser['version']."' $trackingtable a, datamat b, trackaccounts c a.timestamp > date_add(now(), interval -2 year) , a.trackingid = b.trackingid , b.accountid = c.accountid")  or die(mysql_error()); }   ?> 

without knowing how database set , indexed, , table storage engines using, can guess why slow.

  1. your first query return multiple rows consisting of table names
  2. you run 4 simple select statements.
  3. you run insert select statement. select query here joins 3 tables together

the second , third parts of list run each row gets looped first query. if tables innodb, selects slower whereas insert faster. opposite true myisam.

each query has own latency because queries database separately. better off combining 1st, 3rd , 4th queries in while loop query same table.

the insert , select query guess slowest since joins 3 tables. select query can optimised.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -