<?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>Christoffer Kjølbæk &#187; Optimizing</title>
	<atom:link href="http://ostehamster.dk/blog/index.php/tag/optimizing/feed/" rel="self" type="application/rss+xml" />
	<link>http://ostehamster.dk/blog</link>
	<description>I have a blog, therefore I am…</description>
	<lastBuildDate>Tue, 03 Jan 2012 22:37:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>What is an optimal object-oriented program with an underlying database?</title>
		<link>http://ostehamster.dk/blog/index.php/2010/04/26/what-is-an-optimal-object-oriented-program-with-an-underlying-database/</link>
		<comments>http://ostehamster.dk/blog/index.php/2010/04/26/what-is-an-optimal-object-oriented-program-with-an-underlying-database/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 14:18:30 +0000</pubDate>
		<dc:creator>Christoffer Kjølbæk</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Optimizing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[System Architecture]]></category>

		<guid isPermaLink="false">http://ostehamster.dk/blog/?p=875</guid>
		<description><![CDATA[Consider a very simple message board system consisting of a user and amessages table in the database. The user table would look like the following: +----+-------------+ &#124; id &#124; username &#124; +----+-------------+ &#124; 1 &#124; John Doe &#124; &#124; 2 &#124; Jane Doe &#124; &#124; 3 &#124; Richard Roe &#124; +----+-------------+ and the messages would [...]]]></description>
			<content:encoded><![CDATA[<p>Consider a very simple message board system consisting of a user and amessages table in the database. The user table would look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="pre" style="font-family:monospace;">+----+-------------+
| id | username    |
+----+-------------+
|  1 | John Doe    | 
|  2 | Jane Doe    | 
|  3 | Richard Roe | 
+----+-------------+</pre></div></div>

<p>and the messages would look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="pre" style="font-family:monospace;">+----+-------------+---------+--------------------------------------------------------------------------+
| id | in_reply_to | user_id | message                                                                  |
+----+-------------+---------+--------------------------------------------------------------------------+
|  1 |           0 |       2 | What is an optimal object-oriented program with an underlaying database? | 
|  2 |           0 |       1 | What does cohesion mean?                                                 | 
|  3 |           1 |       3 | Donno, check ostehamster.dk                                              | 
|  4 |           2 |       1 | Try http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29         | 
+----+-------------+---------+--------------------------------------------------------------------------+</pre></div></div>

<p>Now, if we want to show the message with id 1 and all answers, together with the name of the user posting it, it is very simple</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> u<span style="color: #66cc66;">.</span>username<span style="color: #66cc66;">,</span> m<span style="color: #66cc66;">.</span>message
<span style="color: #993333; font-weight: bold;">FROM</span> messages m
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> user u <span style="color: #993333; font-weight: bold;">ON</span> u<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> m<span style="color: #66cc66;">.</span>user_id
<span style="color: #993333; font-weight: bold;">WHERE</span> m<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">OR</span> m<span style="color: #66cc66;">.</span>in_reply_to <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> 
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> m<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">ASC</span>;</pre></div></div>

<p>This results in :</p>

<div class="wp_syntax"><div class="code"><pre class="pre" style="font-family:monospace;">+-------------+--------------------------------------------------------------------------+
| username    | message                                                                  |
+-------------+--------------------------------------------------------------------------+
| Jane Doe    | What is an optimal object-oriented program with an underlaying database? | 
| Richard Roe | Donno, check ostehamster.dk                                              | 
+-------------+--------------------------------------------------------------------------+</pre></div></div>

<p>Very simple!</p>
<p><strong>Object-oriented</strong><br />
Now, if we want to make this object-oriented with high cohesion, a possibility for inheritance and all the other buzzwords, we need a User and a MessageBoard class.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> User <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">int</span> user_id<span style="color: #008080;">;</span>
   std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> username<span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
      User<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> user_id<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> get_username<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">int</span> get_user_id<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">void</span> load_user<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> user_id<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> MessageBoard <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">int</span> msg_id<span style="color: #008080;">;</span>
   std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> messages<span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
      MessageBoard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> msg_id<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      std_string print<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">void</span> load_messageboard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> msg_id<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Actually, it might be more correct to have a Message class too. The Message class would handle each unique message, and the MessageBoard would then link a number of Message objects together. However, to make it more simple, we only use the MessageBoard class.</p>
<p>If the User and MessageBoard should have high cohesion, the classes should be wrappers for the underlying tables, and the above SQL query with the LEFT JOIN cannot be used. It should be possible for the <em>User</em> developers to change the database design and the internal functionality of the User class, without the <em>MessageBoard</em> developers needing to make any changes, as long as the User API does not change.</p>
<p>The User class could have a function returning the "LEFT JOIN" statement, which could then be put into the query made by the MessageBoard class. But in my opinion this will result in lower cohesion, because users and messages could be stored in different databases, hosts and even different formats.</p>
<p>If the tables can not be joined in any way, the <em>print</em> function could be implemented like:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   conn <span style="color: #000080;">=</span> mysql_init<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>mysql_real_connect<span style="color: #008000;">&#40;</span>conn, server, user, password, database, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      mysql_query<span style="color: #008000;">&#40;</span>conn, <span style="color: #FF0000;">&quot;SELECT user_id, message FROM messages m WHERE m.id = 1 OR m.in_reply_to = 1  ORDER BY m.id ASC;&quot;</span><span style="color: #008000;">&#41;</span>
&nbsp;
      res <span style="color: #000080;">=</span> mysql_use_result<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>row <span style="color: #000080;">=</span> mysql_fetch_row<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
         User user <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> User<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">atoi</span><span style="color: #008000;">&#40;</span>row<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;User: %s, Message: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, user.<span style="color: #007788;">get_username</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, row<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      mysql_free_result<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   mysql_close<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>However, for each line in the message board this will make a new instance of the User class, which again will make a connection and a query to the database. For a message board it will not be a lot of calls, but in bigger and more complex systems it can be a serious bottleneck.</p>
<p><strong>Caching objects</strong><br />
This could be solved by having a User cache, like this static function:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> User<span style="color: #000040;">*</span> get_cached_user<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> user_id<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">static</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span> User<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span> cache<span style="color: #008080;">;</span>
&nbsp;
   vector<span style="color: #000080;">&lt;</span> User<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span> <span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it<span style="color: #008080;">;</span>
   <span style="color: #ff0000; font-style: italic;">/* Is the user chached? */</span>
   <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> it<span style="color: #000080;">=</span>cache.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> it <span style="color: #000080;">&lt;</span> cache.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">get_user_id</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> user_id<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
         <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #ff0000; font-style: italic;">/* Generate and cache user */</span>
   cache.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> User<span style="color: #008000;">&#40;</span>user_id<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">return</span> cache.<span style="color: #007788;">back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This will decrease the number of queries to the database. However it might still result in a lot of calls to the database compared to the "LEFT JOIN" query.</p>
<p>The last option I can think of is to make a static function in the User class called </pre>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">User<span style="color: #000040;">*</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> get_multiple_user<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ids<span style="color: #008000;">&#41;</span></pre></div></div>

<p> The function takes an array of user ids, fetches all user data from the database in one query, and then generates an array of User objects pointers. This caching method will work like the one above, but all users are found using one database call, and thereby it decreases the total number of database queries to two.</p>
<p><strong>But what is the right solution?</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://ostehamster.dk/blog/index.php/2010/04/26/what-is-an-optimal-object-oriented-program-with-an-underlying-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

