title: Sometime Postgresql Query takes to much time to load Data
body: <p>I am using PostgreSQL 13.4 and has intermediate level experience with PostgreSQL.</p>
  <p>I have a table which stores reading from a device for each customer. For each customer we receive around ~3,000 data in a day and we store them in to our <code>usage_data</code> table as below</p>
  <p><a href="https://i.stack.imgur.com/RKPbn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RKPbn.png" alt="enter image description here" /></a></p>
  <p>We have the proper indexing in place.</p>
  <pre><code>  Column     |            Data Type        |     Index name            | Idx Access Type
  -------------+-----------------------------+---------------------------+---------------------------
   id          | bigint                      |                           | 
   customer_id | bigint                      |  idx_customer_id          | btree
   date        | timestamp                   |  idx_date                 | btree
   usage       | bigint                      |  idx_usage                | btree
   amount      | bigint                      |  idx_amount               | btree
  </code></pre>
  <p>Also I have common index on 2 columns which is below.</p>
  <p><strong>CREATE INDEX idx_cust_date
  ON public.usage_data USING btree
  (customer_id ASC NULLS LAST, date ASC NULLS LAST)</strong>
  ;</p>
  <p><strong>Problem</strong></p>
  <p>Few weird incidents I have observed.
  When I tried to get data for 06/02/2022 for a customer it took almost 20 seconds. It's simple query as below</p>
  <pre><code>SELECT * FROM usage_data WHERE customer_id =1 AND date = '2022-02-06'
  </code></pre>
  <p>The execution plan
  <a href="https://i.stack.imgur.com/YlZFx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YlZFx.jpg" alt="enter image description here" /></a></p>
  <p>When I execute same query for 15 days then I receive result in 32 milliseconds.</p>
  <pre><code>SELECT * FROM usage_data WHERE customer_id =1 AND date &gt; '2022-05-15' AND date &lt;= '2022-05-30'
  </code></pre>
  <p>The execution plan
  <a href="https://i.stack.imgur.com/I7ZzA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I7ZzA.jpg" alt="enter image description here" /></a></p>
  <p><strong>Tried Solution</strong></p>
  <p>I thought it might be the issue of indexing as for this particular date I am facing this issue.</p>
  <p>Hence I dropped all the indexing from the table and recreate it.</p>
  <p>But the problem didn't resolve.</p>
  <p><strong>Solution Worked (Not Recommended)</strong></p>
  <p>To solve this I tried another way. Created a new database and restored the old database
  I executed the same query in new database and this time it takes 10 milliseconds.</p>
  <p>The execution plan
  <a href="https://i.stack.imgur.com/SDKbu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SDKbu.jpg" alt="enter image description here" /></a></p>
  <p>I don't think this is proper solution for <strong>production server</strong></p>
  <p>Any idea why for any specific data we are facing this issue?
  Please let me know if any additional information is required.</p>
  <p>Please guide. Thanks</p>