Skip to content

Commit

Permalink
perf(index): limit query count to improve index performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 13, 2014
1 parent 53aa878 commit 573d7d3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.save = function(package) {
};

exports.stat = function(callback) {
return download.find({}).sort({ time: -1 }).exec(function(err, results) {
return download.find({}).limit(50).sort({ time: -1 }).exec(function(err, results) {
var res = [];
var todayDownloads = [];
for (var i in results) {
Expand Down

11 comments on commit 573d7d3

@sorrycc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before

$ time curl http://spmjs.io
curl http://spmjs.io  0.00s user 0.01s system 0% cpu 6.993 total

after

$ time curl http://spmjs.io/
curl http://spmjs.io/  0.00s user 0.01s system 0% cpu 0.916 total

@afc163
Copy link
Member

@afc163 afc163 commented on 573d7d3 Nov 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该先 sort 再 limit 。

@afc163
Copy link
Member

@afc163 afc163 commented on 573d7d3 Nov 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

而且这么改会导致每天的下载量最高只显示 50 了。

@afc163
Copy link
Member

@afc163 afc163 commented on 573d7d3 Nov 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以另外有一个表来存储下载量数据。这样不用全量查询 feed.db 了。

@sorrycc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort 和 limit 的顺序应该没关系吧,在 exec 时才执行的。

@sorrycc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下载量的我多 count 一次吧,应该还是比较快的。

@afc163
Copy link
Member

@afc163 afc163 commented on 573d7d3 Nov 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

或者先改为 1000 ,一般都到不了这个数。

@sorrycc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下载量 Fixed 了,多 count 了一次。

@afc163
Copy link
Member

@afc163 afc163 commented on 573d7d3 Nov 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

效率如何?

@sorrycc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本地测过没有影响,首页的响应时间是 0.38s 。

@afc163
Copy link
Member

@afc163 afc163 commented on 573d7d3 Nov 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

效率比最开始还是有明显提升。

看来是全表 sort 比较花时间,sort 一定要 limit。

Please sign in to comment.