2017-08-30 6 views
0

私はこのテーブルにインデックスを持っていると仮定します。PostgreSQLインデックス実行時

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' 

を実行すると、クエリプランを説明するためのPostgreSQLを使用した12msの

@結果が得られることです。これはそうです。

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) 

が、この1

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

11460ミリ秒後の結果得?

クエリプラン

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

はたぶんイムが間違ってインデックス付けを行っているか、私は知りません。インデックス作成に慣れていないどんな助けも素晴らしいだろう。どうもありがとう!

+1

下記を参照してください。https://stackoverflow.com/tags/postgresql-performance/infoまた、あなたのインデックスは別のテーブルにあります。 –

+0

申し訳ありません。私は実際のテーブルをコピーして編集しました。 – Bigboss

+0

まだ間違った列です。また、分析結果を含めるようにしてください。 –

答えて

1

ではなくdate(table_date)でソートすることによって問題が発生しています。これは、クエリを次のように変更して修正できます。

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

違いがありました。ありがとう – Bigboss

関連する問題