目前在我的电子商务网站中有 2 个表格:我使用 2 个查询来生成
$query = $this->db->query("SELECT count(*) AS hits, product_id
FROM visitors
GROUP BY product_id");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row){
$hits = $row->hits;
$product_id = $row->product_id;
$data = array(
'product_id' => $product_id ,
'hits' => $hits
);
$this->db->insert('visitor_days', $data);
}
}
问题:
如果10k记录,更新很慢。如何改进这个查询?我可以在单个查询中完成吗?
访客
- ID product_id date
- 534024 69082 2012-09-25
- 534025 69082 2012-09-25
- 534026 69082 2012-09-25
- 534027 69082 2012-09-25
- 534028 53042 2012-09-25
- 534029 53042 2012-09-25
访客天数
- ID product_id hits
- 534024 69082 4
- 534025 53042 2