<?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>Question Re: Scalar Function Memory Allocation - Poor Performance in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423225#M54469</link>
    <description>&lt;P&gt;I use the "SELECT INTO ... FROM DUMMY" statement as example. In my functions I use a some "SELECT INTO" statement. &lt;/P&gt;&lt;P&gt;I understand now that SELECT INTO is not recommended and this is not good for me because I was thinking that scalar functions could help me.&lt;/P&gt;&lt;P&gt;But, why does this simple function allocate so much memory?&lt;/P&gt;</description>
    <pubDate>Mon, 03 Apr 2017 12:09:52 GMT</pubDate>
    <dc:creator>former_member235648</dc:creator>
    <dc:date>2017-04-03T12:09:52Z</dc:date>
    <item>
      <title>Scalar Function Memory Allocation - Poor Performance</title>
      <link>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaq-p/423223</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am doing tests using Scalar Functions and I realize that using this type of function my SQL queries get much slower.
To try to understand the reasons for this poor performance I created two scenarios to compare.&lt;/P&gt;&lt;P&gt;I'm using TABLE_1 whith 6k records.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT FIELD1, FIELD2, (SELECT 4 FROM DUMMY) AS VLR  FROM "TABLE_1" ;
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and the plan of above SQL&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/28261-plan1.png" /&gt;&lt;/P&gt;&lt;P&gt;To compare, I create a simple Scalar Fucntion as follow:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;FUNCTION "MY_SCHEMA"."comp.test.functions::return_value" (P_1 NUMBER ) 
	RETURNS RESULT NUMBER
	LANGUAGE SQLSCRIPT
	SQL SECURITY INVOKER AS
BEGIN


 DECLARE V_V1  NUMBER;
 
 SELECT :P_1 INTO V_V1 FROM DUMMY;
 
 RESULT := V_V1;
 
END;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE&gt;SELECT FIELD1, FIELD2,"MY_SCHEMA"."comp.test.functions::return_value" (3 ) AS VLR FROM "TABLE_1"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and the plan of above SQL&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/28263-plan2.png" /&gt;&lt;/P&gt;&lt;P&gt;Perceive the difference between the execution times, compilation and the volume of memory allocated.&lt;/P&gt;&lt;P&gt;Why Scalar Function are so slow?&lt;/P&gt;&lt;P&gt;Why does Scalar Function allocate so much memory? (14GB in second image)&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 17:42:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaq-p/423223</guid>
      <dc:creator>former_member235648</dc:creator>
      <dc:date>2017-03-31T17:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Scalar Function Memory Allocation - Poor Performance</title>
      <link>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423224#M54468</link>
      <description>&lt;P&gt;The two cases are not comparable. The sub-select in the first statement is a constant and gets factored out by the optimizer. &lt;/P&gt;&lt;P&gt;By rewriting the function to not use a SELECT INTO ... FROM DUMMY you get the same level of performance and memory consumption.&lt;/P&gt;&lt;P&gt;Also, up until HANA 2 SQLScript didn't provide statement caching, which added parsing overhead to every executed query. &lt;/P&gt;&lt;P&gt;Besides that, when you compile your function this is the warning you get:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;java.sql.SQLWarning: Not recommended feature: Using SELECT INTO in Scalar UDF&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Generally, you want to avoid single record processing (which is what you do with scalar functions) but try and process whole sets of data instead. &lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 00:19:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423224#M54468</guid>
      <dc:creator>lbreddemann</dc:creator>
      <dc:date>2017-04-01T00:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: Scalar Function Memory Allocation - Poor Performance</title>
      <link>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423225#M54469</link>
      <description>&lt;P&gt;I use the "SELECT INTO ... FROM DUMMY" statement as example. In my functions I use a some "SELECT INTO" statement. &lt;/P&gt;&lt;P&gt;I understand now that SELECT INTO is not recommended and this is not good for me because I was thinking that scalar functions could help me.&lt;/P&gt;&lt;P&gt;But, why does this simple function allocate so much memory?&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 12:09:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423225#M54469</guid>
      <dc:creator>former_member235648</dc:creator>
      <dc:date>2017-04-03T12:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: Scalar Function Memory Allocation - Poor Performance</title>
      <link>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423226#M54470</link>
      <description>&lt;P&gt;Because it is in fact not a simple function, but one that does involve a SQL query. Any SQL query in itself is treated as a sort of program that needs to be parsed, analysed, optimised, and executed. As you call the program practically in a loop, the intermediate result sets &amp;amp; parsed statements are kept during the lifetime of your query. &lt;/P&gt;&lt;P&gt;So, the high memory consumption represents the accumulated memory consumption of all the thousands of calls. &lt;/P&gt;&lt;P&gt;With HANA2 there is more caching of SQLScript statements and resultset happening and it's possible to declare functions as deterministic - which reduces the need to re-execute function for the same input parameters. &lt;/P&gt;&lt;P&gt;In total, the recommendation is to avoid any SELECT statement in scalar functions when performance is important. Instead, these should be small and ideally self-contained. &lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 23:33:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/scalar-function-memory-allocation-poor-performance/qaa-p/423226#M54470</guid>
      <dc:creator>lbreddemann</dc:creator>
      <dc:date>2017-04-03T23:33:45Z</dc:date>
    </item>
  </channel>
</rss>

