MySQL 提供了一个数据页合并临界值(MERGE_THRESHOLD),在某些场景下,可以人为介入,减少数据页的合并与分裂。
在 InnoDB 表里,每个数据页默认16K 大小,默认 MERGE_THRESHOLD 值为 50,取值范围从 1 到 50,默认值即是最大值。也就是当页面记录数占比小于 50% 时,MySQL 会把这页和相邻的页面进行合并,保证数据页的紧凑,避免太多浪费。
用视图呗..用表得用触发器.或者存储...
create table student_class
(
id int(4) not null auto_increment,
name varchar(20) not null,
class varchar(20) not null,
years int(4) not null,
score double not null,
primary key (id)
)
engine = innodb default charset = utf8
insert into student_class values (null,'张三','高三一班',2012,180.00)
insert into student_class values (null,'张三','高三一班',2013,88.00)
insert into student_class values (null,'张三','高三一班',2014,181.00)
insert into student_class values (null,'李四','高三一班',2012,102.00)
insert into student_class values (null,'李四','高三一班',2013,183.00)
insert into student_class values (null,'李四','高三一班',2014,184.00)
insert into student_class values (null,'王五','高三二班',2012,185.00)
insert into student_class values (null,'王五','高三二班',2013,186.00)
insert into student_class values (null,'王五','高三二班',2014,181.00)
insert into student_class values (null,'赵六','高三二班',2012,183.00)
insert into student_class values (null,'赵六','高三二班',2013,184.00)
insert into student_class values (null,'赵六','高三二班',2014,185.00)
create view student_years as select class,years,sum(score),avg(score) from student_class group by class,years
mysql>select * from student_years
+--------------+-------+------------+------------+
| class| years | sum(score) | avg(score) |
+--------------+-------+------------+------------+
| 高三一班 | 2012 |282 |141 |
| 高三一班 | 2013 |271 | 135.5 |
| 高三一班 | 2014 |365 | 182.5 |
| 高三二班 | 2012 |368 |184 |
| 高三二班 | 2013 |370 |185 |
| 高三二班 | 2014 |366 |183 |
+--------------+-------+------------+------------+
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)