简体   繁体   中英

MongoDB best schema design for efficiency

So I'm fairly new to mongoDB but have used sql a fair bit. I'm hoping this is a fairly straightforward questions so I'll get stuck in.

Out of the following two schema designs, which is the most efficient / easiest to query? I'll be doing fairly straightforward queries (I hope). The "sales" objects will be much more complex than what I have outlined below. There will be approximately 30 shops in the database, expecting to grow to say 60 within a few years.

  • option 1
    {
        name: "shop1",
        sales:
            [
                {
                    weekEnding: "5/1/2020",
                    sales: 156.50,
                    customers: 5
                },
                {
                    weekEnding: "12/1/2020",
                    sales: 256.50,
                    customers: 12
                },
            ]
    },
    {
        name: "shop2",
        sales:
            [
                {
                    weekEnding: "5/1/2020",
                    sales: 456.50,
                    customers: 9
                },
                {
                    weekEnding: "12/1/2020",
                    sales: 446.50,
                    customers: 19
                },
            ]
    },

  • option 2
    {
        name: "shop1",
        weekEnding: "5/1/2020",
        sales: 156.50,
        customers: 5
    },
    {

        name: "shop1",
        weekEnding: "12/1/2020",
        sales: 256.50,
        customers: 12
    },

    {
        name: "shop2",
        weekEnding: "5/1/2020",
        sales: 456.50,
        customers: 9
    },
    {
        name: "shop2",
        weekEnding: "12/1/2020",
        sales: 446.50,
        customers: 19
    },

I'm not a MongoDB expert but I think you are much better off with option 2. With option 1 you have only about 30 documents in your database and all of them can grow quite large and I think MongoDB will have to rearrange them on disk when they change size since objects should be kept in a coherent memory space.

And I see no problem at all with option 2. It will work perfectly well to index the sales by shop and won't cause any performance issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM