I have a simple publish function that takes a long time to run. I’m not sure why. The count goes instantly, and when I run the query in mongodb shell, it runs instantly. But in my publish/subscribe, it takes about 10 seconds for the documents to go across the websocket and show up in my application. When I had only a few items in the collection, it was instant. But now with 100k dummy records, it is slow.
The count is published instantly - no issue with that. I also have an index on updatedAt so no issue there. Further if I run this query in the shell, results come back instantly.
I’m kind of bummed about this because one of the main reasons for using meteor was reactivity on everything! Would somebody please explain what it is about reactivity that makes it so slow vs the raw query which is fast?
@SkyRooms, I am running pagination on this. But it works instantly if I put the query into robomongo. It’s just something about meteor’s publish that is making it slow.
The counts.publish is from a Meteor package recommended on the Angular Meteor tutorial. All it does is publish the count of a query. It works instantly - the count is sent to the client without delay. The delay only occurs when sending the actual documents. @robfallows, I have an index on updatedAt. Also, when I run it in robomongo, it returns the documents in just milliseconds, so I know the query isn’t the issue.
Looking at the github page of publish-counts it says
Publish-counts is designed for counting a small number of documents around an order of 100. Due to the real-time capability, this package should not be used to count all documents in large datasets. Maybe some, but not all. Otherwise you will maximize your server’s CPU usage as each client connects.
try your publish function without invoking Counts.publish and see if the performance changes
In your example you are getting 2 observers - first for Counts and second for publish handle. So your CPU/RAM usage growes up. Its to long to explain all the corners of reactive publish))
Anyway you could take a look for brillinant Kadira Academy to find more about Meteor performance
Ok, @jamgold wins the prize. Even though the count published instantly, and the main query results wouldn’t come back for 10 seconds, it still turns out the count publish was the issue. Disabled it and got results immediately! Now I’m going to have to find another pagination solution, but glad I know the root of the problem.