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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化