我有一个用 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();
}
青春有我