PostgreSQL在第一次查询执行时很慢,但命中共享缓冲区

时间:2017-01-27 14:48:47

标签: postgresql

我对PostgreSQL有些问题。当我第一次执行SQL查询时,它比后续版本慢了6倍,即使使用像SELECT 1这样的基本查询;

SELECT * FROM citygps WHERE  name = 'VILLARS' AND  postalcode = '42390' ORDER BY population DESC NULLS LAST, alias_of DESC NULLS FIRST LIMIT 1;
Time: 62.045ms

EXPLAIN (ANALYZE, BUFFERS TRUE) SELECT * FROM citygps WHERE name = 'VILLARS' AND postalcode = '42390' ORDER BY population DESC NULLS LAST, alias_of DESC NULLS FIRST LIMIT 1;

                                                                QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Limit  (cost=8.32..8.32 rows=1 width=1380) (actual time=0.091..0.091 rows=1 loops=1)
Buffers: shared hit=9
->  Sort  (cost=8.32..8.32 rows=1 width=1380) (actual time=0.090..0.090 rows=1 loops=1)
        Sort Key: population DESC NULLS LAST, alias_of DESC
        Sort Method: quicksort  Memory: 26kB
        Buffers: shared hit=9
        ->  Index Scan using citygps_name_start on citygps  (cost=0.29..8.31 rows=1 width=1380) (actual time=0.028..0.036 rows=1 loops=1)
            Index Cond: ((name)::text = 'VILLARS'::text)
            Filter: (postalcode = '42390'::bpchar)
            Rows Removed by Filter: 3
            Buffers: shared hit=6
Planning time: 1.951 ms
Execution time: 0.162 ms
(13 rows)

Time: 6.269 ms

SELECT * FROM citygps WHERE  name = 'VILLARS' AND  postalcode = '42390' ORDER BY population DESC NULLS LAST, alias_of DESC NULLS FIRST LIMIT 1;
Time: 61.286ms

SELECT * FROM citygps WHERE  name = 'VILLARS' AND  postalcode = '42390' ORDER BY population DESC NULLS LAST, alias_of DESC NULLS FIRST LIMIT 1;
Time: 1.279ms

EXPLAIN (ANALYZE, BUFFERS TRUE) SELECT * FROM citygps WHERE  name = 'VILLARS' AND  postalcode = '42390' ORDER BY population DESC NULLS LAST, alias_of DESC NULLS FIRST LIMIT 1;

                                                                QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Limit  (cost=8.32..8.32 rows=1 width=1380) (actual time=0.076..0.077 rows=1 loops=1)
Buffers: shared hit=6
->  Sort  (cost=8.32..8.32 rows=1 width=1380) (actual time=0.074..0.074 rows=1 loops=1)
        Sort Key: population DESC NULLS LAST, alias_of DESC
        Sort Method: quicksort  Memory: 26kB
        Buffers: shared hit=6
        ->  Index Scan using citygps_name_start on citygps  (cost=0.29..8.31 rows=1 width=1380) (actual time=0.032..0.041 rows=1 loops=1)
            Index Cond: ((name)::text = 'VILLARS'::text)
            Filter: (postalcode = '42390'::bpchar)
            Rows Removed by Filter: 3
            Buffers: shared hit=6
Planning time: 0.384 ms
Execution time: 0.153 ms
(13 rows)

Time: 1.583 ms

EXPLAINBuffers: shared hit=x个查询中,我有<service android:name=".RemoveDownloadService" android:exported="false"/> ,但第一个查询速度很慢。在服务器而不是我的本地PostgreSQL实例上有相同的区别。你有解释和解决方法吗?

0 个答案:

没有答案