Home  >  Article  >  Backend Development  >  2000000万数据库 MongoDB 查询速度慢

2000000万数据库 MongoDB 查询速度慢

WBOY
WBOYOriginal
2016-06-06 20:31:511622browse

一个表里面的数据大概是如下的,这种重复的,现在单纯的根据 toUserId 倒叙拿数据列表,就开始 10 秒左右才能够在前台拿到数据了,数据分页为 100 条每一页,现在数据库也有 1G 左右了,请问要怎么优化一下。但是聚合查询速度挺快的。在 toUserId 上索引也试过了,并不能提升性能。

<code>~~~
{ "_id" : ObjectId("554c697550a1d1a42d00003a"), "time" : Timestamp(1431071093, 33), "text" : "这是数据库测试:17", "toUserId" : ObjectId("554c338c50a1d12022000029"), "state" : "未读", "forUserIdObj" : ObjectId("554c338c50a1d12022000029") }

{ "_id" : ObjectId("554c697550a1d1a42d00003b"), "time" : Timestamp(1431071093, 34), "text" : "这是数据库测试:18", "toUserId" : ObjectId("554c338c50a1d12022000029"), "state" : "未读", "forUserIdObj" : ObjectId("554c338c50a1d12022000029") }
~~~
</code>

回复内容:

一个表里面的数据大概是如下的,这种重复的,现在单纯的根据 toUserId 倒叙拿数据列表,就开始 10 秒左右才能够在前台拿到数据了,数据分页为 100 条每一页,现在数据库也有 1G 左右了,请问要怎么优化一下。但是聚合查询速度挺快的。在 toUserId 上索引也试过了,并不能提升性能。

<code>~~~
{ "_id" : ObjectId("554c697550a1d1a42d00003a"), "time" : Timestamp(1431071093, 33), "text" : "这是数据库测试:17", "toUserId" : ObjectId("554c338c50a1d12022000029"), "state" : "未读", "forUserIdObj" : ObjectId("554c338c50a1d12022000029") }

{ "_id" : ObjectId("554c697550a1d1a42d00003b"), "time" : Timestamp(1431071093, 34), "text" : "这是数据库测试:18", "toUserId" : ObjectId("554c338c50a1d12022000029"), "state" : "未读", "forUserIdObj" : ObjectId("554c338c50a1d12022000029") }
~~~
</code>

建议索引时间戳

索引那个userId不科学

照你这种情况,感觉不应该上mongodb,而应该上mysql

排序慢。分页慢。并不是索引的问题:查询瓶颈在于排序与分页。首先排序,每次分页都要重新排序一次。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:phalcon是什么Next article:php图片处理效果