<?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>Dave Rowe's Blog &#187; Database Design</title>
	<atom:link href="http://blog.roweware.com/category/development/database-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.roweware.com</link>
	<description>Ramblings about things I think I know...</description>
	<lastBuildDate>Tue, 18 Jan 2011 21:28:50 +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>MySQL Query Optimizer Tweak</title>
		<link>http://blog.roweware.com/2010/07/14/mysql-query-optimizer-tweak/</link>
		<comments>http://blog.roweware.com/2010/07/14/mysql-query-optimizer-tweak/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 17:40:09 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=215</guid>
		<description><![CDATA[I recently encountered a situation where I had a query that was built with quite a few joins (~8), and I found that the query was taking a bit longer than I expected, especially for the number of rows to look at / return.  With each join, you add in more complexity for MySQL to [...]]]></description>
			<content:encoded><![CDATA[<p>I recently encountered a situation where I had a query that was built with quite a few joins (~8), and I found that the query was taking a bit longer than I expected, especially for the number of rows to look at / return.  With each join, you add in more complexity for MySQL to handle in how to best utilize indexes, etc.</p>
<p><span id="more-215"></span></p>
<p>I found that tweaking the &#8216;<strong>optimizer_search_depth</strong>&#8216; parameter (in my.cnf) for MySQL significantly sped up the query, as it told MySQL to only go so far in finding the best way to execute the query.</p>
<p>You can read more about it here: <a href="http://dev.mysql.com/doc/refman/5.0/en/controlling-optimizer.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/controlling-optimizer.html</a></p>
<p>I&#8217;d recommend tweaking this variable to find the best level for your worst query.  For simple queries, they should be unaffected since MySQL doesn&#8217;t need to do much analysis on the best execution plan.  As an example, I&#8217;ve set the value to &#8217;3&#8242;, and things are just fine.</p>
<p>To determine if you need this tweak, you can login to the MySQL console while a long-running process is executing and execute &#8216;<strong>SHOW PROCESSLIST\G</strong>&#8216;.  If you see a query in &#8216;statistics&#8217; state for more than a few seconds, try tweaking the value, and re-running the process.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2010/07/14/mysql-query-optimizer-tweak/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Surrogates</title>
		<link>http://blog.roweware.com/2009/12/23/surrogates/</link>
		<comments>http://blog.roweware.com/2009/12/23/surrogates/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 20:14:44 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[keys]]></category>
		<category><![CDATA[linking table]]></category>
		<category><![CDATA[many to many]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=150</guid>
		<description><![CDATA[In my previous post, I used a key style that is open to debate and has been for many years amongst DB folks. The idea of every table having a surrogate key, regardless of the purpose of the table. This says, that for any record in the table I have a single column that acts [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I used a key style that is open to debate and has been for many years amongst DB folks.  The idea of every table having a surrogate key, regardless of the purpose of the table.  This says, that for any record in the table I have a single column that acts as the primary key.  Given a many-to-many relationship, using a surrogate key on the linking table allows me to describe the relationship in terms of objects and how they&#8217;re represented.  As shown in the below diagram &#8211; each user may have many user_role instances, which are tied to a single role instance.  This makes the lives of ORMs much easier since you can create objects for the linking table, which has a simple key to reference.</p>
<p style="text-align: center;"><a href="http://blog.roweware.com/wp-content/uploads/2009/12/Surrogate.png"><img class="size-full wp-image-152 aligncenter" title="Surrogate" src="http://blog.roweware.com/wp-content/uploads/2009/12/Surrogate.png" alt="" width="414" height="124" /></a></p>
<p>The ORM then has a User, UserRole, and Role object to use in accessing these tables and adding / removing relationships with ease, since it only needs to worry about the single surrogate &#8216;id&#8217; key on each table.  In the linking table (as a design concern), one should place a Unique Index on the user_id/role_id column combination.</p>
<p>The other option is using a composite candidate key.  I may have the specific terminology wrong, but the idea is that instead of the single surrogate key to identify a unique record in the linking table, you use a design like the diagram below, which combines the columns that are foreign keys to their respective tables to create the primary key.  The combination of the columns creates a unique identifying key.  The difficulty emerges with ORMs attempting to create objects out this design, and attempting to correctly generate the SQL required to make updates / deletes, etc, using each member of the composite key.</p>
<p style="text-align: center;"><a href="http://blog.roweware.com/wp-content/uploads/2009/12/NoSurrogate.png"><img class="size-full wp-image-151 aligncenter" title="NoSurrogate" src="http://blog.roweware.com/wp-content/uploads/2009/12/NoSurrogate.png" alt="" width="399" height="108" /></a></p>
<p>Personally speaking, I&#8217;m a fan of the surrogate key approach, but I&#8217;ve worked with both.  I won&#8217;t discuss the performance impacts of either design, since I don&#8217;t have nearly the research base to accurately describe it.  But, using simple integer based keys, the difference should be low.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/12/23/surrogates/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Multi-Table Delete</title>
		<link>http://blog.roweware.com/2009/12/08/mysql-multi-table-delete/</link>
		<comments>http://blog.roweware.com/2009/12/08/mysql-multi-table-delete/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 19:27:20 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[multi-delete]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=142</guid>
		<description><![CDATA[MySQL provides cascading updates / deletes with the relationships, but I tend not to use them, specifically because I want to control just how far these updates and deletes cascade!  But, given a situation where I have a design similar to this: I would like to be able to remove a single Foo, without having [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL provides cascading updates / deletes with the relationships, but I tend not to use them, specifically because I want to control just how far these updates and deletes cascade!  But, given a situation where I have a design similar to this:</p>
<p style="text-align: center;"><img class="size-full wp-image-143 aligncenter" title="multi-delete" src="http://blog.roweware.com/wp-content/uploads/2009/12/multi-delete.png" alt="multi-delete" width="270" height="255" /></p>
<p>I would like to be able to remove a single Foo, without having to first remove all the associated data from the other 3 tables.  Or, I know the ID of the Foo I want to remove, so instead of running multiple queries to find the associated rows, lets just knock it out with a single, multi-table delete!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p142code2'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1422"><td class="code" id="p142code2"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DELETE</span>
  db<span style="color: #66cc66;">.</span>zap <span style="color: #993333; font-weight: bold;">as</span> z<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>baz <span style="color: #993333; font-weight: bold;">as</span> bz<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>bar <span style="color: #993333; font-weight: bold;">as</span> z<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>foo <span style="color: #993333; font-weight: bold;">as</span> f
<span style="color: #993333; font-weight: bold;">FROM</span>
  db<span style="color: #66cc66;">.</span>zap <span style="color: #993333; font-weight: bold;">as</span> b<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>baz <span style="color: #993333; font-weight: bold;">as</span> bz<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>bar <span style="color: #993333; font-weight: bold;">as</span> z<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>foo <span style="color: #993333; font-weight: bold;">as</span> f
<span style="color: #993333; font-weight: bold;">WHERE</span>
  b<span style="color: #66cc66;">.</span>baz_id <span style="color: #66cc66;">=</span> bz<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  b<span style="color: #66cc66;">.</span>zap_id <span style="color: #66cc66;">=</span> z<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  bz<span style="color: #66cc66;">.</span>foo_id <span style="color: #66cc66;">=</span> f<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  z<span style="color: #66cc66;">.</span>foo_id <span style="color: #66cc66;">=</span> f<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  f<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> ?</pre></td></tr></table></div>

<p>This will then remove the rows associated with the single Foo record I&#8217;ve referenced, in one fell swoop.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/12/08/mysql-multi-table-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DB Design &#8211; ICD9 Data</title>
		<link>http://blog.roweware.com/2009/12/01/db-design-icd9-data/</link>
		<comments>http://blog.roweware.com/2009/12/01/db-design-icd9-data/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 23:02:48 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[ICD9]]></category>
		<category><![CDATA[medical]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=140</guid>
		<description><![CDATA[In today's Adventures in Database Design, we'll take a look at a relevant topic, ICD9 data.  ICD9 data is the diagnosis and procedure codes used by insurance companies to categorize, well, diagnoses and procedures to be determined / performed by medical professionals.  The design is simple, but very versatile, given the type of data we're storing.]]></description>
			<content:encoded><![CDATA[<p>ICD9 data are the diagnosis and procedure codes used by insurance companies to categorize, well, diagnoses and procedures to be determined / performed by medical professionals.  Typically, if you visit the doctor for an ailment and file insurance, your provider will list the reason for the visit, any diagnoses, and any procedures performed.  This is then sent to the insurance company for processing.  They can then use codes to indicate if the procedure is covered, as well as, (for example) determine if the diagnosis was a pre-existing condition.  The data is a simple hierarchical structure which is shown in the following diagram.</p>
<div id="attachment_139" class="wp-caption alignnone" style="width: 306px"><img class="size-medium wp-image-139" title="ICD9" src="http://blog.roweware.com/wp-content/uploads/2009/12/ICD9-296x300.png" alt="ERD for storing ICD9 data" width="296" height="300" /><p class="wp-caption-text">ERD for storing ICD9 data</p></div>
<p>We see that diagnoses can have sub-diagnoses, etc.  I used this simple structure, and added a qualifying column of &#8216;record_type&#8217; to indicate if the code listed was an actual diagnosis, or a section header.  Sections of diagnoses are part of the data, and can be used in searching the database.  This diagram offers a simple and quick design to handle the data given.</p>
<p>Comments are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/12/01/db-design-icd9-data/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adventures in Database Design</title>
		<link>http://blog.roweware.com/2009/11/23/adventures-in-database-design/</link>
		<comments>http://blog.roweware.com/2009/11/23/adventures-in-database-design/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 03:39:56 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[workbench]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=137</guid>
		<description><![CDATA[I&#8217;ve long neglected this blog, and the power behind it to discuss my feelings toward database design.  Proper database design is the backbone to a solid application.  Failing to correctly normalize tables and enforce business logic with foreign key relationships can cause undue headaches. What I&#8217;ll be doing is going through some simple applications, and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve long neglected this blog, and the power behind it to discuss my feelings toward database design.  Proper database design is the backbone to a solid application.  Failing to correctly normalize tables and enforce business logic with foreign key relationships can cause undue headaches.</p>
<p>What I&#8217;ll be doing is going through some simple applications, and start modeling the tables and relationships, and intersperse some commentary where useful.  The discussion depends on interaction, and I&#8217;m of the opinion that a &#8216;good&#8217; database designer can a) defend their design articulately and b) know when to concede a good point.</p>
<p>Database design is becoming organic.  Strict adherence to the normal forms isn&#8217;t required any longer.  Let me be clear though, denormalizing a table to make things easier for a developer is not a valid reason to denormalize.</p>
<p>The modeling tool I&#8217;ll be using for generating the images is <a title="MySQL Workbench" href="http://wb.mysql.com/" target="_blank">MySQL Workbench</a>.  Workbench is a very powerful tool, from the source of one of the most powerful (and used) databases today.  I cut my teeth on database design with DBDesigner4, whose creator went on to work for MySQL Workbench.</p>
<p>Stay tuned.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/11/23/adventures-in-database-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

