<?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 SQL execution on an Oracle Database in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/405247#M279121</link>
    <description>&lt;P&gt;Hello all you very smart SAS programmers.&amp;nbsp; I have a question for you about processing of SQL statements on an Oracle database.&amp;nbsp; Here is the basics of what I am experiencing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am use an explicit pass through to an Oracle database via proc sql.&amp;nbsp; I wrote a SQL statement that returned roughly 2.1 million records with a run time of about 2 hours.&amp;nbsp; I realized that I was returning more records then I needed so the only change I made to the code was to add a UNIQUE to the select statement.&amp;nbsp; This returned about 150,000 records and increased the run time to about 10 hours.&amp;nbsp; I was in shock!&amp;nbsp; How could a more restrictive query that is designed to return fewer records run loner and not just longer but five times as long.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The obvious answer to me was that there was some sort of system or connectivity issue so I repeated the experiment.&amp;nbsp; It has turned out to be very consistent.&amp;nbsp; My first thought was that I was being a silly little programmer and that I was breaking the pass through by using SAS specific syntax and that to process the unique statement data had to be returned from the native server to the SAS server I am running on.&amp;nbsp; But since I am using an explicit pass through, the statements would not compile or execute if that was the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second thought I had was that UNIQUE is handled by Oracle in some fashion that I didn't anticipate.&amp;nbsp; I did some research on UNIQUE versus DISTINCT hoping to revealed how SQL handles those statements.&amp;nbsp; All I can determine is that the two are synonymous and should return the same results.&amp;nbsp; Nothing about processing.&amp;nbsp; As an experiment, I am going to re-run using a DISTINCT in place of the UNIQUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final thought that I had was in regards to indexes.&amp;nbsp; I know the Oracle table I am pulling from is indexed and I have two WHERE clauses that reference those indexes.&amp;nbsp; One explanation for the increased run times would be that the UNIQUE statement creates a processing situation whereby the indexes are ignored.&amp;nbsp; If this is the case, I can accomplish the same thing as the UNIQUE by using a GROUP BY clause which should then decrease my run time from the 2 hour mark.&amp;nbsp; I am also going to try this as an experiment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, after all of that, I'd like to understand better how the UNIQUE or DISTINCT statements are handled by Oracle so that I can predict when using that code is an acceptable solution or when it should be avoided.&amp;nbsp; Does anyone know how the processing works or have any resources they can point me to where I can learn for myself?&lt;/P&gt;</description>
    <pubDate>Wed, 18 Oct 2017 15:14:49 GMT</pubDate>
    <dc:creator>CurlerBob</dc:creator>
    <dc:date>2017-10-18T15:14:49Z</dc:date>
    <item>
      <title>SQL execution on an Oracle Database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/405247#M279121</link>
      <description>&lt;P&gt;Hello all you very smart SAS programmers.&amp;nbsp; I have a question for you about processing of SQL statements on an Oracle database.&amp;nbsp; Here is the basics of what I am experiencing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am use an explicit pass through to an Oracle database via proc sql.&amp;nbsp; I wrote a SQL statement that returned roughly 2.1 million records with a run time of about 2 hours.&amp;nbsp; I realized that I was returning more records then I needed so the only change I made to the code was to add a UNIQUE to the select statement.&amp;nbsp; This returned about 150,000 records and increased the run time to about 10 hours.&amp;nbsp; I was in shock!&amp;nbsp; How could a more restrictive query that is designed to return fewer records run loner and not just longer but five times as long.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The obvious answer to me was that there was some sort of system or connectivity issue so I repeated the experiment.&amp;nbsp; It has turned out to be very consistent.&amp;nbsp; My first thought was that I was being a silly little programmer and that I was breaking the pass through by using SAS specific syntax and that to process the unique statement data had to be returned from the native server to the SAS server I am running on.&amp;nbsp; But since I am using an explicit pass through, the statements would not compile or execute if that was the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second thought I had was that UNIQUE is handled by Oracle in some fashion that I didn't anticipate.&amp;nbsp; I did some research on UNIQUE versus DISTINCT hoping to revealed how SQL handles those statements.&amp;nbsp; All I can determine is that the two are synonymous and should return the same results.&amp;nbsp; Nothing about processing.&amp;nbsp; As an experiment, I am going to re-run using a DISTINCT in place of the UNIQUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final thought that I had was in regards to indexes.&amp;nbsp; I know the Oracle table I am pulling from is indexed and I have two WHERE clauses that reference those indexes.&amp;nbsp; One explanation for the increased run times would be that the UNIQUE statement creates a processing situation whereby the indexes are ignored.&amp;nbsp; If this is the case, I can accomplish the same thing as the UNIQUE by using a GROUP BY clause which should then decrease my run time from the 2 hour mark.&amp;nbsp; I am also going to try this as an experiment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, after all of that, I'd like to understand better how the UNIQUE or DISTINCT statements are handled by Oracle so that I can predict when using that code is an acceptable solution or when it should be avoided.&amp;nbsp; Does anyone know how the processing works or have any resources they can point me to where I can learn for myself?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2017 15:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/405247#M279121</guid>
      <dc:creator>CurlerBob</dc:creator>
      <dc:date>2017-10-18T15:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL execution on an Oracle Database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/405252#M279122</link>
      <description>Kudos for a well described issue.&lt;BR /&gt;But, which are also clear on, this is an Oracle issue. So I think this question is better asked at an Oracle forum.&lt;BR /&gt;My suggestion is talk to your DBA, share your query, and she/he can apply the execution plan, and eventually either optimizing your query, or do some Oracle config stuff.</description>
      <pubDate>Wed, 18 Oct 2017 15:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/405252#M279122</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-10-18T15:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: SQL execution on an Oracle Database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/406017#M279123</link>
      <description>&lt;P&gt;Yeah, I know it's an Oracle thing but I wanted a SAS user perspective rather than just the Oracle.&amp;nbsp; At the end of the day, I need to know how to use SAS in the Oracle environment and Oracle people can't help with that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That being said, I went to one of our Oracle experts (I didn't know she was an Oracle expert or I would have started with her).&amp;nbsp; She did give some things to optimize but we think we figured out what was going on.&amp;nbsp; I think this is probably of interest to SAS coders and I'm curious if proc sql handles this the same way.&amp;nbsp; It turns out that the DISTINCT statement executes after all other selecting is done.&amp;nbsp; So in my case, after the primary query had returned the 2.1 million records the DISTINCT then executed to reduce the size down to 120,000.&amp;nbsp; In the execution of the DISTINCT (in Oracle at least, I'm curious as to proc sql and other database types) there is an implicit sort that occurs.&amp;nbsp; As we all know, sorts can be very memory intensive and this is what was causing the poor time performance.&amp;nbsp; As a user I am only allocated a limited amount of memory when I connect to the database.&amp;nbsp; The database had to hold the 2.1 million records, perform the sort, and store the resultant table.&amp;nbsp; Because of the size of the dataset, the majority of my allocated memory was already occupied.&amp;nbsp; In order to accomplish the DISTINCT, Oracle was performing all sorts of gymnastics to open up space for temporary use, use it, and then clear it again.&amp;nbsp; The solutions are to be allocated more memory (not going to happen),&amp;nbsp;use less memory (i.e. return fewer records) or not perform the DISTINCT.&amp;nbsp; I went with option number three.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an aside, I was also able to find a different index to select on and added some other code efficiencies that I was unaware of.&amp;nbsp; The end result was twofold, I reduced the run time from 2 hours down to under 5 minutes and I made a new Oracle friend &lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://communities.sas.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 16:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-execution-on-an-Oracle-Database/m-p/406017#M279123</guid>
      <dc:creator>CurlerBob</dc:creator>
      <dc:date>2017-10-20T16:08:37Z</dc:date>
    </item>
  </channel>
</rss>

