我的表A中有100000条记录,我需要A通过Corn插入B表的一些数据和一些其他数据。所以在实现下面的代码和设置分页规则时,每个新页面都需要时间。
每10000条记录(这是一个页面限制(页面就会发生变化,这会增加处理时间。如何减少处理时间。
$DB = new myclassi();
$resource_new = "SELECT COUNT(`gi`.`igems_item_id`) as total
FROM `gems_item` AS `gi`
WHERE NOT EXISTS (SELECT `gicd`.`id` FROM `gems_item_cron_data` As `gicd` WHERE `gi`.`old_sku` = `gicd`.`gems_old_sku` AND `gi`.`igems_item_id` = `gicd`.`gems_itemid`)
AND `gi`.`diamond_video_url` IS NOT NULL
AND (`gi`.`diamond_video_url` LIKE '%http%' OR `gi`.`diamond_video_url` LIKE '%https%')";
$res = $DB->select_assoc($resource_new);
$total_rows = $res[0]['total'];
$no_of_records_per_page = 10000;
$total_pages = ceil ($total_rows / $no_of_records_per_page);
$limit = "";
$pageno = 1;
for($i=0;$i < $total_pages; $i++){
$offset = ($pageno) * $no_of_records_per_page;
if ($pageno > 1) {
$prev_offset = ($offset - $no_of_records_per_page) + 1;
$limit = $prev_offset.", ".$no_of_records_per_page;
}else{
$limit = "0, ".$no_of_records_per_page;
}
$sqlr="SELECT `gi`.`igems_item_id`, `gi`.`old_sku`, `gi`.`diamond_video_url`
FROM `gems_item` AS `gi`
WHERE NOT EXISTS (SELECT `gicd`.`id` FROM `gems_item_cron_data` As `gicd` WHERE `gi`.`old_sku` = `gicd`.`gems_old_sku` AND `gi`.`igems_item_id` = `gicd`.`gems_itemid`)
AND `gi`.`diamond_video_url` IS NOT NULL
AND (`gi`.`diamond_video_url` LIKE '%http%' OR `gi`.`diamond_video_url` LIKE '%https%') LIMIT ". $limit;
$results = $DB->select_assoc($sqlr);
if (isset($results) && !empty($results)) {
foreach ($results as $row) {
$url = urldecode($row['diamond_video_url']);
$k = $row['igems_item_id'];
$video_url = 'xxxfdafas';
$old_sku = $row['old_sku'];
$diamond_video_url = $row['diamond_video_url'];
$diamond_final_video_url = urlencode($video_url);
$is_url_found = '';
$is_processed = 0;
$created_at = date('Y-m-d H:i:s');
$sql = "INSERT INTO gems_item_cron_data (`gems_itemid`,`gems_old_sku`, `diamond_final_video_url`,`is_processed`,`created_at`)
VALUES ($k, '$old_sku','$diamond_final_video_url', '$is_processed', '$created_at')";
$DB->insert($sql);
}
}
$pageno++;
}
$DB->close();
请让我知道如何使插入过程更快。
简化测试
这个
AND `gi`.`diamond_video_url` IS NOT NULL
AND (`gi`.`diamond_video_url` LIKE '%http%'
OR `gi`.`diamond_video_url` LIKE '%https%'
)";
可以简化并显著加快这种
AND `gi`.`diamond_video_url` LIKE 'http%'
特别是如果URL必须以"开始;http";。
消除计数
我建议没有必要先数一下行数。只需执行SELECT
来获取行,然后注意是否有行。仅此一项,就可能(可能(使程序的速度增加一倍以上。
批量插入
如果可行,用替换循环和单行插入
INSERT INTO ... (...)
SELECT ...
这可能会使速度提高10倍。
如果SELECT
不返回任何行,则INSERT
将不会返回任何行。真便宜。
完成这些操作后,可能会有进一步的优化,但之后我需要查看新查询以及SHOW CREATE TABLE
。