Relational Queries: Which is faster?

I have 3 collections of Courses, Units, and CourseUnits, which connects Courses and Units.

Currently if I want to get all the Units a Course has, I’m doing this

const course = Courses.findOne({_id: courseId});
const units = CourseUnits.find({courseId}).map(courseUnit => Units.findOne({_id: courseUnit.unitId}));

But would it be faster if I did this instead?
My thinking here is that it’s less calls to the database. The above would be 2 + number of units while the below would only be 3. (It’s all local anyway because of MiniMongo so maybe it doesn’t matter)

const course = Courses.findOne({_id: courseId});
const unitIds = CourseUnits.find({courseId}).map(courseUnit => courseUnit.unitId);
const units = Units.find({_id: {$in: unitIds}});

Also, how can I run performance tests on things like this in the future? Is there a good guide for that because I would like to be able to do it myself, but I don’t know how

1 Like

Hi

http://grapher.cultofcoders.com will solve your problem.
You can integrate grapher-live and get performance measurements for your requests.

For a query like:

courses: {
    units: { ... }
}

Only does 2 db queries. Use that and your problems will be solved.

1 Like
2 Likes

Thank you but that’s not what I’m looking for. At the moment I do not want to add any more packages to my application and only want builtin solutions.