我基本上做的是计算一个网址被查看的次数,每当某个用户在频道ID上查看某种网址时,我首先选择与此网址匹配的文档和此频道ID,
现在我看到了一些性能问题,我听说MongoDB上的更新操作会对它产生很大的影响,我怎么能以这样的方式实现它呢?它不会在数据库上如此沉重?
这是我正在使用的代码:
Dim client = New MongoClient(MongoHelper.mongoRedirectConnectionString)
Dim domain As String = MerDecisionTaker.FixAddress(url)
mongo = client.GetServer()
db = mongo.GetDatabase("urls_check")
table = db.GetCollection("urls_check")
searchQuery = Builders.Query.And(Builders.Query.EQ("url", url), Builders.Query.EQ("channel", channel))
tableNew = db.GetCollection("ForbiddenURLS")
Dim forbiddenDocumentRedirect = tableNew.FindOneAs(Of MongoLiveNotifiedDocumentClass)(searchQuery)
If (forbiddenDocumentRedirect Is Nothing) Then
relevantObj = table.FindOneAs(Of MongoLiveNotifiedDocumentClass)(searchQuery)
If (relevantObj Is Nothing) Then
Dim settingsTable As MongoCollection = db.GetCollection("urls_checkSettings")
Dim channelSettings = settingsTable.FindOneAs(Of channelSettings)(Builders.Query.EQ("channel", channel))
If channelSettings Is Nothing Then
channelSettings = settingsTable.FindOneAs(Of channelSettings)(Builders.Query.EQ("channel", "channel"))
End If
If channelSettings IsNot Nothing Then
Dim newDoc As MongoLiveNotifiedDocumentClass = New MongoLiveNotifiedDocumentClass()
newDoc.date_created = DateTime.Now
newDoc.channel = channel
newDoc.url = url
newDoc.maxCount = channelSettings.maxCount
newDoc.Count = 1
If (url IsNot Nothing) Then
table.Insert(Of MongoLiveNotifiedDocumentClass)(newDoc)
End If
End If
Else
If relevantObj.Count >= relevantObj.maxCount Then
Dim newDoc As MongoForbiddenURLDocumentClass = New MongoForbiddenURLDocumentClass()
newDoc.url = url
newDoc.channel = channel
tableNew.Update(searchQuery, Builders.Update.Replace(newDoc), MongoDB.Driver.UpdateFlags.Upsert)
Else
relevantObj.Count += 1
table.Save(relevantObj)
End If
End If
End If
有什么建议吗?
非常感谢!