core: Home Assistant 2025.x Causes Long-Running SQL Queries on Percona/MySQL 8.0 (MariaDB not affected)
The problem
The problem
Home Assistant release with the issue: 2025.x (any version up to current)
Last working Home Assistant release (if known): 2024.12.x
Operating environment:
- Home Assistant: Running on a dedicated server
- Database: Percona Server 8.0 (dedicated server)
Integration causing issue: Recorder (assumed, due to SQL queries)
Description of the issue: Ever since upgrading to any version of Home Assistant 2025.x, my Percona 8.0 SQL server experiences excessively long-running SQL queries. These queries take 3+ minutes to execute and repeatedly appear in the process list.
Downgrading to Home Assistant 2024.12.x immediately resolves the issue.
Example of issue:
Running SHOW PROCESSLIST; reveals long-running SQL queries from Home Assistant:
mysql> show processlist;
+-------+-----------------+----------------+---------------+---------+--------+------------------------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+-----------------+----------------+---------------+---------+--------+------------------------+------------------------------------------------------------------------------------------------------+
| 34120 | homeassistant | 10.2.0.5:55988 | homeassistant | Query | 60 | executing | SELECT anon_1.metadata_id, anon_1.state, anon_1.last_updated_ts, anon_1.attributes FROM (SELECT ... |
+-------+-----------------+----------------+---------------+---------+--------+------------------------+------------------------------------------------------------------------------------------------------+
Running SELECT * FROM information_schema.processlist WHERE ID = 34120; shows:
SELECT anon_1.metadata_id, anon_1.state, anon_1.last_updated_ts, anon_1.attributes
FROM (SELECT anon_2.metadata_id AS metadata_id, anon_2.state AS state, anon_2.last_updated_ts AS last_updated_ts, anon_2.attributes AS attributes
FROM (SELECT states.metadata_id AS metadata_id, states.state AS state, 0 AS last_updated_ts, CASE WHEN (state_attributes.shared_attrs IS NULL) THEN states.attributes ELSE state_attributes.shared_attrs END AS attributes
FROM states_meta INNER JOIN states ON states.last_updated_ts = (SELECT states.last_updated_ts FROM states WHERE states_meta.metadata_id = states.metadata_id AND states.last_updated_ts < 1738503299.999999e0 ORDER BY states.last_updated_ts DESC LIMIT 1) AND states.metadata_id = states_meta.metadata_id LEFT OUTER JOIN state_attributes ON states.attributes_id = state_attributes.attributes_id
WHERE states_meta.metadata_id IN (...) ) AS anon_2 UNION ALL SELECT anon_3.metadata_id AS metadata_id, anon_3.state AS state, anon_3.last_updated_ts AS last_updated_ts, anon_3.attributes AS attributes FROM (SELECT states.metadata_id AS metadata_id, states.state AS state, states.last_updated_ts AS last_updated_ts, CASE WHEN (state_attributes.shared_attrs IS NULL) THEN states.attributes ELSE state_attributes.shared_attrs END AS attributes FROM states LEFT OUTER JOIN state_attributes ON states.attributes_id = state_attributes.attributes_id WHERE (states.last_changed_ts = states.last_updated_ts OR states.last_changed_ts IS NULL) AND states.metadata_id IN (...) AND states.last_updated_ts > 1738503299.999999e0 AND states.last_updated_ts < 1738503600.0e0) AS anon_3) AS anon_1 ORDER BY anon_1.metadata_id, anon_1.last_updated_ts;
These queries are executed repeatedly and cause database performance degradation.
Expected behavior: Home Assistant should execute SQL queries in a more optimized manner, avoiding excessive long-running queries.
Steps to reproduce:
- Upgrade Home Assistant from 2024.12.x to any 2025.x version.
- Monitor the MySQL process list (
SHOW PROCESSLIST;). - Observe long-running SQL queries executed by Home Assistant.
- Downgrade to 2024.12.x and verify that the issue no longer occurs.
Relevant logs:
- No specific logs appear in Home Assistant related to these queries, but MySQL logs show prolonged execution times.
Additional information:
- The database runs on a dedicated Percona 8.0 server.
- The issue started immediately after upgrading to Home Assistant 2025.x.
- The SQL queries involved appear related to retrieving historical state data, potentially from the
recordercomponent.
What version of Home Assistant Core has the issue?
2025.*
What was the last working version of Home Assistant Core?
2024.12.4
What type of installation are you running?
Home Assistant OS
Integration causing the issue
recorder
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 28 (11 by maintainers)
Same for me, works perfect. MySql load is gone.
hope so too and think you are right as i noticed that the database update to version 48 is referring to “new optimized queries” what may be the root cause for the issue we are impacted of. Specifically if these queries have not been optimized for database instances with sizes you would expect after a while of ha operation. purging the database for the sake of an upgrade might not be an option if historic data need to be kept. so fingers crossed that a db developer will get this issue resolved - may be also considering a rollback to the formerly used queries which seem to have worked perfectly fine.
apologies but i might have missed something here. in what respect is this an issue of large databases if the same size of database is doing perfectly fine with an earlier ha version? is the sql script causing this issue part of the upgrade to version 2025.2.x and keeps on running even after the upgrade has (as stated in the logs) been completed?
I think I have the same issue but on MySql. Today at 9 PM Home Assistant stopped recording new statistics. When I checked the database it shows a query such as the following running for 1400 seconds and counting
I’m on 2025.1.4. Let me know if you need any info from the logs