標籤:
分區方法1:Hash分區
例子:
create table thash(x int ,y int) partition by hash(x) partitions 4; 就這麼一句話表就分好區了。下一步我們把問題引深一點;
create table thash2(id int primary key,x int,y int) partition by hash(x) partitions 4;
thash2 是沒有辦法建立的不是它的文法有哪裡不對!是它不和規定。在有主鍵或unique的情況下分區列只能是它們的一部分
thash2 的主鍵是id 然而 x 不是主鍵的一部分,所以就不合規定啦。所以就不可以建立啦。別的深入的我就不多說了。下面我們看一下一個
分區方法。
分區方法2:range分區
例子:
create table trang(x int,y int ) partition by range(x) (partition p0 values less than (100),partition p1 values less than(200));
這裡有一個地方我要說一下就是分區的兩個臨界值一定是要有括弧的。不然算語法錯誤。
這樣分區的好處是在查詢時可以只鎖定分區。不需要鎖定表。你要不信我們來看一下它的執行計畫。
explain partitions select * from trang where x <1 ;
只鎖定一個分區p0看到了吧。但是好事並不是一定會發生的。
1:比如你的語句寫成了select * from trange ; 這樣人家一定鎖的不只是一個分區啊。
分區方法3:list分區
例子:
create table tlist(x int ,y int) partition by list(x) (partition p0 values in (1,3,5,7),partition p1 values in (2,4,6,8));
MySQL 表分區的幾種方法和注意