Commit b5a11081 authored by mohammad.salama's avatar mohammad.salama

Added Indexing on MongoDB and modified Query for Optimizing Performance

parent a8c794e9
...@@ -31,9 +31,17 @@ namespace ScheduledMessagesHandler.MongoMessages ...@@ -31,9 +31,17 @@ namespace ScheduledMessagesHandler.MongoMessages
var client = new MongoClient(MongoURL); var client = new MongoClient(MongoURL);
var database = client.GetDatabase(DataBaseName); var database = client.GetDatabase(DataBaseName);
collection = database.GetCollection<BsonDocument>(myCollection); collection = database.GetCollection<BsonDocument>(myCollection);
var keys = Builders<BsonDocument>.IndexKeys.Ascending("timestamp").Ascending("status");
var indexOptions = new CreateIndexOptions { Background = true };
var indexModel = new CreateIndexModel<BsonDocument>(keys, indexOptions);
collection.Indexes.CreateOne(indexModel);
return "ok"; return "ok";
} }
catch (Exception ex) catch (Exception ex)
...@@ -79,7 +87,12 @@ namespace ScheduledMessagesHandler.MongoMessages ...@@ -79,7 +87,12 @@ namespace ScheduledMessagesHandler.MongoMessages
Builders<BsonDocument>.Filter.Eq("status", status) Builders<BsonDocument>.Filter.Eq("status", status)
); );
var docs = collection.Find(filter).Limit(limit).ToList<BsonDocument>(); var sort = Builders<BsonDocument>.Sort.Ascending("timestamp");
var docs = collection.Find(filter)
.Limit(limit)
.Sort(sort)
.ToList<BsonDocument>();
if (docs.Count == 0) if (docs.Count == 0)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment