lundi 22 août 2016

Searching strategy to efficiently load data from service or server?

This question is not very a language-specific question, it's some kind of pattern-related question, but I would like to tag it with some popular languages that I can understand here.

I've not been very experienced with the requirement of efficiently loading data in combination with searching data (especially for mobile environment).

My strategy used before is load everything into local memory and search from there (such as using LINQ in C#).

One more strategy is reload the data every time a new search is executed. Doing something like this is of course not efficient, also we may need to do some more complicated things to sync the newly loaded data with the existing data (already loaded into local memory).

The last strategy I can think of is the hardest one to implement, that is lazily load the data together with the searching execution. That is when the search is executed, the return result should be cached locally. The search should look in the local memory first before fetching new result from the service/server. So the result of each search is a combination of the local search and the server search. The purpose here is to reduce the amount of data being reloaded from server every time a search is run.

Here is what I can think of to implement this kind of strategy:

  • When a search is run, look in the local memory first. Finishing this step gives out the local result.
  • Now before sending request to search on the server side, we need to somehow pass what are already put in the result (locally) to exclude them from the result when searching on the server side. So the searching method may include a list of arguments containing all the item IDs found by the fisrt step. With that searching request, we can exclude the found result and return only new items to the client.
  • The last step is merge the 2 results: from local and server to have the final search result before showing on the UI to the user.

I'm not sure if this is the right approach but what I feel not really good here is at the step 2. Because we need to send a list of item IDs found on the step 1 to the server, so what if we have hundreds or thousands of such IDs, sending them in that case to the server may not be very efficient. Also the query to exclude such a large amount of items may not be also efficient (even using direct SQL or LINQ). I'm still confused at this point.

Finally if you have any better idea and importantly implemented in some production project, please share with me. I don't need any concrete example code, I just need some idea, steps to implement (like how I described my idea above). Thank you very much for your help.

Aucun commentaire:

Enregistrer un commentaire