PHP实战中知识总结 / PgSQL - pg_test_timing(bin目录文件)
一、pg_test_timing
pg_test_timing是评测操作系统计时效率和开销的工具,并确认系统时间永不倒退。收集时间数据速度慢的系统可能会导致EXPLAIN ANALYZE结果不准确。
二、命令格式
pg_test_timing [-d DURATION]
1、option
参数 | 说明 |
---|---|
-d duration | 指定测试的持续时间,以秒计。更长的持续时间会给出更好一些的精确度,并且更可能发现系统时钟回退的问题。默认的测试持续时间是 3 秒。 |
-v --version | 打印pg_test_timing版本并退出 |
-? --help | 显示有关pg_test_timing的命令行参数,然后退出 |
测试SQL执行时间经常使用两种方法\timing 和 explain analyze SQL。但是通常explain analyze SQL统计的执行时间长,其中一个原因是explain analyze SQL会为执行的每一个步骤添加计时信息,会有额外的计时开销。以下例子显示explain analyze比\timing耗时多一倍时长。
postgres=# \timing
Timing is on.
postgres=# select count(*) from test;
count
---------
1000000
(1 row)
Time: 82.678 ms
postgres=# explain analyze select count(*) from test;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=14388.97..14388.98 rows=1 width=8) (actual time=214.159..214.388 rows=1 loops=1)
-> Gather (cost=14388.76..14388.97 rows=2 width=8) (actual time=205.368..214.372 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (cost=13388.76..13388.77 rows=1 width=8) (actual time=187.791..187.792 rows=1 loops=3)
-> Parallel Index Only Scan using index_attack_alarm_reliability on alerts (cost=0.42..12347.09 rows=416667 width=0) (actual time=0.039..115.580 rows=333333 loops=3)
Heap Fetches: 0
Planning Time: 0.173 ms
Execution Time: 214.453 ms
(9 rows)
Time: 215.056 ms
2、pg_test_timing 结果解析
每次循环时间和柱状图用的单位是不同的。循环的解析度可以在几个纳秒(ns),而单个计时调用只能解析到一个微秒(us)。好的机器应该显示90%的调用都在1微秒(100纳秒)内完成。
[postgres@izwz91quxhnlkan8kjak5hz /]$ pg_test_timing -d 3
Testing timing overhead for 3 seconds.
Per loop time including overhead: 58.59 ns # 平均每次循环开销(纳秒)
Histogram of timing durations:
< us % of total count
1 96.16743 49237227 # 显示96%的循环在1微秒(100纳秒)内完成
2 3.82164 1956660
4 0.00042 215
8 0.00367 1880
16 0.00566 2896
32 0.00051 263
64 0.00026 133
128 0.00010 52
256 0.00006 30
512 0.00005 24
1024 0.00002 12
2048 0.00003 15
4096 0.00004 19
8192 0.00005 26
16384 0.00003 17
32768 0.00002 12
65536 0.00000 1
131072 0.00000 0
262144 0.00000 1