我正在测试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秒。
现在如果我要添加到选择部分,至少有一个文本字段查询时间会跳到0.0337秒,单次测试的时间甚至会达到0.1秒
然而,运行单独的文本字段查询要快得多,速度快得多:查询耗时0.0004秒。
在我看来,分离两个查询并对文本字段进行特定查询会更快,例如注释和对varchars和其他索引的单独查询。
现在我的问题是为什么它会影响查询时间,只在选择部分中添加一个文本?为什么单独的查询在预期的时间范围内运行?
提前感谢您的时间和帮助。
答案 0 :(得分:1)
TEXT
列存储在页外,而VARCHAR
存储在线(如果小于合理限制)。通常,这意味着读取其他磁盘页以检索TEXT
数据。
显然,如果`TEXT列包含长字符串,只需要检索“更多数据”可能会大大减慢查询速度。