有一張大桌子,我需要將其中的內容移動到具有不同結構的桌子上。這些表位於不同的數據庫中。爲此,我正在使用PHP腳本。但劇本不符合我的要求。它過度複製並永不停止。也許這是一個不好的和簡單的問題,但現在我的腦袋正在旋轉,從嘗試,但我不能把我的手指在這個問題上。這項工作需要立即完成。如果你有幫助,我會很高興。下面的代碼片段:使用將一張大桌子有效地複製到另一張桌子
function copy_table()
{
$this->load->database();
$num_rows = $this->db->get('orj_table')->num_rows();
$offset = 0;
$limit = 500;
while ($offset <= $num_rows)
{
$this->load->database();
//Query for original table
//......
$this->db->limit($limit, $offset);
$records = $this->db->get('orj_table')->result_array();
$this->db->close();
//Open a connection to new database.
$this->db_new = $this->load->database('new', TRUE);
foreach($records as $record)
{
$data1 = $record['data1'];
$data2 = $record['data2'];
$datas[] = array('data1 => $record['data1'],
'data2 => $record['data2']
);
}
//Insert 500 records at one time with "insert_batch"
$sorgu = $this->db_yeni->insert_batch('new_table', datas);
$this->db->close();
$offset += 500;
}
}
如果你想提高效率,首先不要使用PHP。嘗試使用'mysqldump'。 – Flimzy
我不知道爲什麼它會過度複製並永不停止,但是在調用'insert_batch'函數的行中的數據之前,似乎錯過了'$'。 – bobo
關於你的「overcopy」,它來自於你永遠不會取消將會一遍又一遍填充的$ datas []數組。因此,它會每次在while循環中重新插入整個數組。 – Tom