Get the App
SLTechnology News&Howtos  ›  Database  › 

A point record of an index in MongoDB

Shulou Source: shulou.com Published: 2022-06-01 18:44:06 10月04日 Update

A federated index is used in MongoDB, and some problems are found. Record it for later reference!

First of all, let's look at the description of the table:

Notice the highlighted index:

Prop.lis_sta_1_prop.is_recom_-1_prop.re_t_-1_prop.post_t_-1, these four fields are

Post status, recommendation status, recommendation time, posting time

After a test, the results are as follows

You can use the query indexed above:

Db.post.find ({'prop.lis_sta' {$in [5]}}) .sort ({' prop.is_recom':-1,'prop.re_t':-1,'prop.post_t':-1}) .explain (); db.post.find ({'prop.lis_sta':5}) .sort ({' prop.is_recom':-1,'prop.re_t':-1,'prop.post_t':-1}) .explain () Db.post.find ({'prop.lis_sta' {$in [0 prop.is_recom':-1,'prop.re_t':-1,'prop.post_t':-1 5]}}) .sort ({' prop.is_recom':-1,'prop.re_t':-1,'prop.post_t':-1}) .limit (20) .explain ()

You cannot use the query indexed above:

Db.post.find ({'prop.lis_sta' {$in [0 prop.is_recom':-1,'prop.re_t':-1,'prop.post_t':-1 5]}}) .sort ({' prop.is_recom':-1,'prop.re_t':-1,'prop.post_t':-1}) .explain ()

So here are a few points:

1), try to avoid passing sorting rules in the form of flexible parameters, if there is more data, but there is no relevant index, it is easy to cause problems.

2), modified the collation, to check the index, through the explain to test.

3) it is important to filter out as much data as possible and re-sort using existing indexes, while trying to preserve limit restrictions.

Tags: Index sort data time status rules questions recommendations queries tests important parameters at the same time fields posts forms results highlights checks unions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Xiaomi MySQL Shulou Tech Info