2

让我说,首先,我不是一个 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 专家,但我的直觉表明,这个查询的性能可以大大提高。有什么建议么?

4

2 回答 2

1

左连接 ... 其中 NULL 往往比 MySQL 中的 Not Exists 子句快;在其他 RDBMS 中,情况往往相反。尝试:

SELECT DISTINCT profile2.f3
FROM node AS profile
JOIN node AS profile2 ON profile.f1 = profile2.f1
LEFT JOIN node AS profile3 ON profile.f1 = profile3.f1 
                     AND profile3.f2 = "yetAnotherString"
WHERE  profile.f2 = "aString"
  AND profile.f3 = "anotherString"
  AND profile2.f2 = "aThirdString"
  AND profile3.f1 IS NULL
于 2013-11-13T18:32:08.850 回答
1

你可以试试这个,这应该相对更快,或者你可以去加入

   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 PROFILE.F1 NOT IN (SELECT profile3.f1
                           FROM   node AS profile3
                           WHERE  profile3.f2 = "yetAnotherString") ;
于 2013-11-13T18:27:38.097 回答