How to speed up this query?

I have created a demo table:
create table demo1(d date);
and insert some data to table:
begin
-- add 6000000 rows
for i in 1..1000000 loop
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
end loop;
commit;
end;
The query
select * from demo1
where d=to_date('25.10.2004','DD.MM.YYYY')
executed three times faster than
select from demo1 where d=trunc(sysdate-1);
Why? How to speed up this query if I do not want to use index?
I have created index:
create index demo1_indx on demo1(d);
Execution time of queries became identical (for this volume of data).

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create table demo1(d date);
Table created.
SQL> begin
2 for i in 1..1000000 loop
3 insert into demo1 values(trunc(sysdate-i));
4 insert into demo1 values(trunc(sysdate-i));
5 insert into demo1 values(trunc(sysdate-i));
6 insert into demo1 values(trunc(sysdate-i));
7 insert into demo1 values(trunc(sysdate-i));
8 insert into demo1 values(trunc(sysdate-i));
9 insert into demo1 values(trunc(sysdate-i));
10 insert into demo1 values(trunc(sysdate-i));
11 end loop;
12 commit;
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> alter session set timed_statistics=true;
Session altered.
SQL> alter session set sql_trace=true;
Session altered.
SQL> set timing on;
SQL> set autotrace on;
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:10.70
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
29 recursive calls
1 db block gets
28988 consistent gets
13030 physical reads
1035300 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:03.35
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12837 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:04.95
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12757 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:03.82
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12752 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:17.53
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
6 recursive calls
0 db block gets
14503 consistent gets
12758 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:15.82
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12753 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:14.56
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12758 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:11.84
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12757 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> alter session set sql_trace=false;
Session altered.
Elapsed: 00:00:00.00
SQL> alter session set timed_statistics=false;
Session altered.
Elapsed: 00:00:00.01
SQL>

Similar Messages

  • How do I speed up this query by ignoring computers off the network faster?

    Good afternoon,
    I am running the below script to query the entire domain for local admins. Could anyone reccomend a way to speed this up by more quickly skipping computers that aren't on the network? Currently, every time it reaches a computer that is not on the network
    it hangs for up to 20 seconds (computers on the network return the data in less than a second). If I could decrease the ping time-out time, I could speed up this query tenfold.
    Script pasted below - Thank you!!!
    $Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
    $Searcher.Filter = "(objectClass=computer)"
    $Computers = ($Searcher.Findall())
    md C:\Reports\IBX_Local_Admins_ALL
    Foreach ($Computer in $Computers)
    $Path=$Computer.Path
    $Name=([ADSI]"$Path").Name
    write-host $Name
    $members =[ADSI]"WinNT://$Name/Administrators"
    $members = @($members.psbase.Invoke("Members"))
    $members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty',
    $null, $_, $null) | out-file -append C:\Reports\IBX_Local_Admins_ALL\$name.txt

    This version will not fail no matter what is in the group.
    function Get-LocalAdmins{
    [CmdLetBinding()]
    Param(
    [Parameter(
    Mandatory=$true,
    ValueFromPipeline=$true,
    Position=0
    )]$computer
    Process{
    Write-Verbose "Polling system: $computer"
    if(Test-Connection $Computer -quiet -count 1){
    $group=[ADSI]"WinNT://$computer/Administrators"
    $group.Invoke("Members") |
    ForEach-Object{
    New-Object PsObject -Property @{
    Computer=$Computer
    aDSPath=$_.GetType().InvokeMember('aDSPath', 'GetProperty',$null, $_, $null)
    #UserID=$_.GetType().InvokeMember('Name', 'GetProperty',$null, $_, $null)
    }else{
    Write-Warning "System not found: $computer"
    $computers=([adsisearcher]'(objectClass=computer)').FindAll() |%{$_.Properties['name']}
    $computers | Get-LocalAdmins -verbose
    ¯\_(ツ)_/¯
    Hi JRV,
    Thank you for your help so far! When I ran the above version, it seemed to be going fine but it stopped at a certain point saying the script was "successful", but it only queried 148 computers. I should note that it is also the same 148 computers each time
    I run it. No errors, it just stops and says successful. Any ideas?
    TY!

  • How I can change this query, so I can display the name and scores in one r

    How I can change this query, so I can add the ID from the table SPRIDEN
    as of now is giving me what I want:
    1,543     A05     24     A01     24     BAC     24     BAE     24     A02     20     BAM     20in one line but I would like to add the id and name that are stored in the table SPRIDEN
    SELECT sortest_pidm,
           max(decode(rn,1,sortest_tesc_code)) tesc_code1,
           max(decode(rn,1,score)) score1,
           max(decode(rn,2,sortest_tesc_code)) tesc_code2,
           max(decode(rn,2,score)) score2,
           max(decode(rn,3,sortest_tesc_code)) tesc_code3,
           max(decode(rn,3,score))  score3,
           max(decode(rn,4,sortest_tesc_code)) tesc_code4,
           max(decode(rn,4,score))  score4,
           max(decode(rn,5,sortest_tesc_code)) tesc_code5,
           max(decode(rn,5,score))  score5,
           max(decode(rn,6,sortest_tesc_code)) tesc_code6,
           max(decode(rn,6,score))  score6        
      FROM (select sortest_pidm,
                   sortest_tesc_code,
                   score,
                  row_number() over (partition by sortest_pidm order by score desc) rn
              FROM (select sortest_pidm,
                           sortest_tesc_code,
                           max(sortest_test_score) score
                      from sortest,SPRIDEN
                      where
                      SPRIDEN_pidm =SORTEST_PIDM
                    AND   sortest_tesc_code in ('A01','BAE','A02','BAM','A05','BAC')
                     and  sortest_pidm is not null 
                    GROUP BY sortest_pidm, sortest_tesc_code))
                    GROUP BY sortest_pidm;
                   

    Hi,
    That depends on whether spriden_pidm is unique, and on what you want for results.
    Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevamnt columns only) for all tables, and the results you want from that data.
    If you can illustrate your problem using commonly available tables (such as those in the scott or hr schemas) then you don't have to post any sample data; just post the results you want.
    Either way, explain how you get those results from that data.
    Always say which version of Oracle you're using.
    It looks like you're doing something similiar to the following.
    Using the emp and dept tables in the scott schema, produce one row of output per department showing the highest salary in each job, for a given set of jobs:
    DEPTNO DNAME          LOC           JOB_1   SAL_1 JOB_2   SAL_2 JOB_3   SAL_3
        20 RESEARCH       DALLAS        ANALYST  3000 MANAGER  2975 CLERK    1100
        10 ACCOUNTING     NEW YORK      MANAGER  2450 CLERK    1300
        30 SALES          CHICAGO       MANAGER  2850 CLERK     950On each row, the jobs are listed in order by the highest salary.
    This seems to be analagous to what you're doing. The roles played by sortest_pidm, sortest_tesc_code and sortest_test_score in your sortest table are played by deptno, job and sal in the emp table. The roles played by spriden_pidm, id and name in your spriden table are played by deptno, dname and loc in the dept table.
    It sounds like you already have something like the query below, that produces the correct output, except that it does not include the dname and loc columns from the dept table.
    SELECT    deptno
    ,       MAX (DECODE (rn, 1, job))     AS job_1
    ,       MAX (DECODE (rn, 1, max_sal))     AS sal_1
    ,       MAX (DECODE (rn, 2, job))     AS job_2
    ,       MAX (DECODE (rn, 2, max_sal))     AS sal_2
    ,       MAX (DECODE (rn, 3, job))     AS job_3
    ,       MAX (DECODE (rn, 3, max_sal))     AS sal_3
    FROM       (
               SELECT    deptno
               ,          job
               ,          max_sal
               ,          ROW_NUMBER () OVER ( PARTITION BY  deptno
                                              ORDER BY          max_sal     DESC
                                )         AS rn
               FROM     (
                             SELECT    e.deptno
                       ,           e.job
                       ,           MAX (e.sal)     AS max_sal
                       FROM      scott.emp        e
                       ,           scott.dept   d
                       WHERE     e.deptno        = d.deptno
                       AND           e.job        IN ('ANALYST', 'CLERK', 'MANAGER')
                       GROUP BY  e.deptno
                       ,           e.job
    GROUP BY  deptno
    ;Since dept.deptno is unique, there will only be one dname and one loc for each deptno, so we can change the query by replacing "deptno" with "deptno, dname, loc" throughout the query (except in the join condition, of course):
    SELECT    deptno, dname, loc                    -- Changed
    ,       MAX (DECODE (rn, 1, job))     AS job_1
    ,       MAX (DECODE (rn, 1, max_sal))     AS sal_1
    ,       MAX (DECODE (rn, 2, job))     AS job_2
    ,       MAX (DECODE (rn, 2, max_sal))     AS sal_2
    ,       MAX (DECODE (rn, 3, job))     AS job_3
    ,       MAX (DECODE (rn, 3, max_sal))     AS sal_3
    FROM       (
               SELECT    deptno, dname, loc          -- Changed
               ,          job
               ,          max_sal
               ,          ROW_NUMBER () OVER ( PARTITION BY  deptno      -- , dname, loc     -- Changed
                                              ORDER BY          max_sal      DESC
                                )         AS rn
               FROM     (
                             SELECT    e.deptno, d.dname, d.loc                    -- Changed
                       ,           e.job
                       ,           MAX (e.sal)     AS max_sal
                       FROM      scott.emp        e
                       ,           scott.dept   d
                       WHERE     e.deptno        = d.deptno
                       AND           e.job        IN ('ANALYST', 'CLERK', 'MANAGER')
                       GROUP BY  e.deptno, d.dname, d.loc                    -- Changed
                       ,           e.job
    GROUP BY  deptno, dname, loc                    -- Changed
    ;Actually, you can keep using just deptno in the analytic PARTITION BY clause. It might be a little more efficient to just use deptno, like I did above, but it won't change the results if you use all 3, if there is only 1 danme and 1 loc per deptno.
    By the way, you don't need so many sub-queries. You're using the inner sub-query to compute the MAX, and the outer sub-query to compute rn. Analytic functions are computed after aggregate fucntions, so you can do both in the same sub-query like this:
    SELECT    deptno, dname, loc
    ,       MAX (DECODE (rn, 1, job))     AS job_1
    ,       MAX (DECODE (rn, 1, max_sal))     AS sal_1
    ,       MAX (DECODE (rn, 2, job))     AS job_2
    ,       MAX (DECODE (rn, 2, max_sal))     AS sal_2
    ,       MAX (DECODE (rn, 3, job))     AS job_3
    ,       MAX (DECODE (rn, 3, max_sal))     AS sal_3
    FROM       (
                   SELECT    e.deptno, d.dname, d.loc
              ,       e.job
              ,       MAX (e.sal)     AS max_sal
              ,       ROW_NUMBER () OVER ( PARTITION BY  e.deptno
                                           ORDER BY       MAX (sal)     DESC
                                          )       AS rn
              FROM      scott.emp    e
              ,       scott.dept   d
              WHERE     e.deptno        = d.deptno
              AND       e.job                IN ('ANALYST', 'CLERK', 'MANAGER')
                  GROUP BY  e.deptno, d.dname, d.loc
              ,       e.job
    GROUP BY  deptno, dname, loc
    ;This will work in Oracle 8.1 and up. In Oracle 11, however, it's better to use the SELECT ... PIVOT feature.

  • How can i improve this query.

    Hi guys i am beginner , just wanted to know some info , how can i improve this query ..
    select *
    from tableA A, viewB B,
    where A.key = B.key
    and a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)
    one more thing should function should be on left side of equal sign.
    will a join make it better or something else is needed more than that .

    952936 wrote:
    Hi guys i am beginner , just wanted to know some info , how can i improve this query ..
    select *
    from tableA A, viewB B,
    where A.key = B.key
    and a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)
    one more thing should function should be on left side of equal sign.
    will a join make it better or something else is needed more than that .If you are a beginner try to learn the ANSI Syntax. This will help you a lot to write better queries.
    Your select would look like this in ANSI.
    select *
    from tableA A
    JOIN viewB B ON A.key = B.key
    WHERE a.criteria1 = '111'
    and a.criteria2 = some_function(a.key);The good thing here is that this separates the typical joining part of the select from the typical filter criteria.
    The other syntax very often let you forget one join. Just because there are so many tables and so many filters, that you just don't notice correctly anymore what was join and what not.
    If you notice that the number of column is not what you expect, you can easiely modify the query and compare the results.
    example A
    Remove View B from the query (temporarily comment it out).
    select *
    from tableA A
    --JOIN viewB B ON A.key = B.key
    WHERE a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)
    example B
    You notice, that values from A are missing. Maybe because there is no matching key in ViewB? Then change the join to an outer join.
    select *
    from tableA A
    LEFT OUTER JOIN viewB B ON A.key = B.key
    WHERE a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)(The outer keyword is optional, left join would be enough).

  • How I can change this query

    How I can change this query, I got it from someone on the list, and it works just fine, Thank you! but I can not hardcode the data, the data is actually in a repiting table
    SARAPPD_PIDM, SARAPPD_TERM_CODE_ENTRY, SARAPPD_APPL_NO 1 SARAPPD_SEQ_NO
    We can have a person with one record another one with two or three, we just don't know, each record have a sequence number SARAPPD_SEQ_NO
    this SARAPPD_PIDM = 2232040 with will a paramater pass to the function (SARAPPD_PIDM = p_pidm)
    excuse my ignorance, but I just don't have experience with this kind of queries..
    with t as ( -- sample data
        select 2232040     SARAPPD_PIDM,  200990 SARAPPD_TERM_CODE_ENTRY, 1 SARAPPD_APPL_NO, 1 SARAPPD_SEQ_NO,   to_date('12/03/2008','mm/dd/yyyy') sARAPPD_APDC_DATE,  'S*'     SARAPPD_APDC_CODE from dual union all
        select 2232040     ,200990     ,1    ,2     ,to_date('12/08/2008','mm/dd/yyyy'),     'D1' from dual union all
        select 2232040     ,200990     ,1    ,3    ,to_date('03/18/2009','mm/dd/yyyy'),  'S*'  from dual union all
        select 2232040    ,200990     ,1    ,4     ,to_date('03/29/2009','mm/dd/yyyy')     ,'WL' from dual union all
        select 2232040    ,200990     ,1    ,4     ,to_date('03/27/2009','mm/dd/yyyy')     ,'WL' from dual
        ) -- end sample data
        SELECT * FROM (
        SELECT
       A.SARAPPD_PIDM,
       A.SARAPPD_APDC_CODE,
       A.sarappd_apdc_date,
       ROW_NUMBER() OVER (PARTITION BY SARAPPD_PIDM, SARAPPD_TERM_CODE_ENTRY ORDER BY SARAPPD_APDC_DATE
       DESC) row_num
       ,lead (SARAPPD_APDC_DATE,1) over (ORDER BY SARAPPD_APDC_DATE) AS next_date
       FROM t A
       where
        SARAPPD_PIDM = 2232040
         AND sarappd_apdc_code IN ('SA', 'FA', 'S-', 'F-', 'RF', 'F*','AD', 'W-', 'R2', 'S*', 'HF', 'WL','HD', 'R1', 'HS', 'D2', 'W+')
       where  next_date >= to_date('3/27/2009','mm/dd/yyyy');Edited by: user648177 on May 1, 2009 8:52 AM

    Thank you
    I am getting this result
    SARAPPD_PIDM     SARAPPD_APDC_CODE              SARAPPD_APDC_DATE     SARADAP_ADMT_CODE     ROW_NUM               NEXT_DATE
    2232040                                   S*            12/03/2008 16:30:45     D1                                    3                  03/18/2009 08:58:06
    2232040                                   S*            03/18/2009 08:58:06     D1                                      2                 03/21/2009 13:53:23I only want to retrieve the one with the max date ( 03/18/2009 08:58:06)
    I try to add the
    and SARAPPD_SEQ_NO = (select max(b.SARAPPD_SEQ_NO)
                                   from SARAPPD b
                                   where SARAPPD_pidm = b.SARAPPD_pidm)
    {code}
    {code}
    but it did not work
    {code}
    SELECT *
      FROM (SELECT A.SARAPPD_PIDM,
                   A.SARAPPD_APDC_CODE,
                   A.sarappd_apdc_date,
                   saradap_admt_code,
                   ROW_NUMBER() OVER(PARTITION BY SARAPPD_PIDM, SARAPPD_TERM_CODE_ENTRY ORDER BY SARAPPD_APDC_DATE DESC) row_num,
                   lead(SARAPPD_APDC_DATE, 1) over(ORDER BY SARAPPD_APDC_DATE) AS next_date
              FROM saturn.sarappd A, SARADAP
             where SARAPPD_PIDM = 2232040
               and SARADAP_PIDM = SARAPPD_PIDM
               AND saradap_term_code_entry = SARAPPD_TERM_CODE_ENTRY
               AND SARADAP_APPL_NO = A.SARAPPD_APPL_NO
               --         and SARAPPD_SEQ_NO = (select max(b.SARAPPD_SEQ_NO)
                --                    from SARAPPD b
                --                    where SARAPPD_pidm = b.SARAPPD_pidm)
               AND sarappd_apdc_code IN
                   ('SA', 'FA', 'S-', 'F-', 'RF', 'F*', 'AD', 'W-', 'R2', 'S*', 'HF', 'WL', 'HD', 'R1', 'HS', 'D2', 'W+'))
    Where next_date <=       saturn_midd.utlq.f_get_adm_freeze_date(saradap_admt_code)
    {code}
    i now that if I add and row_num=2 it will work, but this is just an example I don't know the row_num of all the records
    Edited by: user648177 on May 4, 2009 5:57 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How do I correct this query ?

    here is a query:
    select roleid from Apps_USERROLE where guid in (select guid from Apps_USER_MASTER order by userid)
    This does not work ...because the internal subquery retruns multiple result.
    How do I correct this query ?

    Satish Kandi wrote:
    select roleid from Apps_USERROLE where guid in (select guid from Apps_USER_MASTER order by userid)ORDER BY clause is not supported in subqueries since it makes no sense.
    If you want an ordered result set, you need add order by clause to the main query.
    select roleid from Apps_USERROLE where guid in (select guid from Apps_USER_MASTER)
    order by roleid;
    HTH...we have a problem then . Apps_USER_MASTER has userid as primary key .
    select guid from Apps_USER_MASTER => this could give duplicate guid then .

  • Can anyone tell me how can i optimize this query...

    Can anyone tell me how can i optimize this query ??? :
    Select Distinct eopersona.numident From rscompeten , rscompet , rscv , eopersona , rscurso , rseduca , rsexplab , rsinteres
    Where ( ( (LOWER (rscompeten.nombre LIKE '%caracas%') AND ( rscompeten.id = rscompet.idcompeten ) AND ( rscv.id = rscompet.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscurso.nombre) LIKE '%caracas%') AND ( rscv.id = rscurso.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscurso.lugar) LIKE '%caracas%') AND ( rscv.id = rscurso.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rseduca.univinst) LIKE '%caracas%)' AND ( rscv.id = rseduca.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rsexplab.nombempre) LIKE '%caracas%' AND ( rscv.id = rsexplab.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rsinteres.descrip) LIKE '%caracas%' AND ( rscv.id = rsinteres.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscv.cargoasp) LIKE '%caracas%' AND ( eopersona.id = rscv.idpersona ) )
    OR ( LOWER (eopersona.ciudad) LIKE '%caracas%' AND ( eopersona.id = rscv.idpersona )
    PLEASE IF YOU FIND SOMETHING WRONG.. PLEASE HELP ME.. this query takes me aproximatelly 10 minutes and the database is really small ( with only 200 records on each table )

    You are querying eight tables, however in any of your OR predicates you're only restricting 3 or 4 of those tables. That means that the remaining 4 or 5 tables are generating cartesian products. (n.b. the cartesian product of 5 tables with 200 rows each results in g 200^5 = 320,000,000,000 rows) Then you casually hide this behind "distinct".
    A simple restatement of your requirements looks like this:
    Select eopersona.numident
      From rscompeten,
           rscompet,
           rscv,
           eopersona
    Where LOWER (rscompeten.nombre) LIKE '%caracas%'
       AND rscompeten.id = rscompet.idcompeten
       AND rscv.id = rscompet.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    Select eopersona.numident
      From rscurso ,
           rscv,
           eopersona
    Where LOWER (rscurso.nombre) LIKE '%caracas%'
       AND rscv.id = rscurso.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    Select eopersona.numident
      From rscurso ,
           rscv,
           eopersona
    Where LOWER (rscurso.lugar) LIKE '%caracas%'
       AND rscv.id = rscurso.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    ...From there you can eliminate redundancies as desired, but I imagine that the above will perform admirably with the data volumes you describe.

  • Ways to speed up this query

    i was wondering if anyone has any ideas on how to speed this query up
    SELECT NAME, trunc (opr_date + opr_hour/24-numtodsinterval(1,'second'), 'DDD') AS opr_date, PRICE,
    Case OPR_HOUR When 0 THEN 24  ELSE OPR_HOUR END AS OPR_HOUR
    FROM ze_data.pjm_edf_lmp_hourly_int
    WHERE trunc (opr_date + opr_hour/24-numtodsinterval(1,'second'), 'DDD') between to_date ('2010/07/26', 'yyyy/mm/dd') and to_date ('2010/07/28', 'yyyy/mm/dd') and NAME in ('MISO')
    ORDER BY OPR_DATE desc, opr_hour descThe data is stored in the database with hours 0 to 23 this code take hour 0 and switches it to hour 24 of the the day before. I was wondering if there was a way to speed up the query as it takes 8 seconds to run
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0

    Since you're comparing OPR_DATE to a date range, is there any need to add the time to OPR_DATE?
    Would this work better? An index on OPR_DATE might help, but wouldn't an index on NAME be even better?
    I don't know, just a guess. You haven't posted anything we can use.
    SELECT NAME
          ,trunc (opr_date + opr_hour/24-numtodsinterval(1,'second'), 'DDD') AS opr_date
          ,PRICE
          ,Case OPR_HOUR When 0 THEN 24  ELSE OPR_HOUR END AS OPR_HOUR
    FROM   ze_data.pjm_edf_lmp_hourly_int
    WHERE  opr_date >= to_date ('2010/07/26', 'yyyy/mm/dd')
    and    opr_date <= to_date ('2010/07/28', 'yyyy/mm/dd')
    and    NAME in ('MISO')
    ORDER  BY OPR_DATE desc, opr_hour desc;

  • How to speed up the query result time

    While executing the following query the result comes after 22 secs.
    How could i minimize this time ? Please suggest.
    SELECT DISTINCT
           PERIOD_NO,
           ITEM_CODE,
           ITEM_NAME,
           SUM(CASE WHEN LEVEL_TYPE IN 'SUB ZONE' THEN WD_FORECAST_QTY_BU / CNV / ML ELSE 0 END)TOT_FRCST
    FROM   SHEET_HEAD, SHEET_DETAIL, ITEM_DETAIL
    WHERE  WH_SYS_ID = WD_WH_SYS_ID
    AND    ITEM_CODE = WD_ITEM_CODE(+)
    AND    PERIOD_NO BETWEEN :PERIOD1 AND :PERIOD2
    AND    WH_REVISION_NO = 0
    AND    WH_LEVEL_CODE NOT IN (SELECT WH_LEVEL_CODE FROM SHEET_HEAD WHERE WH_REVISION_NO = 1)
    GROUP BY   ITEM_CODE, ITEM_NAME, WH_PERIOD_NOSanjay

    Hi,
    with speed the suggest would always be expirement with indexing on the parameters, after first analysing what is currently happening in terms of full table scans etc that are 'most expensive'.
    In terms of the specifics on your code the following sticks out as costly; -
    AND    WH_LEVEL_CODE NOT IN (SELECT WH_LEVEL_CODE FROM SHEET_HEAD WHERE WH_REVISION_NO = 1)Here I would see if you can rewrite it, perhaps with a left join / self join to achieve the same ends, compare the timings you may be surprised.
    regards,
    Robert.

  • How do I get this query into Discoverer Plus

    Hi all,
    I have the following query:
    SELECT h.hrs, NVL(Quantity, 0) Quantity
    FROM (SELECT TRIM(to_char(LEVEL - 1, '00')) hrs
    FROM dual
    CONNECT BY LEVEL < 25) h
    LEFT JOIN (SELECT TO_CHAR(event_date, 'HH24') AS during_hour,
    COUNT(*) Quantity
    FROM user_activity u
    WHERE event_date BETWEEN
    to_date('15-JUN-2010 14:00:00', 'DD-MON-YYYY HH24:MI:SS') AND
    to_date('16-JUN-2010 13:59:59', 'DD-MON-YYYY HH24:MI:SS')
    AND event = 'user.login'
    GROUP BY TO_CHAR(event_date, 'HH24')) t
    ON (h.hrs = t.during_hour)
    ORDER BY h.hrs;
    Which produces the number of actions performed (from an event table, user_activity) grouped by the hour of the day they occurred (including displaying hours that have zero records - this bit is important!). I want to be able to put this into Discoverer plus as a worksheet, but I'm having trouble trying to figure out how.
    I was able to create a custom folder in Administrator for the select from DUAL, but I cannot link it to the user_activity table because there's no relationship between to two tables to create a join.
    The user_activity table is:
    USER_ID - VARCHAR2(8 CHAR)
    EVENT_DATE - DATE
    EVENT - VARCHAR2(100 CHAR)
    The custom folder is this part of the SQL:
    SELECT TRIM(to_char(LEVEL - 1, '00')) hrs FROM dual CONNECT BY LEVEL < 25
    Any suggestions would be greatly appreciated.
    Thanks.
    Edited by: Cyntech on Aug 12, 2010 10:41 AM

    KK wrote:
    hi,
    In the custom folder we can join tables,but the thing is you said ther is no join between them.
    I would suggest you to built this query into a view and use this view in the DUAL table by writing inline query or subquery what ever way.
    This is the only possibility i can think off.
    Hope it helps you.
    By,
    KKHi,
    Thanks for the reply, though I'm not sure that I understand what you are suggesting.
    Which query would you turn into a view? If you are referring to the select from dual, then how would the view be any different from the custom folder? You still would not be able to join it to user_activity as there are no common columns.
    Edited by: Cyntech on Aug 12, 2010 2:49 PM

  • How can we rewrite this query for better performance

    Hi All,
    The below query is taking more time to run. Any ideas how to improve the performance by rewriting the query using NOT EXITS or any other way...
    Help Appreciated.
    /* Formatted on 2012/04/25 18:00 (Formatter Plus v4.8.8) */
    SELECT vendor_id
    FROM po_vendors
    WHERE end_date_active IS NULL
    AND enabled_flag = 'Y'
    and vendor_id NOT IN ( /* Formatted on 2012/04/25 18:25 (Formatter Plus v4.8.8) */
    SELECT vendor_id
    FROM po_headers_all
    WHERE TO_DATE (creation_date) BETWEEN TO_DATE (SYSDATE - 365)
    AND TO_DATE (SYSDATE))
    Thanks

    Try this one :
    This will help you for partial fetching of data
    SELECT /*+ first_rows(50) no_cpu_costing */
    vendor_id
    FROM po_vendors
    WHERE end_date_active IS NULL
    AND enabled_flag = 'Y'
    AND vendor_id NOT IN (
    SELECT vendor_id
    FROM po_headers_all
    WHERE TO_DATE (creation_date) BETWEEN TO_DATE (SYSDATE - 365)
    AND TO_DATE (SYSDATE))
    overall your query is also fine, because, the in this query the subquery always contain less data compare to main query.

  • How I can change this query? to get the results I want

    This query
    select
    SHRTGPA_pidm,
    SZSTCLA_TERM_CODE,
    SHRTGPA_GPA_HOURS
    from  szstcla,SHRTGPA
    where szstcla_pidm = 74246
    and SHRTGPA_pidm = szstcla_pidm
    and SZSTCLA_TERM_CODE <> SZSTCLA_TERM_CODE_MATRIC
    and SHRTGPA_TERM_CODE = SZSTCLA_TERM_CODEreturns
    74246     201020     4
    74246     201120     4
    74246     201110     4
    74246     201210     4
    74246     201220     4 I want to aggregate so the query will return
    74246 201020     4
    74246 201120     8
    74246 201110     12
    74246 201210     16201220     20

    Hi,
    So, instead of shrtgpa_gpa_hours, you want a cumulative total of shrtgpa_gpa_hours, adding up all the previous values.
    That's a job for the analytic SUM function:
    select
    SHRTGPA_pidm,
    SZSTCLA_TERM_CODE,
    SUM (SHRTGPA_GPA_HOURS) OVER (ORDER BY x)  as running_shrtgpa_gpa_hours     -- *****  CHANGED  *****
    from  szstcla,SHRTGPA
    where szstcla_pidm = 74246
    and SHRTGPA_pidm = szstcla_pidm
    and SZSTCLA_TERM_CODE  SZSTCLA_TERM_CODE_MATRIC
    and SHRTGPA_TERM_CODE = SZSTCLA_TERM_CODEwhere x is the expression (or list of expressions) that determines what are the "previous" rows.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • How to performance tune this query

    I need some inputs on how to do performance tuning on this query to improve performance.
    It takes around 45 secs to run. Is it possible to make any improvements in this by putting hints or writing in another way?
    select count(*)
    as nCount from A ,
    B ,
    C
    WHERE A.COL1 = B.COL1 AND
    A.COl2 <> 'COM' AND
    B.COL2 = C.COL1 AND
    B.COl3 IS NULL AND
    B.COL4 = 'TEST'
    This is the query plan:
    Operation Object Name Rows Bytes Cost Object Node In/Out PStart PStop
    SELECT STATEMENT Optimizer Mode=CHOOSE 1 51
    SORT AGGREGATE 1 37
    HASH JOIN 48 K 1 M 51
    TABLE ACCESS FULL A 68 K 998 K 32
    NESTED LOOPS 98 K 2 M 5
    TABLE ACCESS BY INDEX ROWID B 142 K 2 M 4
    INDEX SKIP SCAN XIF37B 142 K 6
    INDEX UNIQUE SCAN XPKC 1 5

    Mcka
    As well as EXPLAIN PLAN, let us know what proportion of rows are visited by this query. It may be that it is not using a full table scan when it should (or vice versa).
    And of course we'd need to know what indexes are available, and how selective they are for the predicated you have in this query ...
    Regards Nigel

  • How to speed up a query?

    I want to select the MM generated documents from BKPF table where the awkey = MM Invoice no + FY and the awtyp = "RMRP". However since the BKPF table is very huge it takes a lot of time to execute.
    Hence i would like to know if there is any other faster way to speed this query?

    Hi ,
    Following points will help to improve performance.
    1. Creation of secondary index with the fields you are using in select query.You need to do runtime anaysis and trace analysis to view the impact of this.
    2. Try to minimize number of hits to the database table.Get the desired records from table in one go.
    3. Try to minimize the records fetched from the table.You can achive this by making some fields on screen as mandatory and using them in where condition of select statement.
    Hope this helps you.

  • How would I write this query

    [USERNAME]
    [GROUP]
    User1
    New Member
    User2
    New Member
    User1
    All Members
    User1
    Gold Member
    How would I write a query so that I could select * from the above table where a user has more than 1 group (user1) and if one of the groups = 'All members' then ignore that row and just return the all the other rows for the user that has > 1 group. 
    Thanks in advance.  I can write the query for counting if a user has more than 1 group, but I stuck on the rest.
    Thanks again

    Try this
    declare @tab table(USERNAME varchar(10), Usergroup varchar(15));
    insert into @tab values
    ('User1','New Member'),
    ('User2','New Member'),
    ('User1','All Members'),
    ('User1','Gold Member');
    select * from
    (Select *,ROW_NUMBER() OVer(partition by username Order by case when Usergroup ='All members' then 0 else 1 end desc) RN From @tab ) t
    where t.usergroup= case when t.rn >1 and t.Usergroup='All members' then 'Do not return' else t.Usergroup end
    Satheesh
    My Blog |
    How to ask questions in technical forum

Maybe you are looking for

  • How to show google maps in JPanel

    How do i show the google maps in Java Application. Please help me.

  • Program VS Report

    As we all know we can use both Program and report at the beginning of a ABAP program, I want to ask is there any difference between them? When to use program and when to use report?

  • How can we transfer the header fiedl value to the  item

    we have add the "storage location" field to the sales order head and item, now we need to batch change the item's storage location when we change the head's storage fields , we have use the function module in 'crmc_map' transcation, but with some pro

  • Get Repository Service from Renderer

    Hello All I need a repository service for storing and customization some parameters for my KM property renderers. I wrote such service and can see it in KM->Content Management->Repository Services. But when I try to get a list of all repository servi

  • How  the condition of combo box of matrix can be passed in query

    Hi, I want to get the parameter code and description for a particular category. Category code is in the matrix combo box  if i pass tat field in a where condition i cant retrieve the parameter code but its working in the database. Please identify the