简体   繁体   English

Meteor js:如果记录大约为100000,则页面花费太多时间来查询mongodb

[英]Meteor js: The page taking too much time to query with mongodb if records are around 100000

I am working on meteor js, there will be huge data in mongodb database. 我正在研究流星js,mongodb数据库中将有大量数据。 For now it is around 50000 messages in database. 目前,数据库中大约有50000条消息。 I am providing the code that I am currently using. 我正在提供当前正在使用的代码。 For some reason the application is taking too much time to render or load the data from database. 由于某种原因,该应用程序花费太多时间来呈现或加载数据库中的数据。 Also one more thing, if I am doing any activity,eg just like the messages the app fetches the messages again from database. 还有一件事情,如果我正在做任何活动,例如,就像消息一样,应用程序再次从数据库中获取消息。

Template.messages.helper({
    linkMessages() {
        var ids = _.pluck(Meteor.user().subscription, '_id');
        var messages = Messages.find({ $or: [{ feedId: { $exists: false }, link: { $exists: true } }, { feedId: { $in: ids }, link: { $exists: true } }] }, { sort: { timestamp: 1 }, limit: Session.get("linkMessageLimit") }).fetch();
        return messages;
    }

})

calling publication in oncreate method 在oncreate方法中调用发布

Template.roomView.onCreated(function() {
    const self = this;
    Deps.autorun(function() {
        Meteor.subscribe('messages', Session.get('currentRoom'), Session.get('textMessageLimit'), {
            onReady() {
                isReady.messages = true;
                if (scroll.needScroll) {
                    scroll.needScroll = false;
                    if (scroll.previousMessage) {
                        Client.scrollToMessageText(scroll.previousMessage);
                    }
                } else {
                    Meteor.setTimeout(function() {
                        Client.scrollChatToBottomMsg();
                    }, 1000)
                }
            }
        });

    });
});`

The publication function on server: 服务器上的发布功能:

Meteor.publish('messages', function(roomId, limit) {
    check(roomId, String);
    check(limit, Match.Integer);

        let query = { $or: [{ link: {$exists: false} }, { feedId: { $exists: false } }] };

        const thresholdMessage = Messages.findOne(query, { skip: limit, sort: { timestamp: 1 } });

        if (thresholdMessage && thresholdMessage.timestamp) {
            query.timestamp = { $gt: thresholdMessage.timestamp };
        }

        return Messages.find(query, { sort: { timestamp: -1 } });

});

It is not a good practice to allow mini-mongodb to get populated with such a huge data. 允许mini-mongodb填充如此巨大的数据不是一个好习惯。 Though Meteor JS is good at this too, still it will take some amount of time taking into consideration the network traffic, bandwidth etc. 尽管Meteor JS也很擅长,但考虑网络流量,带宽等仍然需要花费一些时间。

Irrespective of whether it is unlimited scroll or simple pagination I would suggest you to use pagination. 无论是无限滚动还是简单分页,我都建议您使用分页。 I have already got it accepted and it works like charm, here is the answer and entire code for pagination . 我已经接受了它,它就像魅力一样工作, 这是答案和分页的整个代码

My pagination solution is server specific, so it performs good. 我的分页解决方案是特定于服务器的,因此性能良好。 Collection publish is limited to the limit provided from subscription. 集合发布仅限于订阅提供的limit

NOTE: There is yet no such proper full fledged solution for table with search and pagination and much more facility which makes it very flexible as per our need. 注意:对于带有搜索和分页功能的表格,还没有合适的完整解决方案,还有更多功能,这使得它可以根据我们的需要非常灵活。 I suggest to create your own. 我建议创建自己的。

MORE INSIGHTS: 更多见解:

https://www.quora.com/Does-Meteor-work-well-with-large-datasets https://www.quora.com/Does-Meteor-work-well-with-large-datasets

https://forums.meteor.com/t/very-big-data-collection-in-mongodb-how-to-fetch-in-meteor-js/6571/7 https://forums.meteor.com/t/very-big-data-collection-in-mongodb-how-to-fetch-in-meteor-js/6571/7

https://projectricochet.com/blog/top-10-meteor-performance-problems https://projectricochet.com/blog/top-10-meteor-performance-problems

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM