是否可以合并以下查询或其中一些查询?我的目标是通过更少的插入来实现更快的性能。但是,我不知道如何处理ON DUPLICATE KEY UPDATE new_table.pages=VALUES(pages)
,我将插入2个以上的变量。
mysql_query("INSERT INTO new_table (hash, pages)
SELECT hash, COUNT( id ) AS pages
FROM behaviour GROUP BY hash
ON DUPLICATE KEY UPDATE new_table.pages=VALUES(pages)");
mysql_query("INSERT INTO new_table (hash, visits)
SELECT hash, visits from audience
ON DUPLICATE KEY UPDATE new_table.visits=audience.visits");
mysql_query("INSERT INTO new_table (hash, first_visit)
SELECT hash, timestamp from audience
ON DUPLICATE KEY UPDATE new_table.first_visit=audience.timestamp");
mysql_query("INSERT INTO new_table (hash, last_visit)
SELECT hash, max(timestamp) from behaviour
group by hash
ON DUPLICATE KEY UPDATE new_table.last_visit=VALUES(last_visit)");
mysql_query("INSERT INTO new_table (hash, goals)
SELECT alerts_data_hash, COUNT( * ) AS goals
FROM alerts_data
WHERE alerts_data_status = 'goal'
GROUP BY alerts_data_hash
ON DUPLICATE KEY UPDATE new_table.goals=VALUES(goals)");
答案 0 :(得分:0)
我相信这将结合前4个查询
INSERT INTO new_table (hash, pages, visits, first_visit, last_visit, goals)
SELECT A.hash, COUNT(B.id), A.visits, A.timestamp, MAX(B.timestamp), 0
FROM audience A
LEFT OUTER JOIN behaviour B ON B.hash = A.hash
GROUP BY A.hash
我不确定最后一个是join
还是必须是联合/单独查询。