TOP-N query optimization

Hi,
We have a 3rd party GUI component which provides data paging capabilities by using the "ROW_NUMBER" analytic function. Sadly it does that in an inefficient way, because it forces Oracle to perform a large sort only to later discard a large portion of data. By slightly modifying the query, it is possible to fix that (demonstrated below), but the problem is that I can't change the SQL the component issues.
I'd appreciate if anyone has any suggestion whether it's possible to fix that in some way.
Test case:
SQL> create table test (
  2     id           number (10) not null,
  3     c1           number (2) not null,
  4     other_data   varchar2 (200)
  5  );
Table created.
SQL> insert into test
  2  with generator as (
  3      select  --+ materialize
  4         rownum id
  5      from dual
  6      connect by
  7          rownum <= 10000
  8  )
  9  select
10      rownum     id,
11      mod(rownum, 20),
12      rpad('x',200)
13  from
14      generator   v1,
15      generator   v2
16  where   rownum <= 1000000
17  ;
1000000 rows created.
SQL> commit;
Commit complete.
SQL> create index i_1 on test(id, c1);
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'TEST',cascade=>true);
PL/SQL procedure successfully completed.This is the SQL the 3rd party component issues:
SQL> SELECT *
  2    FROM (  SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  3              FROM (SELECT *
  4                      FROM test
  5                     WHERE c1 != 5) t2
  6          ORDER BY t2.id) t1
  7   WHERE t1."rnum" <= 10;Here you can see that in line 3 of the execution plan, the whole table is read and in line 2 it's sorted to find the required 10 rows:
| Id  | Operation                | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
|   0 | SELECT STATEMENT         |      |      1 |        |     10 |00:00:00.93 |   30502 |  30497 |    |          |          |
|*  1 |  VIEW                    |      |      1 |    950K|     10 |00:00:00.93 |   30502 |  30497 |    |          |          |
|*  2 |   WINDOW SORT PUSHED RANK|      |      1 |    950K|     11 |00:00:00.93 |   30502 |  30497 |  4096 |  4096 | 4096  (0)|
|*  3 |    TABLE ACCESS FULL     | TEST |      1 |    950K|    950K|00:00:00.49 |   30502 |  30497 |    |          |          |
Predicate Information (identified by operation id):
   1 - filter("T1"."rnum"<=10)
   2 - filter(ROW_NUMBER() OVER ( ORDER BY "TEST"."ID")<=10)
   3 - filter("C1"<>5)However, by moving the "ORDER BY" to the outermost part of the query, Oracle can use the index I_1 to read only 10 rows from it and stop as soon as it reaches that count. And what's more important, no sorting takes place:
SQL>   SELECT *
  2      FROM (SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  3              FROM (SELECT *
  4                      FROM test
  5                     WHERE c1 != 5) t2
  6           )
  7     WHERE "rnum" <= 10
  8  ORDER BY id;
| Id  | Operation                     | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
|   0 | SELECT STATEMENT              |      |      1 |        |     10 |00:00:00.01 |       6 |      2 |       |       |          |
|*  1 |  VIEW                         |      |      1 |    950K|     10 |00:00:00.01 |       6 |      2 |       |       |          |
|*  2 |   WINDOW NOSORT STOPKEY       |      |      1 |    950K|     10 |00:00:00.01 |       6 |      2 |   213M|  4884K|          |
|   3 |    TABLE ACCESS BY INDEX ROWID| TEST |      1 |    950K|     11 |00:00:00.01 |       6 |      2 |       |       |          |
|*  4 |     INDEX FULL SCAN           | I_1  |      1 |    950K|     11 |00:00:00.01 |       4 |      1 |       |       |          |
Predicate Information (identified by operation id):
   1 - filter("rnum"<=10)
   2 - filter(ROW_NUMBER() OVER ( ORDER BY "TEST"."ID")<=10)
   4 - filter("C1"<>5)Is there any way to "fix" the first query without changing it (maybe using an outline, but I don't see how Oracle can avoid the whole table sort)?
Thanks in advance and regards,
Jure

Thank you very much for the link to DBMS_ADVANCED_REWRITE. I didn't know of its existence and it sure seems a useful tool for situations similar to mine (I'm on 11.2.0.1).
However, I encountered two problems when trying to rewrite my query. Maybe I did something obviously wrong, since that's the first time I use DBMS_ADVANCED_REWRITE:
1) The rewrite doesn't seem to work if the source statement contains an ORDER BY clause, which is an essential part of the query I'm trying to rewrite. I reproduced it here such that if the rewrite takes place, I get back the "rewritten query" text (plus some dummy columns to match the resultset structure of the original query), otherwise 10 rows are returned.
First I setup the rewrite equivalence:
SQL> begin
  2    sys.dbms_advanced_rewrite.declare_rewrite_equivalence (
  3      name             => 'DEMO',
  4      source_stmt      =>
  5      'SELECT *
  6         FROM (  SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  7                   FROM (SELECT *
  8                           FROM test
  9                          WHERE c1 != 5) t2
10               ORDER BY t2.id) t1
11        WHERE t1."rnum" <= 10',
12      destination_stmt => 'select null dummy_a, null dummy_b, ''rewritten query'' dummy_c, 1 dummy_rownum from dual',
13      validate         => FALSE,
14      rewrite_mode     => 'GENERAL'
15    );
16  end;
17  /
PL/SQL procedure successfully completed.Now I try to run the query with the exact same query text as defined in "source_stmt". No rewrite takes place:
SQL> SELECT *
  2    FROM (  SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  3              FROM (SELECT *
  4                      FROM test
  5                     WHERE c1 != 5) t2
  6          ORDER BY t2.id) t1
  7   WHERE t1."rnum" <= 10
  8  /
<10 rows are retireved>However, if I remove the "ORDER BY t2.id" part from the "t1" query block, the rewrite works:
SQL> exec sys.dbms_advanced_rewrite.drop_rewrite_equivalence('DEMO');
PL/SQL procedure successfully completed.
SQL> begin
  2    sys.dbms_advanced_rewrite.declare_rewrite_equivalence (
  3      name             => 'DEMO',
  4      source_stmt      =>
  5      'SELECT *
  6         FROM (  SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  7                   FROM (SELECT *
  8                           FROM test
  9                          WHERE c1 != 5) t2
10               ) t1
11        WHERE t1."rnum" <= 10',
12      destination_stmt => 'select null dummy_a, null dummy_b, ''rewritten query'' dummy_c, 1 dummy_rownum from dual',
13      validate         => FALSE,
14      rewrite_mode     => 'GENERAL'
15    );
16  end;
17  /
PL/SQL procedure successfully completed.
SQL> SELECT *
  2    FROM (  SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  3              FROM (SELECT *
  4                      FROM test
  5                     WHERE c1 != 5) t2
  6          ) t1
  7   WHERE t1."rnum" <= 10
  8  /
I C OTHER_DATA            rnum
    rewritten query          1But the problem is of course that the rewrite should take place also when the "ORDER BY" part is provided in the query, since this is what the application issues:
SQL> SELECT *
  2    FROM (  SELECT t2.*, ROW_NUMBER () OVER (ORDER BY t2.id) AS "rnum"
  3              FROM (SELECT *
  4                      FROM test
  5                     WHERE c1 != 5) t2
  6          ORDER BY t2.id) t1
  7   WHERE t1."rnum" <= 10
  8  /
<10 rows are retrieved, so no rewrite took place>Maybe the reason for such a behaviour (I don't know how to prove it) is that materialized views don't store the ORDER BY clause as part of their definition, and the same rewrite engine that is used for materialized views is used for equivalence query rewrite:
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16760/d_advrwr.htm#sthref154
Query rewrite using equivalence declarations occurs simultaneously and in concert with query rewrite using materialized views. The same query rewrite engine is used for both.
http://download.oracle.com/docs/cd/E11882_01/server.112/e16579/basicmv.htm#DWHSG8208
The ORDER BY clause is not considered part of the materialized view definition
Maybe I'm completely wrong, so any suggestion is welcome.
2) I didn't mention that "WHERE "rnum" <= 10" is actually "WHERE "rnum" <= :b", and bind variables aren't supported for rewrite. I checked the MOS document "Using DBMS_ADVANCED_REWRITE When Binds Are Present (Avoiding ORA-30353) [ID 392214.1]" and maybe I could remove the WHERE clause, but I'd have to further test which "similar" queries would be rewritten to this one, since REWRITE_MODE is 'GENERAL' instead of 'TEXT_MATCH'. But I'd first have to resolve problem number 1) above :-)
Thanks in advance for any answers.
Regards,
Jure

Similar Messages

  • Top N query giving error for oracle 8.0.6

    Dear All,
    We are executing this query SELECT XBLNR, WERKS, MATNR, MDV01, BACKFLQUANT, STATUS, SAPTIMESTAMP, PITSTIMESTAMP, PMTIMESTAMP, BATCH FROM (SELECT XBLNR, WERKS, MATNR, MDV01, BACKFLQUANT, STATUS, SAPTIMESTAMP, PITSTIMESTAMP, PMTIMESTAMP, BATCH FROM PMBPITS.PITS_UNITY WHERE STATUS = '01' ORDER BY PMTIMESTAMP) WHERE ROWNUM < 20
    on oracle 8.0.6 but this is giving the following error
    ora - 00907 missing right parenthesis error
    1. Is it that in the inner select we cannot use order by and where clause together.
    2. We also found that if we remove order by from inner select then the query is not giving error
    pls help . points will be awarded

    Hi,
    what ever the Aman said is correct. You check this is supported in 8.1.5, SQL allows you to embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query;
    'Top-N query' is a ORACLE 8i feature which is supported in SQL. However,
    Bug:855720 states the following:
    "PL/SQL does not support top-N queries (ORDER BY in SUBSELECT/SUBQUERY
    or VIEW. Since this feature is available in SQL, but not in PL/SQL,
    it has been logged as a Bug that will be fixed in 8.1.6."
    - Pavan Kumar N

  • Top N query with INLIST Iterator performance problem

    I have a top N query that is giving me problems on Oracle 11.2.0.3.
    First of all, I have a query like the following (simplified from the real query, but produces the same problem):
        select /*+ gather_plan_statistics */ * from
          select rowid
          from payer_subscription ps
          where  ps.subscription_status = :i_subscription_status
          and ps.merchant_id           = :merchant_id2
          order by transaction_date desc
        ) where rownum <= :i_rowcount; This query works well. It can very efficiently find me the top 10 rows for a massive data set, using an index on merchant_id, subscription_status, transaction_date.
        | Id  | Operation                     | Name        | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
        |   0 | SELECT STATEMENT              |             |      1 |        |     10 |00:00:00.01 |       4 |
        |*  1 |  COUNT STOPKEY                |             |      1 |        |     10 |00:00:00.01 |       4 |
        |   2 |   VIEW                        |             |      1 |     11 |     10 |00:00:00.01 |       4 |
        |*  3 |    INDEX RANGE SCAN DESCENDING| SODTEST2_IX |      1 |    100 |     10 |00:00:00.01 |       4 |
        -------------------------------------------------------------------------------------------------------As you can see the estimated actual rows at each stage are 10, which is correct.
    Now, I have a requirement to get the top N records for a set of merchant_Ids, so if I change the query to include two merchant_ids, the performance tanks:
        select /*+ gather_plan_statistics */ * from
          select  rowid
          from payer_subscription ps
          where  ps.subscription_status = :i_subscription_status
              and (ps.merchant_id = :merchant_id or
                   ps.merchant_id = :merchant_id2 )
          order by transaction_date desc
        ) where rownum <= :i_rowcount;
        | Id  | Operation               | Name        | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
        |   0 | SELECT STATEMENT        |             |      1 |        |     10 |00:00:00.17 |     178 |       |       |          |
        |*  1 |  COUNT STOPKEY          |             |      1 |        |     10 |00:00:00.17 |     178 |       |       |          |
        |   2 |   VIEW                  |             |      1 |    200 |     10 |00:00:00.17 |     178 |       |       |          |
        |*  3 |    SORT ORDER BY STOPKEY|             |      1 |    200 |     10 |00:00:00.17 |     178 |  2048 |  2048 | 2048  (0)|
        |   4 |     INLIST ITERATOR     |             |      1 |        |  42385 |00:00:00.10 |     178 |       |       |          |
        |*  5 |      INDEX RANGE SCAN   | SODTEST2_IX |      2 |    200 |  42385 |00:00:00.06 |     178 |       |       |          |Notice now that there are 42K rows coming out of the two index range scans - Oracle is no longer aborting the index range scan when it reaches 10 rows. What I thought would happen, is that Oracle would get at most 10 rows for each merchant_id, knowing that at most 10 rows are to be returned by the query. Then it would sort that 10 + 10 rows and output the top 10 based on the transaction date, but it refuses to do that.
    Does anyone know how I can get the performance of the first query, when I need to pass a list of merchants into the query? I could probably get the performance using a union all, but the list of merchants is variable, and could be anywhere between 1 or 2 to several 100, so that makes that a bit unworkable.

    Across the two merchants_id's there are about 42K rows (this is in test, on Prod there could be several million). In the first query example, Oracle can answer the query in about 4 logical IOs and without even doing a sort as it uses the index to scan and get the relevant rows in Oracle.
    In the second case, I hoped it would pull 10 rows for each merchant_id and then sort the resulting 20 rows to find the top 10 ordered by transaction_date, but instead it is scanning far more rows than it needs to.
    In my example, it takes 4 logical IOs to answer the first query, but ~ 170 to answer the second, while I think it is achievable in 8 or so. For example, this query does what I want, but it is not a feasible option due to how many merchant_id's I may have to deal with:
    select /*+ gather_plan_statistics */ *
    from
      select *
      from 
        select * from
          select  merchant_id, transaction_date
          from payer_subscription ps
          where  ps.subscription_status = :i_subscription_status
          and ps.merchant_id = :merchant_id
          order by transaction_date desc
        ) where rownum <= :i_rowcount
        union all
        select * from  
          select  merchant_id, transaction_date
          from payer_subscription ps
          where  ps.subscription_status = :i_subscription_status
          and ps.merchant_id = :merchant_id2
          order by transaction_date desc
        ) where rownum <= :i_rowcount
      ) order by transaction_date desc
    ) where rownum <= :i_rowcount;
    | Id  | Operation                          | Name        | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
    |   0 | SELECT STATEMENT                   |             |      1 |        |     10 |00:00:00.01 |    6 |          |       |          |
    |*  1 |  COUNT STOPKEY                     |             |      1 |        |     10 |00:00:00.01 |    6 |          |       |          |
    |   2 |   VIEW                             |             |      1 |     20 |     10 |00:00:00.01 |    6 |          |       |          |
    |*  3 |    SORT ORDER BY STOPKEY           |             |      1 |     20 |     10 |00:00:00.01 |    6 |  2048 |  2048 | 2048  (0)|
    |   4 |     VIEW                           |             |      1 |     20 |     20 |00:00:00.01 |    6 |          |       |          |
    |   5 |      UNION-ALL                     |             |      1 |        |     20 |00:00:00.01 |    6 |          |       |          |
    |*  6 |       COUNT STOPKEY                |             |      1 |        |     10 |00:00:00.01 |    3 |          |       |          |
    |   7 |        VIEW                        |             |      1 |    100 |     10 |00:00:00.01 |    3 |          |       |          |
    |*  8 |         INDEX RANGE SCAN DESCENDING| SODTEST2_IX |      1 |    100 |     10 |00:00:00.01 |    3 |          |       |          |
    |*  9 |       COUNT STOPKEY                |             |      1 |        |     10 |00:00:00.01 |    3 |          |       |          |
    |  10 |        VIEW                        |             |      1 |     11 |     10 |00:00:00.01 |    3 |          |       |          |
    |* 11 |         INDEX RANGE SCAN DESCENDING| SODTEST2_IX |      1 |    100 |     10 |00:00:00.01 |    3 |          |       |          |
    ---------------------------------------------------------------------------------------------------------------------------------------This UNION ALL query completes in 6 logical IOs - the original query I posted with 2 IDs takes 178 to return the same results.

  • Oracle 11g on Linux : Query Optimization issue

    Hi guru,
    I am facing one query optimization related problem in group by query
    Table (10 million Records)
    Product(ProductId number,ProductName varchar(100),CategoryId VARCHAR2(38),SubCategoryId VARCHAR2(38))
    Index
    create index idxCategory on Product (CategoryId,SubCategoryId)
    Query1:To find product count for all CategoryId and SubCategoryId
    select CategoryId,SubCategoryId,count(*) from Product group by CategoryId,SubCategoryId
    Above query is not using index idxCategory and doing table scan which is very costly.
    When I fire Query2: select count(*) from Product group by CategoryId,SubCategoryId
    then it is properly using index idxCategory and very fast.
    Even I specified hint in Query1 but it is not using hint.
    Can anybody suggest why oracle is not using index in Query1 and what should I do so that Query1 will use Index.
    Thanks in advance.

    user644199 wrote:
    I am facing one query optimization related problem in group by query
    Query1:To find product count for all CategoryId and SubCategoryId
    select CategoryId,SubCategoryId,count(*) from Product group by CategoryId,SubCategoryId
    Above query is not using index idxCategory and doing table scan which is very costly.
    When I fire Query2: select count(*) from Product group by CategoryId,SubCategoryId
    then it is properly using index idxCategory and very fast.
    Even I specified hint in Query1 but it is not using hint.
    Can anybody suggest why oracle is not using index in Query1 and what should I do so that Query1 will use Index.The most obvious reason that the table needs to be visited would be that the columns "CategoryId" / "SubCategoryId" can be NULL but then this should apply to both queries. You could try the following to check the NULL issue:
    select CategoryId,SubCategoryId,count(*) from Product where CategoryId is not null and SubCategoryId is not null group by CategoryId,SubCategoryId
    Does this query use the index?
    Can you show us the hint you've used to force the index usage and the EXPLAIN PLAN output of the two queries including the "Predicate Information" section? Use DBMS_XPLAN.DISPLAY to get a proper output, and use the \ tag before and after when posting here to format it using fixed font. Use the "Quote" button in the message editor to see how I used the \ tag here.
    Are above queries representing the actual queries used or did you omit some predicates etc. for simplicity?
    By the way, VARCHAR2(38) and ...ID as name, are these columns storing number values?
    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/

  • Can we hide filter in Search Master agreements standard top level query?

    Hi Experts, 
    I have a requirement to hide flter in standard Search Master Agreements top level query.  See below screen shot
    I have an idea how to delete filter in query but when i have added that query to top level navigation i don't have any idea.
    So i want to hide Business Unit & Region filter in Search Master Agreement query when it is in top level navigation.
    If anyone have idea please share with me how to hide.
    Thanks,
    Lava

    Hi Lava,
    It is not a filter but this a Standard field which is coming from the Master Agreement.
    So, you cannot hide or even delete any field.
    But if it is a custom field you can hide it by inactivating the respective field in Extension Defination.
    Please let me know if you need any assistance.
    Thanks,
    Raj.

  • What is query optimization and how to do it.

    Hi
    What is query optimization?
    Any link can any one provide me so that i can read and learn the techniques.
    Thanks
    Elias Maliackal

    THis is an excellent place to start: When your query takes too long ...

  • Reg Query Optimization - doubts..

    Hi Experts,
    This is related to Blog by Mr Prakash Darji regarding "Query Optimization" posted on Jan 26 2006.In this to optimize query Generation of Report is suggested.
    I tried this, but I am not sure I am analyzing this correctly.
    I collected Stats data before and after Generation of Report.But how to be sure that this is helping me? Did any one has tried this?
    What to look for in Stats Data - duration?
    But duration would not be absolute parameter as there is factor of "Wait Time, User", so duration may depend on this.
    Please help me in this.
    Thanks
    Gaurav
    Message was edited by: Gaurav

    Any ideas Experts?

  • OBIEE 11.1.1.7-Ago Function Error-[nQSError: 46008] Internal error: File server\Query\Optimizer\SmartScheduler\PhysicalRequestGenerator\Src\SQOSPMarkMultiTargetSupport.cpp

    Hi All,
    I was performing the steps mentioned in Oracle Tutorial"http://www.oracle.com/webfolder/technetwork/tutorials/obe/fmw/bi/bi11115/biadmin11g_02/biadmin11g.htm#t10"-BI RPD creation.
    After Using the AGO function data in the Time series metric(Month Ago Revenue) was null always. I updated the DB features in RPD physical layers by selecting support for time series functions.
    After that report started to fail with below error. Please let me know if its a bug and any option to fix it.
    Thanks,
    Sreekanth
    Error
    View Display Error
    Odbc driver returned an error (SQLExecDirectW). 
      Error Details
    Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P 
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 43119] Query Failed: [nQSError: 46008] Internal error: File server\Query\Optimizer\SmartScheduler\PhysicalRequestGenerator\Src\SQOSPMarkMultiTargetSupport.cpp, line 1680. (HY000) 
    SQL Issued: SELECT 0 s_0, "Sample Sales"."Time"."Year-L1" s_1, "Sample Sales"."Revenue"."Ago-Year Revenue" s_2, "Sample Sales"."Revenue"."Revenue" s_3 FROM "Sample Sales" FETCH FIRST 65001 ROWS ONLY
      Refresh

    I know that there is no relation between "SampleApp Lite"."D3 Orders (Facts Attributes)"."Order Date", "SampleApp Lite"."D0 Time"."Calendar Date", it's also the same thing in my own RPD.
    But as it's working with the 11.1.1.6.2 BP1 version I don't understand why it's not working with 11.1.1.6.9.
    Implicit fact column is not set on my repository, but I don't have any request with only dimensional column, so if my understanding is correct I don't need to use it. Also, the problem appears during the check of the repository not in answers.
    thanks anyway

  • SQL Server 2008R2 SP2 Query optimizer memory leak ?

    It looks like we are facing a SQL Server 2008R2 queery optimizer memory leak.
    We have below version of SQL Server
    Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)
     Jun 28 2012 08:36:30
     Copyright (c) Microsoft Corporation
     Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
    The instance is set MAximum memory tro 20 GB.
    After executing a huge query (2277 kB generated by IBM SPSS Clementine) with tons of CASE and a lot of AND/OR statements in the WHERE and CASE statements and muliple subqueries the server stops responding on Out of memory in the internal pool
    and the query optimizer has allocated all the memory.
    From Management Data Warehouse we can find that the query was executed at
    7.11.2014 22:40:57
    Then at 1:22:48 we recieve FAIL_PACE_ALLOCATION 1
    2014-11-08 01:22:48.70 spid75       Failed allocate pages: FAIL_PAGE_ALLOCATION 1
    And then tons of below errors
    2014-11-08 01:24:02.22 spid87      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:02.22 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:02.22 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:02.30 Server      Error: 17312, Severity: 16, State: 1.
    2014-11-08 01:24:02.30 Server      SQL Server is terminating a system or background task Fulltext Host Controller Timer Task due to errors in starting up the task (setup state 1).
    2014-11-08 01:24:02.22 spid74      Error: 701, Severity: 17, State: 123.
    2014-11-08 01:24:02.22 spid74      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:13.22 Server      Error: 17312, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:13.22 spid87      Error: 701, Severity: 17, State: 123.
    2014-11-08 01:24:13.22 spid87      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:13.22 spid63      Error: 701, Severity: 17, State: 130.
    2014-11-08 01:24:13.22 spid63      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:13.22 spid57      Error: 701, Severity: 17, State: 123.
    2014-11-08 01:24:13.22 spid57      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:13.22 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:18.26 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:24.43 spid81      Error: 701, Severity: 17, State: 123.
    2014-11-08 01:24:24.43 spid81      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:18.25 Server      Error: 18052, Severity: -1, State: 0. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:18.25 Server      BRKR TASK: Operating system error Exception 0x1 encountered.
    2014-11-08 01:24:30.11 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:30.11 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:35.18 spid57      Error: 701, Severity: 17, State: 131.
    2014-11-08 01:24:35.18 spid57      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:35.18 spid71      Error: 701, Severity: 17, State: 193.
    2014-11-08 01:24:35.18 spid71      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:35.18 Server      Error: 17312, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:35.41 Server      Error: 17312, Severity: 16, State: 1.
    2014-11-08 01:24:35.41 Server      SQL Server is terminating a system or background task SSB Task due to errors in starting up the task (setup state 1).
    2014-11-08 01:24:35.71 Server      Error: 17053, Severity: 16, State: 1.
    2014-11-08 01:24:35.71 Server      BRKR TASK: Operating system error Exception 0x1 encountered.
    2014-11-08 01:24:35.71 spid73      Error: 701, Severity: 17, State: 123.
    2014-11-08 01:24:35.71 spid73      There is insufficient system memory in resource pool 'internal' to run this query.
    2014-11-08 01:24:46.30 Server      Error: 17312, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:51.31 Server      Error: 17053, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:51.31 Server      Error: 17300, Severity: 16, State: 1. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    2014-11-08 01:24:51.31 Logon       Error: 18052, Severity: -1, State: 0. (Params:). The error is printed in terse mode because there was error during formatting. Tracing, ETW, notifications etc are skipped.
    Last error message is half an hour after the inital Out of memory at 2014-11-08 01:52:54.03. Then the Instance is completely shut down
    From the memory information in the error log we can see that all the memory is consumed by the QUERY_OPTIMIZER
    Buffer Pool                                   Value
    Committed                                   2621440
    Target                                      2621440
    Database                                     130726
    Dirty                                          3682
    In IO                                            
    0
    Latched                                          
    1
    Free                                           
    346
    Stolen                                      2490368
    Reserved                                          0
    Visible                                     2621440
    Stolen Potential                                  0
    Limiting Factor                                  17
    Last OOM Factor                                   0
    Last OS Error                                     0
    Page Life Expectancy                             28
    2014-11-08 01:22:48.90 spid75     
    Process/System Counts                         Value
    Available Physical Memory                29361627136
    Available Virtual Memory                 8691842715648
    Available Paging File                    51593969664
    Working Set                               628932608
    Percent of Committed Memory in WS               100
    Page Faults                                48955000
    System physical memory high                       1
    System physical memory low                        0
    Process physical memory low                       1
    Process virtual memory low                        0
    MEMORYCLERK_SQLOPTIMIZER (node 1)                KB
    VM Reserved                                       0
    VM Committed                                      0
    Locked Pages Allocated                            0
    SM Reserved                                       0
    SM Committed                                      0
    SinglePage Allocator                       19419712
    MultiPage Allocator                             128
    Memory Manager                                   KB
    VM Reserved                               100960236
    VM Committed                                 277664
    Locked Pages Allocated                     21483904
    Reserved Memory                                1024
    Reserved Memory In Use                            0
    On the other side MDW reports that the MEMORYCLERK_SQLOPTIMIZER increases since the execution of the query up to the point of OUTOF MEMORY, but the Average value is 54.7 MB during that period as can be seen on attached graph.
    We have encountered this issue already two times (every time the critical query is executed).

    Hi,
    This does seems to me kind of memory Leak and actually it is from SQL Optimizer which leaked memory from buffer pool so much that it did not had any memory to be allocated for new page.
    MEMORYCLERK_SQLOPTIMIZER (node 1)                KB
    VM Reserved                                       0
    VM Committed                                      0
    Locked Pages Allocated                            0
    SM Reserved                                       0
    SM Committed                                      0
    SinglePage Allocator                       19419712
    MultiPage Allocator                             128
    Can you post complete DBCC MEMORYSTATUS output which was generated in errorlog. Is this the only message in errorlog or there are some more messages before and after it.
    select (SUM(single_pages_kb)*1024)/8192 as total_stolen_pages, type
    from sys.dm_os_memory_clerks
    group by typeorder by total_stolen_pages desc
    and
    select sum(pages_allocated_count * page_size_in_bytes)/1024,type from sys.dm_os_memory_objects
    group by type
    If you can post the output of above two queries with dbcc memorystaus output on some shared drive and share location with us here. I would try to find out what is leaking memory.
    You can very well apply SQL Server 2008 r2 SP3 and see if this issue subsides but I am not sure whether this is fixed or actually it is a bug.
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it
    My Technet Wiki Article
    MVP

  • Top utilising Query in SQL server

    Hi,
       Is there any query to get the top utilization query of the day?

    Hi,
    Top CPU utilizing query
    --This might take some time to give result on busy systemselect top 10
    sum(qs.total_worker_time) as total_cpu_time,
    sum(qs.execution_count) as total_execution_count,
    count(*) as number_of_statements,
    t.text
    from
    sys.dm_exec_query_stats qs
    cross apply sys.dm_exec_sql_text(qs.sql_handle) as t
    group by t.text
    order by sum(qs.total_worker_time) desc
    For memory utilization there is no perfect way to find out if query has completed. but
    sys.dm_exec_query_memory_grants would help you
    SELECT mg.granted_memory_kb, mg.session_id, t.text, qp.query_plan
    FROM sys.dm_exec_query_memory_grants AS mg
    CROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) AS t
    CROSS APPLY sys.dm_exec_query_plan(mg.plan_handle) AS qp
    ORDER BY 1 DESC OPTION (MAXDOP 1)
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it.
    My TechNet Wiki Articles

  • How to do query Optimization ,plz share any documents with real examples

    Hi All,
    could any one please provide me some informations, how can i do query optimization in oracle using Third party tool sql developer .
    i am working oracle 10g version,please share with me if any documents or ppt like that.
    Thanks
    Krupa

    879534 wrote:
    Hi All,
    could any one please provide me some informations, how can i do query optimization in oracle using Third party tool sql developer .SQL Developer is Oracle's development tool, not a third party tool.
    Questions about using SQL Developer should go in the SQL Developer forum:
    SQL Developer
    I'll move the question there for you.

  • Any software for Query Optimization?

    Hi,
    Is there any software to be used for query Optimization
    Please help me

    3rd party tools suck (if they did not Oracle would incorporate them in its optimizer long ago). The best tools are:
    1. Gather table and index statistics
    2. Use CBO (FIRST_ROWS, ALL_ROWS)
    3. SQL*Plus AUTOTRACE
    4. EXPLAIN PLAN
    5. SET SQL_TRACE = TRUE + TKPROF
    6. Statspack
    7. Enterprise manager (new Ora10g, did not try it)
    All from Oracle...

  • Regarding Query Optimization

    Hi Gurus,
    I am preparing for an interview. I need to know about Query Optimization, explain plan, Cost analysis.
    Can anyone please give me some reference link or site where i can get step by step details for the above..
    It would be really helpfull.
    Thanks in advance..
    Ameya.

    RTFM [url http://oraclesvca2.oracle.com/docs/cd/B10501_01/server.920/a96533/toc.htm]Performance Tuning Guide and Reference

  • The query processor ran out of stack space during query optimization. Please simplify the query

    Can you suggest me that what should i do in this case.
    Actually i am having one table that is a MasterTable.I am referring this table in more than 300 tables that means i am having foreign key of this primary key in 300+ tables.
    due to this i am getting following error during deleting any row,
    doesn't matter that data is existing in reference table or not.
    Error that i am getting is 
    "The query processor ran out of stack space during query optimization. Please simplify the query"
    Can you suggest me that what should i do to avoid this error,because i am unable to delete this entry.
    Apart from it,i am getting performance problem too,so is it due to such huge FK on it.
    Please suggest me on following points
    1. Is it worst way to handle it,if yes then please suggest me solution for it.
    2. If it is a correct way then what should i do if getting error during deleting any record.
    3. Is it right to create Foreign key on each table where i am saving data of this master. if no then how to manage integrity.
    4. What people do in huge database when they wants to create foreign key for any primary key.
    5. Can you suggest me that how DBA's are handling this in big database,where they are having huge no. of tables.

    The most common reason of getting such error is having more than 253 foreign key constraints on a table. 
    The max limit is documented here:
    http://msdn.microsoft.com/en-us/library/ms143432(SQL.90).aspx 
    Although a table can contain an unlimited number of FOREIGN KEY constraints, the recommended maximum is 253. Depending on the hardware configuration hosting SQL Server, specifying additional foreign key constraints may be expensive for the query
    optimizer to process. If you are on 32 bit, then you might want to move to 64 bit to get little bigger stack space but eventually having 300 FK is not something which would work in long run.
    Balmukund Lakhani | Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
    This posting is provided "AS IS" with no warranties, and confers no rights.
    My Blog |
    Team Blog | @Twitter
    Author: SQL Server 2012 AlwaysOn -
    Paperback, Kindle

  • Need help for top n query

    Hi,
    I want to show top 3 salary records from emp table and I am using below query in oracle * plus. However I am getting an error saying, "FROM keyword not found where expected". I am not getting what mistake I am doing while writing this query. Can someone plz advice?
    Query:
    Select top 3 sal from emp;
    Thanks in advance.

    Hi,
    In Oracle, there is no TOP keyword. The usual way to do a top-N query in Oracle is to use an analytic function, such as RANK:
    WITH     got_r_num     AS
         SELECT     sal
         ,     ename, empno, ...     -- whatever columns you want
         ,     RANK () OVER (ORDER BY sal DESC)    AS r_num
         FROM     scott.emp
    SELECT       sal
    ,       ename, empno, ...     -- if wanted
    ,       r_num                -- if wanted
    FROM       got_r_num
    WHERE       r_num     <= 3
    ORDER BY  sal
    ;Depending on how you want to handle ties, ypu may want to add tie-breaking columns to the end of the analytic ORDER BY clause, and/or use ROW_NUMBER instead of RANK.

Maybe you are looking for

  • BI 7.0 front end installation problem

    Hi Gurus When I click on the Business Explorer -> Analyzer 7.10 It opens up MS Excel but when I click on the connect tool icon the sapgui connect does not showup We are on SAPGUI 7.10 patch 03. Also from the query desiger ,when i go to View>>>standar

  • Export to Excel not Working

    Hi I'm using JDeveloper 11g TP4 and in Firefox 3.X export to excel not working Regards, JavaDeVeLoper

  • My songs in the library keep freezing when playing, how do you solve this?

    I've recently updated my iTunes software and since doing this each time I play one of my songs in my library, It keeps freezing and stuttering inbetween each song. Is there a bug on the latest software? How do i fix this. Thank you.

  • How can I select properties from dropdown lists using only the keyboard?

    Hi, I am using SAP Netweaver Studio and when modifying UI elements in views, I want to select the value for a property from the corresponding dropdown list in the Properties view using only the keyboard. When a property row is selected, "Enter" toggl

  • Wont take my itunes gift card

    It has approved my gift card. I see the amount, but everytime I try and make a purchase of a song, it won't take my gift card.