• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

query time

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
TRACEYMARY
Asked:
TRACEYMARY
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
 
MorDeRorCommented:
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
 
TRACEYMARYAuthor Commented:
Thanks i appreciate it....
0
 
MorDeRorCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now