从执行计划查看索引的影响

作者:瀚高PG实验室 (Highgo PG Lab)-海无涯
创建一个表test,并插入一定的数据


highgo=# select * from test;
  id  |  name  |  profession  
------+--------+--------------
    1 | TOM    | accounting
    1 | JERRY  | accounting
    2 | JERRY  | accounting
    2 | JUGG   | Business
   10 | 瀚高 | 基础软件
   11 | 瀚高 | 基础软件
   12 | 瀚高 | 基础软件
   13 | 瀚高 | 基础软件
   14 | 瀚高 | 基础软件
   15 | 瀚高 | 基础软件
   16 | 瀚高 | 基础软件
   17 | 瀚高 | 基础软件
   18 | 瀚高 | 基础软件
   19 | 瀚高 | 基础软件
   20 | 瀚高 | 基础软件
   21 | 瀚高 | 基础软件
   22 | 瀚高 | 基础软件
   23 | 瀚高 | 基础软件
   24 | 瀚高 | 基础软件
   25 | 瀚高 | 基础软件
   26 | 瀚高 | 基础软件
   27 | 瀚高 | 基础软件
   28 | 瀚高 | 基础软件
   29 | 瀚高 | 基础软件
   30 | 瀚高 | 基础软件
   31 | 瀚高 | 基础软件
……
highgo=# select count(*) from test;
 count 
-------
  1005
(1 row)

打开计时

highgo=# \timing
Timing is on.

查看效果

highgo=# explain analyze  select * from test where id = '13';
                                           QUERY PLAN                                           
------------------------------------------------------------------------------------------------
 Seq Scan on test  (cost=0.00..19.56 rows=1 width=22) (actual time=0.013..0.175 rows=1 loops=1)
   Filter: (id = 13)
   Rows Removed by Filter: 1004
 Planning time: 0.053 ms
 Execution time: 0.201 ms
(5 rows)

Time: 0.685 ms
highgo=# create index idx_test_id on test(id);
CREATE INDEX
Time: 325.900 ms

highgo=# \di
               List of relations
 Schema |    Name     | Type  | Owner  | Table 
--------+-------------+-------+--------+-------
 public | idx_test_id | index | highgo | test
(1 row)

highgo=# explain analyze  select * from test where id = '13';
                                                    QUERY PLAN                                                     
-------------------------------------------------------------------------------------------------------------------
 Index Scan using idx_test_id on test  (cost=0.28..8.29 rows=1 width=22) (actual time=0.023..0.023 rows=1 loops=1)
   Index Cond: (id = 13)
 Planning time: 0.239 ms
 Execution time: 0.042 ms
(4 rows)

Time: 0.322 ms
。
虽然只有一千多行的数据,但是仍然可以看出快了不少。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值