react.js indexDB 大数据 store.put 性能问题

react.js indexDB large data store.put performance issues

我有一个可以有数百万行的对象数组。我试图将它存储在 indexDB 中,我可以成功地做到这一点,但它在“put”方法上有一些性能问题。原因是因为我必须遍历每个索引并将其单独放入 indexDB 中。有没有办法简单地将整个数组转储在这里?这是我的代码...

    var indexDB;
    indexDB = window.indexedDB; 
    var open = indexedDB.open("HistoricalDB");
    open.onupgradeneeded = function(){
    let db = open.result;
    let store = db.createObjectStore("HistoricalTable", {keyPath: "id"});
    let index = store.createIndex("CIndex", ["time", "value"]);
    };
    open.onsuccess = function(){
        let db = open.result;
        let tx = db.transaction("HistoricalTable", "readwrite");
        let store = tx.objectStore("HistoricalTable");
        let index = store.index("CIndex");
        for (let i=0; i<allHistoricalData.length-1; i++){
            store.put(allHistoricalData[i]);
        }
        let getAll = store.getAll();
        getAll.onsuccess = function(){
            console.log(getAll.result)
        }
        tx.oncomplete = function(){
            db.close();
        };
    }

这就是让我崩溃的原因....

        for (let i=0; i<allHistoricalData.length-1; i++){
            store.put(allHistoricalData[i]);
        }

有没有办法在循环中执行 store.put(allHistoricalData) 而不是 store.put(allHistoricalData[i]?

IndexDB 可以存储任何类型的 javascript 可克隆数据,因此您应该能够简单地将数组存储为一个整体。

我看到您正在创建索引并使用它来存储值。 IndexDB 索引用于在存储中查找数据,但不应用于存储数据本身。

您想要做的是像这样将数据直接放入存储中:

var open = window.indexedDB.open('HistoricalDB');

open.onupgradeneeded = function () {
  let db = open.result;
  db.createObjectStore('HistoricalTable');
};

open.onsuccess = function () {
  let db = open.result;
  let tx = db.transaction('HistoricalTable', 'readwrite');
  let store = tx.objectStore('HistoricalTable');
  let request = store.put(allHistoricalData, 'DATA');

  request.onsuccess = function () {
    console.log('success!');
  };
  request.onerror = function () {
    console.log(request.error);
  };
};