feat: 添加导入计时 数据库索引添加

master
yin5th 2023-10-13 11:49:23 +08:00
parent 41e2eaf769
commit 8377e97ead
1 changed files with 30 additions and 2 deletions

View File

@ -347,6 +347,10 @@ class Goods extends AdminBase
set_time_limit(0); set_time_limit(0);
ini_set('memory_limit', '1024M'); ini_set('memory_limit', '1024M');
$begin = microtime(true);
$log = [
'0010-begin' => $begin
];
$shopId = input('shop_id'); $shopId = input('shop_id');
if (empty($shopId)) { if (empty($shopId)) {
return JsonServer::error('请先选择商家'); return JsonServer::error('请先选择商家');
@ -379,6 +383,8 @@ class Goods extends AdminBase
$batchCode = [];// 同批次商品编码 $batchCode = [];// 同批次商品编码
$time1 = microtime(true);
$log['0020-before_foreach'] = sprintf('循环前 时间:%s 总耗时:%s秒', $time1, $time1 - $begin);
// 读取数据并批量导入 // 读取数据并批量导入
for ($row = 2; $row <= $highestRow; $row++) { for ($row = 2; $row <= $highestRow; $row++) {
$data = []; $data = [];
@ -411,6 +417,8 @@ class Goods extends AdminBase
if ($totalCount % $batchSize == 0) { if ($totalCount % $batchSize == 0) {
// 已存在的商品编码数组 // 已存在的商品编码数组
GoodsModel::batchInsert($shopId, $dataAll, $batchCode); GoodsModel::batchInsert($shopId, $dataAll, $batchCode);
$time2 = microtime(true);
$log['0030-after_batch_insert'] = sprintf('批次插入 第%s条记录时 时间:%s 总耗时:%s秒', $row,$time2, $time2 - $begin);
$dataAll = [];//清空该批次 $dataAll = [];//清空该批次
$batchCode = [];// 清空该批次商品编码 $batchCode = [];// 清空该批次商品编码
@ -424,32 +432,52 @@ class Goods extends AdminBase
if (!empty($dataAll)) { if (!empty($dataAll)) {
// Log::write('最后一批次导入'); // Log::write('最后一批次导入');
GoodsModel::batchInsert($shopId, $dataAll, $batchCode); GoodsModel::batchInsert($shopId, $dataAll, $batchCode);
$time3 = microtime(true);
$log['0040-last_batch_insert'] = sprintf('最后一批次插入 时间:%s 总耗时:%s秒', $time3, $time3 - $begin);
} }
// 以上 基础商品记录已插入 // 以上 基础商品记录已插入
// SKU相关完善 包含SKU、规格、规格值 // SKU相关完善 包含SKU、规格、规格值
// 启动事务 // 启动事务
Db::startTrans(); Db::startTrans();
try { try {
$time40 = microtime(true);
$log['0050-after_sku'] = sprintf('规格前 时间:%s 耗时:%s秒', $time40, $time40 - $begin);
GoodsModel::batchHandleSku();//批量插入规格名 GoodsModel::batchHandleSku();//批量插入规格名
$time41 = microtime(true);
$log['0051-after_sku'] = sprintf('规格后 时间:%s 耗时:%s秒', $time41, $time41 - $time40);
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Exception $e) {
return JsonServer::error($e->getMessage());
// 回滚事务 // 回滚事务
Db::rollback(); Db::rollback();
} }
$time5 = microtime(true);
$log['0060-before_relation'] = sprintf('规格后关联前 时间:%s 总耗时:%s秒', $time5, $time5 - $begin);
GoodsModel::handleImages();//处理商品组图 插入到组图表 GoodsModel::handleImages();//处理商品组图 插入到组图表
$time51 = microtime(true);
$log['0061-after_image'] = sprintf('处理组图 时间:%s 耗时:%s秒', $time51, $time51 - $time5);
GoodsModel::handleBrand();//处理商品品牌 插入到品牌表 未更新商品表中的brand_id GoodsModel::handleBrand();//处理商品品牌 插入到品牌表 未更新商品表中的brand_id
$time52 = microtime(true);
$log['0062-after_image'] = sprintf('处理品牌 时间:%s 耗时:%s秒', $time52, $time52 - $time51);
GoodsModel::handleUnit();//处理商品单位 插入到单位表 未更新商品表中的unit_id GoodsModel::handleUnit();//处理商品单位 插入到单位表 未更新商品表中的unit_id
$time53 = microtime(true);
$log['0063-after_image'] = sprintf('处理单位 时间:%s 耗时:%s秒', $time53, $time53 - $time52);
GoodsModel::handleCategory();//处理商品分类 插入到分类表 未更新商品表中的分类ID GoodsModel::handleCategory();//处理商品分类 插入到分类表 未更新商品表中的分类ID
$time6 = microtime(true);
$log['0070-after_relation'] = sprintf('处理分类 时间:%s 总耗时:%s秒', $time6, $time6 - $time53);
GoodsModel::handleSelfInfo();//处理商品自身数据的更新 更新分类ID、品牌ID、单位ID GoodsModel::handleSelfInfo();//处理商品自身数据的更新 更新分类ID、品牌ID、单位ID
$done = microtime(true);
$log['0080-done'] = sprintf('处理自身关联 时间:%s 耗时:%s秒', $done, $done - $time6);
$log['0090-done'] = sprintf('完成 时间:%s 总耗时:%s秒', $done, $done - $begin);
return JsonServer::success('导入成功'); return JsonServer::success('导入成功', $log);
} }
$shopList = Shop::where('del', 0)->field('id,name')->select(); $shopList = Shop::where('del', 0)->field('id,name')->select();
return view('', [ return view('', [
'shop_list' => json_encode($shopList) 'shop_list' => json_encode($shopList)
]); ]);
} }
public function importTemplate() public function importTemplate()