-
-
Notifications
You must be signed in to change notification settings - Fork 727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading from several open databases at once #548
Comments
Hello @spicymatt It's difficult to answer your question, because it contains an inconsistency: First you describe your initial approach by telling you modify db2:
Then you describe your second approach, which only reads. This is important, because in order to address performance issues with SQLite, one need to know exactly when reads and write happens (when and how often the Can you please clarify? |
Ooops. Sorry for the inconsistency. I modified the original post.
So, the difference between the 2 approaches is :
Does it make sense ? |
hmmm, it seems that a somewhat similar question was answered here : access to 2 databases queues at the same time. |
All right @spicymatt, thanks for the clarification. Accessing two databases at the same time is supported: #55 has been solved three years ago). Your first code snippet is correct:
I do not know where your performance issue is. I suggest you profile your app with Instruments in order to find out. Now I want to address this concern of yours:
GRDB generally keeps SQLite connections alive, for the lifetime of a DatabaseQueue or DatabasePool object. Precisely speaking, GRDB may close some connections when you call the But it's best to assume that GRDB connections are long-lived. DatabasePool, in particular, maintains a pool of several SQLite connections, and uses or reuses them each time you invoke the
Now, if your app creates many instances of DatabasePool, GRDB will have to close and create at least as many SQLite connections. Creating several instances of DatabasePool or DatabaseQueue, for accessing a single database file, is a very bad practice. It goes against the Rule number 1 of GRDB concurrency. The Rule number 1 says:
See the Concurrency Guide for more information. It's OK to have two DatabasePools, but if and only if they do not access the same file. If you have a single database file, use one and only one DatabasePool for the whole duration of your database usage (usually, create it when your app starts, and forget it). |
hello again, and thanks for the detailed answer regarding GRDB connections. I am not breaking rule #1 : I do access 2 different database files using 2 DatabasePools. ;-) |
( First of all, congrats on GRDB and the amazing work you put into it !)
What did you do?
I am loading data in memory that comes from several GRDB DatabasePools that I need to maintain open at once.
I wanted to double check on the correct approach and am concerned that I might miss something important while doing this.
Currently my code looks something like this
Is this nesting of database reads the correct way of doing this ?
I moved to this approach because my initial approach - that was somewhat nicer to read - was hitting performance :
Can you make recommendation on the proper way to work with several databases at the same time and avoiding the costs of opening/closing connections.
Thanks
Matthieu
Environment
GRDB flavor(s): GRDB
GRDB version: latest
Installation method: manual
Xcode version: 10.2.1
Swift version: 4.2
Platform(s) running GRDB: macOS
macOS version running Xcode: Mojave
The text was updated successfully, but these errors were encountered: