douhanxujiuji6098 2011-12-14 14:09
浏览 26
已采纳

MongoDB架构设计。 无法得到我想要的东西

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

  1. 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;
        }
    }
    
  2. 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 ?

  • 写回答

1条回答 默认 最新

  • doubinchou4219 2011-12-14 15:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化