× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

When comparing dates with timestamps in PostgreSQL, `BETWEEN` is usually a lot faster than `DATE_TRUNC`

Rick van Hattem
Jun 23, 2015
<p>Instead of comparing dates using `DATE_TRUNC` you should probably just use between instead.</p> <p> </p> <p>For example, instead of this:</p> <pre><code class="language-sql">SELECT some_timestamp FROM some_table WHERE DATE_TRUNC('day', some_timestamp) = '2015-06-23'::timestamp</code></pre> <p>Use this:</p> <pre><code class="language-sql">SELECT some_timestamp FROM some_table WHERE some_timestamp BETWEEN '2015-06-23'::timestamp AND '2015-06-23'::timestamp + '1 days'::interval</code></pre> <p> </p>
comments powered by Disqus