Selecting same column twice makes query slow

Hello,
Has anybody any idea why following statement is slow when selecting the
DATA_TYPE column twice?
SELECT S.OWNER,
S.SYNONYM_NAME,
UCOL.COLUMN_NAME,
UCOL.DATA_TYPE,
UCOL.DATA_TYPE,
UCOL.DATA_LENGTH,
COLUMN_ID
FROM ALL_SYNONYMS S,
ALL_TAB_COLUMNS UCOL
WHERE S.TABLE_OWNER = UCOL.OWNER
AND S.TABLE_NAME = UCOL.TABLE_NAME
and S.owner = 'REPORTING_TEST'
and S.SYNONYM_NAME = 'OV_COMMERCIAL_ENTITY_JN'
Replace the owner and table name with one of your own.
If you take one of the DATA_TYPE's out the select is much faster.
Why, I hear you ask, do you select the same column twice?
Well, it's not me. It's BusinessObjects when it does a structure refresh it
produces a statement similar to the one above except that one of the
DATA_TYPE's is a SUBSTR + DECODE (however they in themselves
have virtually no impact on the statement). I just find it weird that repeating
a column twice can have such a dramatic impact on performance.
I'm not a tuning expert and have looked at EXPLAIN PLAN outputs but can't
make head nor tail of them.
Thanks,
Ruud

Yes it's true that Oracle caches data but I did run both statements several times within the same session. Run statement 1, run statement 2, add 2nd column and run again, take column away and run again, etc. Always the results were the same. The query with duplicate columns ran in seconds, the one without in milliseconds.
If it tells you anything, below both explain plans. The main difference that I noticed is the full table scan on USER$ for the first query. The second one (although a more elaborate execution plan) seems to use indexes more.
Don't spend too much time on this. I've found a workaround where I create temporary tables for ALL_SYNONYMS and ALL_TAB_COLUMNS and re-direct the public synonyms to point at those. After I've done what I wanted to do with BusinessObjects, I restore the original public synonyms. Next thing I'll try is rebuilding my database from scratch and see if the problem is still there.
Apologies if the listings come out in variable font (how do you paste fixed font?).
=== SQL1 ====================================================
EXPLAIN PLAN FOR
SELECT UCOL.COLUMN_NAME,
UCOL.DATA_TYPE,
UCOL.DATA_TYPE
FROM ALL_SYNONYMS S,
ALL_TAB_COLUMNS UCOL
WHERE S.TABLE_OWNER = UCOL.OWNER
AND S.TABLE_NAME = UCOL.TABLE_NAME
AND S.owner = 'REPORTING_TEST'
AND S.SYNONYM_NAME = 'OV_COMMERCIAL_ENTITY_JN'
Explained.
Elapsed: 00:00:00.04
SQL> @E:\Oracle\Product\10.2.0\db_1\RDBMS\ADMIN\utlxpls.sql
Plan hash value: 1692851197
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | TABLE ACCESS BY INDEX ROWID | USER$ |
|* 5 | INDEX UNIQUE SCAN | I_USER1 |
| 6 | VIEW | ALL_TAB_COLUMNS |
|* 7 | FILTER | |
| 8 | NESTED LOOPS OUTER | |
| 9 | NESTED LOOPS OUTER | |
| 10 | NESTED LOOPS OUTER | |
| 11 | NESTED LOOPS OUTER | |
| 12 | NESTED LOOPS | |
| 13 | NESTED LOOPS | |
| 14 | TABLE ACCESS FULL | USER$ |
| 15 | TABLE ACCESS BY INDEX ROWID| OBJ$ |
|* 16 | INDEX RANGE SCAN | I_OBJ2 |
|* 17 | TABLE ACCESS CLUSTER | COL$ |
|* 18 | INDEX UNIQUE SCAN | I_OBJ# |
|* 19 | TABLE ACCESS CLUSTER | COLTYPE$ |
|* 20 | INDEX RANGE SCAN | I_HH_OBJ#_INTCOL# |
|* 21 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 22 | INDEX RANGE SCAN | I_OBJ3 |
| 23 | TABLE ACCESS CLUSTER | USER$ |
|* 24 | INDEX UNIQUE SCAN | I_USER# |
|* 25 | TABLE ACCESS CLUSTER | TAB$ |
|* 26 | INDEX UNIQUE SCAN | I_OBJ# |
| 27 | NESTED LOOPS | |
| 28 | FIXED TABLE FULL | X$KZSRO |
|* 29 | INDEX RANGE SCAN | I_OBJAUTH2 |
|* 30 | FIXED TABLE FULL | X$KZSPR |
|* 31 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 32 | INDEX RANGE SCAN | I_OBJ2 |
|* 33 | TABLE ACCESS BY INDEX ROWID | SYN$ |
|* 34 | INDEX UNIQUE SCAN | I_SYN1 |
Predicate Information (identified by operation id):
5 - access("U"."NAME"='REPORTING_TEST')
7 - filter(("O"."TYPE#"=3 OR "O"."TYPE#"=4 OR "O"."TYPE#"=2 AND NOT
EXISTS (SELECT 0 FROM "SYS"."TAB$" "T" WHERE "T"."OBJ#"=:B1 AND
(BITAND("T"."PROPERTY",512)=512 OR BITAND("T"."PROPERTY",8192)=8192)))
AND ("O"."OWNER#"=USERENV('SCHEMAID') OR EXISTS (SELECT 0 FROM
"SYS"."OBJAUTH$" "OBJAUTH$",SYS."X$KZSRO" "X$KZSRO" WHERE "OBJ#"=:B2
AND "GRANTEE#"="KZSROROL") OR EXISTS (SELECT 0 FROM SYS."X$KZSPR"
"X$KZSPR" WHERE "INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45)
OR (-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))))
16 - access("O"."OWNER#"="U"."USER#")
17 - filter(DECODE("C"."PROPERTY",0,'NO',DECODE(BITAND("C"."PROPERTY",
32),32,'YES','NO'))='NO')
18 - access("O"."OBJ#"="C"."OBJ#")
19 - filter("C"."INTCOL#"="AC"."INTCOL#"(+))
20 - access("C"."OBJ#"="H"."OBJ#"(+) AND
"C"."INTCOL#"="H"."INTCOL#"(+))
21 - filter("OT"."TYPE#"(+)=13)
22 - access("AC"."TOID"="OT"."OID$"(+))
24 - access("OT"."OWNER#"="UT"."USER#"(+))
25 - filter(BITAND("T"."PROPERTY",512)=512 OR
BITAND("T"."PROPERTY",8192)=8192)
26 - access("T"."OBJ#"=:B1)
29 - access("GRANTEE#"="KZSROROL" AND "OBJ#"=:B1)
30 - filter("INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45) OR
(-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))
31 - filter("O"."TYPE#"=5)
32 - access("O"."OWNER#"="U"."USER#" AND
"O"."NAME"='OV_COMMERCIAL_ENTITY_JN')
33 - filter("S"."NAME"="UCOL"."TABLE_NAME" AND
"S"."OWNER"="UCOL"."OWNER")
34 - access("O"."OBJ#"="S"."OBJ#")
Note
- rule based optimizer used (consider using cbo)
=== SQL2 ====================================================
EXPLAIN PLAN FOR
SELECT UCOL.COLUMN_NAME,
UCOL.DATA_TYPE
FROM ALL_SYNONYMS S,
ALL_TAB_COLUMNS UCOL
WHERE S.TABLE_OWNER = UCOL.OWNER
AND S.TABLE_NAME = UCOL.TABLE_NAME
AND S.owner = 'REPORTING_TEST'
AND S.SYNONYM_NAME = 'OV_COMMERCIAL_ENTITY_JN'
Explained.
SQL>
SQL> @E:\Oracle\Product\10.2.0\db_1\RDBMS\ADMIN\utlxpls.sql
Plan hash value: 3285788911
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 2 | INDEX UNIQUE SCAN | I_OBJ1 |
| 3 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 4 | INDEX UNIQUE SCAN | I_OBJ1 |
| 5 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 6 | INDEX UNIQUE SCAN | I_OBJ1 |
| 7 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 8 | INDEX UNIQUE SCAN | I_OBJ1 |
| 9 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 10 | INDEX UNIQUE SCAN | I_OBJ1 |
|* 11 | FILTER | |
| 12 | NESTED LOOPS OUTER | |
| 13 | NESTED LOOPS OUTER | |
| 14 | NESTED LOOPS OUTER | |
| 15 | NESTED LOOPS OUTER | |
| 16 | NESTED LOOPS | |
| 17 | NESTED LOOPS | |
| 18 | NESTED LOOPS | |
| 19 | NESTED LOOPS | |
| 20 | NESTED LOOPS | |
| 21 | TABLE ACCESS BY INDEX ROWID| USER$ |
|* 22 | INDEX UNIQUE SCAN | I_USER1 |
|* 23 | TABLE ACCESS BY INDEX ROWID| OBJ$ |
|* 24 | INDEX RANGE SCAN | I_OBJ2 |
| 25 | TABLE ACCESS BY INDEX ROWID | SYN$ |
|* 26 | INDEX UNIQUE SCAN | I_SYN1 |
| 27 | TABLE ACCESS BY INDEX ROWID | USER$ |
|* 28 | INDEX UNIQUE SCAN | I_USER1 |
| 29 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 30 | INDEX RANGE SCAN | I_OBJ2 |
|* 31 | TABLE ACCESS CLUSTER | COL$ |
|* 32 | INDEX UNIQUE SCAN | I_OBJ# |
|* 33 | TABLE ACCESS CLUSTER | COLTYPE$ |
|* 34 | INDEX RANGE SCAN | I_HH_OBJ#_INTCOL# |
|* 35 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 36 | INDEX RANGE SCAN | I_OBJ3 |
| 37 | TABLE ACCESS CLUSTER | USER$ |
|* 38 | INDEX UNIQUE SCAN | I_USER# |
|* 39 | TABLE ACCESS CLUSTER | TAB$ |
|* 40 | INDEX UNIQUE SCAN | I_OBJ# |
| 41 | NESTED LOOPS | |
| 42 | FIXED TABLE FULL | X$KZSRO |
|* 43 | INDEX RANGE SCAN | I_OBJAUTH2 |
|* 44 | FIXED TABLE FULL | X$KZSPR |
Predicate Information (identified by operation id):
2 - access("O"."OBJ#"=:B1)
4 - access("O"."OBJ#"=:B1)
6 - access("O"."OBJ#"=:B1)
8 - access("O"."OBJ#"=:B1)
10 - access("O"."OBJ#"=:B1)
11 - filter(("O"."TYPE#"=3 OR "O"."TYPE#"=4 OR "O"."TYPE#"=2 AND NOT
EXISTS (SELECT 0 FROM "SYS"."TAB$" "T" WHERE "T"."OBJ#"=:B1 AND
(BITAND("T"."PROPERTY",512)=512 OR BITAND("T"."PROPERTY",8192)=8192)))
AND ("O"."OWNER#"=USERENV('SCHEMAID') OR EXISTS (SELECT 0 FROM
"SYS"."OBJAUTH$" "OBJAUTH$",SYS."X$KZSRO" "X$KZSRO" WHERE "OBJ#"=:B2
AND "GRANTEE#"="KZSROROL") OR EXISTS (SELECT 0 FROM SYS."X$KZSPR"
"X$KZSPR" WHERE "INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45)
OR (-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))))
22 - access("U"."NAME"='REPORTING_TEST')
23 - filter("O"."TYPE#"=5)
24 - access("O"."OWNER#"="U"."USER#" AND
"O"."NAME"='OV_COMMERCIAL_ENTITY_JN')
26 - access("O"."OBJ#"="S"."OBJ#")
28 - access("S"."OWNER"="U"."NAME")
30 - access("O"."OWNER#"="U"."USER#" AND "S"."NAME"="O"."NAME")
31 - filter(DECODE("C"."PROPERTY",0,'NO',DECODE(BITAND("C"."PROPERTY",
32),32,'YES','NO'))='NO')
32 - access("O"."OBJ#"="C"."OBJ#")
33 - filter("C"."INTCOL#"="AC"."INTCOL#"(+))
34 - access("C"."OBJ#"="H"."OBJ#"(+) AND
"C"."INTCOL#"="H"."INTCOL#"(+))
35 - filter("OT"."TYPE#"(+)=13)
36 - access("AC"."TOID"="OT"."OID$"(+))
38 - access("OT"."OWNER#"="UT"."USER#"(+))
39 - filter(BITAND("T"."PROPERTY",512)=512 OR
BITAND("T"."PROPERTY",8192)=8192)
40 - access("T"."OBJ#"=:B1)
43 - access("GRANTEE#"="KZSROROL" AND "OBJ#"=:B1)
44 - filter("INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45) OR
(-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))
Note
- rule based optimizer used (consider using cbo)

Similar Messages

  • Cost of using subquery vs using same table twice in query

    Hi all,
    In a current project, I was asked by my supervisor what is the cost difference between the following two methods. First method is using a subquery to get the name field from table2. A subquery is needed because it requires the field sa_id from table1. The second method is using table2 again under a different alias to obtain table2.name. The two table2 are not self-joined. The outcome of these two queries are the same.
    Using subquery:
    select a.sa_id R1, b.other_field R2,
    (select b.name from b
    where b.b_id = a.sa_id) R3
    from table1 a, table2 b
    where ...Using same table twice (table2 under 2 different aliases)
    select a.sa_id R1, b.other_field R2, c.name R3
    from table1 a, table2 b, table2 c
    where
    c.b_id = a.sa_id,
    and ....Can anyone tell me which version is better and why? (or under what circumstances, which version is better). And what are the costs involved? Many thanks.

    pl/sql novice wrote:
    Hi all,
    In a current project, I was asked by my supervisor what is the cost difference between the following two methods. First method is using a subquery to get the name field from table2. A subquery is needed because it requires the field sa_id from table1. The second method is using table2 again under a different alias to obtain table2.name. The two table2 are not self-joined. The outcome of these two queries are the same.
    Using subquery:
    Using same table twice (table2 under 2 different aliases)
    Can anyone tell me which version is better and why? (or under what circumstances, which version is better). And what are the costs involved? Many thanks.In theory, if you use the scalar "subquery" approach, the correlated subquery needs to be executed for each row of your result set. Depending on how efficient the subquery is performed this could require significant resources, since you have that recursive SQL that needs to be executed for each row.
    The "join" approach needs to read the table only twice, may be it can even use an indexed access path. So in theory the join approach should perform better in most cases.
    Now the Oracle runtime engine (since Version 8) introduces a feature called "filter optimization" that also applies to correlated scalar subqueries. Basically it works like an in-memory hash table that caches the (hashed) input values to the (deterministic) correlated subquery and the corresponding output values. The number of entries of the hash table is fixed until 9i (256 entries) whereas in 10g it is controlled by a internal parameter that determines the size of the table (and therefore can hold different number of entries depending on the size of each element).
    If the input value of the next row corresponds to the input value of the previous row then this optimization returns immediately the corresponding output value without any further action. If the input value can be found in the hash table, the corresponding output value is returned, otherwise execute the query and keep the result combination and eventually attempt to store this new combination in the hash table, but if a hash collision occurs the combination will be discarded.
    So the effectiveness of this clever optimization largely depends on three different factors: The order of the input values (because as long as the input value doesn't change the corresponding output value will be returned immediately without any further action required), the number of distinct input values and finally the rate of hash collisions that might occur when attempting to store a combination in the in-memory hash table.
    In summary unfortunately you can't really tell how good this optimization is going to work at runtime and therefore can't be properly reflected in the execution plan.
    You need to test both approaches individually because in the optimal case the optimization of the scalar subquery will be superior to the join approach, but it could also well be the other around, depending on the factors mentioned.
    Regards,
    Randolf
    Oracle related stuff blog:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle (Open source Oracle GUI for Windows):
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • Showing navigation attributes in the same column of a query

    Hi,
    I would like to know if there is a way to define a query such that TWO navigation attributes that reference the same underlying characteristic can be displayed in the SAME column rather than TWO separate columns.
    I will describe an example scenario to clarify the requirement.  We are using a BI Content InfoCube (0COOM_C02) that has the navigation attributes 0COSTCENTER__0PROFIT_CTR and 0WBS_ELEMT__PROFIT_CTR activated.  Both of these attributes reference the characteristic 0PROFIT_CTR.  The InfoCube contains transaction data for Cost Centres and WBS Elements. 
    The requirement is to define a query that summarizes the data by profit centre.  At the moment, in order to combine Cost Centre and WBS (Project) data in the same query by Profit Centre, both navigation attributes need to be selected, resulting in the following output:
    0COSTCENTER__0PROFIT_CTR......0WBS_ELEMT__PROFIT_CTR......AMOUNT
    PC01...............................................Not assigned...................................10,000
    PC02...............................................Not assigned...................................20,000
    PC03...............................................Not assigned...................................12,000
    PC04...............................................Not assigned....................................2,000
    Not Assigned....................................PC02..............................................5,000
    Not Assigned....................................PC03..............................................8,000
    Not Assigned....................................PC05..............................................30,000
    Not Assigned....................................PC06..............................................50,000
    However, the desired result is to display only one profit centre column using the two profit centre navigation attributes as shown below:
    PROFIT CENTRE......AMOUNT
    PC01..........................10,000
    PC02..........................25,000
    PC03..........................20,000
    PC04............................2,000
    PC05..........................30,000
    PC06..........................50,000
    Is there a way to achieve the above result using the two navigation attributes only, without having to add 0PROFIT_CTR as a characteristic to the InfoCube?
    Any suggestions would be appreciated.
    Thanks,
    Mustafa.

    Hi Mustafa,
    You have to use enhancement RSR00001 in the transaction CMOD to start developing your code.
    For more information how the customer exit variables are processed, refer to the documentation:
    http://help.sap.com/saphelp_nw70/helpdata/EN/f1/0a56f5e09411d2acb90000e829fbfe/frameset.htm
    A short example can be found here:
    http://help.sap.com/saphelp_nw70/helpdata/EN/1d/ca10d858c2e949ba4a152c44f8128a/frameset.htm
    This should help.
    Regards,
    Daniel

  • Selecting Multiple columns in Sub-query

    Hi All,
    I've been using three co-related queries to fetch data as shown below in the pseudo code. All these sub-queries are same, except the column name. I want to avoid the same query to be executed thrice for three columns. Is there any way to avoid this?
    insert into my_table
    select colA,
    colB,
    (select field1 from table1 where table1.fieldx =tableA.fieldy ) col1,
    (select field2 from table1 where table1.fieldx =tableA.fieldy) col2,
    (select field3 from table1 where table1.fieldx =tableA.fieldy) col3
    from tableA , table B 
    where con1= con2
        and con3 = con4
        and con4=con5;All suggestions are welcomed
    Regards,
    K

    Hi,
    Perhaps this is what you want:
    INSERT INTO my_table (colA, colB. col1, col2, col3)    -- list the columns explicitly
    WITH     table_1_summary          AS
         SELECT       fieldx
         ,       MIN (field1)     AS col1
         ,       MIN (field2)     AS col2
         ,       MIN (field3)     AS col3
         FROM       table1
         GROUP BY  fieldx
    SELECT       colA
    ,       colB
    ,       t1.col1
    ,       t1.col2
    ,       t1.col3
    FROM       tableA          a
    ,       tableB          b
    ,       table_1_summary     t1
    WHERE       con1          = con2
    AND       con3          = con4
    AND       con4          = con5
    AND       t1.field1 (+)     = a.fieldy
    ;Something a little shorter (and a little more efficient) might work, depending on you data, especially what columns are unique.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, 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.

  • How to show two amount (based on value type and entry code)  in same column

    Hi, I have to show two amount in the same column how can i do that...
    For example there is 
    1) premium amount which depends upon the entry code lets say entry code 1000 = Premium amount .
    2) Now there is another premium amount but this amount is stored on basis of value type ...let us say for value type 'prem' = premium amount .
    these amount are in different field can i shown it in same column in BEx query.
    If you can you please help .

    use the selection criteria to restrict the field types in the query designer- use other variables to check the value
    if it doesnt reconcile- you can alternatively use a multi provider for the same thing- that way you will get the same amount in 1 column and you could restrict using the infoprovider column

  • Dynamic selection of columns in report and print the output

    Hi,
    I tried to have dynamic selection of columns in SQL query using lexical parameter. The hitch is how to print those selected column in excel ouput ?
    Suppose
    I have select &col from table_name
    Initial value of &col is col1 and in data layout only col1 appears
    where &col was replaced with more than a column (col1,col2,col3)
    now how to print the column selected at the run time,
    i have coded in Before Report Trigger
    :cell_val := ' <td><rw:field id:"cl1" src="col1"></rw:field></td>
    <td><rw:field id:"cl2" src="col2"></rw:field></td>
    <td><rw:field id:"cl3" src="col3"></rw:field></td> ' ;
    and this user parameter was replaced in web source using JSP expression
    <rw:getvalue id: "v_cell" src:"cell_val"></rw:getvalue>
    <rw:foreach src:"g_val">
    <table>
    <tr>
    <% =v_cell%>
    </tr>
    </table>
    </rw:foreach>
    but in the output i can see only blank columns
    Regards
    Suresh

    The strings replaced by expression <%= v_cell %>
    should print the field values contained in col1,col2,col3 columns, but nothing is printed . i think the custom tags (rw...) are not replaced by the jsp expression properly.

  • Sql query slowness due to rank and columns with null values:

        
    Sql query slowness due to rank and columns with null values:
    I have the following table in database with around 10 millions records:
    Declaration:
    create table PropertyOwners (
    [Key] int not null primary key,
    PropertyKey int not null,    
    BoughtDate DateTime,    
    OwnerKey int null,    
    GroupKey int null   
    go
    [Key] is primary key and combination of PropertyKey, BoughtDate, OwnerKey and GroupKey is unique.
    With the following index:
    CREATE NONCLUSTERED INDEX [IX_PropertyOwners] ON [dbo].[PropertyOwners]    
    [PropertyKey] ASC,   
    [BoughtDate] DESC,   
    [OwnerKey] DESC,   
    [GroupKey] DESC   
    go
    Description of the case:
    For single BoughtDate one property can belong to multiple owners or single group, for single record there can either be OwnerKey or GroupKey but not both so one of them will be null for each record. I am trying to retrieve the data from the table using
    following query for the OwnerKey. If there are same property rows for owners and group at the same time than the rows having OwnerKey with be preferred, that is why I am using "OwnerKey desc" in Rank function.
    declare @ownerKey int = 40000   
    select PropertyKey, BoughtDate, OwnerKey, GroupKey   
    from (    
    select PropertyKey, BoughtDate, OwnerKey, GroupKey,       
    RANK() over (partition by PropertyKey order by BoughtDate desc, OwnerKey desc, GroupKey desc) as [Rank]   
    from PropertyOwners   
    ) as result   
    where result.[Rank]=1 and result.[OwnerKey]=@ownerKey
    It is taking 2-3 seconds to get the records which is too slow, similar time it is taking as I try to get the records using the GroupKey. But when I tried to get the records for the PropertyKey with the same query, it is executing in 10 milliseconds.
    May be the slowness is due to as OwnerKey/GroupKey in the table  can be null and sql server in unable to index it. I have also tried to use the Indexed view to pre ranked them but I can't use it in my query as Rank function is not supported in indexed
    view.
    Please note this table is updated once a day and using Sql Server 2008 R2. Any help will be greatly appreciated.

    create table #result (PropertyKey int not null, BoughtDate datetime, OwnerKey int null, GroupKey int null, [Rank] int not null)Create index idx ON #result(OwnerKey ,rnk)
    insert into #result(PropertyKey, BoughtDate, OwnerKey, GroupKey, [Rank])
    select PropertyKey, BoughtDate, OwnerKey, GroupKey,
    RANK() over (partition by PropertyKey order by BoughtDate desc, OwnerKey desc, GroupKey desc) as [Rank]
    from PropertyOwners
    go
    declare @ownerKey int = 1
    select PropertyKey, BoughtDate, OwnerKey, GroupKey
    from #result as result
    where result.[Rank]=1
    and result.[OwnerKey]=@ownerKey
    go
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Using a column twice in a dashboard prompt -- multi-select and wild-carding

    My client is using OBIEE 10.1.3.2; they cannot upgrade at this time. However, they want to be able to enter both a list of values and a wild-card search for additional values at the same time for a single column in a dashboard prompt.
    Since they cannot upgrade, they do not have the wild-card search feature of the enhanced multi-select prompt feature of 10.1.3.3 and later releases.
    Has anyone found another method to do this, perhaps by using a column twice in a dashboard prompt or in separate dashboard prompts on the same dashboard apge, in order to OR the results of a multi-select and an edit box (with wild-card pattern-match entry)? I suspect that will not work -- that the results sets of one prompt for a column will override any other prompt result sets for the same column.
    Has anyone found another way to do this, prior to release 10.1.3.3?

    Yes, it is possible.
    This is how to do it:
    In your report you need to add two filters on the same column and combine them with OR.
    First filter: is prompted.
    Second filter: is like presentation_variable
    Then protect this filter!* This will make that your prompt will be filtered on the pres. variable and that it won't be overwritten by another prompt.
    Now create your dashboard prompt:
    First add your column with an edit box and attach the presentation variable to it.
    Then edit the formula of the column, so it will not reference to the column anymore. (Which isn't needed, since the value will be set to the pres. variable.)
    Then add the same column with a multi-select.
    Then test it and check your results.
    Regards,
    Stijn

  • Crystal Reports XI - How to pull same column value with different select...

    I have a report with many (around 30) sub reports and it is giving serious performance issue. I am currently finding out a strategy to improve the performance. I see that most of the sub reports are taking same parameters from main report except for one parameter which is different for each sub report and hard coded in them and pulling up the same column value from a oracle database with a different select criterion. I am trying to find out a way using either of command/crystal formula/SQL expression which can do the same job for me and give me performance improvement. I tried to take the parameters from the main report in one sub report and mapped them to parameters being transferred to the command and then drag and drop the field for which I need to display the value but due to some reason it is not returning values when I am trying multiple commands...I need some help in selecting a strategy for this issue resolution and some guidance....Any help would be highly appreciated....

    My version is 11.5.8.826. First of all I need to make it clear that I have 6 set of sub reports and each set has 5 sub reports. So if I can resolve the issue for one set, I resolve it for all. The sub reports are doing nothing but fetching a column value using a simple SQL SELECT query and appending to the right of a box in main report. The issue is that all these queries embedded into the sub reports are using same SQL except for one parameter in the "where" clause and they are fetching same column value from the database and fortunately all of them are returning same number of results. So at the end these results create a table when run. The only way I can stop using sub reports is by creating a table in the main report and use some object(formula/command/SQL expression or whatever) to fetch that column value using the same filter conditions. But please take a note that I need to use and append the same column value from a database table and use different filter conditions and append them to the right so that at the end they create a table...as long as I get the table in the output with a decent performance , anyone will be least bothered about what I used inside the report.....please ask me as many questions to get more lights on the issue....

  • Use same image column twice in a report

    Hello,
    I have a question  which is, can I use a same image column (example column A) twice in a report? first occurrence (column A) will show the image thumbnail and the second occurrence (column A) will show  image download link which opens image in a new window. Is it possible?
    Thanks
    Pravish

    LA-APEX-DEv wrote:
    Yes, it's possible. However, if you intent to have two different images, then I would suggest you use a javascript to link or download the blob.
    What on Earth for? Why is JavaScript necessary to download two different images? Even if this were in any way true, the OP is clear that the displayed thumbnail and download link involve the same image.
    To accomplish this task, you either use decode or create a union query.
    No, all that should be necessary is to reference the image column twice in the report query projection, and apply a download format mask to one column and an image mask to the other. That the OP has apparently failed to do this probably indicates that they are unnecessarily complicating things.
    For example:
    select '<a href="javascript:downloadImg(' || image_id || ');"><img src="/i/download.gif"></a>' link_col
    from table
    where allow_view = 'Y'
    union all
    select '<img src="/i/folder.gif">' link_col
    from table
    where allow_view = 'N'
    or
    select decode(allow_view,'Y',
              '<a href="javascript:downloadImg(' || image_id || ');"><img src="/i/download.gif"></a>',
              '<img src="/i/folder.gif">') link_col
    from table
    What is downloadImg? it is not a standard APEX JavaScript API. It is therefore highly unlikely that this is of any use to the OP.

  • Selecting all columns from table in a model query

    I have written a model query which joins 4 tables, applies some rules and returns updated rows by selecting 4 columns out of this. Currently it works fine because all 4 columns are from MODEL aliases. Now I need to select 16 more columns in the same query but none of these columns is added in MODEL aliases. When I tried selecting these columns I got oracle error ORA - 32614. Can someone please guide me to include these columns in the same model query?
    I tried couple of options but no luck. Here are those options for ready reference:
    1. I cannot nest existing model query into another select because there are no columns avaiable to join in output of current query to map with other records.
    2. I cannot include all 16 columns in MODEL aliases because some of these columns are actually output of user defined functions.
    I am using Oracle database version 11g Release 11.2.0.1.0.
    Edited by: Anirudha Dhopate on Jan 23, 2011 5:43 PM

    Thank you Avijit for your reply. There is a syntax error on the ON in this part of the statement which I don't know how to fix - I tried messing around with another INNER JOIN but am not confident that I'm doing the right thing:
    SENAlertType.SENAlertTypeIDONClassMember.ClassMemberStudentID
    Thanks for your help! I will need to do some more bedtime reading on joins.
    Daniel

  • Query result shows the same data twice

    Hello All,
    I've created UDFs like Supplier Name, Supplier Address, and (Excise Details like)Supplier ECC No,
    Supplier Range.
    And I've created Query for Supplier Name and Supplier Address, and I assigned to FMS, Its working Correctly.
    I've created Query for Supplier ECC No. the Query is,
    SELECT  T0.[ECCNo],T1.[CardName]
    FROM CRD7 T0  INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
    WHERE T0.[ECCNo]  Like  '[%]'
    I saved the Query, and assigned to FMS.
    At this stage (i.e. Query for Supplier ECC No.) the above Query showing the same data twice.
    Please suggest to rectify the problem.
    Thanks,
    Pratheeviraj

    You can get rid of the duplicates by changing the query to
    SELECT DISTINCT T0.ECCNo,T1.CardName
    FROM CRD7 T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
    WHERE T0.ECCNo Like '[%]'
    But it is always worth understanding why you are getting multiple records to check that you are getting back what you want.  I think you may be seeing this because CRD7 can have multiple records for a business partner - maybe one for each of 'pay-to' and 'ship-to' address?

  • Select the same attribute in a query more than once

    Hi Experts,
    My requirement is to select the same attribute in a query more than once.
    How can I acheive this?
    Expecting an early solution..
    Pilliga

    Hi,
    Create a calculated attribute that just displays the "same attribute".
    (Note: you will need to uniquely name this object).
    For example, if you wish to display Emp Name twice in the same query, then create a calculated attribute called Emp Name2 and select it and Emp Name in your query and the same info will be displayed twice in your results set.
    Cheers,
    Tanish

  • How to implement 'Quick Select' column in a query result table?

    Hi,
    I have a requirement in OAF to design a search page with 'Quick select' column.
    One of the column in the query result table should be a quick select.
    Once user clicks on the quick select column, we have to navigate back to the previous page with the row value selected.
    Can anyone help me in this.
    Thanks.

    Also refer the search exercise in the toolbox tutorials.
    you can implement the quick search in the same way as update and delete buttons.
    --Prasanna                                                                                                                                                                                                                                                                                                                                   

  • MS SQL query slow using view column as criteria

    HI,
    I am experiencing a very frustrate problem. I have 2 tables, and create a view
    to union these 2 tables, when do a select on this view using the column of the
    view as criteria is took more 1 minutes, but the query runs fine in Qurey Analyzer.
    Anybody has the same experience? is this the problem with jdbc?

    I searched http://e-docs.bea.com/wls/docs70/index.html, also searched the documentation
    for wls6.1, wls5.1. As you pointed I searched support site, they are all the customer
    case, it's not formal documentation.
    Joe Weinstein <[email protected]> wrote:
    >
    >
    jen wrote:
    Thanks. but I search on the table is fine (the same column). is thereany db setting
    could be tuned? so the view is the problem? No, it's a client decision/issue. If you defined your tables to have
    nvarchar columns
    the jdbc driver's parameter values would be fine as-is.
    I searched "useVarChars" on whole
    site and can't find anything.Which site? This is a property of the weblogic.jdbc.mssqlserver4.Driver.
    I just went to www.bea.com/support and entered useVarChars in the Ask
    BEA
    question panel and got hits...
    Joe
    Joe Weinstein <[email protected]> wrote:
    Jen wrote:
    Sorry it's my bad. I am testing on wls81, but the problems is on wls70,so they
    are using different drivers.
    You are the magic man. It worked on wls81 now. I am sure it will curethe problem
    on wls70. Is there any documentation on this? Why it is not a problemon some
    databse server? ThanksSure. The issue has to do with the MS client-DBMS protocol having evolved
    from
    an 8-bit (7-bit really) character representation. Now it has a newer
    16-bit
    way, to transfer NVARCHAR data. Java characters are all 16-bit, so
    by default
    a JDBC driver will send Java parameters over as NVARCHAR data. This
    is
    crucial
    for Japanese data etc. However, once simple ASCII data is transformed
    to an
    NVARCHAR format, the DBMS can't directly compare it to varchar data,
    or use it
    in index searches. The DBMS has to convert the VARCHAR data to NVARCHAR,
    but it
    can't guarantee that the converted data will retain the same ordering
    as the index,
    so the DBMS has to do a table scan!
    The properties I suggested are each driver's way of allowing you
    to say "I'm
    a simple American ;) I am using simple varchar data so please sendmy
    java
    strings that way.". This allows the DBMS to use your varchar indexes
    in queries.
    Joe Weinstein at BEA
    Joe Weinstein <[email protected]> wrote:
    Jen wrote:
    It doesn't cure the problem. Here is my pool
    <JDBCConnectionPool DriverName="weblogic.jdbc.sqlserver.SQLServerDriver"Name="test_pool"
    Password="{3DES}fKSovViFe5kHzl/vTs0LVQ==" Properties="user=user;PortNumber=1543;useVarChars=true;ServerName=194.20.2.10;DatabaseName=devDB"
    Targets="admin" TestTableName="SQL SELECT COUNT(*) FROM sysobjects"URL="jdbc:bea:sqlserver://194.20.2.10:1543"/>
    Strange is some database is fine.Oh, sorry. I thought it was the older weblogic driver. Change the
    useVarChars=true to sendStringParametersAsUnicode=false
    Let me know... Also, I suggest changing the TestTableName to "SQL
    select
    1".
    For MS, that will be much more efficient than involving a full count
    of sysobjects!
    Joe
    Joe Weinstein <[email protected]> wrote:
    Jen wrote:
    You are right. Tadaa! Am I Kreskin, or what? ;) Here's what I recommend:
    In your pool definition, for this driver add a driver property:
    useVarChars=true
    and let me know if it's all better.
    Joe
    I am using weblogic jdbc driver weblogic.jdbc.mssqlserver4.Driver.
    here is the code:
    getData(Connection connection, String stmt, ArrayList arguments)
         PreparedStatement pStatement=null;>>>>>>>>     ResultSet resultSet=null;>>>>>>>>     try {>>>>>>>>         pStatement = connection.prepareStatement(stmt);>>>>>>>>         for (int i = 1; i <= arguments.size(); i++) {>>>>>>>>          pStatement.setString(i, (String) arguments.get(i-1));>>>>>>>>                    resultSet = pStatement.executeQuery(); //this statement takesmore than 1
    min.
    Joe Weinstein <[email protected]> wrote:
    Jen wrote:
    HI,
    I am experiencing a very frustrate problem. I have 2 tables,
    and
    create
    a view
    to union these 2 tables, when do a select on this view using
    the
    column
    of the
    view as criteria is took more 1 minutes, but the query runs
    fine
    in
    Qurey Analyzer.
    Anybody has the same experience? is this the problem with jdbc?
    I have suspicions... Show me the jdbc code. I'm guessing it's
    a
    PreparedStatement,
    and you send the search criterion column value as a parameter
    you
    set
    with a
    setString().... Let me know... (also let me know which jdbc driveryou're
    using).
    Joe

Maybe you are looking for