More Related Content Similar to ふくよかなモデル (20) ふくよかなモデル14. 育ってくModel
class User < ActiveRecord::Base
attr_accessible :name, :birthday, :job, :profile
validates :name, :birthday, :job, :profile presence: true
validates :name, uniqueness: true
def xxx
end
def yyy
end
def zzz
end
・
・
・
end
で、すくすく育った結果が・・・
44. class CreateUsersGroups < ActiveRecord::Migration
def change
create_table :users_groups do | t |
t.references :user
t.references :group
end
end
end
多対多
45. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+---------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+---------+----------+-----------+
| users_groups | InnoDB | 25000572 | 34 | 811.00 | 811.00 | 0.00 |
| users | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 9744 | 47 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+---------+----------+-----------+
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ
49. class CreateUsersGroups < ActiveRecord::Migration
def change
create_table :users_groups, :id => false do | t |
t.references :user
t.references :group
end
end
end
多対多
50. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+---------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+---------+----------+-----------+
| users_groups | InnoDB | 25000427 | 36 | 862.00 | 862.00 | 0.00 |
| users | InnoDB | 9062 | 50 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+---------+----------+-----------+
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ
60. クラスタインデックス
• テーブルに PRIMARY KEY がある場合、それをクラスタ
インデックスとする
• テーブルに PRIMARY KEY が無い場合、 NOT NULL カラ
ムだけを持つ最初のUNIQUE インデックス をクラスタ
インデックスとする
• テーブルに PRIMARY KEY も適切な UNIQUE インデック
スもない場合は、InnoDB の内部で、行 ID 値を含む合成
カラム上に隠しクラスタインデックスが生成される。
行 ID は、新しい行が挿入されると単調に増加する 6 バ
イトのフィールド
62. • テーブルに PRIMARY KEY がある場合、それをクラスタ
インデックスとする
• テーブルに PRIMARY KEY が無い場合、 NOT NULL カ
ラムだけを持つ最初のUNIQUE インデックス をクラス
タインデックスとする
• テーブルに PRIMARY KEY も適切な UNIQUE インデック
スもない場合は、InnoDB の内部で、行 ID 値を含む合成
カラム上に隠しクラスタインデックスが生成される。
行 ID は、新しい行が挿入されると単調に増加する 6 バ
イトのフィールド
クラスタインデックス
• テーブルに PRIMARY KEY がある場合、それをクラスタ
インデックスとする
• テーブルに PRIMARY KEY が無い場合、 NOT NULL カ
ラムだけを持つ最初のUNIQUE インデックス をクラス
タインデックスとする
• テーブルに PRIMARY KEY も適切な UNIQUE インデック
スもない場合は、InnoDB の内部で、行 ID 値を含む合成
カラム上に隠しクラスタインデックスが生成される。
行 ID は、新しい行が挿入されると単調に増加する 6 バ
イトのフィールド
66. class CreateUsersGroups < ActiveRecord::Migration
def change
create_table :users_groups, :id => false do | t |
t.references :user
t.references :group
end
add_index :users_groups, :user_id
add_index :users_groups, :group_id
end
end
多対多
67. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+----------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+----------+----------+-----------+
| users_groups | InnoDB | 25000427 | 36 | 1,870.00 | 862.00 | 1,008.00 |
| users | InnoDB | 9744 | 47 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+----------+----------+-----------+
4 rows in set (0.02 sec)
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ
69. • テーブルに PRIMARY KEY がある場合、それをクラスタ
インデックスとする
• テーブルに PRIMARY KEY が無い場合、 NOT NULL カ
ラムだけを持つ最初のUNIQUE インデックス をクラス
タインデックスとする
• テーブルに PRIMARY KEY も適切な UNIQUE インデック
スもない場合は、InnoDB の内部で、行 ID 値を含む合成
カラム上に隠しクラスタインデックスが生成される。
行 ID は、新しい行が挿入されると単調に増加する 6 バ
イトのフィールド
クラスタインデックス
• テーブルに PRIMARY KEY がある場合、それをクラスタ
インデックスとする
• テーブルに PRIMARY KEY が無い場合、 NOT NULL カ
ラムだけを持つ最初のUNIQUE インデックス をクラス
タインデックスとする
• テーブルに PRIMARY KEY も適切な UNIQUE インデック
スもない場合は、InnoDB の内部で、行 ID 値を含む合成
カラム上に隠しクラスタインデックスが生成される。
行 ID は、新しい行が挿入されると単調に増加する 6 バ
イトのフィールド
70. class CreateUsersGroups < ActiveRecord::Migration
def change
create_table :users_groups, :id => false do | t |
t.references :user, :null => false
t.references :group, :null => false
end
add_index :users_groups, [:user_id, :group_id], :unique => true
end
end
多対多
71. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+----------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+----------+----------+-----------+
| users_groups | InnoDB | 25000427 | 36 | 1,870.00 | 862.00 | 1,008.00 |
| users | InnoDB | 9744 | 47 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+----------+----------+-----------+
4 rows in set (0.02 sec)
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ
72. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+---------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+---------+----------+-----------+
| users_groups | InnoDB | 25000579 | 28 | 683.00 | 683.00 | 0.00 |
| users | InnoDB | 9744 | 47 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+---------+----------+-----------+
4 rows in set (0.09 sec)
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ
80. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+----------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+----------+----------+-----------+
| users_groups | InnoDB | 25000427 | 36 | 1,870.00 | 862.00 | 1,008.00 |
| users | InnoDB | 9744 | 47 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+----------+----------+-----------+
4 rows in set (0.02 sec)
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ
81. mysql> SELECT
-> table_name
-> , engine
-> , table_rows as tbl_rows
-> , avg_row_length as rlen
-> , FORMAT((data_length+index_length)/1024/1024, 2) as 'all(MB)' #総容量
-> , FORMAT((data_length)/1024/1024, 2) as 'data(MB)' #データ容量
-> , FORMAT((index_length)/1024/1024, 2) as 'index(MB)' #インデックス容量
-> FROM information_schema.tables
-> WHERE table_schema=database()
-> ORDER BY (data_length+index_length) DESC;
+-------------------+--------+----------+------+---------+----------+-----------+
| table_name | engine | tbl_rows | rlen | all(MB) | data(MB) | index(MB) |
+-------------------+--------+----------+------+---------+----------+-----------+
| users_groups | InnoDB | 25000579 | 28 | 1,008.88| 683.00 | 325.88 |
| users | InnoDB | 9744 | 47 | 0.44 | 0.44 | 0.00 |
| groups | InnoDB | 10396 | 44 | 0.44 | 0.44 | 0.00 |
| schema_migrations | InnoDB | 3 | 5461 | 0.02 | 0.02 | 0.00 |
+-------------------+--------+----------+------+---------+----------+-----------+
4 rows in set (0.09 sec)
多対多
平均
行サイズ
データ
サイズ
インデックス
サイズ
全体
のサイズ