Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create indexes inline in CREATE TABLE for MySQL #14480

merged 2 commits into from Mar 26, 2014


Copy link

commented Mar 26, 2014

When using mysql or mysql2 adapters, fold index creation into CREATE TABLE statements. This has a few benefits:

  • requires fewer SQL statements to create a table
  • can reverse a table-creating migration with a single DROP TABLE statement, instead of unnecessarily removing all indexes and then dropping (big performance penalty for large tables)
  • requires having only CREATE permissions on a database, instead of CREATE + INDEX permissions
  • prevents early committing of transactions when adding indexes to temporary tables

This is intended as a replacement for @ccutrer's #13787. It fixes some bugs and test failures with that PR and updates tests.

I am happy to squash the commits together, however I left as-is to avoid crushing @ccutrer's authorship.

ccutrer and others added 2 commits Jan 21, 2014
create indexes inline in CREATE TABLE for MySQL
This is important, because adding an index on a temporary table after
it has been created would commit the transaction

Fixes bugs for using indexes in CREATE TABLE by adding checks for tab…
…le existence

  - updates tests by stubbing table_exists? method
  - adds entry for creating indexes in CREATE TABLE to changelog

@rafaelfranca rafaelfranca added this to the 4.2.0 milestone Mar 26, 2014

@rafaelfranca rafaelfranca self-assigned this Mar 26, 2014


This comment has been minimized.

Copy link

commented Mar 26, 2014

Could you check if more adapters included in Active Record support this too and implement?


This comment has been minimized.

Copy link
Contributor Author

commented Mar 26, 2014

PostgreSQL does not support indexes in CREATE (docs) and neither does SQLite (docs).
The exceptions are UNIQUE, PRIMARY, EXCLUDE, and FOREIGN KEY constraints since these are column-level attributes, but correspondingly they need to be handled differently (with a table's columns rather than its indexes). I think it would be unwieldy to try and scope the create_index statement to figure out those particular cases with certain adapters and instead the use of unique: true syntax on the column should be required.


This comment has been minimized.

Copy link

commented Mar 26, 2014

Thank you for the research. I'll take a look in your patch today.

rafaelfranca added a commit that referenced this pull request Mar 26, 2014
Merge pull request #14480 from steverice/mysql-indexes-in-create-table
Create indexes inline in CREATE TABLE for MySQL

@rafaelfranca rafaelfranca merged commit 63c94ef into rails:master Mar 26, 2014

1 check passed

default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.