如果存储引擎是InnoDB,上面的值就是一个近似值,如果你需要确切的值,那你就不能信任它们。
另一个问题是它的性能,这些增强触发了分区修整吗?答案毫不含糊,是的。与MySQL 5.1有所不同,在5.1中日期分区只能与两个函数工作,在MySQL 5.5中,任何使用了COLUMNS关键字定义的分区都可以使用分区修整,下面还是测试一下吧。
select count(*) from employees where gender='F' and hire_date < '1990-01-01';
+----------+
| count(*) |
+----------+
| 66212 |
+----------+
1 row in set (0.05 sec)
explain partitions select count(*) from employees where gender='F' and hire_date < '1990-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employees
partitions: p01
type: ALL possible_keys:
NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300024
Extra: Using where
+----------+
| count(*) |
+----------+
| 66212 |
+----------+
1 row in set (0.05 sec)
explain partitions select count(*) from employees where gender='F' and hire_date < '1990-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employees
partitions: p01
type: ALL possible_keys:
NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300024
Extra: Using where
使用定义第一个分区的条件,我们获得了一个非常优化的查询,不仅如此,部分条件也将从分区修整中受益。
select count(*) from employees where gender='F';
+----------+
| count(*) | +----------+
| 120051 | +----------+
1 row in set (0.12 sec)
explain partitions select count(*) from employees where gender='F'\G
*************************** 1. row ***************************
id: 1 select_type: SIMPLE
table: employees
partitions: p01,p02,p03,p04
type: ALL possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300024
Extra: Using where
+----------+
| count(*) | +----------+
| 120051 | +----------+
1 row in set (0.12 sec)
explain partitions select count(*) from employees where gender='F'\G
*************************** 1. row ***************************
id: 1 select_type: SIMPLE
table: employees
partitions: p01,p02,p03,p04
type: ALL possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300024
Extra: Using where
它和复合索引的算法一样,如果你的条件指的是索引最左边的部分,MySQL将会使用它。与此类似,如果你的条件指的是分区定义最左边的部分,MySQL将会尽可能修整。它和复合索引一起出现,如果你只使用最右边的条件,分区修整不会工作。
select count(*) from employees where hire_date < '1990-01-01';
+----------+
| count(*) |
+----------+
| 164797 |
+----------+
1 row in set (0.18 sec)
explain partitions select count(*) from employees where hire_date < '1990-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employees
partitions:
p01,p02,p03,p04,p05,p06,p07
type: ALL possible_keys:
NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300024
Extra: Using where
+----------+
| count(*) |
+----------+
| 164797 |
+----------+
1 row in set (0.18 sec)
explain partitions select count(*) from employees where hire_date < '1990-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employees
partitions:
p01,p02,p03,p04,p05,p06,p07
type: ALL possible_keys:
NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300024
Extra: Using where
如果不用分区定义的第一部分,使用分区定义的第二部分,那么将会发生全表扫描,在设计分区和编写查询时要紧记这一条。