RSS
热门关键字:
当前位置 :| 主页>网络编程>Mysql>

通过分区(Partition)提升MySQL性能

来源:站长资讯站5229 作者:zzzxz.com.cn 时间:2008-05-13 Tag: 点击:

  下面咱写一个存储过程(感谢Peter Gulutzan给的代码,如果大家需要Peter Gulutzan的存储过程教程的中文翻译也可以跟我要,chenpengyi◎gmail.com),它能向咱刚才建立的已分区的表中平均的向每个分区插入共­8百万条不同的数据。填满后,咱就给没分区的克隆表中插入相同的数据:

mysql> delimiter //

mysql> CREATE PROCEDURE load_part_tab()

    -> begin

    ->  declare v int default 0;

    ->          while v < 8000000

    ->  do

    ->  insert into part_tab

    ->  values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));

    ->  set v = v 1;

    ->  end while;

    -> end

    -> //

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> call load_part_tab();

Query OK, 1 row affected (8 min 17.75 sec)

mysql> insert into no_part_tab select * from part_tab;

Query OK, 8000000 rows affected (51.59 sec)

Records: 8000000  Duplicates: 0  Warnings: 0

  表都准备好了。咱开始对这两表中的数据进行简单的范围查询吧。先分区了的,后没分区的,跟着有执行过程解析(MySQL
Explain命令解析器),可以看到MySQL做了什么:

mysql> select count(*) from no_part_tab where

    -> c3 > date '1995-01-01' and c3 < date '1995-12-31';

----------

| count(*) |

----------

|   795181 |

----------

1 row in set (38.30 sec)

mysql> select count(*) from part_tab where

    -> c3 > date '1995-01-01' and c3 < date '1995-12-31';

----------

| count(*) |

----------

|   795181 |

----------

1 row in set (3.88 sec)

mysql> explain select count(*) from no_part_tab where

    -> c3 > date '1995-01-01' and c3 < date '1995-12-31'G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: no_part_tab

         type: ALL
possible_keys: NULL

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 8000000

        Extra: Using where

1 row in set (0.00 sec)

mysql> explain partitions select count(*) from part_tab where

    -> c3 > date '1995-01-01' and c3 < date '1995-12-31'G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: part_tab

   partitions: p1

         type: ALL

possible_keys: NULL

          key: NULL


      key_len: NULL 

          ref: NULL

         rows: 798458

        Extra: Using where

1 row in set (0.00 sec)

  从上面结果可以容易看出,设计恰当表分区能比非分区的减少90%的响应时间。而命令解析Explain程序也告诉我们在对已分区的表的查询过程中仅对第一个分区­进行了扫描,其他都跳过了。


在百度中搜索通过分区(Partition)提升MySQL性能
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
站长推荐
站长推荐