Reputation: 566
i think i have a problem with my schema design for my music app.
i have 3 collections: Artists
, Tracks
and Albums
.
and 3 classes: artists
, albums
and tracks
document from artists
:
[_id] => MongoId Object
(
[$id] => 4ee5bbfd615c219a07000000
)
[freeze] => false,
[genres] => Array,
[hits] => 0,
[name] => Sarya Al Sawas,
[pictures] => Array,
document from albums
:
[_id] => MongoId Object
(
[$id] => 4ee88308615c218128000000
)
[name] => Sabia
[slug] => wafiq-habib-ft-sarya-al-sawas-sabia
[year] => 1999
[genres] => Array,
[pictures] => Array,
[artists] => Array
(
[0] => MongoId Object
(
[$id] => 4ee34a3b615c21b624010000
)
[1] => MongoId Object
(
[$id] => 4ee5bbfd615c219a07000000
)
)
document from tracks
[_id] => MongoId Object
(
[$id] => 4ee8a056615c21542a000000
)
[name] => Bid Ashok
[slug] => wafiq-habib-ft-sarya-al-sawas-bid-ashok
[genres] => Array,
[file] => /m/tracks/t.4ee8a05540c624.04707814.mp3,
[freeze] => false,
[hits] => 0,
[duration] => 303,
[albums] => Array
(
[0] => MongoId Object
(
[$id] => 4ee5cbc3615c216509000000
)
)
[artists] => Array
(
[0] => MongoId Object
(
[$id] => 4ee5bbfd615c219a07000000
)
[1] => MongoId Object
(
[$id] => 4ee34a3b615c21b624010000
)
)
first of all is that good schema design ??! i designed this schema this way because of many to many relationships sometimes tracks have 2 artists, and albums have 2 artists.
anyway i have problem querying the albums that attached to specific track.
lets say i'm on the artist page
i need to get all the artist albums and tracks so i do this:
$cursors = array(
'albums' => $this->albums->find(array('artists' => $artist->_id))->sort(array('_id' => -1)),
'tracks' => $this->tracks->find(array('artists' => $artist->_id))->sort(array('_id' => -1)),
'clips' => $this->clips->find(array('artists' => $artist->_id))->sort(array('_id' => -1))
);
foreach($cursors as $key => $cursor) {
foreach($cursor as $obj) {
$obj['name'] = ($this->lang->get() != 'ar' ? $obj['translated']['name'] : $obj['name']);
$obj['by'] = $this->artists()->get($obj['artists'])->toString('ft');
${$key}[] = $obj;
}
}
i need to loop on all tracks and get their album names lets say this artist has 3000 tracks i think it will be very slow....
so my question is: Is That a good Schema Design ?
Upvotes: 3
Views: 606
Reputation: 46331
Well, this is a very relational problem, and using a non-relational database for such a problem requires some effort. In general, I think your schema design is good.
What you're describing is called "the N+1 problem", because you'll have to make N+1 queries for N objects (in your case, it's more complicated, but I guess you get the idea).
Some remedies:
You can use the $in
operator to find e.g. all tracks of a certain artist:
db.tracks.find({"artists" : { $in : [artist_id_1, artist_id_2, ...] } });
This doesn't work if the array of artists grows huge, but a few hundred, maybe a thousand should work fine. Make sure artists
is indexed.
You can denormalize some of the information that is needed very often. For example, you might want to show the track list very often, so it makes sense to copy the artist's names to every track. Denormalization depends mostly on what you're trying to achieve from an end-user perspective. You might not want to store each and every artist's name in full, but only the first 50 characters because the UI doesn't show more in the overview anyway.
In fact, you're already denormalizing some data, such as the artist ids in album (which are redundant, because you could get them via the tracks as well). This makes queries easier, but it will be more write-heavy. Updates are ugly because you'll have to make sure they propagate through the system.
In some cases, it might make sense to 'join' on the client(!) rather than the server. This doesn't really fit your problem well, but it's noteworthy: suppose you have a list of friends. Now the sever will have to look up each friend's name whenever it displays them. Instead, it could provide you with a lookup table ids/friends, and the server only serves the ids. Some JavaScript could replace the ids with the real names from the client's cache.
Upvotes: 3