Views taking longer time to fetch data first time

In my application couchabse views are taking longer time when fetching data first time.

below is my code snippet.

  1. Creating and registering the map function -

             MapDelegate usersMapDelegate = (doc, emit) =>
             {
                 if (doc["_id"].ToString().StartsWith("assetTemplate::")
                 {
                     emit(doc["tenantId"], doc);
                 }
             };
    
             var allUsersView = database.GetView("AssetTemplatesView");
             allUsersView.SetMap(usersMapDelegate, "1");
    
  2. Running query against the view

                 var assetsDocsView = database.GetView("AssetTemplatesView");
                 var query = assetsDocsView.CreateQuery();
    
                 query.StartKey = "tenant::";
                 query.EndKey = "tenant::";
    
    
                 query.Skip = 100;
    
                 query.Limit = 100;
    
                 var rows = query.Run();
    
    
                 var objRetValue = rows.Select(result => JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(result.Value, Formatting.Indented))).ToList();
    

When we run “query.Run()” for first time to get records from 0 to 100, Its taking around 30 to 45 seconds. But next time when we run “query.Run()” for records 100 to 200, 200 to 300… all these sub-sequence calls only taking 1 second to get records. Only problem is when we run query first time on that view. I guess the initial time is for indexing the view.

Please help me on this. Why first call taking this much of time and how i can optimise it ?

Environment details -

Couchbase Lite - 1.3.1
Xamarin - 4.2.2.6
Xamarin Android - 7.0.2.42
Xamarin Forms - 10.3.1.7

Thanks.

1 Like

Yes, the initial slowness is due to building up the index. After that’s done the results are persistent and so it is very quick to look them up. The biggest factor in how slow it will be, aside from what kind of phone you are using, is the number of documents in your database. Usually what we suggest to get around this is to immediately run a query and ignore the results, but set the IndexUpdateMode to After. This will start indexing the view asynchronously so that hopefully by the time you get around to your first real query it is done.

30-45 seconds seems a long time to index a view. How many documents are in the database? What sort of device is this running on?

Running on Iphone 6. And around 15000 documents are there.

Running a fake query and ignore the results, Works for me. But is it only the way around ?. Since i have a lot views in my application. I have to do this trick all over. Can you guys suggest me any better way. Or is there any other option ?

Hm, 500 docs/sec seems slow to me; @borrrden is that the range you’d expect for Xamarin on iOS? And does .NET have the documentType optimization for views?

CBL 1.4 adds a View.updateIndex() method. You can invoke this on a background thread.

Also, if the docs are being pulled from a server, you can periodically update the index during the pull. This simulates the behavior of a typical SQL database which updates the index on every document insert.

1 Like

When i need to call “View.updateIndex()” after or before pulling data from server ?.

Yes docs are being pulled from a server. how i can periodically update the index during the pull ?.

Ideally views index should update automatically during the pulling docs from server but its not getting update. Whats wrong with it?

When i need to call “View.updateIndex()” after or before pulling data from server ?.

If you want to avoid a single long delay, you should call it several times while pulling data.

Yes docs are being pulled from a server. how i can periodically update the index during the pull ?.

Watch the replication status. Start a timer that periodically calls updateIndex, like every 10 seconds or so. Stop the timer when replication stops or goes idle.

Ideally views index should update automatically during the pulling docs from server but its not getting update. Whats wrong with it?

There’s nothing wrong with it, it’s just not how views work in Couchbase Lite (or in CouchDB, where this originates.) I assume you’re used to SQL databases, which tend to update the index synchronously with every document update. This avoids pauses, but it doesn’t actually speed things up because every update is slowed down by the work of indexing that record/document.

Couchbase Lite 2.0 uses a more SQL-like indexing/query mechanism (actually it’s like N1QL in Couchbase Server) that may be more familiar to you. It updates the index with every document update.