var assetsDocsView = database.GetView(viewName);
var query = assetsDocsView.CreateQuery();
query.PostFilter = filterFunction ;
query.Descending = true;
query.Limit = 10;
var rows = query.Run();
My problem is that, When i don’t pass any value for all four permeates(i.e assetNumber,serialNumber,barCode,chipId) for the post function, I got max 10 records with in 5 seconds. But when i only pass exact match value for “assetNumber” permeate, It takes around more then a minute to return only one record which is the exact match. My problem why is it taking so long to return that one record. is there any thing else i can do to optimise it. its searching on over 16000 documents.
Please help me on this. Why its taking this much of time and how i can optimise it ?
Environment details -
Couchbase Lite - 1.3.1
Xamarin - 220.127.116.11
Xamarin Android - 18.104.22.168
Xamarin Forms - 10.3.1.7
But when i only pass exact match value for “assetNumber” permeate, It takes around more then a minute
The only difference is the amount of work being done in your postFilter function. When your variables have non-null values, the function does a bunch of string operations. This implies the postFilter is being called a huge number of times, for it to take so much time. (Probably 16000 times, i.e. once per document.)
What does your view’s map function look like? It looks like you’ve set things up so the view itself does almost no filtering and returns all or most of the rows in the database, leaving it to the postFilter to select the rows you want. That’s going to give you very poor performance … it’s the equivalent of running a SQL query without creating an index.
It doesn’t look wrong, but it’s not doing much for you other than eliminating some documents. Emitting the document ID as the key is usually not the right thing to do. The key should be the thing you’re searching for in the query.
Do you have any experience with SQL/relational databases? Because this is very much the same task as optimizing a query there — you want to create indexes on the right columns to help the query engine find results efficiently without scanning the whole table.
In your case it looks like you have four different things you’re searching for independently — assetNumber, serialNumber, assetBarcode, chipIds. That would call for four views/indexes, one for each.
You’re doing a case-insensitive match; if this is really necessary, then you’d emit the lowercased property value as the key in each view, then query for key = the lowercased desired value.
Create four views. In each view’s map function, emit one of those fields as the key.
If you need case-insensitive matching, lowercase the key before emitting it, then query for the lowercased value you’re looking for.
how four different views would work for me ?, Because i need only one view since my search depended on these four fields. of example i can search for a record which need to have exact “assetNumber” and “serialNumber”. so in these case how four different views would work for me ?.
so that’s why i am looking for the way in which only have one view but have key indexes on those four fields.
If you want to search for equality of two or more fields, emit a key consisting of an array of those fields. (The key can have more fields than you’re searching for; so for example you could emit all four fields and then use that view to search on the first field, the first two, the first three, or all four.)
i guess its not since search time still the same.
As long as you don’t set the startKey and/or endKey, the query is going to iterate over every row in the index, i.e. every document. That’s slow. You should set the startKey and endKey to the key or key range you want to search for.
Again, please (re)read the documentation on views/queries. It goes into all of this.