我不确定如何开始优化我的平台运行的高延迟 SQL 查询。除了减少数据集之外,我还可以做哪些事情?
SELECT ctm.*
FROM compound_transaction_map ctm
JOIN bustransaction_map btm ON ctm.transId = btm.transId
WHERE btm.docId = ?
docId 由我的 DBWConnector 插入到 Java 代码中
主要问题是表的大小,compound_transaction_map 是776,387 条目,bustransaction_map 是3,252,772。我正在努力减少表大小,但只是想知道这个查询是否也是一个问题。
只是想从比我更有经验的人那里获得一些见解。我感谢你的帮助。
编辑:有关于transId和bustransaction_map.docID的索引
mysql> show index from compound_transaction_map;
+--------------------------+------------+------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------------+------------+------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| compound_transaction_map | 0 | PRIMARY | 1 | id | A | 765804 | NULL | NULL | | BTREE | | |
| compound_transaction_map | 0 | UNQ_compoundId_transId | 1 | transId | A | 772560 | NULL | NULL | | BTREE | | |
| compound_transaction_map | 0 | UNQ_compoundId_transId | 2 | compoundId | A | 772560 | NULL | NULL | | BTREE | | |
| compound_transaction_map | 1 | transId | 1 | transId | A | 772560 | NULL | NULL | | BTREE | | |
| compound_transaction_map | 1 | compoundId | 1 | compoundId | A | 645871 | NULL | NULL | | BTREE | | |
| compound_transaction_map | 1 | companyId | 1 | companyId | A | 625 | NULL | NULL | | BTREE | | |
+--------------------------+------------+------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
mysql> show index from bustransaction_map;
+--------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| bustransaction_map | 0 | PRIMARY | 1 | id | A | 2869993 | NULL | NULL | | BTREE | | |
| bustransaction_map | 0 | docId | 1 | docId | A | 2883521 | NULL | NULL | YES | BTREE | | |
| bustransaction_map | 1 | transId | 1 | transId | A | 1019639 | NULL | NULL | YES | BTREE | | |
+--------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
mysql> EXPLAIN
-> SELECT ctm.*
IN bustransaction_ma -> FROM compound_transaction_map ctm
-> JOIN bustransaction_map btm ON ctm.transId = btm.transId
-> WHERE btm.docId = '62EFCD90-BC62-9984-0B20-4465528D6359';
+----+-------------+-------+------------+-------+---------------+-------+---------+-------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-------+---------+-------+--------+----------+-------------+
| 1 | SIMPLE | btm | NULL | const | docId,transId | docId | 153 | const | 1 | 100.00 | NULL |
| 1 | SIMPLE | ctm | NULL | ALL | NULL | NULL | NULL | NULL | 772560 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+-------+---------+-------+--------+----------+-------------+
2 rows in set, 1 warning (0.01 sec)
mysql> SHOW CREATE TABLE compound_transaction_map;
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| compound_transaction_map | CREATE TABLE `compound_transaction_map` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`compoundId` varchar(100) NOT NULL,
`transId` varchar(100) NOT NULL,
`companyId` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `UNQ_compoundId_transId` (`transId`,`compoundId`),
KEY `transId` (`transId`),
KEY `compoundId` (`compoundId`),
KEY `companyId` (`companyId`)
) ENGINE=InnoDB AUTO_INCREMENT=865326 DEFAULT CHARSET=latin1 |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE TABLE bustransaction_map;
+--------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bustransaction_map | CREATE TABLE `bustransaction_map` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`transId` varchar(50) DEFAULT NULL,
`docId` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `docId` (`docId`),
KEY `transId` (`transId`)
) ENGINE=InnoDB AUTO_INCREMENT=13033253 DEFAULT CHARSET=utf8 |
+--------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> EXPLAIN select ctm.* from bustransaction_map btm join compound_transaction_map ctm force index (transId) on ctm.transId = btm.transId where btm.docId = '62EFCD90-BC62-9984-0B20-4465528D6359';
+----+-------------+-------+------------+-------+---------------+-------+---------+-------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-------+---------+-------+--------+----------+-------------+
| 1 | SIMPLE | btm | NULL | const | docId,transId | docId | 153 | const | 1 | 100.00 | NULL |
| 1 | SIMPLE | ctm | NULL | ALL | NULL | NULL | NULL | NULL | 772560 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+-------+---------+-------+--------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)
MySQL 每个表只能使用一个索引。
explain
表明它选择使用bustransaction_map.docId
上的索引来帮助解决where docId = ?
; Bustransaction_map 是较大的表,因此它希望减少其行数。它在 compound_transaction_map
上不使用索引。
要解决这个问题,请在
(docId, transId)
的bustransaction_map上创建一个复合索引(顺序很重要)。 MySQL 可以将此索引用于任何仅具有 bustransaction_map.docId
功能的查询以及像您一样使用 bustransaction_map.docId
和 bustransaction_map.transId
的查询。
然后您可以将索引放在
docId
上,这是多余的。您还可以删除compound_transaction_map.transId
上的单列索引,它与(transId, compoundId)
上的复合索引是多余的。
一般来说,如果有两列,则可以使用两个索引覆盖包含它们任意组合的所有查询...