分类: Mysql/postgreSQL
2014-03-23 13:49:51
DAY()
DAYOFMONTH()
DAYOFWEEK()
DAYOFYEAR()
DATEDIFF()
EXTRACT()
HOUR()
MICROSECOND()
MINUTE()
MOD()
MONTH()
QUARTER()
SECOND()
TIME_TO_SEC()
TO_DAYS()
WEEKDAY()
YEAR()
YEARWEEK() 等
当然,还有FLOOR(),CEILING() 等,前提是使用这两个分区函数的分区健必须是整型。
按星期分区的表 WEEKDAY从0开始
CREATE TABLE `bage_visitlist` (
`ipaddress` varchar(16) NOT NULL DEFAULT '',
`visitfrom` varchar(16) NOT NULL DEFAULT '',
`visittimes` int(10) unsigned NOT NULL DEFAULT '0',
`visittime` datetime NOT NULL DEFAULT '2013-01-01 00:00:00',
`deviceype` varchar(16) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=utf8
PARTITION BY LIST (WEEKDAY(visittime))
(PARTITION d1 VALUES IN (0) ENGINE = MyISAM,
PARTITION d2 VALUES IN (1) ENGINE = MyISAM,
PARTITION d3 VALUES IN (2) ENGINE = MyISAM,
PARTITION d4 VALUES IN (3) ENGINE = MyISAM,
PARTITION d5 VALUES IN (4) ENGINE = MyISAM,
PARTITION d6 VALUES IN (5) ENGINE = MyISAM,
PARTITION d7 VALUES IN (6) ENGINE = MyISAM) ;
d1-d7有需要可以换成各个星期的缩写
星期一 MON
星期二 TUE
星期三 WED
星期四 THU
星期五 FRI
星期六 SAT
星期天 SUN
测试发现,分区插入是分区的,但是EXPLAIN PARTITIONS跟踪发现都是全区扫描的,条件里加入WEEKDAY(visittime)这样的也不行
但是如果你插入的datetime字段是不带时间只有日期的话,where条件里没出现函数只用=来判断日期,是可以分区搜索的
分区应该和索引一样,一但where中出现函数,就会全区扫描
下面的表PARTITION BY LIST (month(create_time)),Explain结果不太乐观
mysql> Explain partitions select * from rec_pay where create_time = '2014-06-01 00:00:00' limit 20;
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | rec_pay | p6 | ALL | NULL | NULL | NULL | NULL | 176370 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+-------------+
mysql> Explain partitions select * from rec_pay where create_time < '2014-05-01 00:00:00' and create_time > '2014-04-03 00:00:00' limit 20;
+----+-------------+---------+-------------------------------------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------------------------------------------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | rec_pay | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12 | ALL | NULL | NULL | NULL | NULL | 2517770 | Using where |
+----+-------------+---------+-------------------------------------------+------+---------------+------+---------+------+---------+-------------+
各个分区行数查询
SELECT table_name,partition_name,table_rows FROM information_schema.PARTITIONS WHERE table_schema=database() AND table_name='table_name'
mysql explain字段说明
点击(此处)折叠或打开