如何提高 table 拥有 5000 万条记录并不断增长的性能

How to increase the performance of table having 50 millions records and growing

我们有两个 table,table A 和 table B(包含 5000 万条记录)。在 table A 中,我们存储了商户的唯一值。由此,我们从 table 获取记录 B. 如何提高查询的性能,提高查询性能的方法有哪些?

注意:两个 table 的 table 结构都很简单。
Table A - 存储引擎 (MyISAM)
TABLE B - 存储引擎 (MyISAM)
在 table A 中我们有一个主键对应 table B 的许多记录。
使用的查询:
查询 1:

records = "select field1, field2...  from table A where merchant_id = ''   
and field_date between '23-06-2012' and '23-06-2015' order by field 1";

查询2:循环执行

  foreach (records as records) {
      "select field_b1, field_b2, .. from table B where field_b1 =    
'records['field1']'"
   }

如果您还没有这样做,请在 table_a.merchant_idtable_a.field_date 上添加索引。 还要在 table_2.field_b1.

上添加索引

此外,您可以尝试将 table_b 上的主要 selectjoin 与来自 table_a 的记录一起制作。类似于:

select 
B.field_b1, B.field_b2 
from table_b AS B 
LEFT JOIN table_a AS A on B.field_b1 = A.field_1
WHERE A.merchant_id = '' AND A.field_date between '23-06-2012' and '23-06-2015' 
order by field 1;

这样,您只有一个查询来查询所有记录,而不是 1 个主查询 + 谁知道在第一个查询中找到的每条记录有多少个额外查询。

您应该将逻辑重写为单个查询。并且,使用 ISO 标准日期格式:

select a.field1, a.field2... , b.field1, b.field2, . . 
from a left join
     b
     on a.field1 = b.field1
where a.merchant_id = '' and
      a.field_date between '2012-06-23' and '2015-06-23'
order by a.field1;

对于此查询,您需要索引:a(merchant_id, field_date, field1)b(field1)

注意:如果您只查找一个日期,请不要使用 between。只需使用 =:

select a.field1, a.field2... , b.field1, b.field2, . . 
from a left join
     b
     on a.field1 = b.field1
where a.merchant_id = '' and
      a.field_date = '2012-06-23'
order by a.field1;

查询应该 运行 更快。