MySQL:使用NOT EXISTS子查询优化查询

时间:2013-11-13 18:03:17

标签: mysql sql

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

2 个答案:

答案 0 :(得分: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") ;

答案 1 :(得分:1)

Left Joins ...其中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