sqlalchemy_singlestoredb.SortKey¶
- class sqlalchemy_singlestoredb.SortKey(*columns: str | Tuple[str, str])¶
SingleStore SORT KEY DDL element.
Represents a SORT KEY constraint for optimizing query performance by pre-sorting data within partitions. Properly chosen sort keys can significantly improve query performance for range scans and ordered retrieval.
- Parameters:
*columns (Union[str, Tuple[str, str]]) – Variable number of column specifications. Each can be: - A string (column name, defaults to ASC) - A tuple of (column_name, direction) where direction is ‘ASC’ or ‘DESC’
Examples
Single column sort key (ascending by default):
>>> SortKey('created_at')
Multi-column sort key with mixed directions:
>>> SortKey('user_id', ('created_at', 'DESC'))
Using static helper methods:
>>> SortKey(SortKey.asc('user_id'), SortKey.desc('created_at'))
Table Usage:
from sqlalchemy import Column, DateTime, Integer, MetaData, Table from sqlalchemy_singlestoredb import SortKey metadata = MetaData() events = Table( 'events', metadata, Column('event_id', Integer, primary_key=True), Column('created_at', DateTime), singlestoredb_sort_key=SortKey('created_at'), ) # Multi-column sort key with directions orders = Table( 'orders', metadata, Column('user_id', Integer), Column('created_at', DateTime), Column('order_id', Integer), singlestoredb_sort_key=SortKey('user_id', ('created_at', 'DESC')), )
ORM Usage:
from sqlalchemy import Column, DateTime, Integer from sqlalchemy.orm import declarative_base from sqlalchemy_singlestoredb import SortKey Base = declarative_base() class Event(Base): __tablename__ = 'events' event_id = Column(Integer, primary_key=True) created_at = Column(DateTime) __table_args__ = { 'singlestoredb_sort_key': SortKey('created_at'), } class Order(Base): __tablename__ = 'orders' user_id = Column(Integer, primary_key=True) order_id = Column(Integer) created_at = Column(DateTime) __table_args__ = { 'singlestoredb_sort_key': SortKey('user_id', ('created_at', 'DESC')), }
Methods
__init__(*columns)against(target)Return a copy of this
_schema.ExecutableDDLElementwhich will include the given target.asc(column)Create an ascending sort key column specification.
compare(other, **kw)Compare this
_expression.ClauseElementto the given_expression.ClauseElement.compile([bind, dialect])Compile this SQL expression.
desc(column)Create a descending sort key column specification.
execute_if([dialect, callable_, state])Return a callable that will execute this
_ddl.ExecutableDDLElementconditionally within an event handler.execution_options(**kw)Set non-SQL options for the statement which take effect during execution.
get_children(*[, omit_attrs])Return immediate child
visitors.HasTraverseInternalselements of thisvisitors.HasTraverseInternals.get_execution_options()Get the non-SQL options which will take effect during execution.
memoized_instancemethod(fn)Decorate a method memoize its return value.
options(*options)Apply options to this statement.
params([_ClauseElement__optionaldict])Return a copy with
_expression.bindparam()elements replaced.self_group([against])Apply a 'grouping' to this
_expression.ClauseElement.unique_params([_ClauseElement__optionaldict])Return a copy with
_expression.bindparam()elements replaced.Attributes
allows_lambdadescriptionentity_namespaceinherit_cacheIndicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.is_clause_elementis_deleteis_dmlis_from_statementis_insertis_selectis_selectableis_textis_updatestringify_dialectsupports_executiontargetuses_inspectionnegation_clauseproxy_set