将数据库表拆分为经常查询的数据+其他数据

时间:2016-05-01 21:08:06

标签: mysql database

将数据库表拆分为最多请求的数据其余数据是否会提高性能?我上周花了很多时间观看非常技术性的教程和会议,但我仍然不清楚列中有多大部分,列位置和行数计算性能(如果整个表适合到内存/ RAM )。

我发现了4种不同的选择。每种方法的优缺点是什么?

一些细节:

  • 主要是读取:至少80% - 强调性能
  • 10个不同的类别 - 每个类别有~100K条目/行
  • 每个类别中的30个总值
  • 经常查询的5个值(用于列表和单个帖子视图)
  • 查询值较少的25个(仅在单个帖子中查看)
  • 要指定:如果我说 value ,我的意思是实体/数据库列

enter image description here

1 个答案:

答案 0 :(得分:1)

以下是一个测试用例:

使用1M行创建测试数据:

drop table if exists posts;
create table posts (
    id int not null primary key,
    summary varchar(255),
    post text
) as
    select seq as id
    , repeat(rand(1), 10) as summary
    , repeat(rand(1), 100) as post
    from seq_1_to_1000000
;

drop table if exists small_data;
create table small_data (
    id int not null primary key,
    summary varchar(255)
) as select p.id, p.summary from posts p
;
drop table if exists big_data;
create table big_data (
    id int not null primary key,
    post text
) as select p.id, p.post from posts p;

show table status where Name in('posts', 'small_data', 'big_data');

Name       | Engine | Row_format | Rows   | Avg_row_length | Data_length
big_data   | InnoDB | Compact    | 870341 |           2361 |  2055208960
posts      | InnoDB | Compact    | 838832 |           2627 |  2204106752
small_data | InnoDB | Compact    | 985832 |            229 |   226197504

所以有三张桌子。

  • posts ~2.1 GB。包含所有数据(id,摘要~200字节,后〜2000字节)。
  • small_data ~215 MB。包含(id,摘要)
  • big_data ~1.9 GB。包含(id,post)
select p.id, p.summary
from posts p
order by id asc
limit 10
offset 500000

首次运行:16.552秒。 第二轮:16.723秒。

select p.id, p.summary
from small_data p
order by id asc
limit 10
offset 500000

首次运行:0.702秒。 第二轮:0.093秒

你可以看到,可能会有很大的不同。但这取决于您的数据和查询。所以你应该做自己的基准测试。

注意:

  • 在10.0.19-MariaDB上测试。
  • seq_1_to_1000000是一个包含1M序列号的表。您需要先创建它或使用MariaDBs Sequence插件。