title: Postgres View slow performance
body: <p>My question is related to slow performance of a Postgres view. I've just spent few hours with this issue and now I'm totally out of ideas.</p>
  
  <p>I have created view which can dynamically calculate data based on several tables, view itself has acceptable performance until one specific condition is used.</p>
  
  <p>Name of view is <code>RV_Replenish</code> and most important column is <code>QtyToOrder</code>, which is dynamically calculated based on some other tables.</p>
  
  <p>Very strange for me are next two queries:</p>
  
  <pre><code>1. select count(*) from rv_replenish
     where (case when qtytoorder&gt;0 then 1 else 0 end)&gt;0
  
  2. select count(*) from rv_replenish
     where (case when qtytoorder&gt;0 then 1 else 0 end)=1
  </code></pre>
  
  <p>Execution of second query is 10 times slower than first one, but returned number is same. What can be the reason? Reason can be related maybe to setup of postgres server, because I tested it on local and production development. On local development is execution of both queries same, but production server executes second variant very slowly.</p>