<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Teradata join using SAS is taking very long in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876902#M346410</link>
    <description>&lt;P&gt;I am running a proc sql in teradata and this is taking more than 1 hour to run. The data size is relatively small with about 86,000 rows returned. I create a temp teradata table from a sas dataset with this code:&lt;/P&gt;
&lt;P&gt;libname TDWORK teradata user="&amp;amp;user.@LDAP" password=&amp;amp;pword tdpid=bmg dbmstemp=yes &lt;BR /&gt;connection=global tpt=No ;&lt;/P&gt;
&lt;P&gt;data tdwork.Cust_Mstr_Wk_Curr(dbcommit=20000 multistmt=yes &lt;BR /&gt;dbcreate_table_opts='PRIMARY INDEX (Cust_Num)') ; &lt;BR /&gt;set Cust_Mstr_Wk_Curr_Cdb ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I then need to join this temp table to other tables in teradata with this code.&lt;/P&gt;
&lt;P&gt;libname bmg_pdd teradata server=&amp;lt;servername&amp;gt; tdpid=bmg database="Bmgpdd" &lt;BR /&gt;connection=global mode=teradata user="&amp;amp;user.@LDAP" password = &amp;amp;pword;&lt;BR /&gt;options symbolgen macrogen;&lt;/P&gt;
&lt;P&gt;proc sql ; &lt;BR /&gt;create table tdwork.Bmgpdd_Acct_X_Cust_Hg as &lt;BR /&gt;(Select &lt;BR /&gt;A.* , &lt;BR /&gt;M.Acct_Num As Acct_Num , &lt;BR /&gt;M.Co_Id As Co_Id ,&lt;BR /&gt;B.Cust_Acct_Rel_Cd ,&lt;BR /&gt;m.Short_Name ,&lt;BR /&gt;m.PCode ,&lt;BR /&gt;m.Open_Dt ,&lt;BR /&gt;m.Reopen_Dt ,&lt;BR /&gt;m.Eom_Bal &lt;BR /&gt;From tdwork.Cust_Mstr_Wk_Curr A,&lt;BR /&gt;bmg_pdd.Acct_X_Cust_Hg_&amp;amp;yearmon._Cdb B,&lt;BR /&gt;bmg_pdd.Acct_Mstr_&amp;amp;yearmon._Hg M&lt;BR /&gt;Where A.Cust_Num = B.Cust_Num&lt;BR /&gt;and B.Acct_Num = M.Acct_Num&lt;BR /&gt;);&lt;BR /&gt;quit; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code is within a macro. Given below is the log for this step.&lt;/P&gt;
&lt;P&gt;MACROGEN(LAST12): proc sql ;&lt;BR /&gt;SYMBOLGEN: Macro variable YEARMON resolves to 202302&lt;BR /&gt;SYMBOLGEN: Macro variable YEARMON resolves to 202302&lt;BR /&gt;MACROGEN(LAST12): create table tdwork.Bmgpdd_Acct_X_Cust_Hg as (Select A.* , M.Acct_Num As Acct_Num , M.Co_Id As Co_Id , &lt;BR /&gt;B.Cust_Acct_Rel_Cd , m.Short_Name , m.PCode , m.Open_Dt , m.Reopen_Dt , m.Eom_Bal From tdwork.Cust_Mstr_Wk_Curr A, &lt;BR /&gt;bmg_pdd.Acct_X_Cust_Hg_202302_Cdb B, bmg_pdd.Acct_Mstr_202302_Hg M Where A.Cust_Num = B.Cust_Num and B.Acct_Num = M.Acct_Num );&lt;BR /&gt;NOTE: SAS variable labels, formats, and lengths are not written to DBMS tables.&lt;BR /&gt;NOTE: Table TDWORK.Bmgpdd_Acct_X_Cust_Hg created, with 85880 rows and 15 columns.&lt;BR /&gt;&lt;BR /&gt;MACROGEN(LAST12): quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 1:08:08.17&lt;BR /&gt;cpu time 2:13.59&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to speed up the query? Thanks in advance.&lt;/P&gt;</description>
    <pubDate>Mon, 22 May 2023 13:42:33 GMT</pubDate>
    <dc:creator>jitb</dc:creator>
    <dc:date>2023-05-22T13:42:33Z</dc:date>
    <item>
      <title>Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876902#M346410</link>
      <description>&lt;P&gt;I am running a proc sql in teradata and this is taking more than 1 hour to run. The data size is relatively small with about 86,000 rows returned. I create a temp teradata table from a sas dataset with this code:&lt;/P&gt;
&lt;P&gt;libname TDWORK teradata user="&amp;amp;user.@LDAP" password=&amp;amp;pword tdpid=bmg dbmstemp=yes &lt;BR /&gt;connection=global tpt=No ;&lt;/P&gt;
&lt;P&gt;data tdwork.Cust_Mstr_Wk_Curr(dbcommit=20000 multistmt=yes &lt;BR /&gt;dbcreate_table_opts='PRIMARY INDEX (Cust_Num)') ; &lt;BR /&gt;set Cust_Mstr_Wk_Curr_Cdb ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I then need to join this temp table to other tables in teradata with this code.&lt;/P&gt;
&lt;P&gt;libname bmg_pdd teradata server=&amp;lt;servername&amp;gt; tdpid=bmg database="Bmgpdd" &lt;BR /&gt;connection=global mode=teradata user="&amp;amp;user.@LDAP" password = &amp;amp;pword;&lt;BR /&gt;options symbolgen macrogen;&lt;/P&gt;
&lt;P&gt;proc sql ; &lt;BR /&gt;create table tdwork.Bmgpdd_Acct_X_Cust_Hg as &lt;BR /&gt;(Select &lt;BR /&gt;A.* , &lt;BR /&gt;M.Acct_Num As Acct_Num , &lt;BR /&gt;M.Co_Id As Co_Id ,&lt;BR /&gt;B.Cust_Acct_Rel_Cd ,&lt;BR /&gt;m.Short_Name ,&lt;BR /&gt;m.PCode ,&lt;BR /&gt;m.Open_Dt ,&lt;BR /&gt;m.Reopen_Dt ,&lt;BR /&gt;m.Eom_Bal &lt;BR /&gt;From tdwork.Cust_Mstr_Wk_Curr A,&lt;BR /&gt;bmg_pdd.Acct_X_Cust_Hg_&amp;amp;yearmon._Cdb B,&lt;BR /&gt;bmg_pdd.Acct_Mstr_&amp;amp;yearmon._Hg M&lt;BR /&gt;Where A.Cust_Num = B.Cust_Num&lt;BR /&gt;and B.Acct_Num = M.Acct_Num&lt;BR /&gt;);&lt;BR /&gt;quit; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code is within a macro. Given below is the log for this step.&lt;/P&gt;
&lt;P&gt;MACROGEN(LAST12): proc sql ;&lt;BR /&gt;SYMBOLGEN: Macro variable YEARMON resolves to 202302&lt;BR /&gt;SYMBOLGEN: Macro variable YEARMON resolves to 202302&lt;BR /&gt;MACROGEN(LAST12): create table tdwork.Bmgpdd_Acct_X_Cust_Hg as (Select A.* , M.Acct_Num As Acct_Num , M.Co_Id As Co_Id , &lt;BR /&gt;B.Cust_Acct_Rel_Cd , m.Short_Name , m.PCode , m.Open_Dt , m.Reopen_Dt , m.Eom_Bal From tdwork.Cust_Mstr_Wk_Curr A, &lt;BR /&gt;bmg_pdd.Acct_X_Cust_Hg_202302_Cdb B, bmg_pdd.Acct_Mstr_202302_Hg M Where A.Cust_Num = B.Cust_Num and B.Acct_Num = M.Acct_Num );&lt;BR /&gt;NOTE: SAS variable labels, formats, and lengths are not written to DBMS tables.&lt;BR /&gt;NOTE: Table TDWORK.Bmgpdd_Acct_X_Cust_Hg created, with 85880 rows and 15 columns.&lt;BR /&gt;&lt;BR /&gt;MACROGEN(LAST12): quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 1:08:08.17&lt;BR /&gt;cpu time 2:13.59&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to speed up the query? Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 22 May 2023 13:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876902#M346410</guid>
      <dc:creator>jitb</dc:creator>
      <dc:date>2023-05-22T13:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876906#M346411</link>
      <description>&lt;P&gt;Make sure to run the SQL JOIN in the teradata database and not force SAS to extract all of the data to the SAS environment and then write it back.&amp;nbsp; THe best way is to use explicit passthru code.&lt;/P&gt;
&lt;P&gt;So try something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
connect using TDWORK;
execute by TDWORK
( /* TERADATA SQL CODE GOES HERE */
);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can ask for help from your TERADATA DBA for how to optimize the TERADATA SQL.&amp;nbsp; TERADATA does have an EXPLAIN statement to show you how TERADATA plans to execute the query with estimates for how long each step will take.&amp;nbsp; That is very good for seeing if the query might need to be changed to improve preformance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TERADATA is a highly parallel environment.&amp;nbsp; It uses the PRIMARY INDEX of the table to decide how to distribute the data to the different compute units.&amp;nbsp; It is very important for performance that the distribution is not skewed so that the data is spread evenly across the nodes.&amp;nbsp; &amp;nbsp;If you do not tell it otherwise it will just use the first variable as the PRIMARY INDEX, which may not be optimal.&amp;nbsp; If there is really no good variable to use as the primary index there is a way to tell it that the table does not use any index.&amp;nbsp; If you want to create a new table in TERADATA by using implicit SQL then you can add the PRIMARY INDEX option in the&amp;nbsp; dbcreate_table_opts= dataset name options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 May 2023 14:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876906#M346411</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-22T14:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876909#M346414</link>
      <description>&lt;P&gt;Thank you so much, Tom! Will try this and get back on the thread.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jit&lt;/P&gt;</description>
      <pubDate>Mon, 22 May 2023 14:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/876909#M346414</guid>
      <dc:creator>jitb</dc:creator>
      <dc:date>2023-05-22T14:13:14Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/877018#M346459</link>
      <description>&lt;P&gt;Theoretically you should be able to do implicit SQL pass-through, but try to make your libname statements as identical as possible. I was some time I worked with TD, so I can't really tell if you need tpid= in one connection and database= in the other.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add this prior to the join to see what's happening:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options msglevel=i sastrace=',,,d' sastraceloc=saslog nostsuffix;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2023 09:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/877018#M346459</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-05-23T09:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/877055#M346472</link>
      <description>&lt;P&gt;Ok, thank you. Will try these options to trace the process. I am still struggling a bit with Tom's suggestion. Will include both your suggestions. Will be out for a few days, but will get back later this week. Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2023 11:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/877055#M346472</guid>
      <dc:creator>jitb</dc:creator>
      <dc:date>2023-05-23T11:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/878429#M347057</link>
      <description>&lt;P&gt;Thanks to Tom and LinusH for your responses. I believe the key is to have a dedicated workspace on teradata where temp files could be stored. I have requested this space from the admin. Once I get it, I will try your suggestions again. As of now, the code inserts rows 1 at a time and does not use multi inserts. This is why it's taking so long. The options statement really helped in analyzing this. Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 14:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/878429#M347057</guid>
      <dc:creator>jitb</dc:creator>
      <dc:date>2023-05-31T14:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/878466#M347071</link>
      <description>&lt;P&gt;One issue, regardless of whether this runs in SAS or on Teradata is multiple Cartesian joins:&lt;/P&gt;
&lt;PRE&gt;From tdwork.Cust_Mstr_Wk_Curr A&lt;FONT size="5" color="#FF00FF"&gt;&lt;STRONG&gt;,&lt;/STRONG&gt;&lt;/FONT&gt;
bmg_pdd.Acct_X_Cust_Hg_&amp;amp;yearmon._Cdb B&lt;FONT size="5" color="#FF00FF"&gt;&lt;STRONG&gt;,&lt;/STRONG&gt;&lt;/FONT&gt;
bmg_pdd.Acct_Mstr_&amp;amp;yearmon._Hg M
Where A.Cust_Num = B.Cust_Num
and B.Acct_Num = M.Acct_Num
&lt;/PRE&gt;
&lt;P&gt;Those comma are telling the SQL processor to match up every record in each table with every record in every other table. So if table A has 10 records, B as 10 records and M as 10 then you have used 10*10*10=1000 record matches and then selected from the result.&lt;/P&gt;
&lt;P&gt;I don't have your data but a JOIN, Left, right or what not, ON A.cust_num=B.cust_num and b.acct_num=m.acct_num probably runs faster and should use less temporary space.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 15:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/878466#M347071</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-31T15:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Teradata join using SAS is taking very long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/878477#M347076</link>
      <description>&lt;P&gt;Thank you, BallardW, for the pointers. I will change the code to do joins instead. Still waiting for my work space, but will use your suggestions when I am ready to execute. Thanks again!&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 15:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Teradata-join-using-SAS-is-taking-very-long/m-p/878477#M347076</guid>
      <dc:creator>jitb</dc:creator>
      <dc:date>2023-05-31T15:53:15Z</dc:date>
    </item>
  </channel>
</rss>

