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')),
    }
__init__(*columns: str | Tuple[str, str]) None

Methods

__init__(*columns)

against(target)

Return a copy of this _schema.ExecutableDDLElement which will include the given target.

asc(column)

Create an ascending sort key column specification.

compare(other, **kw)

Compare this _expression.ClauseElement to 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.ExecutableDDLElement conditionally 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.HasTraverseInternals elements of this visitors.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_lambda

description

entity_namespace

inherit_cache

Indicate if this HasCacheKey instance should make use of the cache key generation scheme used by its immediate superclass.

is_clause_element

is_delete

is_dml

is_from_statement

is_insert

is_select

is_selectable

is_text

is_update

stringify_dialect

supports_execution

target

uses_inspection

negation_clause

proxy_set