Firestore 批量插入速度太慢

我有一个用 PHP 开发的 Web API,可以与 MySQL 进行通信。我想将数据库更改为 Firestore,但在 MySQL 中,当我批量插入 4500 条记录时,几乎是立即完成的,但使用 Firestore 时,大约需要 15 分钟。


关于为什么会这样的任何想法吗?


我试过这两个代码:


use Google\Cloud\Firestore\DocumentReference;

use Google\Cloud\Firestore\FirestoreClient;

use Spatie\Async\Pool;


/* @var FirestoreClient */

private $database;

/* @var DocumentReference */

private $collectionReference;

代码1:


$pool = Pool::create();

$graduatesFailed = [];

/** @var Graduate $graduate */

foreach ($graduates as $graduate) {

    $pool[] = async(function () use ($graduate) {

        return $this->collectionReference->add($graduate->jsonSerialize());

    })->catch(function ($e) use ($graduatesFailed, $graduate) {

        array_push($graduatesFailed, $graduate->getUser());

    });

}


await($pool);


代码2:


$writeBatch = $this->database->batch();

$count = 0;

$graduatesFraction = array_slice($graduates, 0, 500);

$graduatesFailed = [];

while ($count < count($graduates)) {

    /** @var Graduate $graduate */

    foreach ($graduatesFraction as $graduate) {

        try {

            $count++;

            $writeBatch->create(

                $this->collectionReference->document($graduate->getUser()),

                $graduate->jsonSerialize()

            );

        } catch (Exception $e) {

            array_push($graduatesFailed, $graduate->getUser());

        }

    }

    $writeBatch->commit();

}


红颜莎娜
浏览 98回答 1
1回答

青春有我

Firestore 未针对繁重的写入负载进行优化。事实上,它对写入有速率限制。以这种方式将 Firestore 与 MySQL 进行比较并不公平。Firestore 针对大规模的高读取负载(每个集合有数十亿个文档)进行了优化,无需进行扩展工作,并且当集合变得庞大时也不会影响性能。
打开App,查看更多内容
随时随地看视频慕课网APP