From a719345f7b08e363880bfa62fc9f6b63fcd334e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 31 Mar 2026 12:02:51 +0200 Subject: [PATCH] Add index for target column of xxx2Many field on history table Closes #14725 --- trytond/trytond/model/modelsql.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/trytond/trytond/model/modelsql.py b/trytond/trytond/model/modelsql.py index ae303e4434..684fd634b9 100644 --- a/trytond/trytond/model/modelsql.py +++ b/trytond/trytond/model/modelsql.py @@ -408,6 +408,9 @@ class ModelSQL(ModelStorage): continue table = cls.__table__() column = Column(table, field_name) + if cls._history: + history_table = cls.__table_history__() + history_column = Column(history_table, field_name) if not field.required and cls != Target: where = column != Null else: @@ -424,12 +427,32 @@ class ModelSQL(ModelStorage): (field.sql_id(column, Target), Index.Range()), where=where), }) + if cls._history: + cls._history_sql_indexes.update({ + Index( + history_table, + (history_column, Index.Equality()), + where=where), + Index( + history_table, + (history_column, + Index.Similarity(begin=True)), + (field.sql_id(history_column, Target), + Index.Range()), + where=history_column != Null), + }) else: cls._sql_indexes.add( Index( table, (column, Index.Range()), where=where)) + if cls._history: + cls._history_sql_indexes.add( + Index( + history_table, + (history_column, Index.Range()), + where=history_column != Null)) break @classmethod