Getting Most Updated/Reliable Data Back From A View

I am looking for best practices for getting updated and reliable data back from a view. I have read a bit about the stale parameter and I understand that sending stale=false will force a reindex prior to returning the results - however it is clear there is a performance hit for this. ( So, I am trying to figure out the best way to go about ensuring a view is updated with current/proper data so reads that will happen right after an update are reliable. Here are a few things I have thought of, any feedback would be very much appreciated.

Lets assume we are working with 2 doc types - states and cities. For this example, I am not actually working with states and cities.

So if I have a view called "get_cities_by_state_id", this should return all cities that are within a current state.

Updating Options:

1) Update the city document, and then immediately call the get_cities_by_state_id view specifying stale="false". So this will ensure the view is updated after the save is made. However, will this re-index the ENTIRE view or if I specify key=state_id will it ensure that any documents with the specified key are inserted.

2) Forget using views all together and just manage the relationships in states reference document - maybe called "state_cities". So anytime a city is added/deleted, update the array in the state_cities document. When we then go to look up all the cities that are in a state, we can look up the state_cities reference doc, get the array of city ids, then pass those Ids back into couch to pull all the city documents. My thinking is this will ensure that the data is current, however it is more complex and a bit of an administrative pain esp if the reference doc were to get out of sync.

I know I am not the first one to face this issue, but I really could not find a good write up on the best way to handle this. At the end of the day, if a city is added I need to be able to see that city when I pull up the state list immediately.


0 Answers

No answers yet