Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A point record of an index in MongoDB

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 245

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report