<?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 Optimizing a SQL code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52965#M11204</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you provide some background information about what the fields in the table mean, and what the purpose of each table is?&amp;nbsp; It's difficult to make any suggestions without understanding the problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 23 Aug 2011 13:26:18 GMT</pubDate>
    <dc:creator>DF</dc:creator>
    <dc:date>2011-08-23T13:26:18Z</dc:date>
    <item>
      <title>Optimizing a SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52964#M11203</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a SAS SQL code which really takes a long time to run. Are there anybody can help me to make it efficient?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;delete from recover as a&lt;/P&gt;&lt;P&gt;where exists (select * from nbcase as b where a.customer_id=b.customer_id and (a.nb_created_date &amp;lt; b.nb_first_date or a.nb_created_date &amp;gt; b.nb_last_date));&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Aug 2011 13:16:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52964#M11203</guid>
      <dc:creator>c8826024</dc:creator>
      <dc:date>2011-08-23T13:16:00Z</dc:date>
    </item>
    <item>
      <title>Optimizing a SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52965#M11204</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you provide some background information about what the fields in the table mean, and what the purpose of each table is?&amp;nbsp; It's difficult to make any suggestions without understanding the problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Aug 2011 13:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52965#M11204</guid>
      <dc:creator>DF</dc:creator>
      <dc:date>2011-08-23T13:26:18Z</dc:date>
    </item>
    <item>
      <title>Optimizing a SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52966#M11205</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; I want to delete some records from Table recover where customer id matched the same id in table nbcase and nb_created _date &amp;lt; nb_first_date or nb_created_date &amp;gt; nb_last_date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Both tables have duplicate customer ids and table nbcase unique on customer id and nb_first_date. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Aug 2011 13:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52966#M11205</guid>
      <dc:creator>c8826024</dc:creator>
      <dc:date>2011-08-23T13:40:05Z</dc:date>
    </item>
    <item>
      <title>Optimizing a SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52967#M11206</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok, so I'm assuming the table nb_case is some kind of history for a customer, where each change is recorded with First_Date and Last_Date as the effective start/end dates for the other information on that row.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What is the relationship between Created Date and first/last, and what is the meaning of the rows in the Recover table?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It might also help to show some made up sample data for both tables.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Aug 2011 16:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52967#M11206</guid>
      <dc:creator>DF</dc:creator>
      <dc:date>2011-08-23T16:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Optimizing a SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52968#M11207</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is just to provide sample data for your example as well as a duplicate of your SQL.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data recover(rename=(i=customer_id) sortedby=customer_id);&lt;/P&gt;&lt;P&gt; do i=1 to 10000;&lt;/P&gt;&lt;P&gt;&amp;nbsp; nb_created_date=round(ranuni(1234)*100,11)+'01AUG2011'd;&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt; end;&lt;/P&gt;&lt;P&gt;run; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data nbcase(rename=(i=customer_id) sortedby=customer_id drop=j k);&lt;/P&gt;&lt;P&gt; do i=1 to 10000;&lt;/P&gt;&lt;P&gt;&amp;nbsp; j=round(ranuni(1234)*10,1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; do k=1 to j;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; nb_first_date=round(ranuni(1234)*100,11)+'01AUG2011'd;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; nb_last_date=round(ranuni(2345)*100,11)+'01AUG2011'd;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;delete from _recover r&lt;/P&gt;&lt;P&gt; where exists ( select * &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from nbcase nb &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where r.customer_id=nb.customer_id &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and ( r.nb_created_date &amp;lt; nb.nb_first_date &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or r.nb_created_date &amp;gt; nb.nb_last_date )&lt;/P&gt;&lt;P&gt; );&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Matthew Kastin&#xD;
Reason: Previous posting was erroneous after testing.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Aug 2011 17:03:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52968#M11207</guid>
      <dc:creator>FriedEgg</dc:creator>
      <dc:date>2011-08-23T17:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Optimizing a SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52969#M11208</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The resource hog is the correlated subquery.&amp;nbsp; Try to do it with an non-correlated subquery instead.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, if FriedEgg's data sets are a good simulation, try this SQL query instead:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;proc sql stimer;
create table del as
select distinct r.customer_ID 
&amp;nbsp;&amp;nbsp; from nbcase as nb
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Recover as r
&amp;nbsp;&amp;nbsp; where r.customer_id=nb.customer_id
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and ( r.nb_created_date &amp;lt; nb.nb_first_date
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or r.nb_created_date &amp;gt; nb.nb_last_date )
;
delete from recover r
where Customer_ID in (select Customer_ID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from del)
;
quit;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Even though I created a temp table, the 2 queries above consumed a total of 0.8 CPU seconds vice 16.5 CPU seconds.&amp;nbsp; Elapsed time was 0.3 seconds vice 16 seconds for the original query (I have a quad core processor, so CPU time sometimes exceeds Elapsed Time when multi-threading).&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With the constant re-execution of the correlated subquery (inner query ) in the original code, the process is probably I/O bound.&amp;nbsp; The non-correlated sub query in the code above (inner query) executes once and passes the resulting list of values to the outer query. This is &lt;SPAN style="text-decoration: underline;"&gt;much&lt;/SPAN&gt; less I/O intensive. YMMV but it's worth a shot.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Mark Jordan due to spelling errors in the original entry.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Aug 2011 03:11:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Optimizing-a-SQL-code/m-p/52969#M11208</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2011-08-25T03:11:37Z</dc:date>
    </item>
  </channel>
</rss>

