I have to decide about how to best save Data Objects for different Workspaces in an application in a database in the most performant way. For the sake of this example, let us assume that there are 1000 DataObjects, and 10 of them are in a Workspace. It is a given that I have a DataObject collection containing all DataObjects.
I can think of two different approaches:
- Creating an additional WorkspaceObject collection that saves a WorkspaceObject containing pointers to the DataObjects contained within that Workspace.
- Adding the WorkspaceId to the DataObject.
In the first scenario, I would first look up the WorkspaceObject by its WorkspaceId , and then look up the DataObjects contained within their IDs. That would mean 11 database calls, each of which target an object by its _id.
In the second scenario, I would look up the DataObjects using a filter for the WorkspaceId. That would be only one database call, but without any _id.
So here’s the question: Which one of these two options is faster? I know that generally fewer database calls are better, so I lean towards the second option, but I don’t know if using filters makes it slower again, and how those two scenarios compare.