Sql Query Plan with ROWNUM
Oracle 10g:
I have a table with index, when I run that query on that index it's fine. I get into problem if I add ROWNUM in that query, query slows down from 1 sec to 30 sec. I don't know why. for eg:
select * from (select * from A where b = 1) where ROWNUM < 1000 becomes very slow:
Query Plan for select * from A where b = 1 (Query is just an example):
Rows Plan
208912 SELECT STATEMENT
208912 SORT ORDER BY
208912 HASH JOIN RIGHT OUTER
408 INDEX FULL SCAN PK_BAD_ACK_TASK
208912 HASH JOIN RIGHT OUTER
16 INDEX FULL SCAN PK_INFORMATIONAL_TASK
208912 HASH JOIN RIGHT OUTER
1 INDEX FULL SCAN PK_SYSTEM_ERROR_TASK
208912 HASH JOIN RIGHT OUTER
1 INDEX FULL SCAN PK_REJECTED_TRANSMISSION
208912 HASH JOIN RIGHT OUTER
1 INDEX FULL SCAN PK_ONHOLD_TASK
208912 HASH JOIN RIGHT OUTER
329465 INDEX FAST FULL SCAN PK_FAILED_MESSAGE
208912 TABLE ACCESS FULL TASK
Query when I add ROWNUM (slow query as mentioned above):
Rows Plan
999 SELECT STATEMENT
<NULL> COUNT STOPKEY
208912 VIEW
208912 SORT ORDER BY STOPKEY
208912 HASH JOIN RIGHT OUTER
408 INDEX FULL SCAN PK_BAD_ACK_TASK
208912 NESTED LOOPS OUTER
208912 NESTED LOOPS OUTER
208912 NESTED LOOPS OUTER
208912 HASH JOIN RIGHT OUTER
329465 INDEX FAST FULL SCAN PK_FAILED_MESSAGE
208912 HASH JOIN RIGHT OUTER
16 INDEX FULL SCAN PK_INFORMATIONAL_TASK
208912 TABLE ACCESS FULL TASK
1 INDEX FAST FULL SCAN PK_ONHOLD_TASK
1 INDEX FAST FULL SCAN PK_REJECTED_TRANSMISSION
1 INDEX FAST FULL SCAN PK_SYSTEM_ERROR_TASK
user628400 wrote:
Oracle 10g:
I have a table with index, when I run that query on that index it's fine. I get into problem if I add ROWNUM in that query, query slows down from 1 sec to 30 sec. I don't know why. for eg:Some notes:
* When using ROWNUM the cost based optimizer switches to a FIRST_ROWS_N mode, this might explain the significant difference in the execution plan
* Try to get a more meaningful output using DBMS_XPLAN.DISPLAY
* Since you're already on 10g, try to compare the actual cardinalities to the estimates using DBMS_XPLAN.DISPLAY_CURSOR with the "ALLSTATS LAST" option and the GATHER_PLAN_STATISTICS hint.
See e.g. here for more information how to do it: http://jonathanlewis.wordpress.com/2006/11/09/dbms_xplan-in-10g/
This way you should be able to tell where the optimizer assumptions go wrong so that it thinks that the second one is better than the first one
A simple remedy you might want to try that should get you back to plan 1 is the following:
select * from (
select ROWNUM as rn, a.* from (select * from A where b = 1 ORDER BY<your_order_criteria>) a
where rn < 1000;Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/
Edited by: Randolf Geist on Jan 11, 2009 9:33 PM
Added the obvious sort ORDER BY
Similar Messages
-
How to numberformat when using sql:query alogn with c:forEach JSTL tags
Is there anyway to format the numeric values returned from the database when using <sql:query> alogn with <c:forEach> tags
Here is my jsp code
<sql:query..../>
<c:forEach var="row" items="${queryResults.rows}">
<tr>
<td><c:out value="${row.COL1}" /></td>
<td><c:out value="${row.COL2}" /></td>
</tr>
</c:forEach>
Col1 values are numeric without any formats Eg: 1000, 10000, 1000000 etc.
how can i format them to 1,000 , 10,1000 , 100,000 etcIt is polite to mention what your answer was. These posts are not just here for you to ask questions, but to be used as a resource for other people to find answers. Saying "I solved it" with no details helps noone.
I presume you discovered the JSTL <fmt:formatNumber> tag? -
SQL Query fails with Fetch error
We are running JDE ERP 9.0 and have a report that gets this error
“JDB3300020 - Fetch not allowed. Prior successful Select operation required for this request.”
I’ve been troubleshooting this problem for months, and I am no closer to the root cause and need some help. I’ve engaged Microsoft support, we collected a trace but could not find anything that pointed to the cause.
Execution details:
Report runs successfully after database server is rebooted. Otherwise we have to keep rerunning as many as 15 times to get a successful run. When we discovered that report would run after the server reboot, we decided
to only restart SQL, to see if it was Query plan related but it still failed. How can we determine why the reboot is making a difference?
Any ideas/help is welcome.Using Profiler to capture what JD Edwards sends to SQL Server seems like a good idea. And include the events Error:Exception and Error:UserMessage to see if that captures anything.
I googled on the message, and I found
http://www.jdelist.com/ubb/showflat.php?Cat=&Number=111077&page=0&view=collapsed&sb=5&o=
where the cause was that different indexes were used for SELECT and FETCH. (Whatever that means; I was not able to grasp it.)
Erland Sommarskog, SQL Server MVP, [email protected] -
SQL query slow with call to function
I have a SQL query that will return in less than a second or two with a function in-line selected in the "from" clause of the statement. As soon as I select that returned value in the SQL statement, the statement takes from anywhere from 2 to 5 minutes to return. Here is a simplified sample from the statement:
This statement returns in a second or 2.
select A.pk_id
from stu_schedule A, stu_school B, stu_year C, school_year D,
(select calc_ytd_class_abs2(Z.PK_ID,'U') ytd_unx
from stu_schedule Z) II
where B.pk_id = A.fk_stu_school
and C.pk_id = B.fk_stu_year
and D.pk_id = C.year
and D.school_year = '2011';
if I add this function call in, the statement runs extremely poor.
select A.pk_id,
II.ytd_unx
from stu_schedule A, stu_school B, stu_year C, school_year D,
(select calc_ytd_class_abs2(Z.PK_ID,'U') ytd_unx
from stu_schedule Z) II
where B.pk_id = A.fk_stu_school
and C.pk_id = B.fk_stu_year
and D.pk_id = C.year
and D.school_year = '2011';
Here is the function that is called:
create or replace FUNCTION calc_ytd_class_abs2 (p_fk_stu_schedule in varchar2,
p_legality in varchar2) return number IS
l_days_absent number := 0;
CURSOR get_class_abs IS
select (select nvl(max(D.days_absent),'0')
from cut_code D
where D.pk_id = C.fk_cut_code
and (D.legality = p_legality
or p_legality = '%')) days_absent
from stu_schedule_detail B, stu_class_attendance C
where B.fk_stu_schedule = p_fk_stu_schedule
and C.fk_stu_schedule_detail = B.pk_id;
BEGIN
FOR x in get_class_abs LOOP
l_days_absent := l_days_absent + x.days_absent;
END LOOP;
return (l_days_absent);
END calc_ytd_class_abs2;Query returns anywhere from 6000 to 32000 rows. For each of those rows a parameter is passed in to 4 different functions to get ytd totals. When I call the functions in the in-line view but do not select from them in the main SQL, the report (oh, this is Application Express 4.0 interactive reports, just an FYI) runs fast. The report comes back in a few seconds. But when I select from the in-line view to display those ytd totals, the report runs extremely slow. I know there are the articles about context switching and how mixing SQL with PL/SQL performs poorly. So I tried a pipeline table function where the function for the ytd totals populate the columns of the pipeline table and I select from the pipeline table in the SQL query in the interactive report. That seemed to perform a little worse from what I can tell.
Thanks for any help you can offer. -
Problem in executeQuery(SQL query sentence with a ' );
for example:
String strName="";
//here strName is searching key value.
String strQueryString="SELECT * FROM Authors WHERE name='" strName "'";
ResultSet rs=st.executeQuery(strQueryString);
if strName value is "yijun_lee",that will return all information which the name columns value is "yijun_lee" with SQL Query sentence SELECT * FROM Authors WHERE name='yijun_lee'
but if strName value is "yijun ' lee",that value contains a '.that will error!
how to do?
thanks very much!You could parse <strName > and insert another ' for each '
A concrete example would be SELECT * FROM Authors WHERE name='yijun '' lee'HTH -
SQL Query Result with Random Sorting
Hi Experts,
My Oracle Version : Oracle9i
I have three tables which are given below,
Table Name: check_team
team_id team_code
100 A
101 B
102 C
103 D
Table Name: check_product
product_id product_code
1 XXX
2 XYZ
Table Name: check_team_products
tprod_id tprod_team_id tprod_product_id
1 100 1
2 100 2
3 101 1
4 101 2
5 102 1
6 102 2
7 103 1
8 103 2
Required Output First Time:
team_id team_code product_id product_code
100 A 1 XXX
101 B 2 XYZ
102 A 1 XXX
103 B 2 XYZ
Required Output Second Time:
team_id team_code product_id product_code
100 B 2 XYZ
101 A 1 XXX
102 B 2 XYZ
103 A 1 XXXI need the result as Required Output specified above and also the result has to be random too.. Can someone help me in writing a SQL Query to get results as that?
Added Oracle VersionSo, is it something like this you want?
SQL> ed
Wrote file afiedt.buf
1 with check_team as (select 100 as team_id, 'A' as team_code from dual union all
2 select 101, 'B' from dual union all
3 select 102, 'C' from dual union all
4 select 103, 'D' from dual)
5 ,check_product as (select 1 as product_id, 'XXX' as product_code from dual union all
6 select 2, 'XYZ' from dual)
7 ,check_team_products as (select 1 as tprod_id, 100 as tprod_team_id, 1 as tprod_product_id from dual union all
8 select 2, 100, 2 from dual union all
9 select 3, 101, 1 from dual union all
10 select 4, 101, 2 from dual union all
11 select 5, 102, 1 from dual union all
12 select 6, 102, 2 from dual union all
13 select 7, 103, 1 from dual union all
14 select 8, 103, 2 from dual)
15 --
16 -- end of test data
17 --
18 select team_id, team_code, product_id, product_code
19 from (
20 select t.team_id, t.team_code, p.product_id, p.product_code
21 ,row_number() over (partition by team_id order by dbms_random.random()) as rn
22 from check_team t join check_team_products tp on (tp.tprod_team_id = t.team_id)
23 join check_product p on (p.product_id = tp.tprod_product_id)
24 )
25* where rn = 1
SQL> /
TEAM_ID T PRODUCT_ID PRO
100 A 2 XYZ
101 B 1 XXX
102 C 2 XYZ
103 D 1 XXX
SQL> /
TEAM_ID T PRODUCT_ID PRO
100 A 2 XYZ
101 B 1 XXX
102 C 2 XYZ
103 D 1 XXX
SQL> /
TEAM_ID T PRODUCT_ID PRO
100 A 1 XXX
101 B 2 XYZ
102 C 1 XXX
103 D 1 XXX -
SQL query result with HTML Data in output
Hello,
I have a SQL table , in one column I store HTML data. I need to query the table and get the HTML data in the columns that have 'HREF'. The output shows as grid on the sql management studio, however when I export it to excel, the HTML data does not get copied
correctly, since there are HTML tags etc.
How can I export the report correctly from SQL ?Hello,
The HTML data is stored in a column with datatype as nvarchar(max). Sample data in the column is shown below. It is with formatting etc and is rendered as is on the web page. the business wants to generate a quick report so that they can see the pages that
have links displayed. I can do that by querying the columns that have a 'HREF' in the text.
Can I get the exact HREF values using just sql query? There can be more than one links on a page.
Also, If I just want to copy the whole column and paste it on excel, how can I do that? If I copy the data below and paste, it does not get copied in one cell.. it spreads across multiple cells, so the report does not make any sense.
<br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 431pt; border-collapse: collapse;" width="574">
<tbody>
<tr height="19" style="height: 14.25pt; ">
<td height="19" style="border: 0px blue; width: 431pt; height: 14.25pt; background-color: transparent;" width="574"><a href="https:"><u><font color="#0066cc" face="Calibri">ax </font></u></a></td>
</tr>
</tbody>
<colgroup>
<col style="width: 431pt; " width="574" />
</colgroup>
</table> -
SQL Query execution with LISTAGG
Hello,
I am trying to get some data using LISTAGG functionality.
I have below query with works fine with the table, but not with the rowsource which is based on same table.
SELECT COL1,LISTAGG(COL3, ',') WITHIN GROUP (ORDER BY COL2) AS fieldName FROM IOP_Sample_RS GROUP BY COL1it gives parsing error
line 1:22: expecting "FROM", found '('col 22 is '(' of LISTAGG function.
One more Question
Does UNION, MINUS works with RSQL in IOP?
Thanks,
Sumant Chhunchha.
Edited by: Sumant on Jun 15, 2011 5:04 AMLISTAGG, UNION, MINUS is not supported with RSQL, instead you can massage your SQL query to load data into IOP and then use a simple RSQL query.
-
Hi all I am looking at the Query plan of a store proc. I found a very strange situation:
There is an index seek of a table which DOES NOT query inside the SP..how does it happen~?
i search around the SP , no where I can find a word related to that table. (Actually the table and index is really there inside the database!!!! )
I am using SQL 2008 R2 SP2, Standard version.Help.I this perhaps an INSERT statement? In that case, the target table might have a foreign key constraint that references another table so the plan includes a seek to enforce the constraint.
Dan Guzman, SQL Server MVP, http://www.dbdelta.com -
ORA-06502 on 'SQL query' report with sort in 'Report Attributes'
Hi All,
We get the next error if we set sorting on a column in a 'Report' based on a 'SQL query'. Removing the sort, error disappeares:
failed to parse SQL query:
ORA-06502: PL/SQL: numeric or value error: NULL index table key value
Any suggestions?
ErikErik,
Thanks, this explains it. By specifying the request as part of your URL you run into the recently uncovered issue. The request you're setting is REMFROMLIST and ADD2LIST. You probably either have links that include those requests or you have branches where you specify them. Either way, in order to get reports sorting to work, you'll have to make sure that the request strings are not part of your URL. This is a work-around and the upcoming HTML DB patch release will solve this issue.
One way of avoiding this is to have computations on the previous pages that set a napplication level or page level item to the REMFROMLIST and ADD2LIST values and then you can use those items for your conditions that are currently evaluating those strings.
Hope this helps and sorry for the inconvenience,
Marc -
SQL query problem with sorting
Hi,
I have question regarding sql query . Right now I am getting the results like this if i use this sql query
select ID,Name,Desc,Priority from emp order by Priority ;
Priority is varchar field. I don't want to change the Priority field and cannot add a new column in the table. Because i don't have permission to do that.
ID Name Desc Priority
=============================================
234 paul paul desc Highest
3452 mike mike desc High
4342 smith smith desc Low
6565 kelly kelly desc Low
9878 nate nate desc Medium
3223 deb deb desc High
============================================
I need a query to get the results like that.
ID Name Desc Priority
=============================================
234 paul paul desc Highest
3452 mike mike desc High
3223 deb deb desc High
9878 nate nate desc Medium
4342 smith smith desc Low
6565 kelly kelly desc Low
============================================
If any one knows about this one, please let me know.
Thanks,
BalaYou are aware that there are differences in the SQL implementation between Sqlserver and Oracle? You could try something like this, if there's a INSTR function:
ORDER BY INSTR('Highest,High,Medium,Low,', Priority || ',')You may have to change the "Priority || ," to a "Priority + ','), if string concatenation is done differently in sqlserver. Don't know about the ('), maybe you need (").
C. -
SQL Query issue with large varchar column
I have a SQL Query (PL/SQL function body returning SQL query) which contains a large varchar column. Even if I substring the column to 30chars when it displays on the page it wraps. I have tried putting nowrap="wrap" for the HTML table cell attributes and it still wraps. I have tried setting the width attributes on the column even though it's not an updateable column. Does anyone have any ideas on how prevent this from wrapping. In some cases 1 line will take up 3 because of this wrapping issue and it's not nice to look at. It seems that the column is somewhere set to a fixed width, which is less than 30 characters, and anything beyond this fixed width wraps.
Hi Netha,
Can you please provide the DDLs of three tables you are using,
Also post us how many rows you are getting output for this query?
select * from dim.store st where
st.store_code = 'MAUR'
also try to run and update statement on this table as below and execute your query
update dim.store
set store_code
= ltrim(rtrim(store_code))
where
store_code = 'MAUR'
once you run this update, then run your query. Let us know the result. -
SQL Query problems with Mysql - possible Java bug
I have a query that works fine on mysql, but does not work in my jsp page.... my db connection and all that jazz is fine.
select somefields from table order by (integerfield / doublefield)
two notes... the query works without the order by. And the query works with the order by when I'm working directly with the db. Any ideas? This is driving me nuts!!!!
Thanks,
TysonNevermind, just a dumb mistake... finally figured it out.
-
SQL query validation with XML?
Hi
Is there anything out there that will validate an SQL statement (validate meaning the SQL statement 'will run') using XML?
I want to make a servlet that takes SQL and adds it to a java class that will in turn do a bunch of magical things. However
I need the SQL to be validated before it gets written to the class.
XML seems like it would be a useful tool to accomplish this but I fear the
document would be friggen huge and run slow as hell.
I guess in essence it would be like creating an SQL compiler completely
out of XML.
Does something like this exist or is this solution just to broad to realistically consider?There is no kind of relashionship between SQL and XML. Moreover, with RDBMs having different flavors of SQL, how can you expect an XML parser to be able to tell you that your query will run on Oracle but non on SQL Server?
-
Pl/sql function body returning SQL query - if with in select statement
hello all,
I have a condition where when summary field is checked alone we have show that column for the users in the report. Is IF statement possible for this case ? I have given the code below.. is it possible to write something like below or is there any other way to do it ?
v_sql := 'select TBLCASES.INVESTIGATOR as INVESTIGATOR,';
v_sql := v_sql ||' TBLCASES.CASENUMBER as CASENUMBER,';
v_sql := v_sql ||' TBLCASES.OPENDATE as OPENDATE,';
v_sql := v_sql ||' TBLCASES.ESTCOMPLETE as TARGETDATE,';
v_sql := v_sql ||' TBLCASES.STATUS as STATUS,';
v_sql := v_sql ||' TBLCASES.CASECODE as CASECODE,';
v_sql := V_sql ||' TBLCASES.FAIR_HOTLINE as FAIRHotline,';
v_sql := v_sql ||' TBLCASES.NYSIG as NYSIGCase,';
v_sql := v_sql ||' TBLCASES.REGION as Region,';
IF :P44_INCLUDE_SUMMARY_FIELD is not null THEN
v_sql := v_sql||' TBLCASES.SUMMARY as SUMMARY,';
END IF ;
v_sql := v_sql ||' TBLCASES.PROGAREA as PROGArea ';
v_sql := v_sql ||' from TBLCASES where 1=1';
..\Hi Lucy,
You are adding and removing a column from a report so it may help to have the extra on the end of the query.
Do you get an error? If so paste it in...
BUT - this I would put a condition on the column where ':P44_INCLUDE_SUMMARY_FIELD is not null '
Under he report's "Column Attributes" Select the column edit icon and it has a "Condition" breadcrumb.
Select "Value of Item in Expression 1 is Not Null" and put P44_INCLUDE_SUMMARY_FIELD in Expression 1 text area.
And I make it more readable like this when I do dynamic SQL:
IF :P44_INCLUDE_SUMMARY_FIELD is not null THEN
v_INCLUDE_SUMMARY_FIELD := ' TBLCASES.SUMMARY as SUMMARY ';
END IF ;
v_sql := 'select TBLCASES.INVESTIGATOR as INVESTIGATOR,
TBLCASES.CASENUMBER as CASENUMBER,
TBLCASES.OPENDATE as OPENDATE,
TBLCASES.ESTCOMPLETE as TARGETDATE,
TBLCASES.STATUS as STATUS,
TBLCASES.CASECODE as CASECODE,
TBLCASES.FAIR_HOTLINE as FAIRHotline,
TBLCASES.NYSIG as NYSIGCase,
TBLCASES.REGION as Region,
TBLCASES.PROGAREA as PROGArea,'||
v_INCLUDE_SUMMARY_FIELD ||
'from TBLCASES where 1=1; ';
Hope it helps,
BC
Maybe you are looking for
-
Problem with application !!!Urgent need Help please!!!
Hello i am trying to make a 2d sprite engine. I have a serious problem. When i start the application sometimes the screen is just gray. and no matter how long i leave it it just stays gray however every now and again if i run it works fine. this is r
-
Printing and Preview does not work anymore after 10.9.5 Update
I discovered today three topics not working anymore correctly without having installed or changed any particular setup (besides standard App Store Updates). I meanwhil repaired permissions but with now success, any hint of anyone to help resolve my i
-
Kernel panic when I connect my LAcie quadra 500 GB external disk
As soon as I connect my hard disk I obtain a kernel panic. the veil fall on my screen and I must restart. Somebody could help me thank you in advance configuration macbook pro
-
Error message-itunes detected a problem with av
itunes has detected a problem with your audio config, av plyback may not opperate properly. i get this message when i open itunes. followed other instructions to uninstall quicktime and reinstall. can't play music files or cd's. hit play and nothing
-
AnyConnect Client v3.1 driver error on windows 7
Hello, I used AnyConnect Client v3.0 on my windows 7 machine and worked well. But after automatic upgrade to v3.1 by the VPN server(ASA) and it does not work any more. It seems that VPN authentication is successful but activation of VPN adapter fails