Skip to content

Instantly share code, notes, and snippets.

@realyze
Last active January 16, 2016 10:58
Show Gist options
  • Save realyze/0530ea6ce0176c86b90d to your computer and use it in GitHub Desktop.
Save realyze/0530ea6ce0176c86b90d to your computer and use it in GitHub Desktop.
Slow meteor query
if (Meteor.isClient) {
Meteor.startup(function () {
var messageThreads = new Mongo.Collection(null);
for (var i = 0; i < 1500; ++i) {
messageThreads.insert({
_id: new Mongo.ObjectID().toHexString(),
"posts": [
{
"_id": "91cb147f7b755809342931e1",
"header": {
"sender": {
"_id": "a18a219f0e28ddf75f9cff29",
"links": {
"userId": "9f72v97wrb9ewfvg4"
},
"givenName": "test",
"familyName": "tester",
"emailAddresses": [
"[email protected]"
],
"type": "contact",
"_meta": {
"ownerId": "eipfh24pirnwbipfngw",
},
"pictureUrl": "https:\/\/lh3.googleusercontent.com\/-VMJsZ9uULto\/AAAAAAAAAAI\/AAAAAAAAABM\/29fb9f9439\/photo.jpg"
},
"recipients": [],
"postedAt": "2016-01-01T00:00:00.000Z",
"wasRead": true,
"isDraft": false
},
"content": "<span>aaa<\/span>",
"needsDigest": false,
"links": {
"threadId": "c420fc7e241fba9cf12b4b27",
},
"_meta": {
"ownerId": "eipfh24pirnwbipfngw",
"deletedAt": 1447206557932
}
}
],
"_meta": {
"ownerId": "6ErzSiTeqwo9zQi8W",
"createdAt": "2015-11-11T01:36:33.225Z"
},
"lastLivePostedAt": "2016-01-01T00:00:00.000Z"
});
}
var thread = messageThreads.findOne();
// Slow query (no projection, uses minimongo sort).
console.time('fetch full');
var threads = messageThreads.find({ 'posts._meta.deletedAt': { $exists: false } }, { sort: { lastLivePostedAt: 1 } }).fetch();
_.sortBy(threads, function (t) { return t.lastLivePostedAt });
console.timeEnd('fetch full');
// Fast query (projection, no minimongo sort).
console.time('fetch optimised');
var threads = messageThreads.find({ 'posts._meta.deletedAt': { $exists: false } }, { fields: { 'posts._id': 1, 'lastLivePostedAt': 1 } }).fetch();
_.sortBy(threads, function (t) { return t.lastLivePostedAt });
console.timeEnd('fetch optimised');
console.time('fetch id');
messageThreads.findOne(thread._id);
console.timeEnd('fetch id');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment