<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Schauderhaft &#187; Oracle</title>
	<atom:link href="http://blog.schauderhaft.de/tag/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 05 Sep 2010 08:43:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Tune a SQL Statement</title>
		<link>http://blog.schauderhaft.de/2010/06/27/how-to-tune-a-sql-statement/</link>
		<comments>http://blog.schauderhaft.de/2010/06/27/how-to-tune-a-sql-statement/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 04:07:03 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=531</guid>
		<description><![CDATA[One of my standard questions I ask when interviewing a candidate with some database or SQL know how is: Assume you have a fairly complex select statement operating on some serious tables (tens of millions of rows). The customer complains the SQL statement is to slow. What options would you consider for tuning such a [...]]]></description>
			<content:encoded><![CDATA[<p>One of my standard questions I ask when interviewing a candidate with some database or SQL know how is:</p>
<blockquote><p>Assume you have a fairly complex select statement operating on some serious tables (tens of millions of rows). The customer complains the SQL statement is to slow. What options would you consider for tuning such a select statement? If it matters assume it is an Oracle database.</p></blockquote>
<p>Just in case you want to apply for a job at a <a href="http://www.lineas.de">LINEAS</a> here is, what I would consider a damn good answer:</p>
<p>First I check if the SQL statement is really slow, for that I&#8217;ll execute the SQL statement with a low level tool (SQL Plus, SQL Developer &#8230;) on the database. I fetch the same amount of rows, as the customer (or his application) does. I do that on the DB machine or one a machine with very good connectivity to the database, in order to take network issues out of the loop. I measure the execution time. If this is as slow as described by the customer, the SQL statement plus DB seem to be the problem. If not probably something else is the problem (network, application logic &#8230;)</p>
<p>I establish a target performance, we need to meet the customer requirements.</p>
<p>I check the execution plan of the statement comparing it with the structure of the schema (tables, indexes, number of rows in tables compared to number of rows actually selected). Are there any obvious problems? For example;</p>
<ul>
<li> Full Table Scans that result only in a small percentage of the rows of that table</li>
<li>Index accesses on a large percentage of rows</li>
<li>Indexes that don&#8217;t get used, although one would expect them to</li>
<li>Identical expensive pieces of the execution plan, on different places  of the execution plan</li>
<li>Estimated number of rows in the execution plan that differ significantly from the actual number of rows returned</li>
</ul>
<p>Depending on what I find I would try some of the following actions, to improve performance</p>
<ul>
<li>gather statistics</li>
<li>create an additional index supporting the where clauses used in the statement</li>
<li>add columns to an index, in order to remove the need for additional table accesses</li>
<li>use hints to force certain execution plans</li>
<li>exchange (not) in with (not) exists and vice versa</li>
<li>rearrange the select statement (e.g. turn a union of  joins into a join of union)</li>
<li>partitioning of tables and indexes</li>
<li>use materialized views</li>
<li>in very special cases use bitmap indexes or dimensions</li>
</ul>
<p>With these options the performance of most of the &#8216;slow&#8217; statements I encountered so far could get brought to acceptable levels. Often improving performance by multiple orders of magnitude. When I am able to identify the problem, but not the solution <a href="http://asktom.oracle.com/pls/apex/f?p=100:1:0">Ask Tom</a> is what I would do next.</p>
<p>There is one answer I get very often, which I consider pretty bad, if it comes without further comment:</p>
<blockquote><p>Replace outer joins with inner joins</p></blockquote>
<p>Outer joins have a different meaning than inner joins. Normally you can&#8217;t replace one for the other. If you actually can, I&#8217;d consider using an outer join a bug, so replacing it with an inner join is bug fixing, not tuning.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/06/27/how-to-tune-a-sql-statement/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building a Turing Engine in Oracle SQL using the Model Clause</title>
		<link>http://blog.schauderhaft.de/2009/06/18/building-a-turing-engine-in-oracle-sql-using-the-model-clause/</link>
		<comments>http://blog.schauderhaft.de/2009/06/18/building-a-turing-engine-in-oracle-sql-using-the-model-clause/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 12:33:36 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[model clause]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[turing]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=157</guid>
		<description><![CDATA[Everybody who understands the working of a Turing machine and that of SQL understands, that SQL probably isn&#8217;t Turing complete, i.e. you can&#8217;t use it to build a Turing machine. Yet I&#8217;m going to show that you actually can implement a Turing Engine using Oracle SQL. So for those among you who haven&#8217;t studied informatics: [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody who understands the working of a <a href="http://en.wikipedia.org/wiki/Turing_machine">Turing machine</a> and that of SQL understands, that SQL probably isn&#8217;t Turing complete, i.e. you can&#8217;t use it to build a Turing machine. Yet I&#8217;m going to show that you actually can implement a Turing Engine using Oracle SQL.</p>
<p>So for those among you who haven&#8217;t studied informatics: &#8220;What is a Turing machine?&#8221;</p>
<p>A Turing machine is a theoretical, extremly simple computer. It consists of an endless array (often called tape), that can store a symbol on every position, and the &#8216;head&#8217; that is positioned at a specific position of the tape. The Turing machine works as follows: The head reads the symbol in the array at its current position. Depending on the current state of the head, and the symbol read, it writes a new symbol in the array at the current position, and moves one step to the right or left. It continues to do so, until it reaches a final state.</p>
<p>Turing machines are interesting, because everything a computer can calculate, can be calculated using a Turing machine and vice versa. The fact that the memory of a real computer is limited can be ignored, as long as it is &#8216;big enough&#8217;.</p>
<p>Since there is no way to iterate in SQL, neither directly nor by means of recursion, it is hard to imagine, how such a machine could be implemented using SQL.</p>
<p>Enter the model clause. The model clause is a Oracle specific extension to SQL. It is supposed to allow Excel like manipulation on resultsets of a SQL statement. Basically you do a normal Select statement, add a model clause, and in the model clause you can adress any cell of your resultset, and you can even add rows or columns. This is done using a very limited iterative language. This feature exists since Oracle 10g (2004?), but I haven&#8217;t seen it used in a production system (thank godness), actually I haven&#8217;t found a person in real live that actualy knows this feature. One reason is probably that Oracle managed to create the worst language I have seen so far, and I have worked with XSLT, Visual Basic 5, Fortran 66 and 6502 Assembler.</p>
<p>So let&#8217;s implement the Turing machine. We need the tape and the definition of how the Turing machine should behave, it&#8217;s program. Since we are in a database this will get stored in tables:</p>
<div class="geshi no sql">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">DROP</span> <span class="kw1">TABLE</span> tape;drop <span class="kw1">TABLE</span> machine; <span class="co1">&#8211; holds the program of the machinecreate table tape (position number, symbol varchar2(1) not null);alter table tape add constraint tape_pk primary key (position);create table machine (state number, input varchar2(1), output varchar2(1), head_move number(1) not null, next_state number);alter table machine add constraint machine_pk primary key (state, input);</span></div>
</li>
</ol>
</div>
<p>In order to test our Turing machine we need an initial state and a program. Since I&#8217;m no expert on Turing machine programming I copied that from the <a href="http://de.wikipedia.org/wiki/Turingmaschine">german Wikipedia page</a>. The initial state are just two 1s, and the program is supposed to copy all the continuous 1s it finds, with a separating 0 in between:</p>
<div class="geshi no sql">
<ol>
<li class="li1">
<div class="de1"><span class="co1">&#8211; create programinsert into machine (state, input, output, next_state, head_move) values (1,1,0,2,1);insert into machine (state, input, output, next_state, head_move) values (1,0,0,6,0);insert into machine (state, input, output, next_state, head_move) values (2,1,1,2,1);insert into machine (state, input, output, next_state, head_move) values (2,0,0,3,1);</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> machine <span class="br0">&#40;</span>state, input, output, next_state, head_move<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">3</span>,<span class="nu0">0</span>,<span class="nu0">1</span>,<span class="nu0">4</span>,<span class="nu0">-1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> machine <span class="br0">&#40;</span>state, input, output, next_state, head_move<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">3</span>,<span class="nu0">1</span>,<span class="nu0">1</span>,<span class="nu0">3</span>,<span class="nu0">1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> machine <span class="br0">&#40;</span>state, input, output, next_state, head_move<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">4</span>,<span class="nu0">1</span>,<span class="nu0">1</span>,<span class="nu0">4</span>,<span class="nu0">-1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> machine <span class="br0">&#40;</span>state, input, output, next_state, head_move<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">4</span>,<span class="nu0">0</span>,<span class="nu0">0</span>,<span class="nu0">5</span>,<span class="nu0">-1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> machine <span class="br0">&#40;</span>state, input, output, next_state, head_move<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">5</span>,<span class="nu0">1</span>,<span class="nu0">1</span>,<span class="nu0">5</span>,<span class="nu0">-1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> machine <span class="br0">&#40;</span>state, input, output, next_state, head_move<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">5</span>,<span class="nu0">0</span>,<span class="nu0">1</span>,<span class="nu0">1</span>,<span class="nu0">1</span><span class="br0">&#41;</span>;<span class="co1">&#8211; create initial state:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> tape <span class="br0">&#40;</span>position, symbol<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> tape <span class="br0">&#40;</span>position, symbol<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">1</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Now comes the actual work. If you are only interested in the final solution, scroll all the way to the end of the article. But if you follow along you can see how I developed the program. This should teach you a little about the model clause, and why you shouldn&#8217;t use it for anything real.</p>
<p>First step, get any model clause at all to work:</p>
<div class="geshi no sql">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> tapemodeldimension <span class="kw1">BY</span> <span class="br0">&#40;</span>position<span class="br0">&#41;</span>measures <span class="br0">&#40;</span>symbol<span class="br0">&#41;</span>rules iterate<span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">symbol<span class="br0">&#91;</span>any<span class="br0">&#93;</span> <span class="sy0">=</span> symbol<span class="br0">&#91;</span>cv<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#93;</span>*<span class="nu0">2</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">ORDER</span> <span class="kw1">BY</span> position;</div>
</li>
</ol>
</div>
<p>The first line selects the intial state from the tape. The second line starts the model clause. <tt>dimension by (position)</tt> allows us to access a row by its position value. Think of it as an array index or primary key. <tt>Measures</tt> defines the cells we are going to work with. A cell can be accessed with an array like syntax like <tt>symbol[23]</tt>, where 23 it the <tt>position</tt>-value we use to specify the cell. The <tt>rules</tt> part is the actual program, it doesn&#8217;t do anything useful. It only multiplies every symbol with 2 and repeats this 3 times. <tt>any</tt> references all rows (that&#8217;s why it is called any), while <tt>cv()</tt> references the current value, i.e. the any/cv combination defines an implicit loop over all rows of the model.</p>
<p>Second Step: make the program or the machine accessible. In order to do that we make it a &#8216;model&#8217; as well. Note that these reference models are read only.</p>
<div class="geshi no sql">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> tapemodelreference machine_model <span class="kw1">ON</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> machine<span class="br0">&#41;</span> dimension <span class="kw1">BY</span> <span class="br0">&#40;</span>state, input<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">measures <span class="br0">&#40;</span>output, head_move, next_state<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">IGNORE</span> nav <span class="co1">&#8211; this affects the handling of NULL</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">main main_model</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">dimension <span class="kw1">BY</span> <span class="br0">&#40;</span>position<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">measures <span class="br0">&#40;</span>symbol, <span class="nu0">1</span> current_state, <span class="nu0">0</span> current_position<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">rules iterate<span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">symbol<span class="br0">&#91;</span>any<span class="br0">&#93;</span> <span class="sy0">=</span> symbol<span class="br0">&#91;</span>cv<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#93;</span>*<span class="nu0">2</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">ORDER</span> <span class="kw1">BY</span> position;</div>
</li>
</ol>
</div>
<p>Using the <tt>measures</tt> I also added two column to store the current state of the Turing machine, and the current position of the head. These cells are available for every row of my model, i.e. the tape, but I&#8217;m going to use only the one in position 0. Maybe just didn&#8217;t look hard enough but I couldn&#8217;t find a proper way to define variables.</p>
<p>Now we have all the data structures that we need, we can actually implement the workings of the Turing machine.</p>
<div class="geshi no sql">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> tapemodelreference machine_model <span class="kw1">ON</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> machine<span class="br0">&#41;</span> dimension <span class="kw1">BY</span> <span class="br0">&#40;</span>state, input<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">measures <span class="br0">&#40;</span>output, head_move, next_state<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">main main_model</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">dimension <span class="kw1">BY</span> <span class="br0">&#40;</span>position<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">measures <span class="br0">&#40;</span>symbol, <span class="nu0">1</span> current_state,<span class="nu0">0</span> current_position, <span class="nu0">0</span> current_symbol<span class="br0">&#41;</span> <span class="kw1">IGNORE</span> nav</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">rules iterate<span class="br0">&#40;</span><span class="nu0">15</span><span class="br0">&#41;</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> nvl<span class="br0">&#40;</span>symbol<span class="br0">&#91;</span>current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,<span class="nu0">0</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">symbol<span class="br0">&#91;</span>current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span> <span class="sy0">=</span> machine_model.output<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> &nbsp;current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> + machine_model.head_move<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> machine_model.next_state<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">ORDER</span> <span class="kw1">BY</span> position;</div>
</li>
</ol>
</div>
<p>The real program part are the four lines after <tt>rules</tt>. The first line reads the current symbol, the second writes the new symbol, depending on the current symbol and the current state. The third line moves the head, and the fourth line sets the new current state. We are almost done.</p>
<p>What&#8217;s left is the implementation of stop states. Right now we just iterate 15 times (<tt>rules iterate(15)</tt>) the little challenge is: The model clause does not offer the equivalent of a while loop. It only knows for loops. Luckily you can exit from a loop with another condition, so when you need a while loop you define a number as being equal to inifinity, and put your exit condition in a <tt>until</tt> clause, like so:</p>
<div class="geshi no sql">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> tapemodelreference machine_model <span class="kw1">ON</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> machine<span class="br0">&#41;</span> dimension <span class="kw1">BY</span> <span class="br0">&#40;</span>state, input<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">measures <span class="br0">&#40;</span>output, head_move, next_state<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">main main_model</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">dimension <span class="kw1">BY</span> <span class="br0">&#40;</span>position<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">measures <span class="br0">&#40;</span>symbol, <span class="nu0">1</span> current_state,<span class="nu0">0</span> current_position, <span class="nu0">0</span> current_symbol<span class="br0">&#41;</span> <span class="kw1">IGNORE</span> nav</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">rules iterate<span class="br0">&#40;</span><span class="nu0">10000</span><span class="br0">&#41;</span> until <span class="br0">&#40;</span>machine_model.next_state<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, <span class="nu0">0</span><span class="br0">&#93;</span> <span class="kw1">IS</span> <span class="kw1">NULL</span> <span class="br0">&#41;</span> <span class="br0">&#40;</span> <span class="co1">&#8211; if the machine reaches an undefined state it considers it a stop state.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> nvl<span class="br0">&#40;</span>symbol<span class="br0">&#91;</span>current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,<span class="nu0">0</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">symbol<span class="br0">&#91;</span>current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span> <span class="sy0">=</span> machine_model.output<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> &nbsp;current_position<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> + machine_model.head_move<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> machine_model.next_state<span class="br0">&#91;</span>current_state<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, current_symbol<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">ORDER</span> <span class="kw1">BY</span> position;</div>
</li>
</ol>
</div>
<p>The Turing machine is done and produces the intended output:</p>
<pre>
position	symbol	c.state	c.position	c.symbol

0		1	6	2		0

1		1	1	0		0

2		0

3		1

4		1</pre>
<p>So, what do we learn from this?</p>
<ul>
<li>Oracle SQL is indeed Turing complete</li>
<li>Oracle jumped the DSL band waggon already in 2004 and fell of the other side</li>
<li>You definetly should learn and use the model clause if
<ul>
<li>You want to make your code unreadable</li>
<li>You are a masochist</li>
<li>You earn your money by betting: &#8220;I bet you can&#8217;t do this in SQL&#8221;</li>
</ul>
</li>
<li>Other then that the Oracle Clause is just another sign that Oracle doesn&#8217;t get anything right if it isn&#8217;t inside the database core engine</li>
</ul>
<p>If you still want to explore the model clause further, I can recommend these links:</p>
<p><a href="http://www.oreillynet.com/pub/a/network/2004/08/10/MODELclause.html">http://www.oreillynet.com/pub/a/network/2004/08/10/MODELclause.html</a></p>
<p><a href="http://rwijk.blogspot.com/2007/10/sql-model-clause-tutorial-part-one.html ">http://rwijk.blogspot.com/2007/10/sql-model-clause-tutorial-part-one.html </a></p>
<p><a href="http://otn.oracle.com/pls/db10g/search?remark=quick_search&amp;word=model+clause">http://otn.oracle.com/pls/db10g/search?remark=quick_search&amp;word=model+clause</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2009/06/18/building-a-turing-engine-in-oracle-sql-using-the-model-clause/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
