changeset 911bcfcb8e90 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=911bcfcb8e90
description:
Add index for target field of Many2Many
Any nested clause makes a join using the target column.
issue11832
review447391003
diffstat:
trytond/model/modelsql.py | 68 ++++++++++++++++++++++++++--------------------
1 files changed, 38 insertions(+), 30 deletions(-)
diffs (84 lines):
diff -r 05b84cf664ba -r 911bcfcb8e90 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Thu Oct 27 13:24:22 2022 +0200
+++ b/trytond/model/modelsql.py Fri Oct 28 00:45:21 2022 +0200
@@ -289,42 +289,50 @@
# Define Range index to optimise with reduce_ids
for field in cls._fields.values():
- if isinstance(field, fields.One2Many) and field.field:
+ field_names = set()
+ if isinstance(field, fields.One2Many):
Target = field.get_target()
- field_name = field.field
- elif isinstance(field, fields.Many2Many) and field.origin:
+ if field.field:
+ field_names.add(field.field)
+ elif isinstance(field, fields.Many2Many):
Target = field.get_relation()
- field_name = field.origin
+ if field.origin:
+ field_names.add(field.origin)
+ if field.target:
+ field_names.add(field.target)
else:
continue
- if field_name == 'id':
- continue
- target_field = getattr(Target, field_name)
- if (issubclass(Target, ModelSQL)
- and not callable(Target.table_query)
- and not hasattr(target_field, 'set')):
- target = Target.__table__()
- column = Column(target, field_name)
- if not target_field.required and Target != cls:
- where = column != Null
- else:
- where = None
- if target_field._type == 'reference':
- Target._sql_indexes.update({
+ field_names.discard('id')
+ for field_name in field_names:
+ target_field = getattr(Target, field_name)
+ if (issubclass(Target, ModelSQL)
+ and not callable(Target.table_query)
+ and not hasattr(target_field, 'set')):
+ target = Target.__table__()
+ column = Column(target, field_name)
+ if not target_field.required and Target != cls:
+ where = column != Null
+ else:
+ where = None
+ if target_field._type == 'reference':
+ Target._sql_indexes.update({
+ Index(
+ target,
+ (column, Index.Equality()),
+ where=where),
+ Index(
+ target,
+ (column, Index.Similarity(begin=True)),
+ (target_field.sql_id(column, Target),
+ Index.Range()),
+ where=where),
+ })
+ else:
+ Target._sql_indexes.add(
Index(
target,
- (column, Index.Equality()),
- where=where),
- Index(
- target,
- (column, Index.Similarity(begin=True)),
- (target_field.sql_id(column, Target),
- Index.Range()),
- where=where),
- })
- else:
- Target._sql_indexes.add(
- Index(target, (column, Index.Range()), where=where))
+ (column, Index.Range()),
+ where=where))
@classmethod
def __table__(cls):