?
Solved

query time

Posted on 2006-11-16
4
Medium Priority
?
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 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 1000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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