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:

  1. Upgrade Home Assistant from 2024.12.x to any 2025.x version.
  2. Monitor the MySQL process list (SHOW PROCESSLIST;).
  3. Observe long-running SQL queries executed by Home Assistant.
  4. 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 recorder component.

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)

Most upvoted comments

Same for me, works perfect. MySql load is gone.

I think it’s a new query that was not-existant in the 2024 versions. Let’s hope the PR gets pulled in soon so I can update again 😃

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.

class _SchemaVersion48Migrator(_SchemaVersionMigrator, target_version=48):
    def _apply_update(self) -> None:
        """Version specific update method."""
        # https://github.com/home-assistant/core/issues/134002
        # If the system has unmigrated states rows, we need to
        # ensure they are migrated now so the new optimized
        # queries can be used. For most systems, this should
        # be very fast and nothing will be migrated.

Maybe super large databases are an issue?

Yes

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

SELECT anon_1.metadata_id, anon_1.state, anon_1.last_updated_ts, anon_1.last_changed_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.last_changed_ts AS last_changed_ts, anon_2.attributes AS attributes 
FROM (SELECT states.metadata_id AS metadata_id, states.state AS state, 0 AS last_updated_ts, 0 AS last_changed_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 < 1738534499.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 (124, 294, 313, 364, 22, 365, 78, 334, 336, 147, 19, 1529, 48, 321, 204, 1545, 1546, 1547, 1548, 1549, 1550, 1551, 1552, 1553, 1554, 1555, 1556, 1557, 1558, 1559, 1560, 1561, 1562, 1563, 1564, 1565, 1566, 1567, 1568, 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 1580, 1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1593, 1594, 1595, 1596, 393, 394, 55, 259, 277, 266, 119, 42, 280, 309, 589, 718, 748, 751, 752, 753, 756, 761, 762, 763, 764, 774, 775, 776, 785, 786, 787, 788, 789, 790, 791, 792, 793, 618, 620, 622, 624, 626, 628, 630, 632, 634, 636, 638, 640, 641, 1509, 1413, 1414, 1415, 1416, 1521, 1417, 1418, 1420, 1421, 1607, 1608, 1609, 1610)) 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.last_changed_ts AS last_changed_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, states.last_changed_ts AS last_changed_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.metadata_id IN (124, 294, 313, 364, 22, 365, 78, 334, 336, 147, 19, 1529, 48, 321, 204, 1545, 1546, 1547, 1548, 1549, 1550, 1551, 1552, 1553, 1554, 1555, 1556, 1557, 1558, 1559, 1560, 1561, 1562, 1563, 1564, 1565, 1566, 1567, 1568, 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 1580, 1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1593, 1594, 1595, 1596, 393, 394, 55, 259, 277, 266, 119, 42, 280, 309, 589, 718, 748, 751, 752, 753, 756, 761, 762, 763, 764, 774, 775, 776, 785, 786, 787, 788, 789, 790, 791, 792, 793, 618, 620, 622, 624, 626, 628, 630, 632, 634, 636, 638, 640, 641, 1509, 1413, 1414, 1415, 1416, 1521, 1417, 1418, 1420, 1421, 1607, 1608, 1609, 1610) AND states.last_updated_ts > 1738534499.999999e0 AND states.last_updated_ts < 1738534800.0e0) AS anon_3) AS anon_1 ORDER BY anon_1.metadata_id, anon_1.last_updated_ts

I’m on 2025.1.4. Let me know if you need any info from the logs