按範圍使用分區。
如果通過鍵分區,無論你的例子查詢需要接觸每個分區。
的理論是,通過KEY分區是由像散列分區,在主鍵的該連續值綁定到被存儲在單獨的分區。通過查詢一系列id值,可以破壞分區修剪。
演示:
CREATE TABLE `game_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`game_id` int(11) NOT NULL DEFAULT '0',
`xyz` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`,`game_id`)
)
PARTITION BY KEY()
PARTITIONS 13;
INSERT INTO game_log (game_id) VALUES (1), (2), (3), (4), (5), (6);
EXPLAIN PARTITIONS SELECT * FROM game_log ORDER BY id DESC LIMIT 3\G
id: 1
select_type: SIMPLE
table: game_log
partitions: p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12
EXPLAIN PARTITIONS SELECT * FROM game_log WHERE game_id = 4 ORDER BY id DESC LIMIT 3\G
id: 1
select_type: SIMPLE
table: game_log
partitions: p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12
如果您在game_id通過分區範圍然而,你可以得到分區修剪當你查詢特定game_id至少幫助你。但是,通過id desc查詢任何game_id順序仍然必然會觸及每個分區。
CREATE TABLE `game_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`game_id` int(11) NOT NULL DEFAULT '0',
`xyz` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`,`game_id`)
)
PARTITION BY RANGE (game_id)
(PARTITION p1 VALUES LESS THAN (3),
PARTITION p2 VALUES LESS THAN (6),
PARTITION p3 VALUES LESS THAN MAXVALUE);
INSERT INTO game_log (game_id) VALUES (1), (2), (3), (4), (5), (6);
EXPLAIN PARTITIONS SELECT * FROM game_log ORDER BY id DESC LIMIT 3\G
id: 1
select_type: SIMPLE
table: game_log
partitions: p1,p2,p3
EXPLAIN PARTITIONS SELECT * FROM game_log WHERE game_id = 4 ORDER BY id DESC LIMIT 3\G
id: 1
select_type: SIMPLE
table: game_log
partitions: p2
使用分區,確保每個分區'innodb_buffer_pool_size'內配合,以避免I/O分頁 – 2013-03-26 04:04:03