如何让我的pg查询更快

时间:2015-03-24 02:15:18

标签: postgresql postgresql-performance

我是pg的新手,并有一个像这样的表:

CREATE TABLE tbl_article (
    uid         TEXT PRIMARY KEY,
    ...
    tags        JSONB
)
CREATE INDEX idxgin ON tbl_article USING gin (tags);

uid就像MongoDB在我的程序中生成的ObjectID。

这个查询:

SELECT * FROM tbl_article
WHERE tags @> '{"Category":0}'::jsonb
ORDER BY uid DESC LIMIT 20 OFFSET 10000

以下是解释:

Limit  (cost=971.77..971.77 rows=1 width=1047) (actual time=121.811..121.811 rows=0 loops=1)
    ->  Sort  (cost=971.46..971.77 rows=125 width=1047) (actual time=110.653..121.371 rows=8215 loops=1)
    Sort Key: uid
    Sort Method: external merge  Disk: 8736kB
        ->  Bitmap Heap Scan on tbl_article  (cost=496.97..967.11 rows=125 width=1047) (actual time=5.292..14.504 rows=8215 loops=1)
          Recheck Cond: (tags @> '{"Category": 0}'::jsonb)
          Heap Blocks: exact=3521
          ->  Bitmap Index Scan on idxgin  (cost=0.00..496.93 rows=125 width=0) (actual time=4.817..4.817 rows=8216 loops=1)
                Index Cond: (tags @> '{"Category": 0}'::jsonb)
Planning time: 0.105 ms
Execution time: 123.016 ms

似乎排序太慢了。那么如何让这更快

抱歉我的英语不好

0 个答案:

没有答案