Problem with Top N Query when no rows returned (takes forever)
I have a table with 100 Million rows and I want to get the latest N records using:
SELECT * FROM
(SELECT * FROM tablename WHERE columnA= 'ABC' ORDER BY TIME DESC)
WHERE rownum <= N;
This works fine and is very fast when there are rows with columnA= 'ABC' but when there are no rows with columnA= 'ABC' the query takes forever.
The strange things is that the inner query returns immediately when run on it's own when no rows with columnA= 'ABC' exist e.g.
SELECT * FROM tablename WHERE columnA= 'ABC' ORDER BY TIME DESC
So why does it take for ever for to run:
SELECT * FROM (no rows inner query) WHERE rownum <= N;
I have also tried using:
SELECT * FROM
(SELECT columnA, rank() over(ORDER BY TIME DESC) time_rank
FROM tablename WHERE columnA='ABC')
WHERE time_rank <= N;
which returns instantly when there are now rows but takes much longer than the first query when there are rows.
I cannot see a real difference:With histogram we can see a difference on the elapse when no row returned and into explain plan.
SQL> drop table tablename
2 /
Table dropped.
Elapsed: 00:00:00.03
SQL>
SQL> create table tablename
2 as
3 select sysdate - l time
4 , decode(trunc(dbms_random.value(1,10)),1,'ABC',2,'DEF',3,'GHI',4,'JKL','MNO') as columnA
5 from (select level l from dual connect by level <= 1000000)
6 /
Table created.
Elapsed: 00:01:19.08
SQL>
SQL> select columnA,count(*) from tablename group by columnA
2 /
COL COUNT(*)
ABC 110806
DEF 111557
GHI 111409
JKL 111030
MNO 555198
Elapsed: 00:00:05.05
SQL>
SQL> create index i1 on tablename(time)
2 /
Index created.
Elapsed: 00:00:34.08
SQL>
SQL> create index i2 on tablename(columna)
2 /
Index created.
Elapsed: 00:00:30.08
SQL>
SQL> exec dbms_stats.gather_table_stats(user,'TABLENAME',cascade=>true)
PL/SQL procedure successfully completed.
Elapsed: 00:01:18.09
SQL>
SQL> set autotrace on explain
SQL> SELECT * FROM
2 (SELECT * FROM tablename WHERE columnA= 'ABC' ORDER BY TIME DESC)
3 WHERE rownum <= 10
4 /
TIME COL
17/09/06 ABC
12/09/06 ABC
08/09/06 ABC
07/09/06 ABC
25/08/06 ABC
22/08/06 ABC
13/08/06 ABC
08/07/06 ABC
14/06/06 ABC
01/05/06 ABC
10 rows selected.
Elapsed: 00:00:01.04
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2364 Card=10 Bytes=120)
1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=2364 Card=200000 Bytes=2400000)
3 2 SORT (ORDER BY STOPKEY) (Cost=2364 Card=200000 Bytes=2400000)
4 3 TABLE ACCESS (FULL) OF 'TABLENAME' (Cost=552 Card=200000 Bytes=2400000)
SQL>
SQL> SELECT * FROM
2 (SELECT * FROM tablename WHERE columnA= 'MNO' ORDER BY TIME DESC)
3 WHERE rownum <= 10
4 /
TIME COL
20/09/06 MNO
19/09/06 MNO
16/09/06 MNO
14/09/06 MNO
13/09/06 MNO
10/09/06 MNO
06/09/06 MNO
05/09/06 MNO
03/09/06 MNO
02/09/06 MNO
10 rows selected.
Elapsed: 00:00:02.04
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2364 Card=10 Bytes=120)
1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=2364 Card=200000 Bytes=2400000)
3 2 SORT (ORDER BY STOPKEY) (Cost=2364 Card=200000 Bytes=2400000)
4 3 TABLE ACCESS (FULL) OF 'TABLENAME' (Cost=552 Card=200000 Bytes=2400000)
SQL>
SQL> SELECT * FROM
2 (SELECT * FROM tablename WHERE columnA= 'PQR' ORDER BY TIME DESC)
3 WHERE rownum <= 10
4 /
no rows selected
Elapsed: 00:00:01.01
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2364 Card=10 Bytes=120)
1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=2364 Card=200000 Bytes=2400000)
3 2 SORT (ORDER BY STOPKEY) (Cost=2364 Card=200000 Bytes=2400000)
4 3 TABLE ACCESS (FULL) OF 'TABLENAME' (Cost=552 Card=200000 Bytes=2400000)
SQL> set autot off
SQL>
SQL> EXECUTE DBMS_STATS.GATHER_TABLE_STATS(user,'TABLENAME',METHOD_OPT => 'FOR COLUMNS SIZE 250 columna')
PL/SQL procedure successfully completed.
Elapsed: 00:00:09.08
SQL>
SQL> set autotrace on explain
SQL> SELECT * FROM
2 (SELECT * FROM tablename WHERE columnA= 'ABC' ORDER BY TIME DESC)
3 WHERE rownum <= 10
4 /
TIME COL
17/09/06 ABC
12/09/06 ABC
08/09/06 ABC
07/09/06 ABC
25/08/06 ABC
22/08/06 ABC
13/08/06 ABC
08/07/06 ABC
14/06/06 ABC
01/05/06 ABC
10 rows selected.
Elapsed: 00:00:01.03
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1434 Card=10 Bytes=120)
1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=1434 Card=110806 Bytes=1329672)
3 2 SORT (ORDER BY STOPKEY) (Cost=1434 Card=110806 Bytes=1329672)
4 3 TABLE ACCESS (FULL) OF 'TABLENAME' (Cost=552 Card=110806 Bytes=1329672)
SQL>
SQL> SELECT * FROM
2 (SELECT * FROM tablename WHERE columnA= 'MNO' ORDER BY TIME DESC)
3 WHERE rownum <= 10
4 /
TIME COL
20/09/06 MNO
19/09/06 MNO
16/09/06 MNO
14/09/06 MNO
13/09/06 MNO
10/09/06 MNO
06/09/06 MNO
05/09/06 MNO
03/09/06 MNO
02/09/06 MNO
10 rows selected.
Elapsed: 00:00:02.05
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6219 Card=10 Bytes=120)
1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=6219 Card=555198 Bytes=6662376)
3 2 SORT (ORDER BY STOPKEY) (Cost=6219 Card=555198 Bytes=6662376)
4 3 TABLE ACCESS (FULL) OF 'TABLENAME' (Cost=552 Card=555198 Bytes=6662376)
SQL>
SQL> SELECT * FROM
2 (SELECT * FROM tablename WHERE columnA= 'STU' ORDER BY TIME DESC)
3 WHERE rownum <= 10
4 /
no rows selected
Elapsed: 00:00:00.00
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=1 Bytes=12)
1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=6 Card=1 Bytes=12)
3 2 SORT (ORDER BY STOPKEY) (Cost=6 Card=1 Bytes=12)
4 3 TABLE ACCESS (BY INDEX ROWID) OF 'TABLENAME' (Cost=5 Card=1 Bytes=12)
5 4 INDEX (RANGE SCAN) OF 'I2' (NON-UNIQUE) (Cost=4 Card=1)
SQL> Nicolas.
Similar Messages
-
What is the problem with native dynamic sql when counting rows in all table
what is the problem with native dynamic sql when counting rows in all table?Giving an error "table or view does not exist". Thanks.
DECLARE
v_sql_string varchar2(1000);
v_no_of_rows number;
BEGIN
for i in ( select table_name from all_tables )
loop
v_sql_string := ' select count(1) from ' || i.table_name;
dbms_output.put_line( v_sql_string );
--execute immediate v_sql_string into v_no_of_rows;
end loop;
END;Usually your problem can be described with 'Who cares'. I mean, for what reason do you do this? I doubt that there's a business need to get 100 % accurate answers for this. Normally such things are used to get a picture about the growth of data.
Personally I would prefer to have up-to-date statistics for all tables and just query the number of rows from there. Sufficient for me in < 99 % of all cases.
Just my $ .02... -
Problems with Input Ready Query
Hello All,
I'm facing problems with input ready query for BI IP.
I've created the input query on aggregation level and maintianed all the planning settings. It is not allowing me to edit, when i'm attaching in the web template.
I also attached a button copy function, and i'm able to execute it and save it, but it not allowing me to change manually.
I also enabled the 3rd option for keyfigures...data can be changed with planning functions and user input.
Please help me in this regard.
We have just started the development of BI-IP, please suggest me if there are any notes to be applied.
Regards
KumarHello Johannes,
Yes I've set to the finest granularity...even if it is Only one characteristic and one key figure without any restrictions...
I've checked even planning tab page under properties...it is also fine..
But still it is not allaowing me to go to edit mode...this is a fresh instalation and the query i'm working is the first one...
Please suggest me if there are any notes to be applied, we are on Support Pack 10.
Regards
Jeevan Kumar -
When I turn my computer on (Windows 7) I receive a message that Apple wants to make a change to the hardware. I click on yes but nothing happens. The change has something to do with Apple "push". I recently had a problem with"push" on iTunes when I tried to sync my iPod Touch. I have latest software for iTunes and iPod. Can anyone tell me what to do to get this change to install?
ptford wrote:
When I turn my computer on (Windows 7) I receive a message that Apple wants to make a change to the hardware
Since hardware cannot be changed with software, perhaps you would be so kind as to post the exact wording of the message? -
i have some problem with my computer because when i turn it up, the screen turns in a blue colour and the sound doesn't work and in this way i can't hear anything
Will u help me or not?
-
I have Windows 7 and just recently experienced a problem with itunes freezing up when I clicked on the "apps" tab which shows the apps that are loaded on my phone. Everything else in the itunes software seems to be working okay. The problem is in the part where you sync the newly purchased apps. The apps will all be blank that are downloaded and that part of the software crashes itunes or freezes up. I can get out of itunes okay after the freeze up, but it just stops me from syncing anything that I have newly added. If anyone has had this problem before or knows someone that has, please let me know how I can fix it. I have uninstalled and reloaded itunes 4 or 5 times, but the problem is still there. I might add that I have been using itunes and purchasing apps for 2 to 3 years without any problems. HELP.
You would get better response from the iTunes community forum.
Have a nice day! -
I have a problem with two PDF's when trying to open them through a link on a web page. The two PDF's open fine with Adobe on my own PC and on the server I have copied it to but when they are opened through a link on a web page (pointing to the server where the PDFs open fine) I get an error 'There was an error processing a page. Invalid function resource' The other one just doesn't open at all. Can anyone help with this please?
Hello,
Are the pdf linked correctly in the website? Is this a public website? If yes, please post the link here.
~Deepak -
Ive used compressor and then tried to put it in dvdsp put its not working. I am having problems with the audio or when i burn it , i cant play the dvd? any help would be appreciated
I suspect you are attempting to burn an HD DVD, which will not play on a DVD player...
But if you provide more details such as the settings you are applying in Compressor, then we would be able to provide you more accurate information.
How are you adding the files to Compressor?
In FCP, what are your footage properties (command-9) and sequence settings (command-0) -
Anyone having problems with Elements organiser 13 when try to import photos from Mac photos which was an upgrade from Iphotos on 9th April 2015. Before when I was using Iphotos all was ok...
hi I have been trying to do this, but PE13 comes up with a fail message saying that file types are not supported, am only trying to import jpeg files so can't understand this. So far have been unable to use PE13 at all, any ideas anyone please!
-
Im have a problem with my adobe id when i try to covert pdf files.
Im have a problem with my adobe id when i try to covert pdf files.
I think your question should go in the ExportPDF forum,
http://forums.adobe.com/community/exportpdf -
Hello I have encounted a problem with Photoshop. Normally when you start a new documant, you are given the options to have A4, A3, ect page set ups. These inbuilt page set ups seem to have disapeared, does anyone please know how to fix this? Many thanks Ben [email protected]
Hello Trevor
Thanks for your message. You can custom set pages but there are no preset of page sizes like A4, A3, ect. I cannot click the size preference, nothing comes up
Ben -
hi, i have problem with my apple id when i want purchase app even free one, asked me for complete my card informations... but i don't want to use debit card because already i have gift card and charge it in my id... please help me.
when i unchek the card information it was show me an error!Though you are being asked for card details it doesn't necessarily mean that the card will be charged for the purchase (unless you want to gift content). Have you tried entering your card's details to confirm the account ? You should then be able to remove it.
From http://support.apple.com/kb/HT5582 :
When making purchases, content credits are used first, followed by Gift Certificate, iTunes Card, or Allowance Account credits; your credit card or PayPal account is then charged for any remaining balance
Is it a new account ? If it is then unless a new account is created by following the instructions on this page (the instructions won't work with existing accounts) : http://support.apple.com/kb/HT2534
then you will need to enter card details (debit cards are no longer accepted in all countries) before the account can be used to download any item from the store. -
I have a problem with elements 8. When ever I start a new project, the program searches for the old, latest used filmmaterial, I have used before. That means, if I use a different DVD, it take so much time, until the program starts. So how can I stopp the automatically uploading of old material?
ThanksYou have 2 unreachable statements in this method.
public static int eval(String s2, String op, String s3) {
return lookup(s2);
return lookup(op);
return lookup(s3);
} You're missing a } at the end of this method:
public static int lookup(String s) {
for(int k = 0; k < symbols.length; k++){
String symbol = symbols[k];
if(s.equals(symbol))
return k;
}You have some loose } and ; at the end of the file:
public static void main(String args[])
commandline();
} -
IS ANY ONE HAVING PROBLEM WITH LIGHTROOM CC BRUSH WHEN THE GPU IS TURN ON
F. McLion wrote:
Update your graphic card driver to the latest version.
btw.: Rule number 2 - Do not use all caps - Netiquette Wiki
And for a very practical reason from your point of view: using all caps looks like SHOUTING, and people will concentrate more on the SHOUTING than on answering your question. Some will just ignore you for this reason.
If updated drivers don't fix the problem, perhaps you can tell us what is the "PROBLEM WITH LIGHTROOM CC BRUSH WHEN THE GPU IS TURN ON" that you are having. -
My daughter's older Ipod Touch is having a problem with images becoming pixilated when taken from an e-mail and placed into her camera roll. ( She wants to put on Instagram ) Any help?
I think that may be a consequence of the email provider she is using. Some services (AOL is one example) inspect and modify email attachments, particularly photos, resulting in reduced resolution compared to the email that was sent.
The iPod does not do this. The resolution of the picture in the email she receives will be identical to what is imported.
Maybe you are looking for
-
How do I get the latest drivers for windows 7 boot camp install?
I have a 24" mac early 2009. I had windows xp installed on a partition using an early edition of boot camp. I had to reformat my mac and so I purchased windows 7 home premium oem edition. When I try to install and copy boot camp drives to a 32 gb us
-
Captiure duties for import PO..?
hi Pls guide me with more clear picture in excise duty capture and posting for import PO. whether i have to maintain any tax code with excise duties , or i have to maintain zero + nil excise duties in PO. and when i have capture and post excise dutie
-
Hi, We are on R12.0.4. I want to know is there any report which gives me the Supplier Balance. Can we use Open Accounts Payables balance Listing report for the same. Regards/Prasanth
-
just completed the upgrade to os x Yosemite on my MacBook 5,4 only to find that iPhoto will not open. It came up with a message that I go to the app store and upgrade to the latest version of iPhoto , however the App Store also refuses to open. I wou
-
PSCC2014 dodge/burn weak?
Installed on my WIN 7-64 system are PS CS6, PS CC and PS CC2014 (each used with a Wacom Intuos 4 tablet, with latest driver). In CC 2014, I notice that now the dodge / burn tool (s) seem to be significantly 'weakened': what used to take a few brush s