php - 保存到 MySQL 的速度慢(PHP - Yii2)

标签 php mysql yii2

我正在尝试将数据从 JSON 文件导入 MySQL。

public function importProductFile($file, $return = true)
    {    
        $products = json_decode($file);
        $dubTableName = Product::tableName() . "_dub";
        $start = time();
        if ($this->db->createDuplicateTable(Product::tableName(), $dubTableName)) {
    
            $i = 0;
    
            foreach ($products as $product) {
                $i++;
                $item = new Product_dub();
                $item->id_1c_product = $product->id;
                $category = Category_dub::findOne(['id_1c_category' => $product->category_id]);
    
                if (!$category) {
                    Answer::failure("В этом товаре отсутствует категория или такой категории не существует: " . $product->title);
                }
    
                $item->category_id = $category->id;
                $item->title = $product->title;
                $brand = Brands_dub::findOne(['id_1c_brand' => $product->brand_id]);
    
                if (!$brand) {
                    Answer::failure("В этом товаре отсутствует бренд/изготовитель: " . $product->title);
                }
    
                $item->brand_id = $brand->id;
                // $item->shortdesc = $product->shortdesc;
                $item->content1 = $product->content1;
                $item->content2 = $product->content2;
                $item->content3 = $product->content3;
                $item->link_order = $product->link_order;
                $item->img = $product->img;
                $item->in_stock = $product->in_stock ? 1 : 0;
                $item->is_popular = $product->is_popular ? 1 : 0;
    
                if (!$item->save()) {
                    Answer::failure("Не удалось импортировать: Проверьте данные в " . $product->title);
                }
    
                if ($i == 200) {
                    break;
                }
            }
        }
    
        $finish = time();
        $res = $finish - $start . "sec. ";
    
        if ($return) {
            echo $res;
            Answer::success();
        }
    }

我的 JSON 文件中有大约 1100 个对象。向数据库添加 100 行需要 7 秒。添加 200 行 - 15 秒。 300 = 33 秒,400 = 58 秒。为什么它会随着时间的推移而变慢以及如何加速这个过程?

我在本地 OpenServer 服务器上执行所有操作。 PHP 7.2 版本,Xeon 2620v3 处理器,16 GB DDR4,硬盘。

UPD 1。

“你能尝试不导入而只确定读取速度吗” - 我评论 $item->save() 并为所有 JSON 文件获取 1-2 秒。 “在周期的每次迭代中,您都会运行 2 个数据库查询来加载类别和品牌。” - 我尝试删除这些行进行测试 - 但结果比 2 个数据库查询快 1-2 秒。

UPD 2。

我将 save() 更改为 insert() - 速度有所提高。现在,所有 JSON(1107 行)都在 40 秒内导入。

是否有更快的方法将现成的数据从 JSON 加载到数据库中? 如果有10万行或者100万行怎么办?等待几个小时是正常做法吗?

public function importProductFile($file, $return = true)
    {    
        $products = json_decode($file);
        $dubTableName = Product::tableName() . "_dub";
        $start = time();

        if ($this->db->createDuplicateTable(Product::tableName(), $dubTableName)) {
            $start = time();
            $categoryMap = Category_dub::find()->select(['id', 'id_1c_category'])->indexBy('id_1c_category')->column();
            $brandMap = Brands_dub::find()->select(['id', 'id_1c_brand'])->indexBy('id_1c_brand')->column();

            foreach ($products as $product) {
                Yii::$app->db->createCommand()->insert('product_dub', [
                    'id_1c_product' => $product->id,
                    'category_id' => $categoryMap[$product->category_id] ?? '0',
                    'title' => $product->title,
                    'brand_id' => $brandMap[$product->brand_id] ?? 'No brand',
                    'content1' => $product->content1,
                    'content2' => $product->content2,
                    'content3' => $product->content3,
                    'link_order' => $product->link_order,
                    'img' => $product->img ?? 'no-image.png',
                    'in_stock' => $product->in_stock ? 1 : 0,
                    'is_popular' => $product->is_popular ? 1 : 0,
                ])->execute();
            }
        }
        }
    
        $finish = time();
        $res = $finish - $start . "sec. ";
    
        if ($return) {
            echo $res;
            Answer::success();
        }
    }

最佳答案

我将 save() 更改为 insert() - 速度提高了。现在,所有 JSON(1107 行)都在 40 秒内导入。 是否有更快的方法将现成的数据从 JSON 加载到数据库中? 如果有10万行或者100万行怎么办?等待几个小时是正常做法吗?

public function importProductFile($file, $return = true)
    {    
        $products = json_decode($file);
        $dubTableName = Product::tableName() . "_dub";
        $start = time();

        if ($this->db->createDuplicateTable(Product::tableName(), $dubTableName)) {
            $start = time();
            $categoryMap = Category_dub::find()->select(['id', 'id_1c_category'])->indexBy('id_1c_category')->column();
            $brandMap = Brands_dub::find()->select(['id', 'id_1c_brand'])->indexBy('id_1c_brand')->column();

            foreach ($products as $product) {
                Yii::$app->db->createCommand()->insert('product_dub', [
                    'id_1c_product' => $product->id,
                    'category_id' => $categoryMap[$product->category_id] ?? '0',
                    'title' => $product->title,
                    'brand_id' => $brandMap[$product->brand_id] ?? 'No brand',
                    'content1' => $product->content1,
                    'content2' => $product->content2,
                    'content3' => $product->content3,
                    'link_order' => $product->link_order,
                    'img' => $product->img ?? 'no-image.png',
                    'in_stock' => $product->in_stock ? 1 : 0,
                    'is_popular' => $product->is_popular ? 1 : 0,
                ])->execute();
            }
        }
        }
    
        $finish = time();
        $res = $finish - $start . "sec. ";
    
        if ($return) {
            echo $res;
            Answer::success();
        }
    }

关于php - 保存到 MySQL 的速度慢(PHP - Yii2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70215963/

相关文章:

css - Yii2 分页改变样式

php - Mysqli:打印记录列表

php - 触发由父 View 引起的任何事件时退出 Tour - Intro.js

php - 我可以在另一个 MySQL 查询的 From 部分中使用前一个 MySQL 查询的结果吗?

mysql - 添加基于字段值的唯一约束

Python,求和返回的MySQL数据

php - 将复选框更新到 mysql 数据库

forms - yii2 从下拉列表中删除 optgroup

javascript - 随机图像服务

php - 无法将空字符串分配给字符串偏移量