mysql 组合索引 长度限制

Posted by wxianfeng Tue, 16 Jun 2009 20:11:00 GMT

环境:ubuntu 10.10 + mysql 5.1 + rails 3.0.3 + ruby 1.9.2

今天创建组合索引时,遇到下面的错误:

Mysql2::Error: Specified key was too long; max key length is 1000 bytes: CREATE UNIQUE INDEX `unique_users_name_password` ON `users` (`name`, `password`)

问题重现:

表结构:(数据库,表,字段都是utf-8 编码,myisam引擎)

users:
name: varchar(255)
password: varchar(255)

Rails创建索引:

add_index :users , [:name,:password] , :unique=>true , :name=>"unique_users_name_password" # mysql 唯一索引可以指定多个字段

执行就 Error 了 ,

Google 后 ,得到是 myisam 表组合索引 有长度限制 , 总长度不能 超过 1000 bytes

计算下上面的 总长度(bytes)
utf8 编码 : 1 char = 3 bytes

255 * 3 + 255 * 3 =  1530 bytes

果然大于1000 了 ,修改 name 的字段长度:

change_column :users ,:name , :string , :limit => 100

再建索引 就 ok了……

那么 表改为 innodb 引擎呢 ,不error 了 ,但是 会抱下面的warning

warning:Specified key was too long;max key length is 767 bytes.

why?

原来 innodb表的组合索引的长度 是 767 bytes , 比 myisam 的还少 ,但是 innodb的可以顺利 建索引,超过767长度的部分 建不被索引,前面的取 前缀索引 ,这里 要注意!

建议;
1,表 设置为 innodb
2,建字段时,一定要根据实际情况,限制长度,在 rails migration中就是 加 :limit 参数

see:
http://blog.fesite.com/2009/02/09/mysql-specified-key-was-too-long-max-key-length-is-1000-bytes/
http://samyubw.blog.51cto.com/978243/223773


netbeans debug rails 3.0

Posted by wxianfeng Sun, 31 May 2009 22:09:00 GMT

环境:ruby 1.9.2 + rails 3.0.3 + nb 6.9.1 + ubuntu 10.10

nb debug rails 3.0 还没有跟上,不可以debug rails 3.0项目了,网上也有很多人遇到,可能出现下面这个异常:

Uncaught exception: no such file to load -- script/rails

我有一个解决办法,把你要debug的项目path直接写进去,

需要用到的gem:

ruby-debug-base19 (0.11.24)
ruby-debug-ide (0.4.9)
linecache19 (0.5.11)

如果是debug test file 的话,还需要 test-unit

wxianfeng@ubuntu:~$ gem list ruby-

*** LOCAL GEMS ***

ruby-debug-base19 (0.11.24)
ruby-debug-ide (0.4.9)
wxianfeng@ubuntu:~$ gem list line

*** LOCAL GEMS ***

linecache19 (0.5.11)
wxianfeng@ubuntu:~$ gem list test

*** LOCAL GEMS ***

test-unit (2.1.2)

当前rvm环境;

wxianfeng@ubuntu:~$ rvm info

ruby-1.9.2-p0:

  system:
    uname:       "Linux ubuntu 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686 GNU/Linux"
    bash:        "/bin/bash => GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)"
    zsh:         " => not installed"

  rvm:
    version:      "rvm 1.1.0 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]"

  ruby:
    interpreter:  "ruby"
    version:      "1.9.2p0"
    date:         "2010-08-18"
    platform:     "i686-linux"
    patchlevel:   "2010-08-18 revision 29036"
    full_version: "ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]"

  homes:
    gem:          "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0"
    ruby:         "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0"

  binaries:
    ruby:         "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"
    irb:          "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/irb"
    gem:          "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem"
    rake:         "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin/rake"

  environment:
    PATH:         "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global/bin:/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/system/mysql/bin:/usr/local/system/jdk1.6.0_22/bin"
    GEM_HOME:     "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0"
    GEM_PATH:     "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global"
    MY_RUBY_HOME: "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0"
    IRBRC:        "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

注意把netbeans的 gem_home , gem_path 设置为 当前 rvm 对应的 gem_home , gem_path , 设置gem_path 时 nb 默认 .rvm 目录不显示,需要直接在 对话框中输入路径

最后修改

/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/gems/ruby-debug-ide-0.4.9/lib/ruby-debug-ide.rb

大概 110 行

      path = "/usr/local/system/projects/entos/ent_os/script/rails"
      bt = debug_load(path, options.stop, options.load_mode)
      #      bt = debug_load(Debugger::PROG_SCRIPT, options.stop, options.load_mode)

注释一行,添加两行

把你需要调试的项目 set as main project ,然后 debug main project !

ok~!

如果你需要debug 单个ruby 文件 , 那么 需要改回来 , 这里注意.

see:
http://forums.netbeans.org/topic31103.html