2

I have a database table having 100 million records. Screen Shot is taken from Robomongo

Table Schema: There are 100 million records

When I run the following code. I get results, but It takes around 1 minute to get completed. I need to optimize the query to get results faster. What I have done till now is here. Please tell me the way forward to achieve the optimized result.

        var collection = _database.GetCollection<BsonDocument>("FloatTable1");

        var sw = Stopwatch.StartNew();
        var builder = Builders<BsonDocument>.Filter;
        int min = Convert.ToInt32(textBox13.Text);  //3
        int max = Convert.ToInt32(textBox14.Text);  //150
        var filt = builder.Gt("Value", min) & builder.Lt("Value", max);
        var list = collection.Find(filt);
        sw.Stop();
        TimeSpan time = sw.Elapsed;
        Console.WriteLine("Time to Fetch Record: " + time.ToString());


        var sw1 = Stopwatch.StartNew();
        var list1 = list.ToList();
        sw1.Stop();
        TimeSpan time1 = sw1.Elapsed;
        Console.WriteLine("Time to Convert var to List: " + time1.ToString());
        Console.WriteLine("Total Count in List: " + list1.Count.ToString());

Out Put is:

  • Time to Fetch Record: 00:00:00.0059207
  • Time to Convert var to List: 00:01:00.7209163
  • Total Count in List: 1003154

I have few question related to the given code.

  1. When line collection.Find(filt) executes, does it fetch filtered record from the database OR Just creating filter?
  2. var list1 = list.ToList(); takes 1 minute to execute, is it only converting from var to list OR First fetching data than converting?
  3. How to achieve this query and result in least possible time. Please Help.
Umer
  • 21
  • 4

1 Answers1

2

When line collection.Find(filt) executes, does it fetch filtered record from the database OR Just creating filter?

It is just creating the filter.

var list1 = list.ToList(); takes 1 minute to execute, is it only converting from var to list OR First fetching data than converting?

It is fetching the data and converting.

How to achieve this query and result in least possible time. Please Help.

The fetch / filtering on the database is eating your time. The easiest way to speed it up would be creating an index on the column you are filtering.

Everything else would need some more effort or database technologies, like creating a column which more roughly presents your date (e.g. grouped by day) and indexing this one, or creating something like table sections grouped by a given timespan (I'm not a DB-Admin and don't know the proper terms for this, but I remember somebody doing it on a database with billions of records ;) )

Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
  • Thank you for information provided. I have tried by making the index on the Value column but it didn't help. any other way forward towards the solution? – Umer Jul 10 '17 at 07:47