javascript - 将大型 JSON 文件保存到 MySQL 时性能更好
问题描述
我有一个问题。
所以,我的故事是:
我有一个特定时间范围内所有 reddit 帖子的 30 GB 大文件 (JSON)。我不会将每个帖子的所有值都插入到表中。
我已经关注了这个系列,他用 Python 编写了我想要做的事情。我试图跟随(在 NodeJS 中),但是当我测试它时,它太慢了。它每 5 秒插入一行。并且有 500000 多个 reddit 帖子,这实际上需要数年时间。
所以这是我正在做的一个例子。
var readStream = fs.createReadStream(location)
oboe(readStream)
.done(async function(post) {
let { parent_id, body, created_utc, score, subreddit } = data;
let comment_id = data.name;
// Checks if there is a comment with the comment id of this post's parent id in the table
getParent(parent_id, function(parent_data) {
// Checks if there is a comment with the same parent id, and then checks which one has higher score
getExistingCommentScore(parent_id, function(existingScore) {
// other code above but it isn't relevant for my question
// this function adds the query I made to a table
addToTransaction()
})
})
})
基本上,它的作用是启动一个读取流,然后将其传递给一个名为oboe的模块。
然后我得到 JSON 作为回报。然后,它检查数据库中是否已经保存了一个父级,然后检查是否存在具有相同父级 ID 的现有评论。
我需要使用这两个函数才能获得我需要的数据(只获得“最佳”评论)
这有点addToTransaction
像:
function addToTransaction(query) {
// adds the query to a table, then checks if the length of that table is 1000 or more
if (length >= 1000) {
connection.beginTransaction(function(err) {
if (err) throw new Error(err);
for (var n=0; n<transactions.length;n++) {
let thisQuery = transactions[n];
connection.query(thisQuery, function(err) {
if (err) throw new Error(err);
})
}
connection.commit();
})
}
}
什么addToTransaction
是获取我所做的查询并将它们推送到表中,然后检查该表的长度,然后创建一个新事务,在 for 循环中执行所有这些查询,然后提交(保存)。
问题是,它太慢了,以至于我制作的回调函数甚至都没有被调用。
我的问题(最后)是,有什么办法可以提高性能吗?
(如果你想知道我为什么这样做,那是因为我正在尝试创建一个聊天机器人)
我知道我已经发布了很多内容,但我尽量向您提供尽可能多的信息,以便您有更好的机会帮助我。我很感激任何答案,我会回答你的问题。
解决方案
推荐阅读
- python-3.x - Console.Cloud 和 Console.Developer 有什么不同
- java - 使用自定义杰克逊序列化程序替换枚举值
- c# - 提供程序“Microsoft.Jet.OLEDB.4.0”未在本地计算机上注册,帮帮我
- php - 将控制器添加到 Prestashop 模块
- javascript - ES6 箭头函数 vs ES5:使用 ES5 非箭头函数时如何知道将 `this` 绑定到哪个函数
- c++ - 将 std::array 传递给按地址接受 C 样式数组的函数
- javascript - 是什么让一个函数在另一个函数之前执行?
- json - 从 JSON 数组中提取第一个对象
- python-3.x - 用 bs4 刮取一个变量的两个值,我怎样才能只使用一个?
- c# - Xamarin Forms Picker SelectedIndex 0 未显示