让我说,首先,我不是一个 mySQL 大师。虽然我充分使用它,但我不知道很多关于它的细节。在我刚刚继承的系统中,我有这个查询:
SELECT DISTINCT profile2.f3
FROM node AS profile
JOIN node AS profile2
ON ( profile.f1 = profile2.f1 )
WHERE profile.f2 = "aString"
AND profile.f3 = "anotherString"
AND profile2.f2 = "aThirdString"
AND NOT EXISTS (SELECT profile3.f1
FROM node AS profile3
WHERE profile3.f1 = profile.f1
AND profile3.f2 = "yetAnotherString") ;
SHOW CREATE TABLE
给出:
CREATE TABLE `node` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`graph` varchar(100) CHARACTER SET latin1 DEFAULT NULL,
`f1` varchar(200) NOT NULL,
`f2` varchar(200) NOT NULL,
`f3` mediumtext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `nodeindex` (`graph`(20),`f1`(100),`f2`(100),`f3`(100)),
KEY `ix_node_f1` (`f1`),
KEY `ix_node_graph` (`graph`),
KEY `ix_node_f3` (`f3`(255)),
KEY `ix_node_f2` (`f2`),
KEY `node_po` (`f2`,`f3`(130)),
KEY `node_so` (`f1`,`f3`(130)),
KEY `node_sp` (`f1`,`f2`(130)),
FULLTEXT KEY `node_search` (`f3`)
) ENGINE=MyISAM AUTO_INCREMENT=455854703 DEFAULT CHARSET=utf8
EXPLAIN EXTENDED
给出:
+----+--------------------+----------+------+--------------------------------------------------------------------------------------+---------+---------+-----------------------------------+-------+----------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+----------+------+--------------------------------------------------------------------------------------+---------+---------+-----------------------------------+-------+----------+------------------------------+
| 1 | PRIMARY | profile | ref | ix_node_f1,ix_node_f3,ix_node_f2,node_po,node_so,node_sp,node_search | node_po | 994 | const,const | 49084 | 100.00 | Using where; Using temporary |
| 1 | PRIMARY | profile2 | ref | ix_node_f1,ix_node_f2,node_po,node_so,node_sp | node_sp | 994 | sumazi_prdf.profile.f1,const | 1 | 100.00 | Using where |
| 2 | DEPENDENT SUBQUERY | profile3 | ref | ix_node_f1,ix_node_f2,node_po,node_so,node_sp | node_sp | 994 | sumazi_prdf.profile.f1,const | 1 | 100.00 | Using where |
+----+--------------------+----------+------+--------------------------------------------------------------------------------------+---------+---------+-----------------------------------+-------+----------+------------------------------+
正如我所说,我不是 RDBMS 专家,但我的直觉表明,这个查询的性能可以大大提高。有什么建议么?