opt(mongo): optimization db index

This commit is contained in:
OldHawk
2017-12-23 17:26:39 +08:00
parent 56d6a4b67a
commit bae7da82c0
14 changed files with 68 additions and 19 deletions

View File

@@ -75,4 +75,8 @@ var MakerSchema = new Schema({
}
}, {usePushEach: true});
MakerSchema.index({torrent_count: -1});
MakerSchema.index({upload_access: -1});
mongoose.model('Maker', MakerSchema);

View File

@@ -251,6 +251,7 @@ exports.announce = function (req, res) {
/*---------------------------------------------------------------
getTorrentItemData
torrent data include peers
and update the seeding/leeching users count number
---------------------------------------------------------------*/
function (done) {
Torrent.findOne({

View File

@@ -56,4 +56,6 @@ var CollectionSchema = new Schema({
}
}, {usePushEach: true});
CollectionSchema.index({recommend_level: 1, ordered_at: -1, created_at: -1});
mongoose.model('Collection', CollectionSchema);

View File

@@ -72,4 +72,7 @@ var ForumSchema = new Schema({
}
}, {usePushEach: true});
ForumSchema.index({order: 1, createdat: -1});
ForumSchema.index({category: 1, order: 1, createdat: -1});
mongoose.model('Forum', ForumSchema);

View File

@@ -146,6 +146,8 @@ var TopicSchema = new Schema({
}
}, {usePushEach: true});
TopicSchema.index({forum: 1, isTop: -1, lastReplyAt: -1, createdAt: -1});
TopicSchema.index({isGlobal: 1, forum: -1});
mongoose.model('Topic', TopicSchema);
mongoose.model('Attach', AttachSchema);

View File

@@ -53,4 +53,12 @@ var InvitationSchema = new Schema({
}
}, {usePushEach: true});
InvitationSchema.index({user: 1, status: 1, invitedat: 1});
InvitationSchema.index({user: 1, status: 1, expiresat: 1, createdat: 1});
InvitationSchema.index({isOfficial: 1, invitedat: -1});
InvitationSchema.index({isOfficial: 1, status: 1, expiresat: 1});
InvitationSchema.index({to_email: 1});
InvitationSchema.index({token: 1});
InvitationSchema.index({token: 1, status: 1, expiresat: 1});
mongoose.model('Invitation', InvitationSchema);

View File

@@ -42,5 +42,6 @@ var AdminMessageSchema = new Schema({
}
}, {usePushEach: true});
AdminMessageSchema.index({type: 1, createdat: -1});
mongoose.model('AdminMessage', AdminMessageSchema);

View File

@@ -52,5 +52,6 @@ var MessageSchema = new Schema({
}
}, {usePushEach: true});
MessageSchema.index({type: 1, from_user: 1, to_user: 1, updatedat: -1, createdat: -1});
mongoose.model('Message', MessageSchema);

View File

@@ -1863,7 +1863,7 @@ exports.getLeecherUsers = function (req, res) {
peer_status: PEERSTATE_LEECHER,
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
})
.sort('-peer_uploaded')
.sort('-peer_downloaded')
.populate('user', 'username displayName profileImageURL isVip')
.skip(skip)
.limit(limit)

View File

@@ -168,8 +168,9 @@ CompleteSchema.methods.removeHnRWarning = function (u) {
};
CompleteSchema.index({torrent: 1, user: 1});
CompleteSchema.index({user: 1, hnr_warning: 1});
CompleteSchema.index({user: -1, createdAt: -1});
CompleteSchema.index({torrent: 1, createdAt: -1});
CompleteSchema.index({torrent: 1, user: 1});
mongoose.model('Complete', CompleteSchema);

View File

@@ -140,8 +140,9 @@ function countPercent(p) {
PeerSchema.index({user: -1, startedat: -1});
PeerSchema.index({torrent: -1, startedat: -1});
PeerSchema.index({peer_status: 1});
PeerSchema.index({torrent: 1, peer_status: 1, last_announce_at: 1});
PeerSchema.index({user: 1, peer_status: 1, last_announce_at: 1});
PeerSchema.index({torrent: 1, peer_status: 1, last_announce_at: 1, peer_uploaded: 1});
PeerSchema.index({torrent: 1, peer_status: 1, last_announce_at: 1, peer_downloaded: 1});
PeerSchema.index({user: 1, peer_status: 1, last_announce_at: 1, peer_uploaded: 1});
PeerSchema.index({user: 1, peer_status: 1, last_announce_at: 1, peer_downloaded: 1});
mongoose.model('Peer', PeerSchema);

View File

@@ -291,21 +291,31 @@ TorrentSchema.methods.globalUpdateMethod = function (cb) {
});
};
TorrentSchema.index({info_hash: 1});
TorrentSchema.index({user: -1, createdat: -1});
TorrentSchema.index({maker: 1, createdat: -1});
TorrentSchema.index({info_hash: 1, createdat: -1});
TorrentSchema.index({torrent_tmdb_id: 1, createdat: -1});
TorrentSchema.index({torrent_status: 1, createdat: -1});
TorrentSchema.index({torrent_type: 1, createdat: -1});
TorrentSchema.index({torrent_hnr: 1, createdat: -1});
TorrentSchema.index({torrent_vip: 1, createdat: -1});
TorrentSchema.index({torrent_recommended: 1, createdat: -1});
TorrentSchema.index({torrent_sale_status: 1, createdat: -1});
TorrentSchema.index({user: 1, torrent_recommended: 1, orderedat: -1, createdat: -1, '_peers.id': 1});
TorrentSchema.index({
torrent_sale_status: 1,
'resource_detail_info.id': 1
});
TorrentSchema.index({
user: 1,
torrent_recommended: 1,
orderedat: -1,
createdat: -1,
'_peers.id': 1
});
TorrentSchema.index({
torrent_status: 1,
torrent_type: 1,
torrent_hnr: 1,
torrent_vip: 1,
torrent_recommended: 1,
orderedat: -1,
@@ -314,16 +324,14 @@ TorrentSchema.index({
});
TorrentSchema.index({
user: 1,
maker: 1,
torrent_status: 1,
torrent_type: 1,
torrent_hnr: 1,
torrent_vip: 1,
maker: 1,
orderedat: -1,
createdat: -1
});
mongoose.model('Torrent', TorrentSchema);
mongoose.model('Comment', CommentSchema);
//mongoose.model('Thumb', ThumbSchema);
mongoose.model('Rating', RatingSchema);

View File

@@ -21,4 +21,7 @@ var TraceSchema = new Schema({
}
}, {usePushEach: true});
TraceSchema.index({createdat: -1});
TraceSchema.index({'content.action': -1});
mongoose.model('Trace', TraceSchema);

View File

@@ -543,10 +543,6 @@ UserSchema.statics.generateRandomPassphrase = function () {
});
};
UserSchema.statics.seed = seed;
mongoose.model('User', UserSchema);
/**
* Seeds the User collection with document (User)
* and provided options.
@@ -632,3 +628,21 @@ function seed(doc, options) {
});
}
UserSchema.statics.seed = seed;
UserSchema.index({passkey: 1});
UserSchema.index({email: 1});
UserSchema.index({uptotal: 1});
UserSchema.index({status: 1, uploaded: -1, downloaded: -1, ratio: -1, score: -1});
UserSchema.index({status: 1, downloaded: -1, uploaded: -1, ratio: -1, score: -1});
UserSchema.index({status: 1, score: -1, uploaded: -1, downloaded: -1, ratio: -1});
UserSchema.index({status: 1, ratio: -1, uploaded: -1, downloaded: -1, score: -1});
UserSchema.index({isVip: 1, username: 1, displayName: 1, email: 1, created: -1});
UserSchema.index({roles: 1, username: 1, displayName: 1, email: 1, created: -1});
UserSchema.index({status: 1, username: 1, displayName: 1, email: 1, created: -1});
UserSchema.index({username: 1, email: 1});
UserSchema.index({resetPasswordToken: 1, resetPasswordExpires: 1});
UserSchema.index({signUpActiveToken: 1, status: 1, signUpActiveExpires: 1});
mongoose.model('User', UserSchema);