Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Patch to add index length support

#1852

This patch add support for index length in MySQL adapter. Define a index length is a common practice for avoiding large indexes data, and improving performance.

You can now define a length for you indexes:

add_index(:accounts, :name, :name => 'by_name', :limit => 10) generates CREATE INDEX by_name ON accounts(name(10))

add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :limit => 10) generates CREATE INDEX by_name_surname ON accounts(name(10), surname(10))

add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :limit => {:name => 10, :surname => 20}) generates CREATE INDEX by_name_surname ON accounts(name(10), surname(20))

Reported by Emili Parreño · February 2nd, 2009 @ 10:55 PM

State: stale
Milestone: 2.3.10
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Medium

Activity

  1. Carlos Júnior (xjunior)
    Carlos Júnior (xjunior)

    Fine! but I would replace this:

    
    add_index(:accounts, [:name, :surname], :name => 'by_name_surname',
    :limit => {:name => 10, :surname => 20})
    

    by:

    
    add_index(:accounts, {:name => 10, :surname => 20}, :name =>
    'by_name_surname')
    

    which also cover the first situation (with a simple index):

    
    add_index(:accounts, :name => 10, :name => 'by_name')
    

    With this, I don't think that the :limit option is really needed.

    February 3rd, 2009 @ 12:58 AM

  2. Emili Parreño
    Emili Parreño

    Hash doesn't preserve order, which is critical when declaring indices. Using limit parameter, I wanted to keep the syntax of the migrations when you specify a limit for a field.

    February 3rd, 2009 @ 06:28 AM

  3. Deleted User
    Deleted User

    Very good idea!

    query_reviewer is always complaining about long indices in my apps, I guess this way I could solve this by defining shorter indices

    February 3rd, 2009 @ 11:42 AM

  4. Amos King
    Amos King

    This is a great idea. Anything that can help speed things up a little is wonderful.

    +1

    February 3rd, 2009 @ 03:34 PM

  5. José Valim
    José Valim

    I'm posting +2 since it solve two problems, but you should also check #734. :)

    Support to index length is really necessary. For example, let's suppose I want to slug a title. Since I don't want to index the whole title, I set it to 10 characters.

    Nowadays, I have to execute SQL inside migrations. This would fix it, so +1.

    But even if I say to myself "just add the index, forget the length", it wouldn't be possible because the title is probably bigger than the max index length in MySQL. This takes us to the second problem is solves:

    If I insert an index with length inside my migrations using execute, when dumping the schema it dumps the index but not the length. So when I'm applying db:test:clone it will raise an SQL error, saying that the column can not be indexed because it's bigger than the max index length.

    So +1 again.

    I'm just doing this "essay", because Pratik refused the other ticket, which I hope not to happen again. :)

    February 4th, 2009 @ 03:29 PM

  6. Emili Parreño
    Emili Parreño

    I updated the patch with schema dumper support, more tests and sqlite tests fixed.

    Thanks for your comments.

    February 4th, 2009 @ 11:23 PM

  7. Raul Murciano
  8. Deleted User
  9. Aitor García Rey
  10. Luismi Cavallé
  11. YoNoSoyTu
    YoNoSoyTu

    +1, works as described and test pass.

    February 7th, 2009 @ 03:53 PM

  12. porras
    porras

    +1, tests pass and create the indices as described

    February 7th, 2009 @ 03:54 PM

  13. Fernando Guillen
    Fernando Guillen

    Good implementation, very userfull. Althought too much mysql dependent.

    Please review the schema_statements.rb documentation between lines 262 and 278.

    I see things like:

    
          #  add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :limit => {:name => 10, :surname => 15})
          # generates
          #  CREATE INDEX by_name_surname ON accounts(name(10), surname(20))
          #  CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
    

    Regards

    February 7th, 2009 @ 04:42 PM

  14. Deleted User
    Deleted User

    +1 this will be most welcome

    February 11th, 2009 @ 02:54 PM

  15. Pratik
    Pratik
    • Assigned user set to Pratik

    March 12th, 2009 @ 04:57 PM

  16. Jonathan del Strother
    Jonathan del Strother

    Looks promising. However - am I right in thinking that mysql is the only database that handles index lengths?

    Correct me if I'm wrong, but pretty much everything else in schema_statements is database-agnostic, right? Would it be feasible to push any of this patch into the mysql adapter? Failing that, we probably ought to at least note that :limit will only be supported on mysql.

    March 17th, 2009 @ 08:09 PM

  17. José Valim
    José Valim

    Any chance this making into core now? :)

    March 21st, 2009 @ 11:41 AM

  18. Emili Parreño
    Emili Parreño

    @jonathan this patch is only for mysql adapter, and only modifies the schema if you works with this adapter

    March 26th, 2009 @ 12:18 PM

  19. Michael Koziarski
    Michael Koziarski
    • Milestone changed from 2.x to 2.3.4

    June 9th, 2009 @ 09:33 AM

  20. Emili Parreño
    Emili Parreño

    I published a plugin with this feature for use while we wait the inclusion in the core.

    http://github.com/eparreno/mysql_index_length/

    I have to revise the patch to add more tests. In a few days I'll send a new patch.

    June 26th, 2009 @ 09:12 AM

  21. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  22. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open
    • Assigned user changed from Pratik to Jeremy Kemper

    Emili, could you update a patch against latest master? I'd like to get this in, as well as a backport to 2-3-stable.

    April 22nd, 2010 @ 08:29 PM

  23. Emili Parreño
    Emili Parreño

    OK, I'll take a look this week.

    April 26th, 2010 @ 09:00 PM

  24. Repository
    Repository
    • State changed from open to resolved

    (from [3616141fa2d2f35675d5962a1b329c8c51a5e9a3]) Add index length support for MySQL [#1852 state:resolved]

    Example:

    add_index(:accounts, :name, :name => 'by_name', :length => 10) => CREATE INDEX by_name ON accounts(name(10))

    add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15}) => CREATE INDEX by_name_surname ON accounts(name(10), surname(15))

    Signed-off-by: Pratik Naik pratiknaik@gmail.com
    http://github.com/rails/rails/commit/3616141fa2d2f35675d5962a1b329c...

    May 8th, 2010 @ 12:43 PM

  25. Repository
    Repository

    (from [5b95730edc33ee97f53da26a3868eb983305a771]) Add index length support for MySQL [#1852 state:resolved]

    Example:

    add_index(:accounts, :name, :name => 'by_name', :length => 10) => CREATE INDEX by_name ON accounts(name(10))

    add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15}) => CREATE INDEX by_name_surname ON accounts(name(10), surname(15))

    Signed-off-by: Pratik Naik pratiknaik@gmail.com
    http://github.com/rails/rails/commit/5b95730edc33ee97f53da26a3868eb...

    May 8th, 2010 @ 12:43 PM

  26. Repository
    Repository
    • State changed from resolved to open

    (from [6626833db13a69786f9f6cd56b9f53c4017c3e39]) Revert "Add index length support for MySQL [#1852 state:open]"

    This commit breaks dumping a few tables, as the sessions table.
    To reproduce, just create a new application and:

    rake db:sessions:create rake db:migrate rake db:test:prepare

    And then look at the db/schema.rb file (ht: Sam Ruby).

    This reverts commit 5b95730edc33ee97f53da26a3868eb983305a771.
    http://github.com/rails/rails/commit/6626833db13a69786f9f6cd56b9f53...

    May 8th, 2010 @ 03:48 PM

  27. Repository
    Repository
    • State changed from open to resolved

    (from [eababa35cf5917c4ebd3f48cb29b9a2f6d1db404]) Revert "Add index length support for MySQL [#1852 state:resolved]" (breaks the build)

    This reverts commit 3616141fa2d2f35675d5962a1b329c8c51a5e9a3.
    http://github.com/rails/rails/commit/eababa35cf5917c4ebd3f48cb29b9a...

    May 8th, 2010 @ 09:56 PM

  28. José Valim
    José Valim
    • State changed from resolved to open

    May 8th, 2010 @ 10:11 PM

  29. Repository
    Repository
    • State changed from open to resolved

    (from [77adb4bc2019e3a5b64ae8678310d176e30834d0]) Revert "Revert "Add index length support for MySQL [#1852 state:resolved]" (breaks the build)"

    This reverts commit eababa35cf5917c4ebd3f48cb29b9a2f6d1db404.
    http://github.com/rails/rails/commit/77adb4bc2019e3a5b64ae8678310d1...

    May 9th, 2010 @ 12:50 PM

  30. Repository
    Repository
    • State changed from resolved to open

    (from [8d2f6c16e381f5fff6d3f24f5c73a443577a1488]) Revert "Revert "Add index length support for MySQL [#1852 state:open]""

    This reverts commit 6626833db13a69786f9f6cd56b9f53c4017c3e39.
    http://github.com/rails/rails/commit/8d2f6c16e381f5fff6d3f24f5c73a4...

    May 9th, 2010 @ 12:50 PM

  31. Emili Parreño
    Emili Parreño

    wooooow! so , now what's exactly the situation? I tried length indexes in 2-3-stable vanilla app and seems to work properly (I see you've changed :limit for :length)

    May 10th, 2010 @ 07:57 AM

  32. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, migrations, patch, schema to activerecord, bugmash, migrations, patch, schema

    May 16th, 2010 @ 02:41 AM

  33. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.6 to 2.3.7

    May 23rd, 2010 @ 05:54 PM

  34. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.7 to 2.3.8

    May 24th, 2010 @ 09:40 AM

  35. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.8 to 2.3.9

    May 25th, 2010 @ 11:45 PM

  36. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10
    • Importance changed from to Medium

    August 30th, 2010 @ 02:28 AM

  37. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 5th, 2011 @ 12:00 AM

  38. rails
    rails
    • State changed from open to stale

    March 5th, 2011 @ 12:00 AM