结构 mysql 大 table

structuring mysql large table

我 运行 我的 mysql 数据库 table 遇到了一些问题。我有一个脚本,每 5 分钟、10 分钟插入 200 行,包括 date_inserted,我有一个查询显示基于某些连接(如 4 个连接)插入的最后记录。 tables 是这样的:

table : id, location, int_id, in, out, steps, date_added

我有一个这样的连接

...
join (
   select a.location, a.int_id, a.in, a.out, a .date_added
   from table a
   join (
      select location, int_id, max(date_added) as `date_added`
      from table
      group by location, int_id 
        ) b on b.location = a.location
          and b.int_id = a.int_id
          and b.date_added = a.date_added
) c on c.location like concat('%', b.location_name, '%')
    and c.int_id = b.interface_id
...

并且由于这个 table 会增长到 1000 万,10000 万条记录查询将 运行 越来越慢。

减小此 table 大小的最佳方法是什么?我阅读了分区,但我不知道它是否对我来说是最好的解决方案?或者?

也许每个月创建一个新数据库或 table?

我不想最终不得不加入一个拥有 1 亿条记录并不断增加的 table。

非常感谢,但我不知道如何缩放它。

更新 1

id : int primary key auto_increment , location varchar, int_id int, in bigint, out bigint, steps varchar, date_added timestamp

我有一个日志服务器,它每 5 分钟在数据库中保存一个位置名称、一个接口 ID 和一些用于输入和输出的 bigint 计数器(总是递增,但我可以重置它们)以及插入的当前日期。 我正在实时做一份报告,以查看插入的最新日期的这个计数器(如果我重置它们,它们将从 0 开始并且不能使用 max(in) 或 max(out) 以防组加速 select)

问题是:这个 table 在大约半年内会增长很多,以至于这个实时报告会受到 table 中行数的影响。为了报告的速度,拆分它们以保持行数有限的 table 的最佳方法是什么。我不想删除旧的计数器,因为我会创建一个天/周/月的详细报告,但我不想只有一个 table 就有 1 亿条记录。我正在考虑创建一个每月数据库并在数据库 2016_04_report , 2016_05_report ... 等

中插入一个月的记录

避免 on c.location like concat('%', b.location_name, '%') 因为有任何性能希望。

您的查询是否搜索整个 table?如果是这样,你最终会被 IO 束缚,对性能没有希望。

能否每天(或其他时间间隔)汇总新数据,从而避免看大table?

INDEX(location, int_id, date_added)

按照这个顺序会有所帮助。

请提供SHOW CREATE TABLE;您提供的内容遗漏了太多细节。还提供整个查询。有了这些,我可能会对 PARTITIONing 发表评论。 (分区不太可能有任何帮助。)