pgSQL笔记07-性能优化

【性能优化】

一、优化查询

1.分析查询语句explain

pgSQL笔记07-性能优化

explain analyze select * from person;

pgSQL笔记07-性能优化

2. 索引对查询速度的影响

pgSQL笔记07-性能优化

/*分析未使用索引的查询情况*/

explain select * from person where name='rx';

pgSQL笔记07-性能优化

/*然后,在person表的name字段加上索引,再进行explain */

create index index_name ON person(name);

explain analyze select * from person where name='rx';

pgSQL笔记07-性能优化

通过对比可以看到cost的数值减少了,表明查询语句执行速度提高了

3. 优化子查询

pgSQL笔记07-性能优化

二、优化数据库结构

—— 主要考虑:数据冗余、查询和更新速度、字段的数据类型是否合理。

1. 将字段很多的表分解成多个表

pgSQL笔记07-性能优化

2. 增加中间表

pgSQL笔记07-性能优化

3. 增加冗余字段

pgSQL笔记07-性能优化

4. 优化插入记录的速度

—— 影响插入速度的主要是:索引、唯一性校验、一次插入记录条数等。

—— 删除索引、使用批量插入、删除外键约束、禁止自动提交、使用COPY批量导入。

pgSQL笔记07-性能优化

5. 分析表的统计信息

pgSQL笔记07-性能优化

analyze verbose person;

pgSQL笔记07-性能优化

三、优化PostgreSQL服务器

1. 优化服务器硬件

pgSQL笔记07-性能优化

2.优化PostgreSQL参数 —— postgresql.conf

pgSQL笔记07-性能优化