Search code examples
sqlperldbix-class

How can I specify index value order when calling SQL::Translator::Table's add_index function?


I am using DBIx::Class to create tables and their indexes and so calling SQL::Translator::Table add_index() but I'd like to be able to specify a value order on some of the columns, like this:

CREATE INDEX myindex ON mytable (
    username ASC,
    created  DESC
);

Is it possible to supply the 'ASC' and 'DESC' modifiers to add_table()?

If not, I suspect that this SQL would be inefficient:

SELECT text FROM mytable WHERE username = ? ORDER BY created DESC LIMIT 20;

Any suggested work-around to reduce database overhead in this case? (I'm using MySql 5.x


Solution

  • No, currently SQL::Translator::Producer::MySQL (and even DBIx::Class itself I think) do not provide a way to do this.

    However, according to the MySQL manual, these index collations are currently ignored:

    An index_col_name specification can end with ASC or DESC. These keywords are permitted for future extensions for specifying ascending or descending index value storage. Currently, they are parsed but ignored; index values are always stored in ascending order.