Using an index to access the last n values of a certain combination

I have a large table of the following form:
Name Typ
ID NUMBER
M_DATE DATE
M_LOC NUMBER
M_ARTICLE VARCHAR2(30)
M_TYPE VARCHAR2(30)
M_MEASURE VARCHAR2(50)
VALUE NUMBER
In this table there is only a small number of different locations, articles, types and maesures but a large number of m_date values.
I want to access the last 10 values for a certain combination of locations, articles, types and measures.
I have an index
create index i_res_2 on results (m_loc, m_article, m_type, m_measure, m_date);
In my opinion it should be possible to access the last 10 values directly by this index, but
when I do my select in the following way it seems to me that first all datasets with the same
locations, articles, types and maesures are selected, then sorted by date and then the last 10
dates are selected. As those datasets are spread over the whole table it needs a higher number
of physical reads. Is there a way to have an index on m_date and an other form of this query
that selects the requested values with less phyical reads? (Database version is 10.2)
SQL> select
2 id,
3 m_date,
4 value
5 from (
6 select
7 id,
8 m_date,
9 value,
10 rank() over (order by m_date desc) r
11 from results
12 where m_loc = '14001'
13 and m_article = '11211-00-00'
14 and m_type = '0002'
15 and m_measure = '032')
16 where r <= 10
17 order by m_date asc;
ID M_DATE Value
3931958 05.03.10 69.00
3931960 05.03.10 69.00
3905712 10.03.10 68.60
3999535 10.03.10 69.70
3985125 11.03.10 69.40
3957851 11.03.10 69.60
3949799 21.03.10 68.70
4017369 21.03.10 69.00
3951981 22.03.10 68.80
3983554 22.03.10 68.80
Abgelaufen: 00:00:03.00
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 48 | 6 (34)| 00:00:01 |
| 1 | SORT ORDER BY | | 1 | 48 | 6 (34)| 00:00:01 |
|* 2 | VIEW | | 1 | 48 | 5 (20)| 00:00:01 |
|* 3 | WINDOW SORT PUSHED RANK | | 1 | 39 | 5 (20)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID| RESULTS | 1 | 39 | 4 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | I_RES_2 | 1 | | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - filter("R"<=10)
3 - filter(RANK() OVER ( ORDER BY INTERNAL_FUNCTION("M_DATE") DESC )<=10)
5 - access("M_LOC"=14001 AND "M_ARTICLE"='11211-00-00' AND "M_TYPE"='0002' AND
"M_MEASURE"='032')
Statistiken
1 recursive calls
0 db block gets
547 consistent gets
474 physical reads
0 redo size
766 bytes sent via SQL*Net to client
380 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
10 rows processed

I found a solution, which is much better in terms of physical reads and consistent gets even if the optimizer calculates higher costs. But I still think, that there should be an easier way to do this:
SQL> select
2 id, m_date, value from results
3 where
4 (m_loc, m_article, m_type, m_measure, m_date)
5 in
6 (select m_loc, m_article, m_type, m_measure, m_date
7 from (
8 select /*+ first_rows(10) */ m_loc, m_article, m_type, m_measure, m_date
9 from results
10 where m_loc = '14001'
11 and m_article = '11370-00-00'
12 and m_type = '0002'
13 and m_measure = '032'
14 order by m_loc,m_article,m_type,m_measure,m_date desc
15 ) where rownum < 10);
ID M_DATE VALUE
2495995 01.02.09 70,4
2457657 19.01.09 66,9
2556262 13.02.09 67,4
2496232 01.02.09 67,6
2465051 20.01.09 67
2454994 19.01.09 67,4
2545951 13.02.09 67,4
2502216 01.02.09 67,5
2497533 01.02.09 67,2
9 Zeilen ausgewählt.
Ausführungsplan
Plan hash value: 3924867000
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 122 | 8 (25)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| RESULTS | 1 | 39 | 3 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 1 | 122 | 8 (25)| 00:00:01 |
| 3 | VIEW | VW_NSO_1 | 1 | 83 | 4 (25)| 00:00:01 |
| 4 | HASH UNIQUE | | 1 | 31 | | |
|* 5 | COUNT STOPKEY | | | | | |
| 6 | VIEW | | 1 | 31 | 4 (25)| 00:00:01 |
|* 7 | SORT ORDER BY STOPKEY| | 1 | 31 | 4 (25)| 00:00:01 |
|* 8 | INDEX RANGE SCAN | I_RES_2 | 1 | 31 | 3 (0)| 00:00:01 |
|* 9 | INDEX RANGE SCAN | I_RES_2 | 1 | | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
5 - filter(ROWNUM<10)
7 - filter(ROWNUM<10)
8 - access("M_LOC"=14001 AND "M_ARTICLE"='11370-00-00' AND "M_TYPE"='0002'
AND "M_MEASURE"='032')
9 - access("M_LOC"="$nso_col_1" AND "M_ARTICLE"="$nso_col_2" AND
"M_TYPE"="$nso_col_3" AND "M_MEASURE"="$nso_col_4" AND "M_DATE"="$nso_col_5")
Statistiken
1 recursive calls
0 db block gets
36 consistent gets
14 physical reads
0 redo size
748 bytes sent via SQL*Net to client
381 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
9 rows processed

Similar Messages

  • I "stopped scripting" at Unresponsive Script error in Thunderbird now I cannot access the last year of emails.

    Thunderbird prompted me with a pop up window regarding "scripting". When I stopped the script and Thunderbird restarted I was unable to access the last year of my emails up to the date that I stopped the script. Emails from 2013 were archived, unfortunately I hadn't gotten to 2014. I can still see the emails when I conduct a global search, and I found the profile folder on my hard drive but cannot figure out how to reload them into Thunderbird. I would like to access them in my inbox and archive them properly. Please let me know if there is a fix for this, I don't understand most of the entries in the forum as they are written for someone with a higher level of technical expertise than I possess.

    LSamuels, what antivirus software do you use? And have you gotten back your missing emails yet?

  • Downloading images used to go automatically to the last folder used to save images, even when reopening Firefox. In just the past few days, possibly coinciding wih the most recent updates, the folder defaults to the 'Downloads' folder. This is most annoyi

    Downloading images used to go automatically to the last folder used to save images, even when reopening Firefox. In just the past few days, possibly coinciding wih the most recent updates, the folder defaults to the 'Downloads' folder. This is most annoying. What happened? Internet Explorer 8.0 did the same thing. This was one of the reasons why I started using Firefox to download all images. I went into Tools>Options and it only lets me set another folder. I dont want to set a specific folder I want it to always go to the last folder used. So what gives?
    == This happened ==
    Every time Firefox opened
    == possibly when the most recent updates were installed, a few days ago

    Thanks jscher 2000. I guess I didn't make it clear. "It restarts with all the addons activated, and resumes with the tabs that were open before closing it." IE, it's running fine now with all the extensions activated. Everything is OK now.
    So something in the Firefox code was causing the bad behavior. It's not essential that I find out what the problem was - I'm just curious. And if anybody else has this same problem, it might be nice to have it corrected at the source.

  • When I use my Apple ID for the last 12 hours I get this response:  "There was an error in the App Store. Please try again later. (4)".   How do I get around this?  Try two apple ID's.  Same thing.

    When I use my Apple ID for the last 12 hours I get this response:  "There was an error in the App Store. Please try again later. (4)".   How do I get around this?  Try two apple ID's.  Same thing. 

    What does it mean when I get...: Apple Support Communities
    'there was an error in the App Store....: Apple Support Communities

  • HT203983 my apple id does not accept my payment method. why? i am using that same visa card the last 5 years!

    my apple id does not accept my payment method. why? i am using that same visa card the last 5 years!

    there may be several reasons why your credit card may be declined by the iTunes store ,
    the reason the iTunes store require c.card when you setup your apple id is for verification purposes
    if you have called the iTunes store in the past about unrecognised charges, or unauthorised activity ,
    then your bank may have blocked your card from being used on the iTunes store, i would recommend contacting your banking institute

  • Can I use my iPhone to access the Internet with my laptop

    I would like to use my iPhone to access the Internet from my laptop.  Is a connection between my iPhone and my laptop (using Vista OS) possible?

    This may help
    http://support.apple.com/kb/HT3574
    and this completes the picture
    http://support.apple.com/kb/HT4517

  • I have 6p from a gift card left in the English store and can't spend it. I want to change to the Irish store as I have an Irish gift card for me to redeem but it won't let me change store. Also I don't want to use a credit card for the last 6p.

    I have 6p from a gift card left in the English store and can't spend it. I want to change to the Irish store as I have an Irish gift card for me to redeem but it won't let me change store. Also I don't want to use a credit card for the last 6p.

    See:
    How to manage unused iTunes Gift Card and Gift Certificate balances

  • Does iTunes use Internet Explorer to access the internet from a PC with Windows Vista?

    Does iTunes use Internet Explorer to access the internet from a PC with Windows Vista?

    Are you using Windows XP or Windows Vista?
    Did you remove iTunes and the other Apple software also, and then reinstall iTunes.
    Removing and Reinstalling iTunes, QuickTime, and other software components for Windows XP
    Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7
    Did you try to put the iPhone into recovery mode?

  • I don't remember my security answer and it's not my 1st time for using this device,i using this before..but the last time I try to buy some games using iTunes card its saying this is your 1st time using this device so I need to answer the security questio

    i don't remember my security answer and it's not my 1st time for using this device,i using this before..but the last time I try to buy some games using iTunes card its saying this is your 1st time using this device so I need to answer the security questio

    https://expresslane.apple.com/

  • I am using Iphone 5. Since the last 3 weeks the phones has been acting weird. on dialling a call it get hung on disconnecting it stays on the call ending screen only. this happens for atleast 5 to 7 minutes and then it gets to normal

    I am using Iphone 5. SInce the last 3 weeks the phones has been acting weird. on dialling a call it get hung on disconnecting it stays on the call ending screen only. this happens for atleast 5 to 7 minutes and then it gets to normal or the other way is to turn the device off and then turn it on. the phones also running slow and therefore at times becomes very cumbersome. a few of my friends have also been having the same problems.
    I have taken the phone to the service outles but the best they did was to reboot the phone it works fine for a day or two but again the same issue.
    Can anyone share any info on it?

    I'm glad to hear that they replaced your phone. Any experience I have had with Apple customer service and/or Genius bar has been extremely positive. While I have had positive outcomes from trouble with other phones in the past, they are not as quick and  friendly. I had a Motorola phone and a Samsung phone go bad several years ago and warranty work through AT&T required that I wait for a replacement unit and then mail back the original unit to keep from being charged for the device. This usually meant almost a week without a phone. Nice to walk into Apple and walk out with a replacement device. Enjoy your phone.

  • Formula to calculate the last active value in a row?

    When using a simpe table, what formula will display the last active value in the row? Some of the cells are blank. I want a column to show the most recent value, which in this case will be the rightmost, non-blank cell.

    B2 through J2 are your data
    B3 =IF(B2="","",COLUMN())
    Fill that to the rest of row 3
    A2 =INDEX(B2:J2,MATCH(MAX(B3:J3),B3:J3,1))
    If there are no values in the row, the answer will be an error triangle. You can put the IFERROR function around the formula and give it an answer when there is an error.

  • Is it possible to maintain the last AO value after the DAQmx task is done on device DAQ USB-6341 ?

    Hi all,
    I use the device DAQ USB-6341 to generate the desired voltage waveform by the "finite samples" sample mode.
    I notice that the channel resets to zero volts after the DAQmx task is done and the DAQ USB-6341 does not have 
    the property AO.idleOutputBehavior to select "Maintain Existing Value".
    How can I maintain the last AO value after the DAQmx task is done on device DAQ USB-6341 ?
    Thanks.
    Godel

    How many computers do you have in the network?
    What is the IP address and default gateway number on your computer?
    In order to connect to the wireless network, WVC80N should get valid IP address from the access point.
    If the access point has capacity to assign a private IP address to the computers and devices then I think you can use WVC80N.

  • Msg 2601, Level 14, State 1, Procedure sp_flush_commit_table, Line 15 Cannot insert duplicate key row in object 'sys.syscommittab' with unique index 'si_xdes_id'. The duplicate key value is (2238926153). The statement has been terminated.

    I am using SQL server 2008 R1 SP3. And when we are doing back up operations we are facing the below error
    Msg 2601, Level 14, State 1, Procedure sp_flush_commit_table, Line 15
    Cannot insert duplicate key row in object 'sys.syscommittab' with unique index 'si_xdes_id'. The
    duplicate key value is (2238926153).
    The statement has been terminated.
    Please assist me with your inputs.
    Thanks,
    Rakesh.

    Hello,
    Did you enable change tracking on the database? If so, please try to disable and re-enable the change tracking.
    The following thread is about the similar issue, please refer to:
    http://social.msdn.microsoft.com/forums/sqlserver/en-US/c2294c73-4fdf-46e9-be97-8fade702e331/backup-fails-after-installing-sql2012-sp1-cu1-build-3321
    Regards,
    Fanny Liu
    Fanny Liu
    TechNet Community Support

  • New measurement starts with the last measurement value before program stop

    New measurement starts with the last measurement value before program stop. This is very embarrassing because we finish at a high measured value and start with small value, which can not be seen on the autoscale screen due to the memorized high first value. We use NI PCI-6220 DAQ board and software written in LabView 7.1 using Express VIs and running in the LabView environment. The program (attached) every half second measures 3 AI channels, filters and scales the data, visualizes, averages and saves it in a file. Please advise me how can I avoid this problem. Please note that I am not very experienced in LabView.
    Attachments:
    OLED_Monitor2.vi ‏785 KB

    I can not run your vi, but if you initialize your controls you should be ok.
    Look at the picture
    Attachments:
    Clipboard-2.jpg ‏16 KB

  • Making an average of the last 100 values?

    Hi everyone,
    I am acquiring a signal every 1000mS with Labview 7.1. I need to make an average of the last 100 values every 1000mS. I know how to do this with shift registers but it would be a bit long to do it this way...There must be a better way
    Would anybody have an example of an array that takes into consideration the new value that has just been acquired, so that I can make an average of the array?
    Thank you very much for your help,
    Toto

    Hi Toto,
    Here is the first thing I thought of.  Initialize an array of 1000 elements, and replace an element each iteration of the loop.  Use the Quotient and Remainder function along with the iteration count of the loop to replace the oldest element in the array.  Add the array and divide by 1000 in each iteration to get the current average.  I've attached the VI (saved in LabVIEW 7.1), along with a screenshot for anyone who wants to see it but doesn't have LabVIEW 7.1 or later.
    Good luck,
    -D
    Message Edited by Darren on 02-05-2006 07:40 PM
    Darren Nattinger, CLA
    LabVIEW Artisan and Nugget Penman
    Attachments:
    Running_average.vi ‏11 KB
    Running_average.jpg ‏32 KB

Maybe you are looking for