Android Room concurrent reads with Coroutines

Viewed 632

I am using Room with coroutines to store/retrieve data. From another post I saw that you could technically have unlimited number of retrievals from the database at the same time. Currently, I am working with a very large dataset that I want to retrieve. This is on the order of 10,000,000 rows. I split this up into smaller reads say by 4 (2,500,000), that all occur in parallel. This decreases my retrieval time by 30-40% which is what I want (this is relative to a single DB read). What bothers me is that when I increase this split value past 4, it actually slows down. I would think adding more parallel coroutines would keep splitting the time down.

I verified that all coroutines that are working in parallel are all unique threads so they are not clashing that way.

Does anyone have any idea why this is happening? I was thinking maybe Room/SQL has a limit of the number of concurrent connections, however this should not be the case based on (5) in this link:

SQlite Frequently Asked Questions

Any help would be appreciated!

1 Answers

You misunderstand the coroutines concept a little. Coroutines are called light-weight threads, actually the number of used threads depends on the selected Dispatcher. For example the maximum number of threads of the Default dispatcher is equal to the number of CPU cores, but is at least two. It would be wasteful to create a separate thread for each coroutine and it contradicts one of the main ideas of coroutines - app threads consumption optimization. See Coroutine context and dispatchers for more details.

Related