记一次sql的10倍效率提升

在数据中心后台,浏览WATER专题文献的时候,速度明显很慢,感觉哪儿有问题。
开始还以为是SQL中提取了大BLOB字段的问题,后来对比了一下,发现和这个没有关系。

于是尝试在pgadmin中直接修改sql看看效果:

首先用老的代码执行:
select distinct ref.* from mdref r left join reference ref on r.refid=ref.id
left join datasource ds on r.uuid=ds.uuid left join source s on s.id=ds.sourceid
where s.code='water'
ORDER BY ref.year,ref.title

执行下来,平均花费时间大约是460ms。

修改为单表嵌套查询后:
select distinct ref.* from reference ref
where ref.id in (
select r.refid from mdref r left join datasource ds on r.uuid=ds.uuid
left join source s on s.id=ds.sourceid where s.code='water')
ORDER BY ref.year,ref.title;

执行下来,平均花费时间大约是36ms。

这样下来,速度就有10倍以上的提升。


已发布

分类

来自

标签:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注