Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:44:41+00:00 2026-06-16T22:44:41+00:00

I have a sql query that is very slow: select number1 from mytable where

  • 0

I have a sql query that is very slow:

        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'

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:

    select number1 from mytable
    where symbol = 25
    and timeframe = 1
    and date = '2008-02-05 11:10:00'

I thought it would be faster if I would not use ::date and ::time, but when I execute a query like:

    select number1 from mytable
    where symbol = 25
    and timeframe = 1
    and date = '2008-02-05 10:40:00' + interval '30 minutes'

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.

The table got a multi column index on symbol, timeframe, date.

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?

Postgresql version is 9.2.

Edit:
The format of the table is:
date = timestamp with time zone,
symbol, timeframe = numeric.

Edit 2:
Using

select open from ohlc_dukascopy_bid
where symbol = 25
and timeframe = 1
and date = timestamp '2008-02-05 10:40:00' + interval '30' minute

Explain shows:

"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))"

Time is now considerably faster: 86ms on first run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:44:42+00:00Added an answer on June 16, 2026 at 10:44 pm

    The first version will not use a (regular) index on the column named date.

    You didn’t provide much information, but assuming the column named date has the datatype timestamp (and not date), then the following should work:

    and date = timestamp '2008-02-05 10:40:00' + interval '30 minutes'
    

    this should use an index on the column named date (but only if it is in fact a timestamp not a date). It is essentially the same as yours, the only difference is the explicit timestamp literal (although Postgres should understand '2008-02-05 10:40:00' as a timestamp literal as well).

    You will need to run an explain to find out if it’s using an index.

    And please: change the name of that column. It’s bad practise to use a reserved word as an identifier, and it’s a really horrible name, which doesn’t say anything about what kind of information is stored in the column. Is it the “start date”, the “end date”, the “due date”, …?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql query pulling from a view that is very very slow,
I have an SQL Query (For SQL Server 2008 R2) that takes a very
I have an SQL query that I am looking to optimize. SELECT * FROM
I have a SQL query that takes a very long time to run on
I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]
I have a very simple question. I have an SQL query that looks like
I have a particular SQL query that seems to suffer from a mysterious performance
I have the following SQL query: SELECT DISTINCT business_key FROM Memory WHERE concept <>
I have a very complex SQL View. My Problem is, that its very slow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.