Note that there are some explanatory texts on larger screens.

plurals
  1. POPostgresql Query very slow with ::date, ::time, and interval
    primarykey
    data
    text
    <p>I have a sql query that is very slow:</p> <pre><code> select number1 from mytable where symbol = 25 and timeframe = 1 and date::date = '2008-02-05' and date::time='10:40:00' + INTERVAL '30 minutes' </code></pre> <p>The goal is to return one value, and postgresql takes 1.7 seconds to return the desired value(always a single value). I need to execute hundreds of those queries for one task, so this gets extremely slow. Executing the same query, but pointing to the time directly without using interval and ::date, ::time takes only 17ms:</p> <pre><code> select number1 from mytable where symbol = 25 and timeframe = 1 and date = '2008-02-05 11:10:00' </code></pre> <p>I thought it would be faster if I would not use ::date and ::time, but when I execute a query like:</p> <pre><code> select number1 from mytable where symbol = 25 and timeframe = 1 and date = '2008-02-05 10:40:00' + interval '30 minutes' </code></pre> <p>I get a sql error (22007). I've experimented with different variations but I couldn't get interval to work without using ::date and ::time. Date/Time Functions on postgresql.org didn't help me out.</p> <p>The table got a multi column index on symbol, timeframe, date.</p> <p>Is there a fast way to execute the query with adding time, or a working syntax with interval where I do not have to use ::date and ::time? Or do I need to have a special index when using queries like these? </p> <p>Postgresql version is 9.2.</p> <p>Edit: The format of the table is: date = timestamp with time zone, symbol, timeframe = numeric.</p> <p>Edit 2: Using</p> <pre><code>select open from ohlc_dukascopy_bid where symbol = 25 and timeframe = 1 and date = timestamp '2008-02-05 10:40:00' + interval '30' minute </code></pre> <p>Explain shows:</p> <pre> "Index Scan using mcbidindex on mytable (cost=0.00..116.03 rows=1 width=7)" " Index Cond: ((symbol = 25) AND (timeframe = 1) AND (date = '2008-02-05 11:10:00'::timestamp without time zone))" </pre> <p>Time is now considerably faster: 86ms on first run.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload