我的答案是,沒有一個真正有效的方法來做到這一點。 我曾經想過合併,但爲了高效,首先將它插入臨時表中,您仍然會更好。那麼你可能只需截斷table_service,然後再從$ service數組中填充它。 即使Kristoffer的anwser可以工作,它仍然可能比截斷插入更慢。
這是我的php快速插入大量記錄的方法: 這樣做的好處是,插入語句只會被解析一次,而不是每次插入,這將大大提高速度。有時會減少100倍左右。
$connection = oci_connect(<YOUR CONNECTION>);
$sql = insert into table_service (id, var) values (:id, :var); // desc is a reserved word, cannot be a column name
$parsed = oci_parse($connection, $sql);
$binds = array(':id', ':var');
$sizes = array(6, 20);
$data = $services;
$errors = execute_multiple($binds, $sizes, $data);
if ($errors > 0)
// log or show
else
// feedback: full succes!
function execute_multiple($binds, $sizes, $data, $commit = true)
{
$errorCount = 0;
// first determine all binds once
foreach ($binds as $i => $bind)
{
// ${trim($bind, ':')} example: :some_id -> $some_id
oci_bind_by_name($parsed, $bind, ${trim($bind, ':')}, $sizes[$i]);
}
// Then loop over all rows and give the variables the new value for that row
// This is because the variables remain binded!
for ($row=0; $row<count($data); $row++)
{
foreach ($binds as $i => $bind)
{
$value = array_key_exists($i, $data[$row]) ? substr($data[$row][$i], 0, $sizes[$i]) : null;
${trim($bind, ':')} = trim($value);
}
if (! @oci_execute($this->parsed, OCI_DEFAULT)) // don't auto commit
$errorCount++;
}
if ($commit)
oci_commit($connection);
return $errorCount;
}
不幸的是,Oracle不支持ON DUPLICATE KEY。這是一個MySql語法 – winkbrace 2012-02-17 11:01:27