Solved

query time

Posted on 2006-11-16
4
368 Views
Last Modified: 2008-03-06
Hi there i was using quest and playing around and i gave it  sql to look at and run their wizard it gives me this

orginal
SELECT T1."ACCT_ID" AS "c1",
       T1."ORG_ID" AS "c2",
       T1."PROJ_ID" AS "c3",
       T1."FY_CD" AS "c4",
       T1."PD_NO" AS "c5",
       T1."S_JNL_CD" AS "c6",
       T1."POST_SEQ_NO" AS "c7",
       T2."ACCT_NAME" AS "c8",
       T3."ID" AS "c9",
       T3."AMT" AS "c10",
       T3."NAME" AS "c13",
       T3."TRN_DESC" AS "c14",
       T3."COMMENTS_NT" AS "c15",
       T1."MODIFIED_BY" AS "c16",
       T1."TIME_STAMP" AS "c17"
  FROM "UnionCP"."DELTEK"."ACCT" T2,
("UnionCP"."DELTEK"."GL_POST_SUM" T1
       LEFT OUTER JOIN "UnionCP"."DELTEK"."GL_DETL" T3
         ON T1."SRCE_KEY" = T3."GLPSUM_SRCE_KEY"
            AND T1."LVL1_KEY" = T3."GLPSUM_LVL1_KEY"
            AND T1."LVL2_KEY" = T3."GLPSUM_LVL2_KEY"
            AND T1."LVL3_KEY" = T3."GLPSUM_LVL3_KEY")
 WHERE T1."ACCT_ID" = T2."ACCT_ID"
   AND T1."ORG_ID" LIKE '1.014%'
   AND T1."PD_NO" = 1
   AND T1."ACCT_ID" LIKE '%'
 ORDER BY 6 asc


--

SELECT T1."ACCT_ID" AS "c1",
       T1."ORG_ID" AS "c2",
       T1."PROJ_ID" AS "c3",
       T1."FY_CD" AS "c4",
       T1."PD_NO" AS "c5",
       T1."S_JNL_CD" AS "c6",
       T1."POST_SEQ_NO" AS "c7",
       T2."ACCT_NAME" AS "c8",
       T3."ID" AS "c9",
       T3."AMT" AS "c10",
       T3."NAME" AS "c13",
       T3."TRN_DESC" AS "c14",
       T3."COMMENTS_NT" AS "c15",
       T1."MODIFIED_BY" AS "c16",
       T1."TIME_STAMP" AS "c17"
  FROM "UnionCP"."DELTEK"."ACCT" T2,
       "UnionCP"."DELTEK"."GL_POST_SUM" T1
       LEFT OUTER JOIN "UnionCP"."DELTEK"."GL_DETL" T3
         ON COALESCE (T1."SRCE_KEY" , T1."SRCE_KEY") = T3."GLPSUM_SRCE_KEY"
            AND T1."LVL1_KEY" = T3."GLPSUM_LVL1_KEY"
            AND T1."LVL2_KEY" = T3."GLPSUM_LVL2_KEY"
            AND T1."LVL3_KEY" = T3."GLPSUM_LVL3_KEY"
 WHERE T1."ACCT_ID" = T2."ACCT_ID"
   AND T1."ORG_ID" LIKE '1.014%'
   AND T1."PD_NO" = 1
   AND T1."ACCT_ID" LIKE '%'
 ORDER BY 6 asc
OPTION (LOOP JOIN)

I never seen the option loop join before let alone the coalesce COALESCE (T1."SRCE_KEY" , T1."SRCE_KEY") = T3."GLPSUM_SRCE_KEY"

Anyone explain what it is doing now
0
Comment
Question by:TRACEYMARY
  • 2
4 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 17959914
>COALESCE (T1."SRCE_KEY" , T1."SRCE_KEY")
will return the value of the first parameter that is not null.
as the 2 fields are the same, this does not change anything here, except that it might influence the usage of an index on that field, ie avoid using an index on that field.


>option (loop join)
these are hints to force sql server (assumming it is sql server you use) to use a given execution plan on the join.
these options should be avoided unless you really (really really) need them and can assure that they will solve a slow-running query.
http://msdn2.microsoft.com/en-us/ms181714.aspx
0
 
LVL 3

Accepted Solution

by:
MorDeRor earned 250 total points
ID: 17959993
well, LOOP JOIN is just a join hint. See this quote from BOL that describes this part of the OPTION clause:

{ LOOP | MERGE | HASH } JOIN

Specifies that all join operations are performed by loop join, merge join, or hash join in the whole query. If more than one join hint is specified, the optimizer selects the least expensive join strategy from the allowed ones.

If, in the same query, a join hint is also specified for a specific pair of tables, this join hint takes precedence in the joining of the two tables although the query hints still must be honored. Thus, the join hint for the pair of tables may only restrict the selection of allowed join methods in the query hint. See Hints for details.

as far as COALESCE (T1."SRCE_KEY" , T1."SRCE_KEY") = T3."GLPSUM_SRCE_KEY" - it's just T1."SRCE_KEY"=T3."GLPSUM_SRCE_KEY". I don't know what's the point of using COALESCE with a repeated expression. certainly defeats the purpuse, because you will get an error if T1."SRCE_KEY" is ever null (try "select coalesce(null,null)")
0
 
LVL 7

Author Comment

by:TRACEYMARY
ID: 17967981
Thanks i appreciate it....
0
 
LVL 3

Expert Comment

by:MorDeRor
ID: 17972872
By the way, I have a correction on what I said, about COALESCE - COALESCE (T1."SRCE_KEY" , T1."SRCE_KEY") will return a NULL when T1.SCRCE_KEY is null, not an error like I wrote before. While it is true that COALESCE(NULL,NULL) will return an error, it is for a different reason.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now