我目前有一个用于从 DB2 服务器填充 MySQL 数据库的脚本。它可以工作,但似乎以极慢的速度将行插入 MySQL。脚本运行时,服务器进程以约 1% 的 CPU 执行,我想知道如何加快插入速度。
出于安全原因,DB2 数据库的管理员只为我们提供了数据库中所需表的只读视图。
这是我的脚本:
<?php
$selectQuery = "SELECT
PK AS COL1,
COL2,
COL3,
COL4,
CASE WHEN DATE > '" . date('Y-m-d') . "'
THEN 1
ELSE 0
END AS COL5
FROM table1";
$insertQuery = "INSERT INTO `table1` (
`fk`,
`col2`,
`col3`,
`col4`,
`col5`,
`last_updated`
)
SELECT :col1, f.`fid`, :col3, :col4, :col5, NOW()
FROM f
WHERE f.`code` = :col2
LIMIT 1
ON DUPLICATE KEY UPDATE
`col2` = VALUES(col2),
`col3` = VALUES(col3),
`col4` = VALUES(col4),
`col5` = VALUES(col5),
`last_updated` = NOW();";
$paramTypes = array(
'col1' => PDO::PARAM_STR,
'col2' => PDO::PARAM_STR,
'col3' => PDO::PARAM_STR,
'col4' => PDO::PARAM_STR,
'col5' => PDO::PARAM_BOOL
);
$sync->populate($selectQuery, $insertQuery, $paramTypes);
在同步类($sync
作为实例的类)中:
<?php
class SyncObject {
private $db2;
private $db2_user = '...';
private $db2_pass = '...';
private $db2_dbname = '...';
private $db2_host = 'secure.example.net';
private $db2_port = ...;
private $mysql;
public function __construct() {
// Establish a DB2 connection
$this->db2 = db2_pconnect("DATABASE={$this->db2_dbname};HOSTNAME={$this->db2_host};PORT={$this->db2_port};PROTOCOL=TCPIP;UID={$this->db2_user};PWD={$this->db2_pass};", '', '');
// Establish a MySQL connection
$this->mysql = new PDO('mysql:host=secure-mysql.example.net;port=...;dbname=...', '...', '...', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
public function populate($selectQuery, $insertQuery, $paramTypes = array()) {
$insStmt = $this->mysql->prepare($insertQuery);
foreach ($paramTypes as $parameterName => $parameterType) {
$$parameterName = '';
$insStmt->bindParam(":$parameterName", $$parameterName, $parameterType);
}
// Retrieve the data
$stmt = db2_exec($this->db2, $selectQuery);
while ($row = db2_fetch_assoc($stmt)) {
foreach ($row as $fieldName => &$fieldValue) {
$fieldName = strtolower($fieldName);
$$fieldName = trim($fieldValue);
$insStmt->execute();
}
}
}
}
顺便说一句,这个populate
方法被调用了六次,每个表一次。我在这里只展示了一张桌子。表的大小范围从 20 行到 2100 万行。
我在想我可以在查询中绑定大写参数以避免strtolower
函数全部在foreach
.