我正在测试 mysql 查询响应时间,我注意到一旦我只添加一个文本类型字段到 select 语句,查询速度会减慢近 10 倍以上。由于 mysql 不支持 FULL OUTER JOIN 语句,因此我使用以下解决方法:
select
a.idACCOUNT,p.idPROJECT,p.projectName,t.TRAINING_idPROJECT,t.idTRAINING,t.trainingName,m.MODULE_idTRAINING,m.idMODULE,m.moduleName,c.idCONTENT,c.contentName,
s.idSUBCONTENT
from ACCOUNT as a
left outer join PROJECT as p on a.idACCOUNT = p.PROJECT_idACCOUNT
left outer join TRAINING as t on a.idACCOUNT = t.TRAINING_idACCOUNT AND p.idPROJECT = t.TRAINING_idPROJECT
left outer join MODULE as m on a.idACCOUNT = m.MODULE_idACCOUNT AND p.idPROJECT = m.MODULE_idPROJECT AND t.idTRAINING = m.MODULE_idTRAINING
left outer join CONTENT as c on a.idACCOUNT = c.CONTENT_idACCOUNT AND p.idPROJECT = c.CONTENT_idPROJECT AND t.idTRAINING = c.CONTENT_idTRAINING AND
m.idMODULE = c.CONTENT_idMODULE
left outer join SUBCONTENT as s on a.idACCOUNT = s.SUBCONTENT_idACCOUNT AND p.idPROJECT = s.SUBCONTENT_idPROJECT AND t.idTRAINING = s.SUBCONTENT_idTRAINING
AND m.idMODULE = s.SUBCONTENT_idMODULE AND c.idCONTENT = s.SUBCONTENT_idCONTENT
WHERE a.idACCOUNT='1'
union all
select
a.idACCOUNT,p.idPROJECT,p.projectName,t.TRAINING_idPROJECT,t.idTRAINING,t.trainingName,m.MODULE_idTRAINING,m.idMODULE,m.moduleName,c.idCONTENT,c.contentName,
s.idSUBCONTEN
from ACCOUNT as a
right outer join PROJECT as p on a.idACCOUNT = p.PROJECT_idACCOUNT
right outer join TRAINING as t on a.idACCOUNT = t.TRAINING_idACCOUNT AND p.idPROJECT = t.TRAINING_idPROJECT
right outer join MODULE as m on a.idACCOUNT = m.MODULE_idACCOUNT AND p.idPROJECT = m.MODULE_idPROJECT AND t.idTRAINING = m.MODULE_idTRAINING
right outer join CONTENT as c on a.idACCOUNT = c.CONTENT_idACCOUNT AND p.idPROJECT = c.CONTENT_idPROJECT AND t.idTRAINING = c.CONTENT_idTRAINING AND
m.idMODULE = c.CONTENT_idMODULE
right outer join SUBCONTENT as s on a.idACCOUNT = s.SUBCONTENT_idACCOUNT AND p.idPROJECT = s.SUBCONTENT_idPROJECT AND t.idTRAINING =
s.SUBCONTENT_idTRAINING AND m.idMODULE = s.SUBCONTENT_idMODULE AND c.idCONTENT = s.SUBCONTENT_idCONTENT
WHERE a.idACCOUNT is null;
上面的查询仅包含最多 255 个 varchars 和 INT 11 字段,此查询以以下速度返回结果:查询耗时 0.0010 秒。
现在,如果我将添加到 Select 部分,至少一个文本字段查询时间会跳到 0.0337 秒,单次测试有时甚至会达到 0.1 秒
这并不重要,但是对文本字段运行单独的查询要快得多,速度为:查询耗时 0.0004 秒。
在我看来,分离两个查询并对文本字段(例如注释)进行特定查询以及对 varchars 和其他索引进行单独查询会更快。
现在我的问题是,为什么只在选择部分添加一个文本会影响查询时间?为什么单独的查询会在预期的时间范围内运行?
提前感谢您的时间和帮助。