SQL 查询优化:提高 SELECT 语句性能
SQL 查询优化:提高 SELECT 语句性能
本文将针对以下 SQL 代码运行超时的问题提供优化方案:
SELECT info.InfoId, info.InfoTypeId, ' ' AS Title, ' ' AS Summary, ' ' AS Image, ' ' AS Source,
' ' AS Tags, info.UpdateTime, info.Weight, ' ' AS Content, info.Enabled, info.addTime,
info.bLocked, info.UserId FROM info LEFT OUTER JOIN
infotype ON info.InfoTypeId = infotype.InfoTypeId WHERE (info.Enabled = 0) AND (infotype.SiteId = 9) ORDER BY info.InfoId LIMIT 0,100000
优化方案
-
添加索引: 在
info表的Enabled字段和infotype表的SiteId字段上添加索引,可以有效提高查询效率。 -
优化查询字段: 将查询字段中的空字符串替换为
NULL,可以节省一定的存储空间和查询时间。
修改后的代码
SELECT info.InfoId, info.InfoTypeId, NULL AS Title, NULL AS Summary, NULL AS Image, NULL AS Source, NULL AS Tags,
info.UpdateTime, info.Weight, NULL AS Content, info.Enabled, info.addTime, info.bLocked, info.UserId
FROM info
LEFT OUTER JOIN infotype ON info.InfoTypeId = infotype.InfoTypeId
WHERE info.Enabled = 0 AND infotype.SiteId = 9
ORDER BY info.InfoId
LIMIT 0,100000
通过以上优化措施,可以有效提升 SQL 查询效率,解决查询超时的问题。
原文地址: https://www.cveoy.top/t/topic/kEVR 著作权归作者所有。请勿转载和采集!