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 9007835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:47:47+00:00 2026-06-16T01:47:47+00:00

I have a query which basically finds lastcost of certain item on certain date

  • 0

I have a query which basically finds lastcost of certain item on certain date range.

select first 1 lastcost from stock 
where itemid = :itemid and date > :startdate and date < :enddate 
order by date desc

This query takes seconds to complete on millions of records because ">" does not use index. Would it be faster if I split the query by year/month and iterate until it reach startdate (with assumption 1 mil records per month)?

while (endate > startdate) do
begin
  var_year = extract(year from :endate);
  var_month = extract(month from :endate);
  select first 1 lastcost from stock 
  where itemid = :itemid and year=:var_year and month=:var_month
  order by date desc
  enddate = dateadd (-1 month to enddate);
end

I don’t have access to Firebird this couple of days, so I haven’t been able to try this myself.

  • 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-16T01:47:49+00:00Added an answer on June 16, 2026 at 1:47 am

    Firebird use an index if available for >, <, >=, <= and between operators.

    I made a test before posting this with this table:

    SQL> show table stock2;
    ITEMID                          INTEGER Not Null
    STOCKDATE                       TIMESTAMP Not Null
    LASTCOST                        DOUBLE PRECISION Nullable
    

    populated it with some data (not millions per month, but enough to test performance)

    SQL> select extract(year from stockdate),
    CON>        extract(month from stockdate), count(*)
    CON>   from stock2
    CON>  group by 1, 2
    CON>  order by 1, 2;
    
    EXTRACT EXTRACT        COUNT
    ======= ======= ============
       2012       1       706473
       2012       2       628924
       2012       3       670038
       2012       4       649411
       2012       5       671512
       2012       6       648878
       2012       7       671182
       2012       8       671212
       2012       9       649312
       2012      10       671881
       2012      11       648815
       2012      12       671579
    

    I ran your query, first without any index (takes seconds), then indexing only the itemid column, proving a better plan and better performance, and finally using a index by itemid and date, where the performance is much better. The show plan allows you see the engine uses the index by default.

    SQL> set plan on;
    SQL>
    SQL> select first 1 lastcost
    CON>   from stock2
    CON>  where itemid = 127
    CON>    and stockdate > '2012-01-15'
    CON>    and stockdate < '2012-03-27'
    CON> order by stockdate desc;
    
    PLAN SORT ((STOCK2 INDEX (IDX_STOCK2IDDATE)))
    
                   LASTCOST
    =======================
          149.7170031070709
    
    SQL>
    

    the index definitions I’m using are:

    create index idx_stock2id on stock2 (itemid);
    create index idx_stock2iddate on stock2 (itemid, stockdate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have a query which returns a series of cells of data from a
I have this query which on executing in my sql command line client executes
I have a query which returns two rows of data as count. I want
I have this query which is a dependant query and taking much execution time
I have a query which produces a list of orders like this: Invoice Description
I have this query which works correctly in MySQL. More background on it here
I have a query which searches two separate fields in the same table... looking
I have a query which part of a SP which is executed fairly regularly,

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.