-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
Avoid creating multiple sqlalchemy sessions in a single history call #35721
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
Avoid creating multiple sqlalchemy sessions in a single history call #35721
Conversation
50ab34d
to
4c9e3c6
Compare
Nice! For a separate pull, maybe keeping a pool of sessions that are delay-closed. So that when a request comes in, there is always a free session to use. Would reduce latency of the call. |
I believe that is already the default for mysql/pgsql/etc. sqlite is defaulting to NullPool but should be able to use QueuePool if compiled with threadsafety. All the open/close is probably another thing that eats SD cards for lunch. |
Done in #35754 |
The history api was creating a job to fetch the states and another job to convert the states to json. This can be done in a single job which decreases the overhead of the operation.
query. Most queries created three sqlalchemy sessions which was especially slow with sqlite since it opens and closes the database. In testing the UI is noticeably faster at generating history graphs for entites.
bc655a7
to
92d6ac0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my perspective this looks good. A tip for reviewer, you can skip whitespace changes in github as well via the gear ⚙️button on the top of the review page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very familiar with our history integration, but this looks good.
Marking it as breaking change so we mention it in release notes for any custom integrations that might use this. |
Note: this is easier to review with
git diff -w upstream/dev -- homeassistant/components/history
Breaking change
The
history
functionstates_to_json
is now a protected function_states_to_json
and is not expected to be called from outside the module.Not sure if this is a breaking change since there were no outside callers in the codebase.
Proposed change
Ensure there is only one sqlalchemy session created per history query.
Functionality should be exactly the same otherwise as the changes
are rearranging and breaking apart functions only to avoid the multiple
sessions.
Most history queries created three sqlalchemy sessions which was
especially slow with sqlite since it opens and closes the
database.
In testing the UI is noticeably faster at generating history
graphs for entities.
Requires #35716
Testing:
strace
home assistant and verified the sqlite database was only opened and closed once per history api request instead of 3 times.Type of change
Example entry for
configuration.yaml
:# Example configuration.yaml
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.The integration reached or maintains the following Integration Quality Scale: