我已将示例表上传到保管箱。load into mysql with table name balanceload into mysql with table name income显示数据结构:
describe balance;
+----------------------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------+------+-----+---------+-------+
| code | text | YES | | NULL | |
| report_date | date | YES | | NULL | |
| total_assets | double | YES | | NULL | |
| total_owner_equities | double | YES | | NULL | |
+----------------------+--------+------+-----+---------+-------+
4 rows in set (0.00 sec)
describe income;
+--------------------------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------+------+-----+---------+-------+
| code | text | YES | | NULL | |
| report_date | date | YES | | NULL | |
| total_operating_revenue | double | YES | | NULL | |
| net_profit | double | YES | | NULL | |
| np_parent_company_owners | double | YES | | NULL | |
+--------------------------+--------+------+-----+---------+-------+
5 rows in set (0.01 sec)
两个表都包含一些记录。
select count(*) from income;
+----------+
| count(*) |
+----------+
| 51250 |
+----------+
1 row in set (0.06 sec)
select count(*) from balance;
+----------+
| count(*) |
+----------+
| 50832 |
+----------+
1 row in set (0.05 sec)
[执行相等的连接查询还需要4分钟。
select a.code ,a.report_date ,a.total_assets ,a.total_owner_equities,
b.total_operating_revenue,b.net_profit ,b.np_parent_company_owners
from balance as a ,income as b
where a.code=b.code and a.report_date=b.report_date;
50793 rows in set (4 min 5.15 sec)
在这种情况下如何优化mysql相等连接查询以提高速度?
查询之所以花很长时间,是因为它试图将第一个表中的每个记录与第二个表中的每个记录进行匹配。 51,250 * 50,832 = 2,605,140,000支票。
通过减小范围,可以显着提高查询速度。
select a.code ,a.report_date ,a.total_assets ,a.total_owner_equities,
b.total_operating_revenue,b.net_profit ,b.np_parent_company_owners
from balance as a ,income as b
where a.code=b.code and a.report_date=b.report_date
and a.report_date between date1 and date2
and b.report_date between samedate1 and samedate2;
为了提高查询性能,您需要在表中进行几处更改:
code
字段可以改为VARCHAR
而不是TEXT
所以您需要:
ALTER TABLE `TableA`
CHANGE `code` VARCHAR(256),
ADD INDEX `code_idx` (`code`),
ADD INDEX `report_date_idx` (`report_date`);
ALTER TABLE `TableB`
CHANGE `code` VARCHAR(256),
ADD INDEX `code_idx` (`code`),
ADD INDEX `report_date_idx` (`report_date`);