douhao6557 2014-11-19 09:54
浏览 77
已采纳

为什么我的mysql查询需要太长时间才能产生结果

Hi i wrote the following code to retrieve data from a database, but its too slow on its execution, can anybody have a look on it and give a few optimizing tips for better performance.

what really happens is im creating a new table by binding existing current data from few other tables.

$table_count = mysql_query("SELECT TABLE_NAME FROM information_schema.tables 
WHERE table_schema = 'datamatrix' 
AND table_name 
LIKE 'tracking_%' ");

while($row = mysql_fetch_array($table_count)){

$trackingtable = $row["TABLE_NAME"];

$update = mysql_query("SELECT id as unp FROM $trackingtable");
$col = mysql_fetch_assoc($update);
$col_id = ($col["unp"]);

$var1 = mysql_query("SELECT useragent as ua 
FROM trackpanel 
WHERE id = $col_id");
$col_ua = ($var1["ua"]);
$browser = get_browser($col_ua, true);


$new_timestamp = mysql_query("SELECT TIMESTAMP FROM $trackingtable 
ORDER BY TIMESTAMP 
DESC LIMIT 1");
        $col_new = mysql_fetch_assoc($new_timestamp);

$new_timestamp1 = mysql_query("SELECT TIMESTAMP FROM $trackingtable 
ORDER BY TIMESTAMP 
ASC LIMIT 1");
$col_old = mysql_fetch_assoc($new_timestamp1);

mysql_query("
INSERT INTO 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']."'
FROM $trackingtable a, datamat b, trackaccounts c
WHERE a.timestamp > DATE_ADD(NOW(), INTERVAL -2 YEAR) AND a.trackingID = b.trackingID AND b.accountID = c.accountID") 
or die(mysql_error());
} 

?>
  • 写回答

2条回答 默认 最新

  • dpw70180 2014-11-19 10:03
    关注

    Without knowing how your database is set up and indexed, and what table storage engines you are using, I can only guess as to why its slow.

    1. Your first query could return multiple rows consisting of table names
    2. You run 4 simple select statements.
    3. You run an insert with a select statement. The select query here also joins 3 tables together

    The second and third parts of the list are run for each row which gets looped from the first query. If your tables are InnoDB, the selects will be slower whereas the insert is faster. Opposite is true for MyISAM.

    Each query has its own latency because it queries the database separately. You would be better off combining the 1st, 3rd and 4th queries in the while loop as they query the same table.

    The insert and select query I guess will most probably be the slowest since it joins 3 tables. That select query can definitely be optimised.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码