From 917b9c6c8f9caeead21da927d18298a739648811 Mon Sep 17 00:00:00 2001 From: typhoonsimon <38272969+typhoonsimon@users.noreply.github.com> Date: Sat, 14 Mar 2020 16:13:53 +0800 Subject: [PATCH] Gigantic performance improvemet ... over large chains. Added indexes and brought back last_txs setting (set to 0 will display the whole chain in the index, but without sacrificing server or mongo performance). --- lib/database.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/database.js b/lib/database.js index f2f8afdbf..298278d53 100644 --- a/lib/database.js +++ b/lib/database.js @@ -435,28 +435,33 @@ module.exports = { }, get_last_txs_ajax: function(start, length, min, cb) { - Tx.countDocuments({'total': {$gte: min}}, function(err, count){ - Tx.find({'total': {$gte: min}}).sort({blockindex: 'desc'}).skip(Number(start)).limit(Number(length)).exec(function(err, txs){ - if (err) { - return cb(err); - } else { - return cb(txs, count); - } - }); + lib.get_blockcount(function(blockcount) { + var blockFrom = blockcount - Number(start); + if (settings.index.last_txs != 0) { + blockcount = settings.index.last_txs; + } + var q = {$and: [{total: {$gt: Number(min)}}, {blockindex: {$lte: blockFrom}}]}; + Tx.find(q).sort({blockindex: -1}).limit(Number(length)).exec(function(err, txs) { + if (err) { + return cb(err); + } else { + return cb(txs, blockcount); + } + }); }); }, get_address_txs_ajax: function(hash, start, length, cb) { var totalCount = 0; - AddressTx.find({a_id: hash}).countDocuments({}, function(err, count){ + AddressTx.find({a_id: hash}).count(function(err, count){ if(err) { return cb(err); } else { totalCount = count; AddressTx.aggregate([ - { $match: { a_id: hash } }, + { $match: { a_id: hash } }, { $sort: {blockindex: -1} }, - { $skip: Number(start) }, + { $skip: Number(start) }, { $group: { _id: '', @@ -474,7 +479,7 @@ module.exports = { if (err) { return cb(err); } else { - AddressTx.find({a_id: hash}).sort({blockindex: 'desc'}).skip(Number(start)).limit(Number(length)).exec(function (err, address_tx) { + AddressTx.find({a_id: hash}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function (err, address_tx) { if (err) { return cb(err); } else {