search

Home  >  Q&A  >  body text

Mongodb limited query return field query is slower?

When I directly use find to return all fields, the results are as follows, and the query time is 155

If the return field is limited, the query time will be longer, and the query time is 251

Why? If I limit the fields returned, the query character bytes will be smaller, shouldn't the transmission be faster?

大家讲道理大家讲道理2773 days ago866

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-17 10:06:05

    The execution plan you posted mainly reveals the following information:

    1. The first execution plan:

    Because no index is used, collscan reveals that it is a full collection scan, so you can consider creating an index

    2. The second execution plan:

    It is still a full collection scan, and documents that meet the conditions are scanned into the memory, and projection is completed in the memory, the specified field is selected, and the field is returned. It seems like this will consume more time.

    Although only the specified field is returned, when reading the storage or scanning the entire collection, only the entire documents can be returned. This is probably some basic principles of databases: reading and writing according to documents; additionally, some columnar databases are saved according to columns, which is the situation you imagined; but many databases are saved according to rows or documents. .

    If you create a covered index on a specified field and only return the specified field, this is the most efficient, because only scanning the index can return the specified field.

    For reference.

    Love MongoDB! Have fun!

    reply
    0
  • Cancelreply