Response time of query utterly upside down because of small where clause change

Hello,
I'm wondering why a small change on a where clause in a query has a dramatic impact on its response time.
Here is the query, with its plan and a few details:
select * from (
SELECT xyz_id, time_oper, ...
     FROM (SELECT 
                    d.xyz_id xyz_id,
                    TO_CHAR (di.time_operation, 'DD/MM/YYYY') time_oper,
                    di.time_operation time_operation,
                    UPPER (d.delivery_name || ' ' || d.delivery_firstname) custname,
                    d.ticket_language ticket_language, d.payed,
                    dsum.delivery_mode delivery_mode,
                    d.station_delivery station_delivery,
                    d.total_price total_price, d.crm_cust_id custid,
                    d.bene_cust_id person_id, d.xyz_num, dpe.ers_pnr ers_pnr,
                    d.delivery_name,
                    TO_CHAR (dsum.first_travel_date, 'DD/MM/YYYY') first_traveldate,
                    d.crm_company custtype, UPPER (d.client_name) partyname,
                    getremark(d.xyz_num) remark,
                    d.client_app, di.work_unit, di.account_unit,
                    di.distrib_code,
                    UPPER (d.crm_name || ' ' || d.crm_firstname) crm_custname,
                   getspecialproduct(di.xyz_id) specialproduct
               FROM xyz d, xyz_info di, xyz_pnr_ers dpe, xyz_summary dsum
              WHERE d.cancel_state = 'N'
             -- AND d.payed = 'N'
                AND dsum.delivery_mode NOT IN ('DD')
                AND dsum.payment_method NOT IN ('AC', 'AG')
                AND d.xyz_blocked IS NULL
                AND di.xyz_id = d.xyz_id
                AND di.operation = 'CREATE'
                AND dpe.xyz_id(+) = d.xyz_id
                AND EXISTS (SELECT 1
                              FROM xyz_ticket dt
                             WHERE dt.xyz_id = d.xyz_id)
                AND dsum.xyz_id = di.xyz_id
           ORDER BY di.time_operation DESC)
    WHERE ROWNUM < 1002
) view
WHERE view.DISTRIB_CODE in ('NS') AND view.TIME_OPERATION > TO_DATE('20/5/2013', 'dd/MM/yyyy')
plan with "d.payed = 'N'" (no rows, *extremely* slow):
| Id  | Operation                          | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT                   |                  |  1001 |  4166K| 39354   (1)| 00:02:59 |
|*  1 |  VIEW                              |                  |  1001 |  4166K| 39354   (1)| 00:02:59 |
|*  2 |   COUNT STOPKEY                    |                  |       |       |            |          |
|   3 |    VIEW                            |                  |  1001 |  4166K| 39354   (1)| 00:02:59 |
|   4 |     NESTED LOOPS OUTER             |                  |  1001 |   130K| 39354   (1)| 00:02:59 |
|   5 |      NESTED LOOPS SEMI             |                  |   970 |   111K| 36747   (1)| 00:02:47 |
|   6 |       NESTED LOOPS                 |                  |   970 |   104K| 34803   (1)| 00:02:39 |
|   7 |        NESTED LOOPS                |                  |   970 | 54320 | 32857   (1)| 00:02:30 |
|*  8 |         TABLE ACCESS BY INDEX ROWID| XYZ_INFO         |    19M|   704M| 28886   (1)| 00:02:12 |
|   9 |          INDEX FULL SCAN DESCENDING| DNIN_IDX_NI5     | 36967 |       |   296   (2)| 00:00:02 |
|* 10 |         TABLE ACCESS BY INDEX ROWID| XYZ_SUMMARY      |     1 |    19 |     2   (0)| 00:00:01 |
|* 11 |          INDEX UNIQUE SCAN         | SB11_DSMM_XYZ_UK |     1 |       |     1   (0)| 00:00:01 |
|* 12 |        TABLE ACCESS BY INDEX ROWID | XYZ              |     1 |    54 |     2   (0)| 00:00:01 |
|* 13 |         INDEX UNIQUE SCAN          | XYZ_PK           |     1 |       |     1   (0)| 00:00:01 |
|* 14 |       INDEX RANGE SCAN             | DNTI_NI1         |    32M|   249M|     2   (0)| 00:00:01 |
|  15 |      TABLE ACCESS BY INDEX ROWID   | XYZ_PNR_ERS      |     1 |    15 |     4   (0)| 00:00:01 |
|* 16 |       INDEX RANGE SCAN             | DNPE_XYZ         |     1 |       |     2   (0)| 00:00:01 |
Predicate Information (identified by operation id):
  1 - filter("DISTRIB_CODE"='NS' AND "TIME_OPERATION">TO_DATE(' 2013-05-20', 'syyyy-mm-dd'))
  2 - filter(ROWNUM<1002)
  8 - filter("DI"."OPERATION"='CREATE')
10 - filter("DSUM"."DELIVERY_MODE"<>'DD' AND "DSUM"."PAYMENT_METHOD"<>'AC' AND "DSUM"."PAYMENT_METHOD"<>'AG')
11 - access("DSUM"."XYZ_ID"="DI"."XYZ_ID")
12 - filter("D"."PAYED"='N' AND "D"."XYZ_BLOCKED" IS NULL AND "D"."CANCEL_STATE"='N')
              ^^^^^^^^^^^^^^
13 - access("DI"."XYZ_ID"="D"."XYZ_ID")
14 - access("DT"."XYZ_ID"="D"."XYZ_ID")
16 - access("DPE"."XYZ_ID"(+)="D"."XYZ_ID")
plan with "d.payed = 'N'" (+/- 450 rows, less than two minutes):
| Id  | Operation                          | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT                   |                  |  1001 |  4166K| 58604   (1)| 00:04:27 |
|*  1 |  VIEW                              |                  |  1001 |  4166K| 58604   (1)| 00:04:27 |
|*  2 |   COUNT STOPKEY                    |                  |       |       |            |          |
|   3 |    VIEW                            |                  |  1002 |  4170K| 58604   (1)| 00:04:27 |
|   4 |     NESTED LOOPS OUTER             |                  |  1002 |   130K| 58604   (1)| 00:04:27 |
|   5 |      NESTED LOOPS SEMI             |                  |  1002 |   115K| 55911   (1)| 00:04:14 |
|   6 |       NESTED LOOPS                 |                  |  1476 |   158K| 52952   (1)| 00:04:01 |
|   7 |        NESTED LOOPS                |                  |  1476 | 82656 | 49992   (1)| 00:03:48 |
|*  8 |         TABLE ACCESS BY INDEX ROWID| XYZ_INFO         |    19M|   704M| 43948   (1)| 00:03:20 |
|   9 |          INDEX FULL SCAN DESCENDING| DNIN_IDX_NI5     | 56244 |       |   449   (1)| 00:00:03 |
|* 10 |         TABLE ACCESS BY INDEX ROWID| XYZ_SUMMARY      |     1 |    19 |     2   (0)| 00:00:01 |
|* 11 |          INDEX UNIQUE SCAN         | AAAA_DSMM_XYZ_UK |     1 |       |     1   (0)| 00:00:01 |
|* 12 |        TABLE ACCESS BY INDEX ROWID | XYZ              |     1 |    54 |     2   (0)| 00:00:01 |
|* 13 |         INDEX UNIQUE SCAN          | XYZ_PK           |     1 |       |     1   (0)| 00:00:01 |
|* 14 |       INDEX RANGE SCAN             | DNTI_NI1         |    22M|   168M|     2   (0)| 00:00:01 |
|  15 |      TABLE ACCESS BY INDEX ROWID   | XYZ_PNR_ERS      |     1 |    15 |     4   (0)| 00:00:01 |
|* 16 |       INDEX RANGE SCAN             | DNPE_XYZ         |     1 |       |     2   (0)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter("DISTRIB_CODE"='NS' AND "TIME_OPERATION">TO_DATE(' 2013-05-20', 'syyyy-mm-dd'))
   2 - filter(ROWNUM<1002)
   8 - filter("DI"."OPERATION"='CREATE')
  10 - filter("DSUM"."DELIVERY_MODE"<>'DD' AND "DSUM"."PAYMENT_METHOD"<>'AC' AND "DSUM"."PAYMENT_METHOD"<>'AG')
  11 - access("DSUM"."XYZ_ID"="DI"."XYZ_ID")
  12 - filter("D"."XYZ_BLOCKED" IS NULL AND "D"."CANCEL_STATE"='N')
  13 - access("DI"."XYZ_ID"="D"."XYZ_ID")
  14 - access("DT"."XYZ_ID"="D"."XYZ_ID")
  16 - access("DPE"."XYZ_ID"(+)="D"."XYZ_ID")
XYZ.PAYED values breakdown:
P   COUNT(1)
Y   12202716
N    9430207
tables nb of records:
TABLE_NAME           NUM_ROWS
XYZ                  21606776
XYZ_INFO            186301951
XYZ_PNR_ERS           9716471
XYZ_SUMMARY          21616607
Everything that comes inside the "select * from(...) view" parentheses is defined in a view. We've noticed that the line "AND d.payed = 'N'" (commented above) is the guilty clause: the query takes one or two seconds to return between 400 and 500 rows if this line is removed, when included in the query, the response time then switches to *hours* -sic !- but then the result set is empty (no rows returned). The plan is exactly the same whether this "d.payed = 'N'" is added or removed, I mean the nb of steps, access paths, join order etc., only the rows/bytes/cost columns values change, as you can see.
We've found no other way of solving this perf issue but by taking out this "d.payed = 'N'" condition and setting it outside the view along with view.DISTRIB_CODE and view.TIME_OPERATION.
But we would like to understand why such a small change on the XYZ.PAYED column turns everything upside down that much, and we'd like to be able to tell the optimizer to perform this check on payed = 'N' by itself in the end, just like we did, through the use of a hint if possible...
Anybody ever encountered such a behaviour before ? Do you have any advice regarding the use of a hint to reach the same response time as that we've got by setting the payed = N condition outside of the view definition ??
Thanks a lot in advance.
Regards,
Seb

I am really sorry I couldn't get back earlier to this forum...
Thanks to you all for your answers.
First I'd just like to correct a small mistake I made, when writing
"the query takes one or two seconds": I meant one or 2 *minutes*. Sorry.
> What table/columns are indexed by "DNTI_NI1"?
aaaa.dnti_ni1 is an index ON aaaa.xyz_ticket(xyz_id, ticket_status)
> And what are the indexes on xyz table?
Too many:
XYZ_ARCHIV_STATE_IND           ARCHIVE_STATE
XYZ_BENE_CUST_ID_IND           BENE_CUST_ID
XYZ_BENE_TTL_IND               BENE_TTL
XYZ_CANCEL_STATE_IND           CANCEL_STATE
XYZ_CLIENT_APP_NI              CLIENT_APP
XYZ_CRM_CUST_ID_IND            CRM_CUST_ID
XYZ_DELIVE_MODE_IND            DELIVERY_MODE
XYZ_DELIV_BLOCK_IND            DELIVERY_BLOCKED
XYZ_DELIV_STATE_IND            DELIVERY_STATE
XYZ_XYZ_BLOCKED                XYZ_BLOCKED
XYZ_FIRST_TRAVELDATE_IND       FIRST_TRAVELDATE
XYZ_MASTER_XYZ_IND             MASTER_XYZ_ID
XYZ_ORG_ID_NI                  ORG_ID
XYZ_PAYMT_STATE_IND            PAYMENT_STATE
XYZ_PK                         XYZ_ID
XYZ_TO_PO_IDX                  TO_PO
XYZ_UK                         XYZ_NUM
For ex. XYZ_CANCEL_STATE_IND on CANCEL_STATE seems superfluous to me, as the column may only contain Y or N (or be null)...
> Have you traced both cases to compare statistics? What differences did it reveal?
Yes but it only shows more of *everything* (more tables blocks accessed, the same
for indexes blocks, for almost all objects involved) for the slowest query !
Greping WAIT on the two trc files made for every statement and counting the
object IDs access show that the quicker query requires much less I/Os; the
slowest one overall needs much more blocks to be read (except for the indexes
DNSG_NI1 or DNPE_XYZ for example). Below I replaced obj# with the table/index
name, the first column is the figure showing how many times the object was
accessed in the 10053 file (I ctrl-C'ed my second execution ofr course, the
figures should be much higher !!):
[login.hostname] ? grep WAIT OM-quick.trc|...|sort|uniq -c
    335 XYZ_SUMMARY
  20816 AAAA_DSMM_XYZ_UK (index on xyz_summary.xyz_id)
    192 XYZ
   4804 XYZ_INFO
    246 XYZ_SEGMENT
      6 XYZ_REMARKS
     63 XYZ_PNR_ERS
    719 XYZ_PK           (index on xyz.xyz_id)
   2182 DNIN_IDX_NI5     (index on xyz.xyz_id)
    877 DNSG_NI1         (index on xyz_segment.xyz_id, segment_status)
    980 DNTI_NI1         (index on xyz_ticket.xyz_id, ticket_status)
    850 DNPE_XYZ         (index on xyz_pnr_ers.xyz_id)
[login.hostname] ? grep WAIT OM-slow.trc|...|sort|uniq -c
   1733 XYZ_SUMMARY
  38225 AAAA_DSMM_XYZ_UK  (index on xyz_summary.xyz_id)
   4359 XYZ
  12536 XYZ_INFO
     65 XYZ_SEGMENT
     17 XYZ_REMARKS
     20 XYZ_PNR_ERS
   8598 XYZ_PK
   7406 DNIN_IDX_NI5
     29 DNSG_NI1
   2475 DNTI_NI1
     27 DNPE_XYZ
The overwhelmingly dominant wait event is by far 'db file sequential read':
[login.hostname] ? grep WAIT OM-*elect.txt|cut -d"'" -f2|sort |uniq -c
     36 SQL*Net message from client
     38 SQL*Net message to client
107647 db file sequential read
      1 latch free
      1 latch: object queue header operation
      3 latch: session allocation
> It will be worth knowing the estimations...
It show the same plan with a higher cost when PAYED = N is added:
SQL> select * from sb11.dnr d
  2* where d.dnr_blocked IS NULL and d.cancel_state = 'N'
SQL> /
| Id  | Operation                   | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT            |                      |  1002 |   166K|    40   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| XYZ                  |  1002 |   166K|    40   (3)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | XYZ_CANCEL_STATE_IND |       |       |     8   (0)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter("D"."XYZ_BLOCKED" IS NULL)
   2 - access("D"."CANCEL_STATE"='N')
SQL> select * from sb11.dnr d
  2  where d.dnr_blocked IS NULL and d.cancel_state = 'N'
  3* and d.payed = 'N'
SQL> /
Execution Plan
Plan hash value: 1292668880
| Id  | Operation                   | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT            |                      |  1001 |   166K|    89   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| XYZ                  |  1001 |   166K|    89   (3)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | XYZ_CANCEL_STATE_IND |       |       |    15   (0)| 00:00:01 |

Similar Messages

  • Query Tuning - using CASE statement in the WHERE clause - Duplicate Post

    Duplicate Post by mistake.
    Please check
    Query Tuning - using CASE statement in the WHERE clause
    Edited by: Chaitanya on Jun 9, 2011 2:45 AM
    Edited by: Chaitanya on Jun 9, 2011 2:46 AM

    Duplicate Post by mistake.
    Please check
    Query Tuning - using CASE statement in the WHERE clause
    Edited by: Chaitanya on Jun 9, 2011 2:45 AM
    Edited by: Chaitanya on Jun 9, 2011 2:46 AM

  • Slow response time on query from one schema to another

    I've got two separate databases (one test, the other production). The schemas are pretty much identical and contain the same data. This particular query is executed from .NET (SQL text command, not SP) and takes about 3 seconds when performed against my test db, but takes anywhere from 45 seconds to 5 minutes on the production system. I've exceuted the same statement using Aquadata studio and get the same response times. I didn't build the queries or the schemas, nor do I manage the database servers. But I am responsible for making this work now. These queries have been working in the past, but recently have been changed. I am able to speed this one up with some sql changes, but the others I'm not sure about. I've checked and production db has indexes on the fields I'm querying while the test db does not. I would expect test to be slower, but it's not. Any ideas that I could try. On Monday I'll ask our server admins to check out the server end of it, and see if they need to reboot or something. In the meantime, I'm going to work to revamp the SQL.
    Any ideas you can provide would be much appreciated.
    Sincerely,
    Zam

    when your query takes long...
    When your query takes too long ...

  • Query slow down when added a where clause

    I have a procedure that has performance issue, so I copy some of the query and run in the sql plus and try to spot which join cause the problem, but I get a result which I can figuer out why. I have a query which like below:
    Select Count(a.ID) From TableA a
    -- INNER JOIN other tables
    WHERE a.TypeID = 2;
    TableA has 140000 records, when the where clause is not added, the count return quite quick, but if I add the where clause, then the query slow down and seems never return so I have to kill my SQL Plus session. TableA has index on TypeID and TypeID is a number type. When TablA has 3000 records, the procedure return very quick, but it slow down and hang there when the TableA contains 140000 records. Any idea why this will slow down the query?
    Also, the TypeID is a foreign key to another table (TableAType), so the query above can written as :
    Select Count(a.ID) From TableA a
    -- INNER JOIN other tables
    INNER JOIN TableAType atype ON a.TypeID = atype.ID
    WHERE atype.Name = 'typename';
    TableAType table is a small table only contains less than 100 records, in this case, would the second query be more efficient to the first query?
    Any suggestions are welcome, thanks in advance...
    Message was edited by:
    user500168

    TableA now has 230000 records and 28000 of them has the TypeID 2.
    I haven't use the hint yet but thank you for your reply which let me to to run a query to check how many records in TableA has TypeID 2. When I doing this, it seems pretty fast. So I begin with the select count for TableA only and gradually add table to join and seems the query is pretty fast as long as TableA is the fist table to select from.
    Before in my query TableA is the second table to join from, there is another table (which is large as well but not as large as TableA) before TableA. So I think this is why it runs slow before. I am not at work yesterday so the query given in my post is based on my roughly memory and I forget to mention another table is joined before TableA, really sorry about that.
    I think I learn a lesson here, the largest table need to be in the begining of the select statement...
    Thank you very much everyone.

  • Query Tuning - using CASE statement in the WHERE clause

    Hi All,
    My query has been modified to use a CASE statement in the WHERE clause to consider data from certain columns based on a parameter value. This modified query is doing a full table scan and running endlessly. Please suggest what may be done to improve its performance:
    Query:
    SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
    os.acctnum,
    os.avieworigin_refid,
    COUNT(1) cnt_articleview,
    SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
    FROM TABLE1 os
    WHERE os.acctnum IS NOT NULL
    AND os.avieworigin_refid IS NOT NULL
    AND os.requestdatetime IS NOT NULL
    AND UPPER(os.success_ind) = 'S'
    AND CASE WHEN
    Param_ValueToCheck  = 'FULL' AND get_date_timestamp(os.requestdatetime)  BETWEEN
    TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
    TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    THEN 1
    WHEN
    Param_ValueToCheck  = 'INCR' AND os.entry_createddate  BETWEEN
    TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
    TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    THEN 1
    END = 1
    AND CASE WHEN
    Param_ValueToCheck  = 'FULL' AND os.entry_CreatedDate BETWEEN
    TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
    TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    THEN 1
    WHEN
    Param_ValueToCheck  = 'INCR' THEN 1
    END = 1
    GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Table Description:
    (Number of rows : approx > 600,000,000)
    Name                           Null     Type      
    ARTICLEID                      NOT NULL NUMBER(20)
    USERKEY                                 NUMBER(10)
    AVIEWORIGIN_REFID                       VARCHAR2(10)
    SUCCESS_IND                             VARCHAR2(2)
    ENTRY_CREATEDDATE                       DATE      
    CREATED_BY                              VARCHAR2(10)
    FILENUMBER                              NUMBER(10)
    LINENUMBER                              NUMBER(10)
    ACCTNUM                                 VARCHAR2(10)
    AUTOCOMPLETEDTERM                       NUMBER(2) 
    REQUESTDATETIME                         VARCHAR2(19)Explain Plan
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 2224314832
    | Id  | Operation            | Name              | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT     |                   |   590 | 33040 |  2501K  (1)| 08:20:15 |       |       |
    |   1 |  HASH GROUP BY       |                   |   590 | 33040 |  2501K  (1)| 08:20:15 |       |       |
    |   2 |   PARTITION RANGE ALL|                   |   590 | 33040 |  2501K  (1)| 08:20:15 |     1 |1048575|
    |*  3 |    TABLE ACCESS FULL | TABLE1 |   590 | 33040 |  2501K  (1)| 08:20:15 |     1 |1048575|
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       3 - filter(UPPER("OS"."SUCCESS_IND")='S' AND CASE  WHEN ('FULL'='FULL' AND
                  "OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  "OS"."ENTRY_CREATEDDATE"<=TO_DATE(' 2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN
                  'FULL'='INCR' THEN 1 END =1 AND "OS"."REQUESTDATETIME" IS NOT NULL AND CASE  WHEN ('FULL'='FULL'
                  AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd
                  hh24:mi:ss') AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00',
                  'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN ('FULL'='INCR' AND "OS"."ENTRY_CREATEDDATE">=TO_DATE('
                  2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
    PLAN_TABLE_OUTPUT
                  2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 END =1 AND "OS"."ACCTNUM" IS NOT NULL AND
                  "OS"."AVIEWORIGIN_REFID" IS NOT NULL)Edited by: Chaitanya on Jun 9, 2011 2:44 AM
    Edited by: Chaitanya on Jun 9, 2011 2:47 AM

    Hi Dom,
    Modified Query:
    SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
    os.acctnum,
    os.avieworigin_refid,
    COUNT(1) cnt_articleview,
    SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
    FROM TABLE1 os
    WHERE os.acctnum IS NOT NULL
    AND os.avieworigin_refid IS NOT NULL
    AND os.requestdatetime IS NOT NULL
    AND UPPER(os.success_ind) = 'S'
    AND (('FULL'  = 'FULL'
    AND  (get_date_timestamp(os.requestdatetime)  BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
                   AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    AND   os.entry_CreatedDate BETWEEN TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
                   AND TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    OR ('FULL'  = 'INCR'
    AND os.entry_createddate  BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
                   AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') ))
    GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Execute Plan:
    PLAN_TABLE_OUTPUT
    Plan hash value: 3615447714
    | Id  | Operation                 | Name              | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT          |                   | 25125 |  1374K|       |   407K  (1)| 01:21:36 |       |       |
    |   1 |  HASH GROUP BY            |                   | 25125 |  1374K|  3768K|   407K  (1)| 01:21:36 |       |       |
    |   2 |   PARTITION RANGE ITERATOR|                   | 25125 |  1374K|       |   407K  (1)| 01:21:32 |    29 |    31 |
    |*  3 |    TABLE ACCESS FULL      | TABLE1 | 25125 |  1374K|       |   407K  (1)| 01:21:32 |    29 |    31 |
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       3 - filter("OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  UPPER("OS"."SUCCESS_IND")='S' AND "OS"."REQUESTDATETIME" IS NOT NULL AND
                  "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  "OS"."ACCTNUM" IS NOT NULL AND "OS"."AVIEWORIGIN_REFID" IS NOT NULL AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
                  2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))Edited by: Chaitanya on Jun 9, 2011 4:51 AM

  • SQL Query with a little bit more complicated WHERE clause performance issue

    Hello, I have some performance issue in this case:
    Very simplified query:
    SELECT COUNT(*) FROM Items
    WHERE
    ConditionA OR
    ConditionB OR
    ConditionC OR ...
    Simply I have to determine how many Items the user has access through some complicated conditions.
    When there is a large number of records (100,000+) in the Items table and say ~10 complicated conditions concatenated in WHERE clause, I get the result about 2 seconds in my case. The problem is when very few conditions are met, f.e. when I get only
    10 Items from 100,000.
    How can I improve the performace in this "Get my items" case?
    Additional information:
    the query is generated by EF 6.1
    MS SQL 2012 Express
    Here is the main part of the real SQL Execution Plan:

    Can you post table/index DDL?  Query?
    Sample query:
    exec sp_executesql N'SELECT
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
    COUNT(1) AS [A1]
    FROM [dbo].[Tickets] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Services] AS [Extent2] ON [Extent1].[ServiceId] = [Extent2].[Id]
    WHERE (@p__linq__0 = 1) OR ([Extent1].[SubmitterKey] = @p__linq__1) OR ([Extent1].[OperatorKey] = @p__linq__2) OR (([Extent1].[OperatorKey] IS NULL) AND (@p__linq__2 IS NULL)) OR ([Extent1].[SolverKey] = @p__linq__3) OR (([Extent1].[SolverKey] IS NULL) AND (@p__linq__3 IS NULL)) OR ([Extent1].[Incident2ndLineSupportKey] = @p__linq__4) OR (([Extent1].[Incident2ndLineSupportKey] IS NULL) AND (@p__linq__4 IS NULL)) OR ((@p__linq__5 = 1) AND ((1 = CAST( [Extent1].[TicketType] AS int)) OR ((@p__linq__6 = 1) AND (((2 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[IncidentManager] = @p__linq__7) OR (([Extent2].[IncidentManager] IS NULL) AND (@p__linq__7 IS NULL)))) OR ((3 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[ServiceRequestManager] = @p__linq__8) OR (([Extent2].[ServiceRequestManager] IS NULL) AND (@p__linq__8 IS NULL)))) OR ((4 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[ProblemManager] = @p__linq__9) OR (([Extent2].[ProblemManager] IS NULL) AND (@p__linq__9 IS NULL)))) OR ((5 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[ChangeManager] = @p__linq__10) OR (([Extent2].[ChangeManager] IS NULL) AND (@p__linq__10 IS NULL)))))) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceDeputyManagers] AS [Extent3]
    WHERE ([Extent1].[ServiceId] = [Extent3].[ServiceId]) AND ( CAST( [Extent3].[TicketType] AS int) = CAST( [Extent1].[TicketType] AS int)) AND ([Extent3].[UserProviderKey] = @p__linq__11)
    )))) OR ((2 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsIncidents] = 1) OR ((201 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableIncidentTakeFromQueueAction] <> cast(1 as bit)))) AND ([Extent2].[Incident1stLineSupportLimitedAccess] <> cast(1 as bit))) OR ((3 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsServiceRequests] = 1) OR ((301 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableServiceRequestTakeFromQueueAction] <> cast(1 as bit)))) AND ([Extent2].[ServiceRequestLimitedAccess] <> cast(1 as bit))) OR ((4 = CAST( [Extent1].[TicketType] AS int)) AND ([Extent2].[AllowAccessToOtherOperatorsProblems] = 1) AND ([Extent2].[ProblemLimitedAccess] <> cast(1 as bit))) OR ((5 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsChanges] = 1) OR ((501 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableChangeTakeFromQueueAction] <> cast(1 as bit)))) AND ([Extent2].[ChangeLimitedAccess] <> cast(1 as bit))) OR ((2 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsIncidents] = 1) OR ((201 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableIncidentTakeFromQueueAction] <> cast(1 as bit)))) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent4]
    WHERE ([Extent1].[ServiceId] = [Extent4].[ServiceId]) AND (2 = CAST( [Extent4].[TicketType] AS int)) AND ([Extent4].[UserProviderKey] = @p__linq__12) AND (1 = [Extent4].[SupportLine])
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent5]
    WHERE ([Extent5].[UserProviderKey] = @p__linq__13) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent6]
    WHERE ([Extent6].[ServiceId] = [Extent1].[ServiceId]) AND (2 = CAST( [Extent6].[TicketType] AS int)) AND (1 = [Extent6].[SupportLine]) AND ([Extent6].[OperatorGroupId] = [Extent5].[OperatorGroupId])
    )))) OR ((2 = CAST( [Extent1].[TicketType] AS int)) AND ([Extent1].[IncidentFunctionEscalatedTo2ndLineSupport] = 1) AND ([Extent1].[Incident2ndLineSupportKey] IS NULL) AND (([Extent2].[Incident2ndLineSupportLimitedAccess] <> cast(1 as bit)) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent7]
    WHERE ([Extent1].[ServiceId] = [Extent7].[ServiceId]) AND (2 = CAST( [Extent7].[TicketType] AS int)) AND ([Extent7].[UserProviderKey] = @p__linq__14) AND (2 = [Extent7].[SupportLine])
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent8]
    WHERE ([Extent8].[UserProviderKey] = @p__linq__15) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent9]
    WHERE ([Extent9].[ServiceId] = [Extent1].[ServiceId]) AND (2 = CAST( [Extent9].[TicketType] AS int)) AND (2 = [Extent9].[SupportLine]) AND ([Extent9].[OperatorGroupId] = [Extent8].[OperatorGroupId])
    )))) OR ((3 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsServiceRequests] = 1) OR ((301 = CAST( [Extent1].[TicketState] AS int)) AND ([Extent2].[WfDisableServiceRequestTakeFromQueueAction] <> cast(1 as bit)))) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent10]
    WHERE ([Extent1].[ServiceId] = [Extent10].[ServiceId]) AND (3 = CAST( [Extent10].[TicketType] AS int)) AND ([Extent10].[UserProviderKey] = @p__linq__16)
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent11]
    WHERE ([Extent11].[UserProviderKey] = @p__linq__17) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent12]
    WHERE ([Extent12].[ServiceId] = [Extent1].[ServiceId]) AND (3 = CAST( [Extent12].[TicketType] AS int)) AND ([Extent12].[OperatorGroupId] = [Extent11].[OperatorGroupId])
    )))) OR ((4 = CAST( [Extent1].[TicketType] AS int)) AND ([Extent2].[AllowAccessToOtherOperatorsProblems] = 1) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent13]
    WHERE ([Extent1].[ServiceId] = [Extent13].[ServiceId]) AND (4 = CAST( [Extent13].[TicketType] AS int)) AND ([Extent13].[UserProviderKey] = @p__linq__18)
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent14]
    WHERE ([Extent14].[UserProviderKey] = @p__linq__19) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent15]
    WHERE ([Extent15].[ServiceId] = [Extent1].[ServiceId]) AND (4 = CAST( [Extent15].[TicketType] AS int)) AND ([Extent15].[OperatorGroupId] = [Extent14].[OperatorGroupId])
    )))) OR ((5 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsChanges] = 1) OR ((501 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableChangeTakeFromQueueAction] <> cast(1 as bit)))) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent16]
    WHERE ([Extent1].[ServiceId] = [Extent16].[ServiceId]) AND (5 = CAST( [Extent16].[TicketType] AS int)) AND ([Extent16].[UserProviderKey] = @p__linq__20)
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent17]
    WHERE ([Extent17].[UserProviderKey] = @p__linq__21) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent18]
    WHERE ([Extent18].[ServiceId] = [Extent1].[ServiceId]) AND (5 = CAST( [Extent18].[TicketType] AS int)) AND ([Extent18].[OperatorGroupId] = [Extent17].[OperatorGroupId])
    )))) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[TicketInvitations] AS [Extent19]
    WHERE ([Extent19].[TicketId] = [Extent1].[Id]) AND (([Extent19].[InvitedUserProviderKey] = @p__linq__22) OR (([Extent19].[InvitedUserProviderKey] IS NULL) AND (@p__linq__22 IS NULL)))
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM (SELECT
    [Extent20].[CustomerId] AS [CustomerId]
    FROM [dbo].[CustomerUsers] AS [Extent20]
    WHERE ([Extent20].[UserProviderKey] = @p__linq__23) AND ([Extent20].[CanAccessOthersTickets] = 1)
    INTERSECT
    SELECT
    [Extent21].[CustomerId] AS [CustomerId]
    FROM [dbo].[CustomerUsers] AS [Extent21]
    WHERE [Extent21].[UserProviderKey] = [Extent1].[SubmitterKey]) AS [Intersect1]
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM (SELECT
    [Extent22].[InternalGroupId] AS [InternalGroupId]
    FROM [dbo].[InternalGroupUsers] AS [Extent22]
    WHERE ([Extent22].[UserProviderKey] = @p__linq__24) AND ([Extent22].[CanAccessOthersTickets] = 1)
    INTERSECT
    SELECT
    [Extent23].[InternalGroupId] AS [InternalGroupId]
    FROM [dbo].[InternalGroupUsers] AS [Extent23]
    WHERE [Extent23].[UserProviderKey] = [Extent1].[SubmitterKey]) AS [Intersect2]
    ) AS [GroupBy1]',N'@p__linq__0 bit,@p__linq__1 varchar(8000),@p__linq__2 varchar(8000),@p__linq__3 varchar(8000),@p__linq__4 varchar(8000),@p__linq__5 bit,@p__linq__6 bit,@p__linq__7 varchar(8000),@p__linq__8 varchar(8000),@p__linq__9 varchar(8000),@p__linq__10 varchar(8000),@p__linq__11 varchar(8000),@p__linq__12 varchar(8000),@p__linq__13 varchar(8000),@p__linq__14 varchar(8000),@p__linq__15 varchar(8000),@p__linq__16 varchar(8000),@p__linq__17 varchar(8000),@p__linq__18 varchar(8000),@p__linq__19 varchar(8000),@p__linq__20 varchar(8000),@p__linq__21 varchar(8000),@p__linq__22 varchar(8000),@p__linq__23 varchar(8000),@p__linq__24 varchar(8000)',@p__linq__0=0,@p__linq__1='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__2='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__3='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__4='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__5=1,@p__linq__6=0,@p__linq__7='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__8='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__9='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__10='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__11='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__12='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__13='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__14='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__15='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__16='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__17='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__18='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__19='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__20='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__21='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__22='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__23='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__24='31555851-b89d-4a15-bb05-5a6fd42f9552'
    Generated DDL for related tables: (indexes are primary on PKs and FKs)
    CREATE TABLE [dbo].[CustomerUsers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [CustomerId] [int] NOT NULL,
    [CanAccessOthersTickets] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.CustomerUsers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[InternalGroupUsers] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[InternalGroupUsers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [InternalGroupId] [int] NOT NULL,
    [CanAccessOthersTickets] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.InternalGroupUsers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [InternalGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[OperatorGroupUsers] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[OperatorGroupUsers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [OperatorGroupId] [int] NOT NULL,
    CONSTRAINT [PK_dbo.OperatorGroupUsers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [OperatorGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[ServiceDeputyManagers] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[ServiceDeputyManagers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [ServiceId] [int] NOT NULL,
    [TicketType] [int] NOT NULL,
    CONSTRAINT [PK_dbo.ServiceDeputyManagers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [ServiceId] ASC,
    [TicketType] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[ServiceOperatorGroups] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[ServiceOperatorGroups](
    [ServiceId] [int] NOT NULL,
    [OperatorGroupId] [int] NOT NULL,
    [TicketTypeValue] [int] NOT NULL,
    [SupportLine] [int] NOT NULL,
    [TicketType] [int] NOT NULL,
    CONSTRAINT [PK_dbo.ServiceOperatorGroups] PRIMARY KEY CLUSTERED
    [ServiceId] ASC,
    [OperatorGroupId] ASC,
    [TicketTypeValue] ASC,
    [SupportLine] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object: Table [dbo].[ServiceOperators] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[ServiceOperators](
    [UserProviderKey] [varchar](184) NOT NULL,
    [ServiceId] [int] NOT NULL,
    [TicketTypeValue] [int] NOT NULL,
    [SupportLine] [int] NOT NULL,
    [TicketType] [int] NOT NULL,
    CONSTRAINT [PK_dbo.ServiceOperators] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [ServiceId] ASC,
    [TicketTypeValue] ASC,
    [SupportLine] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[Services] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Services](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ParentId] [int] NULL,
    [Name] [nvarchar](256) NOT NULL,
    [Description] [nvarchar](max) NULL,
    [Disabled] [bit] NOT NULL,
    [NewTicketLimitedAccess] [bit] NOT NULL,
    [Incident1stLineSupportLimitedAccess] [bit] NOT NULL,
    [Incident2ndLineSupportLimitedAccess] [bit] NOT NULL,
    [ServiceRequestLimitedAccess] [bit] NOT NULL,
    [ProblemLimitedAccess] [bit] NOT NULL,
    [ServiceRequestManager] [varchar](184) NOT NULL,
    [IncidentManager] [varchar](184) NOT NULL,
    [ProblemManager] [varchar](184) NOT NULL,
    [Deleted] [bit] NOT NULL,
    [WfDisableIncidentAssignedState] [bit] NOT NULL,
    [WfDisableIncidentConfirmedState] [bit] NOT NULL,
    [WfDisableIncidentTakeFromQueueAction] [bit] NOT NULL,
    [WfDisableIncidentFinishSolutionAction] [bit] NOT NULL,
    [WfDisableServiceRequestAssignedState] [bit] NOT NULL,
    [WfDisableServiceRequestConfirmedState] [bit] NOT NULL,
    [WfDisableServiceRequestTakeFromQueueAction] [bit] NOT NULL,
    [WfDisableServiceRequestFinishSolutionAction] [bit] NOT NULL,
    [WfDisableServiceRequestPostponeAction] [bit] NOT NULL,
    [ChangeLimitedAccess] [bit] NOT NULL,
    [ChangeManager] [varchar](184) NOT NULL,
    [WfDisableChangeTakeFromQueueAction] [bit] NOT NULL,
    [WfDisableChangeAssignedState] [bit] NOT NULL,
    [WfDisableChangeStartPreparationAction] [bit] NOT NULL,
    [IsDepartment] [bit] NOT NULL,
    [InheritsFromDepartment] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForIncidents] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForServiceRequests] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForProblems] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForChanges] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsIncidents] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsServiceRequests] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsProblems] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsChanges] [bit] NOT NULL,
    [AllowChangeDeadlineForIncidents] [bit] NOT NULL,
    [AllowChangeDeadlineForServiceRequests] [bit] NOT NULL,
    [AllowChangeDeadlineForProblems] [bit] NOT NULL,
    [AllowChangeDeadlineForChanges] [bit] NOT NULL,
    [AllowSelectPriorityForServiceRequests] [bit] NOT NULL,
    [WfDisableIncidentCompletedState] [bit] NOT NULL,
    [WfDoIncidentCompleteActionBySubmittersMessage] [bit] NOT NULL,
    [WfDisableServiceRequestCompletedState] [bit] NOT NULL,
    [WfDoServiceRequestCompleteActionBySubmittersMessage] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.Services] PRIMARY KEY CLUSTERED
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[TicketInvitations] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[TicketInvitations](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [TicketId] [int] NOT NULL,
    [InitiatorUserProviderKey] [varchar](184) NULL,
    [InitiatorFullName] [nvarchar](max) NULL,
    [InvitedUserProviderKey] [varchar](184) NULL,
    [InvitedFullName] [nvarchar](max) NULL,
    [Type] [int] NOT NULL,
    [CreatedUTC] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.TicketInvitations] PRIMARY KEY CLUSTERED
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[Tickets] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Tickets](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ParentId] [int] NULL,
    [ServiceId] [int] NULL,
    [ServiceMailboxId] [int] NULL,
    [TicketTypeValue] [int] NOT NULL,
    [TicketTypeIdREF] [int] NOT NULL,
    [SubmitterKey] [varchar](184) NOT NULL,
    [SubmitterFullName] [nvarchar](256) NULL,
    [CustomerId] [int] NULL,
    [SolverKey] [varchar](184) NULL,
    [SolverFullName] [nvarchar](256) NULL,
    [Subject] [nvarchar](max) NULL,
    [CreatedUTC] [datetime] NOT NULL,
    [Archived] [bit] NOT NULL,
    [MarkedAsSolvedUTC] [datetime] NULL,
    [ArchivedUTC] [datetime] NULL,
    [TicketSourceValue] [int] NOT NULL,
    [OperatorKey] [varchar](184) NULL,
    [DeadlineUTC] [datetime] NULL,
    [DeadlineLastNotificatedPercentage] [int] NULL,
    [UrgencyValue] [int] NULL,
    [ImpactValue] [int] NULL,
    [PriorityValue] [int] NULL,
    [TicketStateValue] [int] NOT NULL,
    [IncidentFunctionEscalatedTo2ndLineSupport] [bit] NOT NULL,
    [Incident2ndLineSupportKey] [varchar](184) NULL,
    [Incident2ndLineSupportFullName] [nvarchar](max) NULL,
    [TicketType] [int] NOT NULL,
    [Source] [int] NOT NULL,
    [TicketState] [int] NOT NULL,
    [Urgency] [int] NULL,
    [Impact] [int] NULL,
    [TicketSummaryState] [int] NOT NULL,
    [ResolutionText] [nvarchar](max) NULL,
    [ResolutionModifiedUTC] [datetime] NULL,
    [ResolutionEdited] [bit] NOT NULL,
    [ResolutionUserProviderKey] [varchar](184) NULL,
    [ResolutionFullName] [nvarchar](max) NULL,
    [TicketSubType] [int] NULL,
    [ChangeRiskProbabilityValue] [int] NULL,
    [ChangeImpactValue] [int] NULL,
    [ChangeRiskCategoryValue] [int] NULL,
    [RfcText] [nvarchar](max) NULL,
    [RfcModifiedUTC] [datetime] NULL,
    [RfcEdited] [bit] NOT NULL,
    [RfcUserProviderKey] [varchar](184) NULL,
    [RfcFullName] [nvarchar](max) NULL,
    [ManualDeadline] [bit] NOT NULL,
    [ContactInformation] [nvarchar](256) NULL,
    [Imported] [bit] NOT NULL,
    [ForceClosed] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.Tickets] PRIMARY KEY CLUSTERED
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Index [IX_CustomerId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_CustomerId] ON [dbo].[CustomerUsers]
    [CustomerId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_InternalGroupId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_InternalGroupId] ON [dbo].[InternalGroupUsers]
    [InternalGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_OperatorGroupId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_OperatorGroupId] ON [dbo].[OperatorGroupUsers]
    [OperatorGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[ServiceDeputyManagers]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_OperatorGroupId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_OperatorGroupId] ON [dbo].[ServiceOperatorGroups]
    [OperatorGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[ServiceOperatorGroups]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[ServiceOperators]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ParentId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ParentId] ON [dbo].[Services]
    [ParentId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_TicketId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_TicketId] ON [dbo].[TicketInvitations]
    [TicketId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_TicketInvitations_InvitedUserProviderKey_TicketId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE UNIQUE NONCLUSTERED INDEX [IX_TicketInvitations_InvitedUserProviderKey_TicketId] ON [dbo].[TicketInvitations]
    [InvitedUserProviderKey] ASC,
    [TicketId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_CustomerId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_CustomerId] ON [dbo].[Tickets]
    [CustomerId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ParentId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ParentId] ON [dbo].[Tickets]
    [ParentId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[Tickets]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceMailboxId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceMailboxId] ON [dbo].[Tickets]
    [ServiceMailboxId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SolverFullName] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SolverFullName] ON [dbo].[Tickets]
    [SolverFullName] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SolverKey] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SolverKey] ON [dbo].[Tickets]
    [SolverKey] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SubmitterFullName] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SubmitterFullName] ON [dbo].[Tickets]
    [SubmitterFullName] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SubmitterKey] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SubmitterKey] ON [dbo].[Tickets]
    [SubmitterKey] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_Tickets_TicketType_TicketTypeIdREF] Script Date: 7.5.2014 8:39:38 ******/
    CREATE UNIQUE NONCLUSTERED INDEX [IX_Tickets_TicketType_TicketTypeIdREF] ON [dbo].[Tickets]
    [TicketType] ASC,
    [TicketTypeIdREF] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    ALTER TABLE [dbo].[CustomerUsers] WITH CHECK ADD CONSTRAINT [FK_dbo.CustomerUsers_dbo.Customers_CustomerId] FOREIGN KEY([CustomerId])
    REFERENCES [dbo].[Customers] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[CustomerUsers] CHECK CONSTRAINT [FK_dbo.CustomerUsers_dbo.Customers_CustomerId]
    GO
    ALTER TABLE [dbo].[InternalGroupUsers] WITH CHECK ADD CONSTRAINT [FK_dbo.InternalGroupUsers_dbo.InternalGroups_InternalGroupId] FOREIGN KEY([InternalGroupId])
    REFERENCES [dbo].[InternalGroups] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[InternalGroupUsers] CHECK CONSTRAINT [FK_dbo.InternalGroupUsers_dbo.InternalGroups_InternalGroupId]
    GO
    ALTER TABLE [dbo].[OperatorGroupUsers] WITH CHECK ADD CONSTRAINT [FK_dbo.OperatorGroupUsers_dbo.OperatorGroups_OperatorGroupId] FOREIGN KEY([OperatorGroupId])
    REFERENCES [dbo].[OperatorGroups] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[OperatorGroupUsers] CHECK CONSTRAINT [FK_dbo.OperatorGroupUsers_dbo.OperatorGroups_OperatorGroupId]
    GO
    ALTER TABLE [dbo].[ServiceDeputyManagers] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceDeputyManagers_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceDeputyManagers] CHECK CONSTRAINT [FK_dbo.ServiceDeputyManagers_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.OperatorGroups_OperatorGroupId] FOREIGN KEY([OperatorGroupId])
    REFERENCES [dbo].[OperatorGroups] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] CHECK CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.OperatorGroups_OperatorGroupId]
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] CHECK CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[ServiceOperators] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceOperators_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceOperators] CHECK CONSTRAINT [FK_dbo.ServiceOperators_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[Services] WITH CHECK ADD CONSTRAINT [FK_dbo.Services_dbo.Services_ParentId] FOREIGN KEY([ParentId])
    REFERENCES [dbo].[Services] ([Id])
    GO
    ALTER TABLE [dbo].[Services] CHECK CONSTRAINT [FK_dbo.Services_dbo.Services_ParentId]
    GO
    ALTER TABLE [dbo].[TicketInvitations] WITH CHECK ADD CONSTRAINT [FK_dbo.TicketInvitations_dbo.Tickets_TicketId] FOREIGN KEY([TicketId])
    REFERENCES [dbo].[Tickets] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[TicketInvitations] CHECK CONSTRAINT [FK_dbo.TicketInvitations_dbo.Tickets_TicketId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.Customers_CustomerId] FOREIGN KEY([CustomerId])
    REFERENCES [dbo].[Customers] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.Customers_CustomerId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.ServiceMailboxes_ServiceMailboxId] FOREIGN KEY([ServiceMailboxId])
    REFERENCES [dbo].[ServiceMailboxes] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.ServiceMailboxes_ServiceMailboxId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.Tickets_ParentId] FOREIGN KEY([ParentId])
    REFERENCES [dbo].[Tickets] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.Tickets_ParentId]
    GO

  • Response time issue querying a view

    Hi All,
    The following query is taking more than 20-25 mins to run and it does not return any result. Ideally it should return 0 rows but still it takes 20-25 mins to achieve that.
    Select * from WV_WMS_STOCK_MOVEMENT; --> it’s a View
    Database: Oracle 8i
    O/S : Solaris 8
    Below is the explain plan for above stmt
    SQL> @?/rdbms/admin/utlxpls
    | Operation                 |  Name    |  Rows | Bytes|  Cost  | Pstart| Pstop |                                                 
    | SELECT STATEMENT          |          |   364 |  774K|1042445 |       |       |                                                 
    |  VIEW                     |WV_WMS_ST |   364 |  774K|1042445 |       |       |                                                 
    |   SORT UNIQUE             |          |   364 |   30K|1042445 |       |       |                                                 
    |    UNION-ALL              |          |       |      |        |       |       |                                                 
    |     FILTER                |          |       |      |        |       |       |                                                 
    |      NESTED LOOPS         |          |     1 |  116 | 173829 |       |       |                                                 
    |       HASH JOIN           |          |     1 |  102 | 173828 |       |       |                                                 
    |        NESTED LOOPS       |          |    12K|  822K| 172929 |       |       |                                                 
    |         TABLE ACCESS FULL |MSK       |   145K|    7M| 172929 |       |       |                                                 
    |         INDEX UNIQUE SCAN |TBL_IDX1  |    15 |  210 |        |       |       |                                                 
    |        TABLE ACCESS FULL  |OST       |    85K|    2M|    445 |       |       |                                                 
    |       INDEX UNIQUE SCAN   |PRO_IDX1  |   261K|    3M|      1 |       |       |                                                 
    |      TABLE ACCESS BY INDEX|TBL       |     1 |   25 |      7 |       |       |                                                 
    |       INDEX RANGE SCAN    |TBL_IDX1  |     1 |      |      2 |       |       |                                                 
    |     FILTER                |          |       |      |        |       |       |                                                 
    |      HASH JOIN            |          |   240 |   19K| 174015 |       |       |                                                 
    |       NESTED LOOPS        |          |    12K|  822K| 172929 |       |       |                                                 
    |        TABLE ACCESS FULL  |MSK       |   145K|    7M| 172929 |       |       |                                                 
    |        INDEX UNIQUE SCAN  |TBL_IDX1  |    15 |  210 |        |       |       |                                                 
    |       INDEX FAST FULL SCAN|PRO_IDX1  |   261K|    3M|    293 |       |       |                                                 
    |      TABLE ACCESS BY INDEX|TBL       |     1 |   25 |      7 |       |       |                                                 
    |       INDEX RANGE SCAN    |TBL_IDX1  |     1 |      |      2 |       |       |                                                 
    |     FILTER                |          |       |      |        |       |       |                                                 
    |      HASH JOIN            |          |   120 |   10K| 174017 |       |       |                                                 
    |       NESTED LOOPS        |          |    12K|  858K| 172929 |       |       |                                                 
    |        TABLE ACCESS FULL  |MSK       |   145K|    8M| 172929 |       |       |                                                 
    |        INDEX UNIQUE SCAN  |TBL_IDX1  |    15 |  210 |        |       |       |                                                 
    |       INDEX FAST FULL SCAN|PRO_IDX1  |   261K|    3M|    293 |       |       |                                                 
    |      TABLE ACCESS BY INDEX|TBL       |     1 |   25 |      7 |       |       |                                                 
    |       INDEX RANGE SCAN    |TBL_IDX1  |     1 |      |      2 |       |       |                                                 
    |     FILTER                |          |       |      |        |       |       |                                                 
    |      NESTED LOOPS         |          |     1 |  165 | 173485 |       |       |                                                 
    |       NESTED LOOPS        |          |     1 |  151 | 173484 |       |       |                                                 
    |        HASH JOIN          |          |     1 |  101 | 173478 |       |       |                                                 
    |         TABLE ACCESS FULL |MSK       |   803 |   54K| 172929 |       |       |                                                 
    |         TABLE ACCESS FULL |OST       |    85K|    2M|    445 |       |       |                                                 
    |        TABLE ACCESS BY IND|MSK       |    32K|    1M|      6 |       |       |                                                 
    |         INDEX RANGE SCAN  |MSK_IDX1  |    32K|      |      5 |       |       |                                                 
    |       INDEX UNIQUE SCAN   |PRO_IDX1  |   261K|    3M|      1 |       |       |                                                 
    |      TABLE ACCESS BY INDEX|TBL       |     1 |   25 |      7 |       |       |                                                 
    |       INDEX RANGE SCAN    |TBL_IDX1  |     1 |      |      2 |       |       |                                                 
    |     FILTER                |          |       |      |        |       |       |                                                 
    |      NESTED LOOPS         |          |     1 |  133 | 173560 |       |       |                                                 
    |       HASH JOIN           |          |    16 |    1K| 173464 |       |       |                                                 
    |        TABLE ACCESS FULL  |MSK       |   803 |   54K| 172929 |       |       |                                                 
    |        INDEX FAST FULL SCA|PRO_IDX1  |   261K|    3M|    293 |       |       |                                                 
    |       TABLE ACCESS BY INDE|MSK       |    32K|    1M|      6 |       |       |                                                 
    |        INDEX RANGE SCAN   |MSK_IDX1  |    32K|      |      5 |       |       |                                                 
    |      TABLE ACCESS BY INDEX|TBL       |     1 |   25 |      7 |       |       |                                                 
    |       INDEX RANGE SCAN    |TBL_IDX1  |     1 |      |      2 |       |       |                                                 
    |     FILTER                |          |       |      |        |       |       |                                                 
    |      NESTED LOOPS         |          |     1 |  133 | 173512 |       |       |                                                 
    |       HASH JOIN           |          |     8 |  664 | 173464 |       |       |                                                 
    |        TABLE ACCESS FULL  |MSK       |   803 |   54K| 172929 |       |       |                                                 
    |        INDEX FAST FULL SCA|PRO_IDX1  |   261K|    3M|    293 |       |       |                                                 
    |       TABLE ACCESS BY INDE|MSK       |    32K|    1M|      6 |       |       |                                                 
    |        INDEX RANGE SCAN   |MSK_IDX1  |    32K|      |      5 |       |       |                                                 
    |      TABLE ACCESS FULL    |TBL       |     1 |   25 |     49 |       |       |                                                 
    --------------------------------------------------------------------------------                                                  Code of view is :
    CREATE OR REPLACE VIEW "SOC1"."WV_WMS_STOCK_MOVEMENT" ("CODSOC",
        "CODOSK","WAREHOUSE_SITE","SIGDEP_FROM","SIGDEP_TO","CODPRO",
        "QTEOPE","DATMVT","HEUMVT","REFLOT","DATLC","LIBMSK","NUMMSK",
        "INDTRT","NUMLOT","ID","PUMP") AS
        SELECT 
      msk.codsoc, msk.codosk, DECODE(SUBSTR(msk.sigdep,1,3), 'GER', 'GAR', SUBSTR(msk.sigdep,1,3)), 
      msk.sigdep, msk.sigdep, 
      msk.codpro, msk.qteope, msk.datmvt, WF_WMS_HEURE(msk.heumvt), ost.reflot, 
      ost.datlc,  msk.libmsk, msk.nummsk, msk.indtrt, msk.numlot, 
      RPAD(msk.sigdep,12,' ')||RPAD(msk.codpro,12,' ')||RPAD(msk.numlot,12,' ')||msk.nummsk, '1.23' 
    from tbl t954, pro, ost, msk 
    where 
         msk.codsoc = 0 
    and  msk.indtrt = ' ' 
    and  ost.codsoc = msk.codsoc 
    and  ost.codpro = msk.codpro 
    and  ost.numlot = msk.numlot 
    and  pro.codsoc = msk.codsoc 
    and  pro.codpro = msk.codpro 
    and  WF_WMS_SUISTK(msk.sigdep, pro.codpro) in ('L', 'X') 
    and  msk.sigdep NOT IN (select a.lib1 
                              from tbl a 
                             where a.codsoc = msk.codsoc 
                               and a.codtbl = '961' 
                               and a.lir = 'DEP') 
    and t954.codsoc = msk.codsoc 
    and t954.codtbl = '954' 
    and t954.cletbl = msk.codosk 
    UNION 
    -- Single MVT 
    SELECT 
      msk.codsoc, msk.codosk, DECODE(SUBSTR(msk.sigdep,1,3), 'GER', 'GAR', SUBSTR(msk.sigdep,1,3)), 
      msk.sigdep, msk.sigdep, 
      msk.codpro, msk.qteope, msk.datmvt, WF_WMS_HEURE(msk.heumvt), ' ', 
      ' ', msk.libmsk, msk.nummsk, msk.indtrt, msk.numlot, 
      RPAD(msk.sigdep,12,' ')||RPAD(msk.codpro,12,' ')||RPAD(msk.numlot,12,' ')||msk.nummsk , '1.23' 
    from tbl t954, pro, msk 
    where 
         msk.codsoc = 0 
    and  msk.indtrt = ' ' 
    and  pro.codsoc = msk.codsoc 
    and  pro.codpro = msk.codpro 
    and  WF_WMS_SUISTK(msk.sigdep, pro.codpro) in ('S', 'E') 
    and  msk.sigdep NOT IN (select a.lib1 
                              from tbl a 
                             where a.codsoc = msk.codsoc 
                               and a.codtbl = '961' 
                               and a.lir = 'DEP') 
    and t954.codsoc = msk.codsoc 
    and t954.codtbl = '954' 
    and t954.cletbl = msk.codosk 
    UNION 
    -- Single MVT 
    SELECT 
      msk.codsoc, msk.codosk, DECODE(SUBSTR(msk.sigdep,1,3), 'GER', 'GAR', SUBSTR(msk.sigdep,1,3)), 
      msk.sigdep,   msk.sigdep, 
      msk.codpro, msk.qteope, msk.datmvt, WF_WMS_HEURE(msk.heumvt), msk.numdeb, 
      ' ', msk.libmsk, msk.nummsk, msk.indtrt, msk.numlot, 
      RPAD(msk.sigdep,12,' ')||RPAD(msk.codpro,12,' ')||RPAD(msk.numlot,12,' ')||msk.nummsk, '1.23' 
    from tbl t954, pro, msk 
    where 
         msk.codsoc = 0 
    and  msk.indtrt = ' ' 
    and  pro.codsoc = msk.codsoc 
    and  pro.codpro = msk.codpro 
    and  WF_WMS_SUISTK(msk.sigdep, pro.codpro) = 'U' 
    and  msk.sigdep NOT IN (select a.lib1 
                              from tbl a 
                             where a.codsoc = msk.codsoc 
                               and a.codtbl = '961' 
                               and a.lir = 'DEP') 
    and t954.codsoc = msk.codsoc 
    and t954.codtbl = '954' 
    and t954.cletbl = msk.codosk 
    UNION 
    -- transfer 
    SELECT 
      a.codsoc, a.codosk, DECODE(SUBSTR(a.sigdep,1,3), 'GER', 'GAR', SUBSTR(a.sigdep,1,3)), 
      a.sigdep, b.sigdep, 
      a.codpro, a.qteope, a.datmvt, WF_WMS_HEURE(a.heumvt), ost.reflot, 
      ost.datlc,DECODE(a.typeve, 'RET', a.typeve||a.numeve, a.libmsk), a.nummsk, a.indtrt, a.numlot, 
      RPAD(a.sigdep,12,' ')||RPAD(a.codpro,12,' ')||RPAD(a.numlot,12,' ')||a.nummsk , '1.23' 
    from ost, pro, msk a, msk b 
    where 
        a.codsoc=0 
    and a.codosk = 'WHOR' 
    and a.indtrt = ' ' 
    and b.codsoc = a.codsoc 
    and b.codpro = a.codpro 
    and b.numlot = a.numlot 
    and b.numdeb = a.numdeb 
    and b.datmvt = a.datmvt 
    and b.heumvt = a.heumvt 
    and b.qteope = a.qteope 
    and b.codosk = 'WHIR' 
    and  ost.codsoc = a.codsoc 
    and  ost.codpro = a.codpro 
    and  ost.numlot = a.numlot 
    and  pro.codsoc = a.codsoc 
    and  pro.codpro = a.codpro 
    and  WF_WMS_SUISTK(a.sigdep, pro.codpro) in ('L', 'X') 
    and  a.sigdep NOT IN (select t.lib1 
                              from tbl t 
           where t.codsoc = a.codsoc 
             and t.codtbl = '961' 
            and t.lir = 'DEP') 
    UNION 
    -- Transfer 
    SELECT 
      a.codsoc, a.codosk, DECODE(SUBSTR(a.sigdep,1,3), 'GER', 'GAR', SUBSTR(a.sigdep,1,3)), 
      a.sigdep, b.sigdep, 
      a.codpro, a.qteope, a.datmvt, WF_WMS_HEURE(a.heumvt), ' ', 
      ' ', DECODE(a.typeve, 'RET', a.typeve||a.numeve, a.libmsk), a.nummsk, a.indtrt, a.numlot, 
        RPAD(a.sigdep,12,' ')||RPAD(a.codpro,12,' ')||RPAD(a.numlot,12,' ')||a.nummsk , '1.23' 
    from pro, msk a, msk b 
    where 
        a.codsoc=0 
    and a.indtrt = ' ' 
    and a.codosk = 'WHOR' 
    and b.codsoc = a.codsoc 
    and b.codpro = a.codpro 
    and b.numlot = a.numlot 
    and b.numdeb = a.numdeb 
    and b.datmvt = a.datmvt 
    and b.heumvt = a.heumvt 
    and b.qteope = a.qteope 
    and b.codosk = 'WHIR' 
    and  pro.codsoc = a.codsoc 
    and  pro.codpro = a.codpro 
    and  WF_WMS_SUISTK(a.sigdep, pro.codpro) in ('S', 'E') 
    and  a.sigdep NOT IN (select t.lib1 
                              from tbl t 
           where t.codsoc = a.codsoc 
             and t.codtbl = '961' 
             and t.lir = 'DEP') 
    UNION 
    -- TRFINI 
    SELECT 
      a.codsoc, a.codosk, DECODE(SUBSTR(a.sigdep,1,3), 'GER', 'GAR', SUBSTR(a.sigdep,1,3)), 
      a.sigdep, b.sigdep, 
      a.codpro, a.qteope, a.datmvt,WF_WMS_HEURE(a.heumvt), a.numdeb, 
      ' ', DECODE(a.typeve, 'RET', a.typeve||a.numeve, a.libmsk), a.nummsk, a.indtrt, a.numlot, 
      RPAD(a.sigdep,12,' ')||RPAD(a.codpro,12,' ')||RPAD(a.numlot,12,' ')||a.nummsk, '1.23' 
    from pro, msk a, msk b 
    where 
        a.codsoc=0 
    and a.indtrt = ' ' 
    and a.codosk = 'WHOR' 
    and b.codsoc = a.codsoc 
    and b.codpro = a.codpro 
    and b.numlot = a.numlot 
    and b.numdeb = a.numdeb 
    and b.datmvt = a.datmvt 
    and b.heumvt = a.heumvt 
    and b.qteope = a.qteope 
    and b.codosk = 'WHIR' 
    and  pro.codsoc = a.codsoc 
    and  pro.codpro = a.codpro 
    and  WF_WMS_SUISTK(a.sigdep, pro.codpro) = 'U' 
    and  a.sigdep NOT IN (select a.lib1 
                              from tbl a 
           where a.codsoc = a.codsoc 
             and a.codtbl = '961' 
             and a.lir = 'DEP')Please help...
    Thanks

    I see three problems, one of which was pointed out by Ignacio Ruiz.
    #1 IN (SELECT ...) and NOT IN (SELECT ...) tends to be slow in Oracle 8i due to the requirement that the subquery be processed multiple times. It is often more efficient to convert this syntax into an outer join, and specify that the join column is either NOT NULL or NULL, depending on if you are trying to replace IN (SELECT ...) or, NOT IN (SELECT ...) syntax.
    #2 It appears that there area couple PL/SQL calls, which can cause a context switch, and hinder performance: WF_WMS_HEURE(msk.heumvt), WF_WMS_SUISTK(msk.sigdep, pro.codpro), WF_WMS_HEURE(a.heumvt), etc.
    #3 UNION syntax is used rather than UNION ALL - if possible, use UNION ALL instead.
    An example of fixing problem #1, adjusting just the portion of the query before the "UNION":
    CREATE OR REPLACE VIEW "SOC1"."WV_WMS_STOCK_MOVEMENT" ("CODSOC",
        "CODOSK","WAREHOUSE_SITE","SIGDEP_FROM","SIGDEP_TO","CODPRO",
        "QTEOPE","DATMVT","HEUMVT","REFLOT","DATLC","LIBMSK","NUMMSK",
        "INDTRT","NUMLOT","ID","PUMP") AS
    SELECT 
      msk.codsoc, msk.codosk, DECODE(SUBSTR(msk.sigdep,1,3), 'GER', 'GAR', SUBSTR(msk.sigdep,1,3)), 
      msk.sigdep, msk.sigdep, 
      msk.codpro, msk.qteope, msk.datmvt, WF_WMS_HEURE(msk.heumvt), ost.reflot, 
      ost.datlc,  msk.libmsk, msk.nummsk, msk.indtrt, msk.numlot, 
      RPAD(msk.sigdep,12,' ')||RPAD(msk.codpro,12,' ')||RPAD(msk.numlot,12,' ')||msk.nummsk, '1.23' 
    from tbl t954, pro, ost, msk,
      (select distinct
         a.codsoc,
         a.lib1 
       from
         tbl a 
       where
         a.codtbl = '961' 
         and a.lir = 'DEP') a
    where 
         msk.codsoc = 0 
    and  msk.indtrt = ' ' 
    and  ost.codsoc = msk.codsoc 
    and  ost.codpro = msk.codpro 
    and  ost.numlot = msk.numlot 
    and  pro.codsoc = msk.codsoc 
    and  pro.codpro = msk.codpro 
    and  WF_WMS_SUISTK(msk.sigdep, pro.codpro) in ('L', 'X') 
    and  msk.codsoc=a.codsoc(+)
    and  msk.sigdep=a.lib1(+) 
    and  a.codsoc is null
    and t954.codsoc = msk.codsoc 
    and t954.codtbl = '954' 
    and t954.cletbl = msk.codosk 
    UNION 
    -- Single MVT 
    SELECT 
      msk.codsoc, msk.codosk, DECODE(SUBSTR(msk.sigdep,1,3), 'GER', 'GAR', SUBSTR(msk.sigdep,1,3)), 
      msk.sigdep, msk.sigdep, 
      msk.codpro, msk.qteope, msk.datmvt, WF_WMS_HEURE(msk.heumvt), ' ', 
      ' ', msk.libmsk, msk.nummsk, msk.indtrt, msk.numlot, 
      RPAD(msk.sigdep,12,' ')||RPAD(msk.codpro,12,' ')||RPAD(msk.numlot,12,' ')||msk.nummsk , '1.23' 
    from tbl t954, pro, msk 
    where 
         msk.codsoc = 0 
    and  msk.indtrt = ' ' 
    and  pro.codsoc = msk.codsoc 
    and  pro.codpro = msk.codpro 
    and  WF_WMS_SUISTK(msk.sigdep, pro.codpro) in ('S', 'E') 
    and  msk.sigdep NOT IN (select a.lib1 
                              from tbl a 
                             where a.codsoc = msk.codsoc 
                               and a.codtbl = '961' 
                               and a.lir = 'DEP') 
    and t954.codsoc = msk.codsoc 
    and t954.codtbl = '954' 
    and t954.cletbl = msk.codosk 
    UNION 
    -- Single MVT 
    SELECT 
      msk.codsoc, msk.codosk, DECODE(SUBSTR(msk.sigdep,1,3), 'GER', 'GAR', SUBSTR(msk.sigdep,1,3)), 
      msk.sigdep,   msk.sigdep, 
      msk.codpro, msk.qteope, msk.datmvt, WF_WMS_HEURE(msk.heumvt), msk.numdeb, 
      ' ', msk.libmsk, msk.nummsk, msk.indtrt, msk.numlot, 
      RPAD(msk.sigdep,12,' ')||RPAD(msk.codpro,12,' ')||RPAD(msk.numlot,12,' ')||msk.nummsk, '1.23' 
    from tbl t954, pro, msk 
    where 
         msk.codsoc = 0 
    and  msk.indtrt = ' ' 
    and  pro.codsoc = msk.codsoc 
    and  pro.codpro = msk.codpro 
    and  WF_WMS_SUISTK(msk.sigdep, pro.codpro) = 'U' 
    and  msk.sigdep NOT IN (select a.lib1 
                              from tbl a 
                             where a.codsoc = msk.codsoc 
                               and a.codtbl = '961' 
                               and a.lir = 'DEP') 
    and t954.codsoc = msk.codsoc 
    and t954.codtbl = '954' 
    and t954.cletbl = msk.codosk 
    UNION 
    -- transfer 
    SELECT 
      a.codsoc, a.codosk, DECODE(SUBSTR(a.sigdep,1,3), 'GER', 'GAR', SUBSTR(a.sigdep,1,3)), 
      a.sigdep, b.sigdep, 
      a.codpro, a.qteope, a.datmvt, WF_WMS_HEURE(a.heumvt), ost.reflot, 
      ost.datlc,DECODE(a.typeve, 'RET', a.typeve||a.numeve, a.libmsk), a.nummsk, a.indtrt, a.numlot, 
      RPAD(a.sigdep,12,' ')||RPAD(a.codpro,12,' ')||RPAD(a.numlot,12,' ')||a.nummsk , '1.23' 
    from ost, pro, msk a, msk b 
    where 
        a.codsoc=0 
    and a.codosk = 'WHOR' 
    and a.indtrt = ' ' 
    and b.codsoc = a.codsoc 
    and b.codpro = a.codpro 
    and b.numlot = a.numlot 
    and b.numdeb = a.numdeb 
    and b.datmvt = a.datmvt 
    and b.heumvt = a.heumvt 
    and b.qteope = a.qteope 
    and b.codosk = 'WHIR' 
    and  ost.codsoc = a.codsoc 
    and  ost.codpro = a.codpro 
    and  ost.numlot = a.numlot 
    and  pro.codsoc = a.codsoc 
    and  pro.codpro = a.codpro 
    and  WF_WMS_SUISTK(a.sigdep, pro.codpro) in ('L', 'X') 
    and  a.sigdep NOT IN (select t.lib1 
                              from tbl t 
           where t.codsoc = a.codsoc 
             and t.codtbl = '961' 
            and t.lir = 'DEP') 
    UNION 
    -- Transfer 
    SELECT 
      a.codsoc, a.codosk, DECODE(SUBSTR(a.sigdep,1,3), 'GER', 'GAR', SUBSTR(a.sigdep,1,3)), 
      a.sigdep, b.sigdep, 
      a.codpro, a.qteope, a.datmvt, WF_WMS_HEURE(a.heumvt), ' ', 
      ' ', DECODE(a.typeve, 'RET', a.typeve||a.numeve, a.libmsk), a.nummsk, a.indtrt, a.numlot, 
        RPAD(a.sigdep,12,' ')||RPAD(a.codpro,12,' ')||RPAD(a.numlot,12,' ')||a.nummsk , '1.23' 
    from pro, msk a, msk b 
    where 
        a.codsoc=0 
    and a.indtrt = ' ' 
    and a.codosk = 'WHOR' 
    and b.codsoc = a.codsoc 
    and b.codpro = a.codpro 
    and b.numlot = a.numlot 
    and b.numdeb = a.numdeb 
    and b.datmvt = a.datmvt 
    and b.heumvt = a.heumvt 
    and b.qteope = a.qteope 
    and b.codosk = 'WHIR' 
    and  pro.codsoc = a.codsoc 
    and  pro.codpro = a.codpro 
    and  WF_WMS_SUISTK(a.sigdep, pro.codpro) in ('S', 'E') 
    and  a.sigdep NOT IN (select t.lib1 
                              from tbl t 
           where t.codsoc = a.codsoc 
             and t.codtbl = '961' 
             and t.lir = 'DEP') 
    UNION 
    -- TRFINI 
    SELECT 
      a.codsoc, a.codosk, DECODE(SUBSTR(a.sigdep,1,3), 'GER', 'GAR', SUBSTR(a.sigdep,1,3)), 
      a.sigdep, b.sigdep, 
      a.codpro, a.qteope, a.datmvt,WF_WMS_HEURE(a.heumvt), a.numdeb, 
      ' ', DECODE(a.typeve, 'RET', a.typeve||a.numeve, a.libmsk), a.nummsk, a.indtrt, a.numlot, 
      RPAD(a.sigdep,12,' ')||RPAD(a.codpro,12,' ')||RPAD(a.numlot,12,' ')||a.nummsk, '1.23' 
    from pro, msk a, msk b 
    where 
        a.codsoc=0 
    and a.indtrt = ' ' 
    and a.codosk = 'WHOR' 
    and b.codsoc = a.codsoc 
    and b.codpro = a.codpro 
    and b.numlot = a.numlot 
    and b.numdeb = a.numdeb 
    and b.datmvt = a.datmvt 
    and b.heumvt = a.heumvt 
    and b.qteope = a.qteope 
    and b.codosk = 'WHIR' 
    and  pro.codsoc = a.codsoc 
    and  pro.codpro = a.codpro 
    and  WF_WMS_SUISTK(a.sigdep, pro.codpro) = 'U' 
    and  a.sigdep NOT IN (select a.lib1 
                              from tbl a 
           where a.codsoc = a.codsoc 
             and a.codtbl = '961' 
             and a.lir = 'DEP')Try changing the remaining NOT IN subqueries to outer joins, and compare the performance.
    Charles Hooper
    IT Manager/Oracle DBA
    K&M Machine-Fabricating, Inc.

  • I have Apple O/S 10.6.7 and Firefox 4 and when I play a homemade slideshow/movie from my webgallery it plays correctly the first time, and then play upside down after that, unless I shut down Firefox and then restart it...why?

    I discovered this incompatibility (?) with the help of a Sr. Advisor from Apple. I have uninstalled Firefox and the plist and reinstalled it and the same thing happened. Is there something that I can to do circumvent this problem?

    I discovered this incompatibility (?) with the help of a Sr. Advisor from Apple. I have uninstalled Firefox and the plist and reinstalled it and the same thing happened. Is there something that I can to do circumvent this problem?

  • How to find the Response time for a particular Transaction

    Hello Experts,
            Am implementing a BAdI to achieve some customer enhancement for XD01 Transaction . I need to confirm to customer that after the implementation and before implementation what is the response time of the system
    Response time BEFORE BAdI Implementation
    Response time AFTER BAdI Implementation
    Where can i get this.
    Help me in this regard
    Best Regards
    SRiNi

    Hello,
    Within STAD, enter the time range that the user was executing the transaction within as well as the user name. The time field indicates the time when the transaction would have ended. STAD adds some extra time on using your time interval. Depending on how long the transaction ran, you can set the length you want it to display. This means that if it is set to 10, STAD will display statistical records from transactions that ended within that 10 minute period.
    The selection screen also gives you a few options for display mode.
    - Show all statistic records, sorted by star
    This shows you all of the transaction steps, but they are not grouped in any way.
    -Show all records, grouped by business transaction
    This shows the transaction steps grouped by transaction ID (shown in the record as Trans. ID). The times are not cumulative. They are the times for each individual step.
    -Show Business Transaction Tots
    This shows the transaction steps grouped by transaction ID. However, instead of just listing them you can drill from the top level down. The top level will show you the overall response time, and as you drill down, you can get to the overall response time.
    Note that you also need to add the user into the selection criteria. Everything else you can leave alone in this case.
    Once you have the records displayed, you can double click them to get a detailed record. This will show you the following:
    - Breakdown of response time (wait for work process, processing time, load time, generating time, roll time, DB time, enqueue time). This makes STAD a great place to start for performance analysis as you will then know whether you will need to look at SQL, processing, or any other component of response time first.
    - Stats on the data selected within the execution
    - Memory utilization of the transaction
    - RFCs executed (including the calling time and remote execution time - very useful with performance analysis of interfaces)
    - Much more.
    As this chain of comments has previously indicated, you are best off using STAD if you want an accurate indication of response time. The ST12 (combines SE30 ABAP trace and ST05 SQL trace) trace times are less accurate that the values you get from ST12. I am not discounting the value of ST12 by any means. This is a very powerful tool to help you tune your transactions.
    I hope this information is helpful!
    Kind regards,
    Geoff Irwin
    Senior Support Consultant
    SAP Active Global Support

  • Slow response time with terminal emulator

    We are using Oracle Financials pkg. 10.7 Character mode using Oracle DB 7.3.3 on a IBM (sequent) S5000 running at DYNIX/ptx 4.2.3 (soon to be 4.2.4). On the end user side, the accounting dept. is connecting to the server using EXTRA Personal Client. During the course of the day, the response time of the emulator slows down. I can count to 25 sometimes b/4 the emulator responds (catches up with the key strokes). Some days it does not happen until the end of the work day and other times it can happen after just a few hours. After checking the network, application and server, we can find nothing wrong. If we have the user reboot their p.c., this seems to clear up the problem. Has anyone else seen or heard of this problem??? If so, does anyone know how to correct it??? We have installed and tried a different emulator that seems to be doing the samething, but only not as bad (slow).
    Thanks,
    Tony Dopkins
    UNIX System Administrator
    Andersen Windows

    Never heard of this problem or this emulator product.
    Oracle did/does have their own emulator that sort of similuates a GUI environment.
    It is called OADM (Oracle Display Manager).
    If you call support, they may be able to send it to you. However, it's no longer supported, and is a little buggy. But it has no performance problems.
    If it straight character feel the users need, Kea TERM works 4 me.
    Or, possibly call the your emulators support line.. it could be a known issue with their product..
    Most likely it has nothing to do with Oracle products.

  • Slow Response Time Joining Multiple Domain Indexes

    Hi All,
    I am working with a schema that has several full text searchable columns along with several other convential columns. I noticed that when I join on 2 Domain Indexes, the response time for query completion nearly doubles. However, If I were to combine my 2 clob columns into 1 clob, the extra cost of finding the intersection of 2 rows sets can be saved..
    In my query, I am taking 2 sets of random high probability words (the top 1000 sorted by token_count DESC).
    NOTE: All of my query execution times are taken with words not previously used to avoid caching by the engine..
    HERE IS THE SLOW VERSION OF THE QUERY WHICH REFERENCES THE BODY CLOB TWICE:
    SELECT count(NSS_ID) FROM jk_test_2 WHERE
    CONTAINS (body, '( STANDARDS and HELP ) ' ) > 0
    AND
    CONTAINS (body, '( WORKING and LIMITED ) ' ) > 0 ;
    THE EXPLAIN PLAN shows the intersection being calculated:
    | Id | Operation               | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT          |     | 1 | 99 | 3836     (0)| 00:00:47 |
    | 1 | SORT AGGREGATE           |     | 1 | 99 |     |     |
    | 2 | BITMAP CONVERSION TO ROWIDS     |     | | |     |     |
    | 3 | BITMAP AND               |     | | |     |     |
    | 4 | BITMAP CONVERSION FROM ROWIDS|     | | |     |     |
    | 5 | SORT ORDER BY          |     | | |     |     |
    |* 6 | DOMAIN INDEX          | JK_BODY_NDX | | | 1284     (0)| 00:00:16 |
    | 7 | BITMAP CONVERSION FROM ROWIDS|     | | |     |     |
    | 8 | SORT ORDER BY          |     | | |     |     |
    |* 9 | DOMAIN INDEX          | JK_BODY_NDX | | | 2547     (0)| 00:00:31 |
    Predicate Information (identified by operation id):
    6 - access("CTXSYS"."CONTAINS"("BODY",'( PURCHASE and POSSIBLE)')>0 AND
         "CTXSYS"."CONTAINS"("BODY",'(NATIONAL and OFFICIAL)')>0)
    9 - access("CTXSYS"."CONTAINS"("BODY",'(NATIONAL and OFFICIAL)')>0)
    I RAN 3 QUERIES and got these times:
    Elapsed: 00:00:00.25
    Elapsed: 00:00:00.21
    Elapsed: 00:00:00.27
    HERE IS THE QUERY RE-WRITTEN INTO A DIFFERENT FORMAT WHICH COMBINES THE 2 PARTS INTO 1:
    SELECT count(NSS_ID) FROM jk_test_2 WHERE
    CONTAINS (body, '( ( STANDARDS and HELP ) AND ( WORKING and LIMITED ) ) ' ) > 0;
    The Plan is now:
    | Id | Operation     | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT |     | 1 | 99 | 3207     (0)| 00:00:39 |
    | 1 | SORT AGGREGATE |     | 1 | 99 |     |     |
    |* 2 | DOMAIN INDEX     | JK_BODY_NDX | 5 | 495 | 3207     (0)| 00:00:39 |
    Predicate Information (identified by operation id):
    2 - access("CTXSYS"."CONTAINS"("BODY",'( ( FORM and GIVE ) AND (WEB and AGREED) ) ')>0)
    I RAN 3 QUERIES using new words and got these times:
    Elapsed: 00:00:00.12
    Elapsed: 00:00:00.11
    Elapsed: 00:00:00.13
    Although the cost is only %15 lower, it executes twice as fast. Also, the same improvement is gained if OR's are used instead of ands:
    With --> CONTAINS (BODY,'( ( ( FORM OR GIVE ) and (WEB OR AGREED) ) ') >0
    My 2 timings are .25 and .50 with the OR'ed clause above getting the better response time..
    BASED ON THIS, On my project I am tempted to merged 2 totally seperate clob columns into 1, to get the better response time. The 2 columns are 1) Body Text and 2) Codes. They LOGICALLY DO NOT BELONG WITH ONE ANOTHER. This would be very ackward for the project as it would cause a lot of fudging of the data in many places throughout the system. I have done this testing taking averages of 500 unique queries, my indexes are up to date with full statistics computed on my tables and indexes. Response time is HIGHLY CRITICAL for this project.
    Does anyone have any advice that would allow me to obtain the good response time avoiding the ackwardness of globbing all the data into one clob ?
    Joek

    You might try adding sections and querying using within.

  • Finding query access frequency or how many times a query has been executed?

    Dear Experts
    I need to find the total number of access frequency of individual queries that are requested by the users say at a particular time.
    Say there are 20 distinct queries requested in the time difference of 3 hours. All of the 20 queries or some of the queries may be requested for more than 2/3 times at that time by other users. By the way say query Q1 is requested 5 times at that so its query access frequency or how many times this query is executed is 5.
    From where and how can I can find this counting of query access frequency or how many times a query has been executed at particular time or a session?
    Normally we know the there SQL history dynamic performance views or if it can be possible to query the Shared pool library cache for SQL area it may be possible to find the total number of execution time for a query. But how to find that if anyone knows, please help me about this.
    Regards-
    Engr. A.N.M. Bazlur Rashid
    OCP DBA

    That's one of the stats reported by statspack - assuming that your query does sufficient work to meet the thresholds for the standard report. Executions is of course one of the columns of v$sql so you might just wish to sample that yourself. Finally if you are on 11g and the sql you are interested is relatively low resource intensive and you are licensed for AWR then you can use the slightly madly named "colored sql" feature that ensures that a specific statement will always be sampled for AWR.
    Niall Litchfield
    http://www.orawin.info/

  • Difference between max response time of operation and service.

    Hi All,
    I am using ALSB to implement SOA.While monitoring the statistics for a proxy service based on WSDL (web service) ,noticed one thing that max response time of a service operation is different than max response time for that particular service.E.g max response time for one of the operation is 3462 (ms) where as max response time for that service was 4467 (ms).
    Can any one help me to identify why there is difference between these two?

    I have also noticed this inconsistence. There is probably no explanation in OSB (ALSB) documentation, so I can only guess. Maybe OSB starts to measure response time for service earlier then for an operation. The reason for that could be that OSB can identify service (based on endpoint) earlier then operation (based on request data which have to be parsed first). This could cause a difference in response times. However, this difference should be much smaller than a second (your case). In my case it is usually a matter of few milliseconds.
    Please remember that all of above is just my imagination. :-)

  • LOADED VIDEO FROM MY DROID AND IS PLAYING UPSIDE DOWN

    I TOOK A VIDEO ON MY MOTOROLA DROID AND DOWNLOADED ON TO MY HP USING MEDIA PLAYER AND IT IS PLAYING THE VIDEO UPSIDE DOWN...HOW DO I CHANGE IT TO PLAY RIGHT SIDE UP?
    This question was solved.
    View Solution.

    Hi,
    Most video editing software will be able to rotate video - a free option is Windows Movie Maker - see the answer on the following link.
    http://answers.microsoft.com/en-us/windows/forum/windows_7-pictures/my-video-is-playing-upside-down-...
    Regards,
    DP-K
    ****Click the White thumb to say thanks****
    ****Please mark Accept As Solution if it solves your problem****
    ****I don't work for HP****
    Microsoft MVP - Windows Experience

  • I have an iphone 4s when I take pictures and send them to my computer or someone elses computer they so times appear upside down. Is it because I have windows? Can I do anything about this?

    I have an iphone 4s when I take pictures and send them to my computer or someone else they appear upside down. I have a window operating system. Is there anything I can do to reverse the pictures?

    OK, here's the explanation.  When you take a picture, the iPhone knows what orientation it is in (portrait, landscape, etc.) and adds that information to the picture's EXIF data so it knows which side is up.  Apple devices can decode this information and show the picture right side up when it is viewed.  However, Windows computers cannot do this.  They are not capable of decoding the orientation information added by the iPhone, so they are displayed in a default orientation (which may not be right side up).
    This only happens on Windows machines, and unfortunately there is nothing you can do about it, except use the rotation buttons in Windows picture view to orient the photo correctly.

Maybe you are looking for

  • HT204053 can I use the same apple ID/icloud account for 2 iphones?

    I have 2 Iphones in the family.  Want the same music, ad apps on both phones.  Can I do this with one Itunes account?

  • How to have two times the same iTunes library ?

    My parents are separated, so I have two computers (one with my father and the other one with my mother) and all my music is in the computer with my mother, I want to have the same iTunes library to my father how I should do?

  • Problem with printing txt file

    I have a problem with printing txt file. My code looks like : String filename = something.txt"; DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintService[] services = PrintServiceLookup.lookupPrintServices(           flavor, null); PrintReque

  • Redo last action not working

    I am new to Pages and am playing with it to see what it can do.  In the course of this I have tried to do an action e.g.: copy and paste some text on the page, and then redo this action again somewhere else in my doc and "Redo" last action is greyed

  • I need the circular activety icon that use to be in the classic toolbar.

    Hi, I,m in need of the circular icon of dots that alerts you that an activety is in process. Please let me know if I can get it back. Thank you very much.