mysql table partitioning in SA

699 views
Skip to first unread message

Michal Nowikowski

unread,
Jul 26, 2013, 7:00:28 AM7/26/13
to sqlal...@googlegroups.com
Hello,

MySQL supports partitioning:
http://dev.mysql.com/doc/refman/5.5/en/partitioning-overview.html
It requires special CREATE TABLE statement.

Is it possible to make use of this partitioning in SA?
How to do this?

Regards,
Godfryd

Michael Bayer

unread,
Jul 26, 2013, 10:31:54 AM7/26/13
to sqlal...@googlegroups.com
these keywords aren't built into the mysql options at the moment so here is a @compiles recipe:

from sqlalchemy import *
from sqlalchemy.schema import CreateTable
from sqlalchemy.ext.compiler import compiles

@compiles(CreateTable, "mysql")
def add_partition_scheme(element, compiler, **kw):
    table = element.element
    partition_by = table.kwargs.pop("mysql_partition_by", None)
    partitions = table.kwargs.pop("mysql_partitions", None)

    ddl = compiler.visit_create_table(element, **kw)
    ddl = ddl.rstrip()

    if partition_by:
        ddl += "\nPARTITION BY %s" % partition_by
        table.kwargs['mysql_partition_by'] = partition_by
    if partitions:
        ddl += "\nPARTITIONS %s" % partitions
        table.kwargs['mysql_partitions'] = partitions

    return ddl

m = MetaData()

t = Table('ti', m,
            Column('id', Integer),
            Column('amount', DECIMAL(7, 2)),
            Column('tr_date', Date),
            mysql_engine='InnoDB',
            mysql_partition_by='HASH( MONTH(tr_date) )',
            mysql_partitions='6'
            )

e = create_engine("mysql://scott:tiger@localhost/test", echo=True)
t.create(e)



--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michal Nowikowski

unread,
Jul 30, 2013, 6:26:16 AM7/30/13
to sqlal...@googlegroups.com
It works!

Thanks,
Godfryd
Reply all
Reply to author
Forward
0 new messages