Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions oceanbase-sqlalchemy-plugin/oceanbase_sqlalchemy/cx_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,62 @@ def _constraint_query(self, owner):
local.c.position,
)
)

@lru_cache()
def _index_query(self, owner):
"""
Override _index_query to optimize performance in OceanBase Oracle mode.

Key optimization: Add filtering on all_ind_columns.table_name to reduce
the amount of data scanned early in the query execution.

Only for sqlalchemy 2.x compatibility.
"""
return (
select(
dictionary.all_ind_columns.c.table_name,
dictionary.all_ind_columns.c.index_name,
dictionary.all_ind_columns.c.column_name,
dictionary.all_indexes.c.index_type,
dictionary.all_indexes.c.uniqueness,
dictionary.all_indexes.c.compression,
dictionary.all_indexes.c.prefix_length,
dictionary.all_ind_columns.c.descend,
dictionary.all_ind_expressions.c.column_expression,
)
.select_from(dictionary.all_ind_columns)
.join(
dictionary.all_indexes,
and_(
dictionary.all_ind_columns.c.index_name
== dictionary.all_indexes.c.index_name,
dictionary.all_ind_columns.c.index_owner
== dictionary.all_indexes.c.owner,
),
)
.outerjoin(
dictionary.all_ind_expressions,
and_(
dictionary.all_ind_expressions.c.index_name
== dictionary.all_ind_columns.c.index_name,
dictionary.all_ind_expressions.c.index_owner
== dictionary.all_ind_columns.c.index_owner,
dictionary.all_ind_expressions.c.column_position
== dictionary.all_ind_columns.c.column_position,
),
)
.where(
dictionary.all_indexes.c.table_owner == owner,
dictionary.all_indexes.c.table_name.in_(bindparam("all_objects")),
# Key optimization: add this condition to reduce all_ind_columns scan
dictionary.all_ind_columns.c.table_name.in_(bindparam("all_objects")),
)
.order_by(
dictionary.all_ind_columns.c.index_name,
dictionary.all_ind_columns.c.column_position,
)
)


# Register dialect, similar to SQLAlchemy's cx_oracle.py
dialect = OceanBaseDialect_cx_oracle
Loading