Node.js 在 findOne Sequelize 方法期间堆内存不足,急切加载

Nodejs heap out of memory during findOne Sequelize method with eager loading

我的 NodeJS 应用程序当前使用 Sequelize 对象来跨多个阶段工作。此对象是 Model.findOne 操作的结果,其中包括其他几个模型,类似于在普通 SQL.

中对多个表进行左联接
transaction  = await Transaction.findOne({
            where: {
                id: transaction.id
            },
            include: transactionIncludes //this includes other models such as client, orderDetail, orderLog, etc
        })
    }
    return transaction

有时,我的应用程序可以很好地装载此事务对象。 但是,当我尝试扩展它时,我经常会遇到这些 OOM 错误,例如:

<--- Last few GCs --->

[736:0x60632a0]    72273 ms: Mark-sweep (reduce) 2036.3 (2045.7) -> 2036.2 (2045.7) MB, 13.2 / 0.0 ms  (+ 0.1 ms in 4 steps since start of marking, biggest step 0.1 ms, walltime since start of marking 184 ms) (average mu = 0.943, current mu = 0.951) final[736:0x60632a0]    72342 ms: Mark-sweep (reduce) 2041.9 (2051.4) -> 2041.9 (2051.4) MB, 10.9 / 0.0 ms  (+ 0.5 ms in 4 steps since start of marking, biggest step 0.3 ms, walltime since start of marking 64 ms) (average mu = 0.931, current mu = 0.833) finali

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb2c2b0 node::Abort() [node]
 2: 0xa4025c node::FatalError(char const*, char const*) [node]
 3: 0xd1d11e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xd1d497 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xed68f5  [node]
 6: 0xee7d3d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 7: 0xee8bf7 v8::internal::Heap::FinalizeIncrementalMarkingIfComplete(v8::internal::GarbageCollectionReason) [node]
 8: 0xeec6b0 v8::internal::IncrementalMarkingJob::Task::RunInternal() [node]
 9: 0xddae0b non-virtual thunk to v8::internal::CancelableTask::Run() [node]
10: 0xb9a7f4 node::PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<v8::Task, std::default_delete<v8::Task> >) [node]
11: 0xb9c669 node::PerIsolatePlatformData::FlushForegroundTasksInternal() [node]
12: 0x159d8a6  [node]
13: 0x15afe14  [node]
14: 0x159e1f8 uv_run [node]
15: 0xa65fe5 node::SpinEventLoop(node::Environment*) [node]
16: 0xb6ed86 node::NodeMainInstance::Run(node::EnvSerializeInfo const*) [node]
17: 0xaef83a node::Start(int, char**) [node]
18: 0x7f8cda3cb09b __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
19: 0xa62f6c  [node]
Aborted (core dumped)

转储的核心位于大约 2 GB 的内存中。 我想在不增加 RAM 的情况下解决这个问题。

编辑:有人要求我展示包含模型关联的内容,这里是:

[redacted]

如果您在一个 Sequelize 查询中包含多个 hasMany 关联(在同一级别或嵌套级别),那么您需要为所有这些关联指明 separate: true 以便 Sequelize 将分别执行所有此类包含的查询以避免增加记录:

export const transactionIncludes = [
    {model: CartaoPagamentos, as: 'cartaopagamento', type: 'hasOne', foreignKey: 'transaction_id'},
    {model: Cliente, as: 'cliente', type: 'belongsTo', foreignKey: 'cliente_id'},
    {model: Fila, as: 'fila', type: 'hasOne', foreignKey: 'transaction_id'},
    {model: Voo, as: 'voo', type: 'hasMany', foreignKey: 'transaction_id', include: VooIncludes, separate: true }, 
    {model: Passageiros, as: 'passageiro', type: 'hasMany', foreignKey: 'transaction_id', separate: true },
    {model: Cartaomilhas, as: 'cartaomilha', type: 'hasOne', foreignKey: 'transaction_id'},
    {model: Robot, as: 'robot', type: 'belongsTo', foreignKey: 'robo_id'},
];

另请查看我的回答 (关于 hasMany 的部分,以便更好地理解当您在 Sequelize 查询中指示多个 hasMany 时发生了什么separate: true 选项。