Last active
August 4, 2022 11:08
-
-
Save realyze/f430042e9c4605634ac8 to your computer and use it in GitHub Desktop.
Slow meteor query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Meteor.isClient) { | |
Meteor.startup(function () { | |
var collection = new Mongo.Collection(null); | |
for (var i = 0; i < 1000; ++i) { | |
collection.insert({ | |
_id: new Mongo.ObjectID().toHexString(), | |
"kind": "t3", | |
"data": { | |
"domain": "i.imgur.com", | |
"banned_by": null, | |
"media_embed": { | |
"content": "<iframe class=\"embedly-embed\" src=\"\/\/cdn.embedly.com\/widgets\/media.html?src=https%3A%2F%2Fi.imgur.com%2Fwg2Uswp.mp4&src_secure=1&url=http%3A%2F%2Fi.imgur.com%2Fwg2Uswp.gifv&image=http%3A%2F%2Fi.imgur.com%2Fwg2Uswph.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=video%2Fmp4&schema=imgur\" width=\"600\" height=\"600\" scrolling=\"no\" frameborder=\"0\" allowfullscreen><\/iframe>", | |
"width": 600, | |
"scrolling": false, | |
"height": 600 | |
}, | |
"subreddit": "aww", | |
"selftext_html": null, | |
"selftext": "", | |
"likes": null, | |
"suggested_sort": null, | |
"user_reports": [ | |
], | |
"secure_media": { | |
"oembed": { | |
"provider_url": "http:\/\/i.imgur.com", | |
"description": "Source https:\/\/www.instagram.com\/p\/BAiCL6pGfKR \/", | |
"title": "Imgur GIF", | |
"type": "video", | |
"thumbnail_width": 640, | |
"height": 600, | |
"width": 600, | |
"html": "<iframe class=\"embedly-embed\" src=\"https:\/\/cdn.embedly.com\/widgets\/media.html?src=https%3A%2F%2Fi.imgur.com%2Fwg2Uswp.mp4&src_secure=1&url=http%3A%2F%2Fi.imgur.com%2Fwg2Uswp.gifv&image=http%3A%2F%2Fi.imgur.com%2Fwg2Uswph.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=video%2Fmp4&schema=imgur\" width=\"600\" height=\"600\" scrolling=\"no\" frameborder=\"0\" allowfullscreen><\/iframe>", | |
"version": "1.0", | |
"provider_name": "Imgur", | |
"thumbnail_url": "https:\/\/i.embed.ly\/1\/image?url=http%3A%2F%2Fi.imgur.com%2Fwg2Uswph.jpg&key=b1e305db91cf4aa5a86b732cc9fffceb", | |
"thumbnail_height": 640 | |
}, | |
"type": "i.imgur.com" | |
}, | |
"link_flair_text": null, | |
"id": "415st2", | |
"from_kind": null, | |
"gilded": 0, | |
"archived": false, | |
"clicked": false, | |
"report_reasons": null, | |
"author": "Calcd_Uncertainty", | |
"media": { | |
"oembed": { | |
"provider_url": "http:\/\/i.imgur.com", | |
"description": "Source https:\/\/www.instagram.com\/p\/BAiCL6pGfKR \/", | |
"title": "Imgur GIF", | |
"type": "video", | |
"thumbnail_width": 640, | |
"height": 600, | |
"width": 600, | |
"html": "<iframe class=\"embedly-embed\" src=\"\/\/cdn.embedly.com\/widgets\/media.html?src=https%3A%2F%2Fi.imgur.com%2Fwg2Uswp.mp4&src_secure=1&url=http%3A%2F%2Fi.imgur.com%2Fwg2Uswp.gifv&image=http%3A%2F%2Fi.imgur.com%2Fwg2Uswph.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=video%2Fmp4&schema=imgur\" width=\"600\" height=\"600\" scrolling=\"no\" frameborder=\"0\" allowfullscreen><\/iframe>", | |
"version": "1.0", | |
"provider_name": "Imgur", | |
"thumbnail_url": "http:\/\/i.imgur.com\/wg2Uswph.jpg", | |
"thumbnail_height": 640 | |
}, | |
"type": "i.imgur.com" | |
}, | |
"score": 5470, | |
"approved_by": null, | |
"over_18": false, | |
"hidden": false, | |
"preview": { | |
"images": [ | |
{ | |
"source": { | |
"url": "https:\/\/i.redditmedia.com\/kEHGMCOnm4QHLRvZHougkBXgf-2m_62wW6ZJNYBWdVw.jpg?s=d8e997f7e5c5884c19b0f3d7c11146aa", | |
"width": 640, | |
"height": 640 | |
}, | |
"resolutions": [ | |
{ | |
"url": "https:\/\/i.redditmedia.com\/kEHGMCOnm4QHLRvZHougkBXgf-2m_62wW6ZJNYBWdVw.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=108&s=a2e37a03689a2e4342bc4dfe48277169", | |
"width": 108, | |
"height": 108 | |
}, | |
{ | |
"url": "https:\/\/i.redditmedia.com\/kEHGMCOnm4QHLRvZHougkBXgf-2m_62wW6ZJNYBWdVw.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=216&s=172479669cad45fba137993546425c59", | |
"width": 216, | |
"height": 216 | |
}, | |
{ | |
"url": "https:\/\/i.redditmedia.com\/kEHGMCOnm4QHLRvZHougkBXgf-2m_62wW6ZJNYBWdVw.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=320&s=833e9ec09f440d8a3f9158ec74e529cb", | |
"width": 320, | |
"height": 320 | |
}, | |
{ | |
"url": "https:\/\/i.redditmedia.com\/kEHGMCOnm4QHLRvZHougkBXgf-2m_62wW6ZJNYBWdVw.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=640&s=623c41a38937fa9559745f65d7c1bfb5", | |
"width": 640, | |
"height": 640 | |
} | |
], | |
"variants": { | |
}, | |
"id": "zlYS9Z3N3T8QnsI_xCsCMTPfYmzvrulT8DlD-TA_ZTs" | |
} | |
] | |
}, | |
"num_comments": _.random(1000), | |
"thumbnail": "http:\/\/b.thumbs.redditmedia.com\/mS5TYLC8J8SslUV9_StpzQLekNBV993POMYnQ7AE4Kg.jpg", | |
"subreddit_id": "t5_2qh1o", | |
"hide_score": false, | |
"edited": false, | |
"link_flair_css_class": null, | |
"author_flair_css_class": null, | |
"downs": 0, | |
"secure_media_embed": { | |
"content": "<iframe class=\"embedly-embed\" src=\"https:\/\/cdn.embedly.com\/widgets\/media.html?src=https%3A%2F%2Fi.imgur.com%2Fwg2Uswp.mp4&src_secure=1&url=http%3A%2F%2Fi.imgur.com%2Fwg2Uswp.gifv&image=http%3A%2F%2Fi.imgur.com%2Fwg2Uswph.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=video%2Fmp4&schema=imgur\" width=\"600\" height=\"600\" scrolling=\"no\" frameborder=\"0\" allowfullscreen><\/iframe>", | |
"width": 600, | |
"scrolling": false, | |
"height": 600 | |
}, | |
"saved": false, | |
"removal_reason": null, | |
"post_hint": "rich:video", | |
"stickied": false, | |
"from": null, | |
"is_self": false, | |
"from_id": null, | |
"permalink": "\/r\/aww\/comments\/415st2\/the_crew_is_all_here\/", | |
"locked": false, | |
"name": "t3_415st2", | |
"created": 1452930447, | |
"url": "http:\/\/i.imgur.com\/wg2Uswp.gifv", | |
"author_flair_text": null, | |
"quarantine": false, | |
"title": "The crew is all here", | |
"created_utc": 1452901647, | |
"distinguished": null, | |
"mod_reports": [ | |
], | |
"visited": false, | |
"num_reports": null, | |
"ups": 5470 | |
} | |
}); | |
} | |
var doc = collection.findOne(); | |
var query = {'data.preview.images.source.width': {$gt: 500}}; | |
var results; | |
// Slow query (no projection, uses minimongo sort). | |
console.time('fetch full'); | |
results = collection.find(query, { sort: { 'data.num_comments': 1 } }, {limit: 50}).fetch(); | |
console.timeEnd('fetch full'); | |
// Fast query (projection, no minimongo sort). | |
console.time('fetch optimised'); | |
var docs = collection.find(query, { fields: { 'data.num_comments': 1 } }).fetch(); | |
results = _.sortBy(docs, function (doc) { return doc.data.num_comments }); | |
console.timeEnd('fetch optimised'); | |
console.time('fetch id'); | |
collection.findOne(doc._id); | |
console.timeEnd('fetch id'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment