I’m looking for tips on how to improve the database performance in the following situation.
(Or maybe I should think about DB redesign).
I have the video slots social application with 50+ different video slots. The problem is that on every user spin I have to save the slot information to DB.
Currently I have the following DB structure: 1 collection with user profiles and a lot of slots subdocuments in slots array.
{
_id: 1,
firstName: John,
lastName: Smoth,
money: 1000,
aLotOfOtherUserInfo: true,
slots:
[
{
slotId: 1,
bet: 1,
lines: 25,
payout: 100,
reels: [...]
winnings: [...]
freeSpins: [...]
}
]
}
On each user spin in any slot I have to fully update slot information in slots array. The slot is found by slotId.
update(
{
"_id": 1,
"slots.slotId": 1
},
{
$set: {"slots.$":
{
"slotId":1,
aLotOfUpdatedSlotInfo: true
}}
}
)
- Do I need to create index on “slots.slotId” in order to improve needed slot search?
- Or maybe I should think about DB redesign and store each user slot in separate collections? But, for example, on user login I will have to cycle through all collections and load all slots information of current user. So this is not the good option I think to make 50+ queries on each user login.
The help is much appreciated. I’m new to NoSQL and sometimes it is hard to think in NoSQL way. Thanks in advance
If your slots data is always invalidated on each “user spin” and you must fully set it each time, then it probably works just fine being there in the User document. Saves you a query of having to look up the “slots” id reference from a User doc, and then update a “slots” doc.
Yes you should definitely have an index not only on the
slots.slotId, but a compound index on that and the _id to match your actual query:That should make it pretty instant to look up your docs, and again pretty easy to just set the entire slots array value.