Does Firebase load all the items when you use a limitToFirst or limitToLast?

For example, I have a database structure like this:

"chatMessages": {
    "chat1": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      "m2": { ... },
      "m3": { ... }
    "chat2": { ... },
    "chat3": { ... }

Now, some chats have, let's say, 1,000 messages. When I use a LimitToLast to retrieve the last 100 messages. Does Firebase first load all the 1,000 messages before sending me the 100 messages I want to retrieve or does it limit the loading to the 100 messages I really need? I am not certain, because I read a lot about keeping the database as flat as possible.

In case Firebase does load all 1,000 messages first, should I consider nesting it in a specific way?

I hope the community can make things clear to me!

1 answer

  • answered 2017-06-17 20:01 Frank van Puffelen

    There are two possibilities here:

    1. There is an index (either implicit on key, or explicitly defined by you) for the query, the data is filtered on the server.

    2. There is no index for the query, in which case all data at the location is retrieved and ordered/filtered on the client.