簡體   English   中英

PostgreSQL索引運行時

[英]PostgreSQL Indexing Run Time

假設我有這個帶有索引的表。

 Create Table sample
 (
    table_date timestamp without timezone,
    description
 );
  CREATE INDEX dailyinv_index
  ON sample
  USING btree
  (date(table_date));

並且它有3300萬行。

為什么運行此查詢

   select count(*) from sample where date(table_date) = '8/30/2017' and   desc = 'desc1'

在12ms時產生結果

使用PostgreSQL解釋查詢計划。 這就是它的作用。

    Aggregate  (cost=288678.55..288678.56 rows=1 width=0)
      ->Bitmap Heap Scan on sample (cost=3119.63..288647.57 rows=12393 width=0)
           Recheck Cond: (date(table_date) = '2017-08-30'::date)
           Filter: ((description)::text = 'desc1'::text)
               ->  Bitmap Index Scan on dailyinv_index  (cost=0.00..3116.54 rows=168529 width=0)
                     Index Cond: (date(table_date) = '2017-08-30'::date)

但是這個

   select date(table_date) from sample where date(table_date)<='8/30/2017' order by table_date desc limit 1

在11,460毫秒后產生結果?

查詢計划

   Limit  (cost=798243.52..798243.52 rows=1 width=8)
     ->  Sort  (cost=798243.52..826331.69 rows=11235271 width=8)
             Sort Key: table_date
                  ->  Bitmap Heap Scan on sample  (cost=210305.92..742067.16 rows=11235271 width=8)
                        Recheck Cond: (date(table_date) <= '2017-08-30'::date)
                             ->  Bitmap Index Scan on dailyinv_index  (cost=0.00..207497.10 rows=11235271 width=0)
                                    Index Cond: (date(table_date) <= '2017-08-30'::date)

PostgreSQL版本:9.4

也許我在做索引錯誤或我不知道。 真的不熟悉索引編制。 任何幫助都會很棒。 非常感謝!

您的問題是由您對table_date而不是date(table_date)排序引起的。 可以通過將查詢修改為以下內容來更正此問題:

SELECT DATE(table_date)
FROM sample
WHERE DATE(table_date) <= '8/30/2017'
ORDER BY DATE(table_date) DESC 
LIMIT 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM