我有一個用 PHP 開發(fā)的 Web API,可以與 MySQL 進行通信。我想將數(shù)據(jù)庫更改為 Firestore,但在 MySQL 中,當我批量插入 4500 條記錄時,幾乎是立即完成的,但使用 Firestore 時,大約需要 15 分鐘。關(guān)于為什么會這樣的任何想法嗎?我試過這兩個代碼: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();}
1 回答

青春有我
TA貢獻1784條經(jīng)驗 獲得超8個贊
Firestore 未針對繁重的寫入負載進行優(yōu)化。事實上,它對寫入有速率限制。以這種方式將 Firestore 與 MySQL 進行比較并不公平。Firestore 針對大規(guī)模的高讀取負載(每個集合有數(shù)十億個文檔)進行了優(yōu)化,無需進行擴展工作,并且當集合變得龐大時也不會影響性能。
- 1 回答
- 0 關(guān)注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消