MySQL 表分區的幾種方法和注意

來源:互聯網
上載者:User

標籤:

分區方法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 表分區的幾種方法和注意

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.