Mysql表分区性能分析那些事儿

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LYTIT/article/details/89222209

每个技术的研究需要进行性能测试,然后在进行分析是否满足当前业务需要,以下为Mysql表分区性能测试执行情况:

1、插入语句
INSERT INTO erp_bill_index_test SELECT * from erp_bill_index;
– 受影响的行: 4501076
– 时间: 877.984s

2、分区语句
– 删除主键
alter table erp_bill_index_test drop primary key;
– 受影响的行: 4501076
– 时间: 724.435s

– 添加主键
alter table erp_bill_index_test add primary key(billid,billdate);
– 时间: 1281.912s

– 创建分区

ALTER TABLE erp_bill_index_test PARTITION by RANGE(to_days(billdate))
(
  PARTITION p201801 VALUES LESS THAN (to_days('2018-02-01')),
  PARTITION p201802 VALUES LESS THAN (to_days('2018-03-01')),
  PARTITION p201803 VALUES LESS THAN (to_days('2018-04-01')),
  PARTITION p201804 VALUES LESS THAN (to_days('2018-05-01')),
  PARTITION p201805 VALUES LESS THAN (to_days('2018-06-01')),
  PARTITION p201806 VALUES LESS THAN (to_days('2018-07-01')),
  PARTITION p201807 VALUES LESS THAN (to_days('2018-08-01')),
  PARTITION p201808 VALUES LESS THAN (to_days('2018-09-01')),
  PARTITION p201809 VALUES LESS THAN (to_days('2018-10-01')),
  PARTITION p201810 VALUES LESS THAN (to_days('2018-11-01')),
  PARTITION p201811 VALUES LESS THAN (to_days('2018-12-01')),
  PARTITION p201812 VALUES LESS THAN (to_days('2019-01-01')),
  PARTITION p201901 VALUES LESS THAN (to_days('2019-02-01')),
  PARTITION p201902 VALUES LESS THAN (to_days('2019-03-01')),
  PARTITION p201903 VALUES LESS THAN (to_days('2019-04-01')),
  PARTITION p201904 VALUES LESS THAN  MAXVALUE
);

受影响的行: 4501076
时间: 561.828s

3、测试语句
– 分区前(原始数据)

EXPLAIN 
SELECT COUNT(*) from erp_bill_index where  billdate>'2019-01-01' and billdate<'2019-02-01' LIMIT 0,20; -- 29.36s,0.99s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index where  billdate>'2019-01-01' and billdate<'2019-02-01' ORDER BY billdate DESC LIMIT 0,20; -- 0.11s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index where  billdate>'2019-01-01' and billdate<'2019-02-01' GROUP BY cityid ORDER BY billdate DESC LIMIT 0,20; -- 255.12s,250.46s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index where  billdate>'2019-01-01' and billdate<'2019-02-01' and creatorfullname LIKE '%建梅%' GROUP BY cityid ORDER BY billdate DESC LIMIT 0,20; -- 250.95s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index a
LEFT JOIN erp_bill_index_ext ext on a.profileid=ext.profileid and a.billid=ext.billid
WHERE  a.billdate>'2019-01-01' and a.billdate<'2019-02-01' LIMIT 0,20; -- 6.49s,1.36s
-- 没使用分区字段
EXPLAIN 
SELECT COUNT(*) from erp_bill_index where billid>3976858 and billid<4976858; -- 0.09s,0.04s
SELECT COUNT(*) from erp_bill_index where billid>4976858 and billid<8976858; -- 2.50s,0.10s
SELECT COUNT(*) from erp_bill_index where billid>8976858 and billid<16976858; -- 8.59s,0.18s

– 分区前(相同主键)

EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test_diff  where  billdate>'2019-01-01' and billdate<'2019-02-01' LIMIT 0,20; -- 9.62s,0.93s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test_diff  where  billdate>'2019-01-01' and billdate<'2019-02-01' ORDER BY billdate DESC LIMIT 0,20; -- 0.99s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test_diff  where  billdate>'2019-01-01' and billdate<'2019-02-01' GROUP BY cityid ORDER BY billdate DESC LIMIT 0,20; -- 186.02s,189.73s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test_diff  where  billdate>'2019-01-01' and billdate<'2019-02-01' and creatorfullname LIKE '%建梅%' GROUP BY cityid ORDER BY billdate DESC LIMIT 0,20; -- 188.04s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test_diff  a
LEFT JOIN erp_bill_index_ext ext on a.profileid=ext.profileid and a.billid=ext.billid
WHERE  a.billdate>'2019-01-01' and a.billdate<'2019-02-01' LIMIT 0,20; -- 12.85s,1.34s
-- 没使用分区字段
SELECT MAX(billid),MIN(billid) FROM erp_bill_index;-- 3976858 97865998
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test_diff  where billid>3976858 and billid<4976858; -- 0.21s,0.04s
SELECT COUNT(*) from erp_bill_index_test_diff  where billid>4976858 and billid<8976858; -- 5.26s,0.11s
SELECT COUNT(*) from erp_bill_index_test_diff  where billid>8976858 and billid<26976858;-- 28.87s,0.47s

– 分区后

EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test where  billdate>'2019-01-01' and billdate<'2019-02-01' LIMIT 0,20; -- 4.15s,0.47s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test where  billdate>'2019-01-01' and billdate<'2019-02-01' ORDER BY billdate DESC LIMIT 0,20; -- 0.46s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test where  billdate>'2019-01-01' and billdate<'2019-02-01' GROUP BY cityid ORDER BY billdate DESC LIMIT 0,20; -- 71.44s,71.79s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test where  billdate>'2019-01-01' and billdate<'2019-02-01' and creatorfullname LIKE '%建梅%' GROUP BY cityid ORDER BY billdate DESC LIMIT 0,20; -- 69.93s
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test a
LEFT JOIN erp_bill_index_ext ext on a.profileid=ext.profileid and a.billid=ext.billid
WHERE  a.billdate>'2019-01-01' and a.billdate<'2019-02-01' LIMIT 0,20; -- 5.21s,0.81s
-- 没使用分区字段
EXPLAIN 
SELECT COUNT(*) from erp_bill_index_test where billid>3976858 and billid<4976858; -- 0.39s,0.04s
SELECT COUNT(*) from erp_bill_index_test where billid>4976858 and billid<8976858; -- 4.92s,0.10s
SELECT COUNT(*) from erp_bill_index_test where billid>8976858 and billid<16976858; -- 10.78s,0.19s

总结

随着服务器的配置提升将会增加数据库的性能,分区能够解决部分单表的查询性能优化,数据量一旦达到一定量级和业务比较复杂进行分组和排序,表分区不能达到质的飞跃,只是有部分提升;如果需要用到表分区,需要结合自身的业务复杂度进行综合评定

以上是结合本人当前业务需要做出的分析,如有不理解之处,还望积极留言,相互探讨学习!

扫描二维码关注公众号,回复: 5929970 查看本文章

猜你喜欢

转载自blog.csdn.net/LYTIT/article/details/89222209