加速 NeDB 中的数组插入
Speeding up array inserts in NeDB
我正在制作一个应用程序,它可以从 reddit API 中获取用户的评论并将它们加载到本地数据库中。我正在为数据库使用 NeDB。 reddit API returns 一个包含 25 个元素的数组(评论),然后您必须使用最后一个元素的日期进行分页以获取下一个元素数组。我映射每个数组并单独插入每个元素。我不想插入整个数组,因为这样元素就会被编入索引,而我将得到一个充满不同索引的数据库。我通常会抓取几百个阵列。我也在使用 nedb-promises。
整个数组插入的平均速度从 3 秒变为 10 秒
问题是,数组插入时间太长,最后这个过程变慢了。我的代码有问题吗?使用 SQLite 会更快吗?谢谢
const asyncForEach = async function (array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const fetch = async (paginationHelper) => {
//response tree: response.data.data[n].body
const {data:{data}} = await pushshift.get(`?before=${paginationHelper !==null ? paginationHelper : ''}&author=${author}`)
return data.map(comment => {
return ({
body: comment.body,
created: comment.created_utc,
subreddit: comment.subreddit,
author: comment.author,
postId: comment.link_id.split('t3_')[1],
parentId: comment.parent_id.split('t1_')[1],
linkId: comment.id
})
})
}
const load = async (comments) => {
let i = 0
await asyncForEach(comments, async comment => {
await db.insert(comment).then(res => {
console.log(res);
i++
console.log(i);
})
})
}
const loadAll = async () => {
let comments = await fetch(null)
//Pagination is made using the comments' date when they were created
let paginationHelper = comments[comments.length-1].created
while(paginationHelper){
[comments] = await Promise.all( [fetch(paginationHelper), load(comments)] )
paginationHelper = comments.length !== 0 ? comments[comments.length-1].created : null
console.log(paginationHelper);
}
}
我是个白痴,你可以毫无问题地插入整个数组,而且速度非常快。
我正在制作一个应用程序,它可以从 reddit API 中获取用户的评论并将它们加载到本地数据库中。我正在为数据库使用 NeDB。 reddit API returns 一个包含 25 个元素的数组(评论),然后您必须使用最后一个元素的日期进行分页以获取下一个元素数组。我映射每个数组并单独插入每个元素。我不想插入整个数组,因为这样元素就会被编入索引,而我将得到一个充满不同索引的数据库。我通常会抓取几百个阵列。我也在使用 nedb-promises。
整个数组插入的平均速度从 3 秒变为 10 秒
问题是,数组插入时间太长,最后这个过程变慢了。我的代码有问题吗?使用 SQLite 会更快吗?谢谢
const asyncForEach = async function (array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const fetch = async (paginationHelper) => {
//response tree: response.data.data[n].body
const {data:{data}} = await pushshift.get(`?before=${paginationHelper !==null ? paginationHelper : ''}&author=${author}`)
return data.map(comment => {
return ({
body: comment.body,
created: comment.created_utc,
subreddit: comment.subreddit,
author: comment.author,
postId: comment.link_id.split('t3_')[1],
parentId: comment.parent_id.split('t1_')[1],
linkId: comment.id
})
})
}
const load = async (comments) => {
let i = 0
await asyncForEach(comments, async comment => {
await db.insert(comment).then(res => {
console.log(res);
i++
console.log(i);
})
})
}
const loadAll = async () => {
let comments = await fetch(null)
//Pagination is made using the comments' date when they were created
let paginationHelper = comments[comments.length-1].created
while(paginationHelper){
[comments] = await Promise.all( [fetch(paginationHelper), load(comments)] )
paginationHelper = comments.length !== 0 ? comments[comments.length-1].created : null
console.log(paginationHelper);
}
}
我是个白痴,你可以毫无问题地插入整个数组,而且速度非常快。