🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### list分区 list :条件值为一个数据列表。 算法 : ~~~ 根据"字段的内容值"是否在某个"列表"中进行分区,通过预定义的列表的值来对数据进行分割。 ~~~ 例: 假如你创建一个如下的一个表,该表保存有全国20家分公司的职员记录,这20家分公司的编号从1到20.而这20家分公司分布在全国4个区域,如下表所示: 职员表 : ~~~ +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(32) | YES | | NULL | | | store_id | int(11) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ ~~~ 分区信息 ~~~ 北部 1,4,5,6,17,18 南部 2,7,9,10,11,13 东部 3,12,19,20 西部 8,14,15,16 ~~~ 建表语法 : ~~~ create table p_list( id int, name varchar(32), store_id int )engine innodb charset utf8 partition by list (store_id)( //分区字段 store_id partition p_north values in (1,4,5,6,17,18), partition p_east values in(2,7,9,10,11,13), partition p_south values in(3,12,19,20), partition p_west values in(8,14,15,16) ); ~~~ 分区文件信息 ![](https://box.kancloud.cn/b4b1fdacb32905f1b5d8076f33531240_878x280.png) #### 查询测试 语法: ~~~ explain partitions select * from 表名 where 分区字段 = 条件\G ~~~ 结果: ~~~ id: 1 select_type: SIMPLE table: p_list partitions: p_east //使用到了分区 type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 1 filtered: 100.00 Extra: Using where ~~~ 不使用分区字段作为条件进行查询 ~~~ explain partitions select * from 表名 where 其他字段 = 条件\G ~~~ 结果 ~~~ id: 1 select_type: SIMPLE table: p_list partitions: p_north,p_east,p_south,p_west //查找了所有分区 type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3 filtered: 33.33 Extra: Using where ~~~ **注意**:在使用分区时,where后面的字段必须是分区字段,才能使用到分区.