<?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>zedia flash blog &#187; SQLStatement</title>
	<atom:link href="http://www.zedia.net/tag/sqlstatement/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Wed, 21 Jul 2010 17:07:49 +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>AIR SQLite Optimization tricks</title>
		<link>http://www.zedia.net/2009/air-sqlite-optimization-tricks/</link>
		<comments>http://www.zedia.net/2009/air-sqlite-optimization-tricks/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 22:10:48 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[SQLStatement]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=343</guid>
		<description><![CDATA[I have been playing with AIR lately and I have mostly been optimizing the interactions between the application and its SQLite database because that&#8217;s where I noticed the biggest speed impediment. Having a PHP/MySQL background, some of these tricks were not so obvious to me. Most of what I will list here come from this [...]]]></description>
			<content:encoded><![CDATA[<p>I have been playing with AIR lately and I have mostly been optimizing the interactions between the application and its SQLite database because that&#8217;s where I noticed the biggest speed impediment. Having a PHP/MySQL background, some of these tricks were not so obvious to me. Most of what I will list here come from this <a title="Improving database performance" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7d47.html" target="_blank">help page from Adobe</a>, but I thought I would give more example since some weren&#8217;t so clear to me at first.</p>
<p><strong>Specify column names in a SELECT or INSERT statement. </strong></p>
<p>Well this is not specific to AIR, but it is still really easy to implement so I thought it was worth mentioning. So in a SELECT Statement list the column name you want instead of using the *. Even if you need all the columns, list them all, it&#8217;s going to execute faster.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> selectStmt:SQLStatement = <span style="color: #000000; font-weight: bold;">new</span> SQLStatement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//so instead of doing this:</span>
selectStmt.<span style="color: #0066CC;">text</span> =  <span style="color: #ff0000;">&quot;SELECT * FROM myTable&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//do this:</span>
selectStmt.<span style="color: #0066CC;">text</span> =  <span style="color: #ff0000;">&quot;SELECT columnName1, columnName2 FROM myTable&quot;</span>;</pre></div></div>

<p><strong> Always write the name of the database when you write the name of a table</strong></p>
<p>I don&#8217;t know just how much speed gain this is suppoed to give but it is pretty easy to do so I suggest doing this all the time. Now, at first I was confused by  how I could find the name of the database I wanted to do a query on. It happens that most of the time, the name of your database is just going to be &#8220;main&#8221;. This can change when you use the attach method of a SQLConnection to add multiple database to a connection to do a query on tables from different database. When you attach a new database you will have to provide a name (you can choose what you want) and the first database will still be called &#8220;main&#8221;</p>
<p>So from my previous example, if we follow this advise, it will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> selectStmt:SQLStatement = <span style="color: #000000; font-weight: bold;">new</span> SQLStatement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
selectStmt.<span style="color: #0066CC;">text</span> =  <span style="color: #ff0000;">&quot;SELECT columnName1, columnName2 FROM main.myTable&quot;</span>;
<span style="color: #808080; font-style: italic;">//main.myTable instead of just myTable</span></pre></div></div>

<p><strong>Parametrize your statements</strong></p>
<p>This is something I was completely new to. From my PHP/MySQL experience here is what I would have written for an UPDATE statement with a WHERE clause:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">_updateStmt = <span style="color: #000000; font-weight: bold;">new</span> SQLStatement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_updateStmt.<span style="color: #006600;">sqlConnection</span> = _conn;
_updateStmt.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;UPDATE main.myTable SET statusF=&quot;</span> + currentStatus + <span style="color: #ff0000;">&quot;  WHERE keyId=&quot;</span> + currentId;
_updateStmt.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This is ok if you will use that statement only once in your application, but if you are going to use it multiple times like in a for loop, you&#8217;d better use a parametrized statement. It seems like a costly operation is preparing the SQLStatement and a statement is prepared everytime you change its text property. Now here is how you would parametrize the previous example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">_updateStmt = <span style="color: #000000; font-weight: bold;">new</span> SQLStatement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_updateStmt.<span style="color: #006600;">sqlConnection</span> = _conn;
_updateStmt.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;UPDATE main.myTable SET statusF=@STATUS  WHERE keyId=@ID&quot;</span>;
_updateStmt.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;@STATUS&quot;</span><span style="color: #66cc66;">&#93;</span> = currentStatus;
_updateStmt.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;@ID&quot;</span><span style="color: #66cc66;">&#93;</span> = currentId;
_updateStmt.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Again, just like this it doesn&#8217;t save much, it starts to make sense in a for loop_updateStmt = new SQLStatement();</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">_updateStmt.<span style="color: #006600;">sqlConnection</span> = _conn;
_updateStmt.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;UPDATE main.myTable SET statusF=@STATUS  WHERE keyId=@ID&quot;</span>;
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:uint = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> currentArray.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  _updateStmt.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;@STATUS&quot;</span><span style="color: #66cc66;">&#93;</span> = currentArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">status</span>;
  _updateStmt.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;@ID&quot;</span><span style="color: #66cc66;">&#93;</span> = currentArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span>;
  _updateStmt.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><strong>Use Transactions </strong></p>
<p>This is probably the biggest optimisation you can do. The principle of a transaction is that instead of executing all your statement separately, it will execute them all at the same time. Refer to the previously linked <a title="Improving database performance" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7d47.html" target="_blank">help file from Adobe</a> for more information on this. Transactions are best suited for query that don&#8217;t need results like INSERT and UPDATE statements. Transactions are easy to implement:_updateStmt = new SQLStatement();</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">_updateStmt.<span style="color: #006600;">sqlConnection</span> = _conn;
_updateStmt.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;UPDATE main.myTable SET statusF=@STATUS  WHERE keyId=@ID&quot;</span>;
&nbsp;
_conn.<span style="color: #006600;">begin</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//_conn is a SQLConnection, I didn't take the time to write the code for it, but this is where the magic happens</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:uint = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> currentArray.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  _updateStmt.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;@STATUS&quot;</span><span style="color: #66cc66;">&#93;</span> = currentArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">status</span>;
  _updateStmt.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;@ID&quot;</span><span style="color: #66cc66;">&#93;</span> = currentArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span>;
  _updateStmt.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
_conn.<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Only when the application will run the commit method will all the SQLStatement will be executed.</p>
<p>This is all the SQLite optimization tricks I have for now, but I still have to investigate indexes which might also help speed up queries.</p>
<div id="facebook_like"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.zedia.net%2F2009%2Fair-sqlite-optimization-tricks%2F&amp;layout=standard&amp;show-faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:550px; height:70px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2009/air-sqlite-optimization-tricks/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
