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

问题描述

我有一个可以有数百万行的对象数组。我试图将它存储在 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);
  };
};

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...