<?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>Always Get Better &#187; SQL</title>
	<atom:link href="http://www.alwaysgetbetter.com/blog/category/general-programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alwaysgetbetter.com/blog</link>
	<description>Never stop looking for ways to improve</description>
	<lastBuildDate>Mon, 30 Jan 2012 12:00:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL Pulled From Mac OS X Lion</title>
		<link>http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 13:41:47 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=439</guid>
		<description><![CDATA[Apple-based LAMP developers be warned: the new version of OS X does not include MySQL, which was formerly part of the developer tools shipped with the operating system. In its place look for deliciously Oracle-free PostgreSQL. Of course, developers can and will continue to download MySQL and install it themselves, but the out-of-box experience moving [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/' rel='bookmark' title='Drizzle &#8211; MySQL for the Cloud'>Drizzle &#8211; MySQL for the Cloud</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Apple-based LAMP developers be warned: the new version of OS X does not include MySQL, which was formerly part of the developer tools shipped with the operating system. In its place look for deliciously Oracle-free PostgreSQL. Of course, developers can and will continue to download MySQL and install it themselves, but the out-of-box experience moving forward will be with PostgreSQL.</p>
<p>Although it is still an extremely popular database, Oracle&#8217;s presence in the MySQL world has put a chill over business users considering using the product as the backbone of their data solutions. <a href="http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/">Other databases with similar purposes exists</a> but none have the deep community boasted by PostgreSQL.</p>
<div style="width:468px;margin:0 auto">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-0410364841759590";
/* Homepage, 468x60 ads, After posts, created 12/19/08 */
google_ad_slot = "4210204644";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/' rel='bookmark' title='Drizzle &#8211; MySQL for the Cloud'>Drizzle &#8211; MySQL for the Cloud</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Migrations</title>
		<link>http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 01:39:32 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Employment]]></category>
		<category><![CDATA[planning]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=404</guid>
		<description><![CDATA[Maintaining database schemas across development environments (especially in teams) and in production can be a real nightmare. Fortunately there are a number of solutions which make database management easier. Migrations This can be done manually or automatically. As database changes are made by developers, scripts are generated which can be run against a master database [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Maintaining database schemas across development environments (especially in teams) and in production can be a real nightmare. Fortunately there are a number of solutions which make database management easier.</p>
<p><strong>Migrations</strong><br />
This can be done manually or automatically. As database changes are made by developers, scripts are generated which can be run against a master database to bring it in line with the developer&#8217;s version. The most basic way to accomplish this is by writing a script manually, but frameworks like Django and Rails have built-in migration tools which manage this process. Rails in particular allows developers to move back and forth between snapshots of database schemas.</p>
<p><strong>Evolutions</strong><br />
Evolutionary systems detect database schema changes against program code definitions. As of April 2011, <a href="http://www.alwaysgetbetter.com/blog/2011/04/11/play-framework-saves-world/">Play Framework supports Evolutions</a>.</p>
<p><strong>Schema Versions</strong><br />
Microsoft SQL Server supports schema versions; wherein the underlying data remains the same, but multiple versions of the database schema rest on top and can be accessed simultaneously. This keeps older versions of the application or supporting clients working with the existing data set.</p>
<p><strong>Keep Tracking&#8230;</strong><br />
Managing database changes can be a challenge for organizations of any size. The correct tool depends on a wide range of factors including your project size, number of team members, release schedule.</p>
<p>What kind of tools and processes do you use to manage database changes?</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Memcache with MySQL</title>
		<link>http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 00:12:35 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=395</guid>
		<description><![CDATA[MySQL+Memcache have been bedfellows for awhile and at this point are the de facto standard for highly-available, scalable websites. Even with other SQL and NoSQL solutions starting to become popular, this pair holds on as the winner for LAMP programmers. Is the complexity of working with this technology pair worth the investment? Read vs Write [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/' rel='bookmark' title='Drizzle &#8211; MySQL for the Cloud'>Drizzle &#8211; MySQL for the Cloud</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/' rel='bookmark' title='MySQL Pulled From Mac OS X Lion'>MySQL Pulled From Mac OS X Lion</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>MySQL+Memcache have been bedfellows for awhile and at this point are the de facto standard for highly-available, scalable websites. Even with <a href="http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/">other SQL</a> and NoSQL solutions starting to become popular, this pair holds on as the winner for LAMP programmers. Is the complexity of working with this technology pair worth the investment?</p>
<p><strong>Read vs Write</strong><br />
Traditional relational databases place the burden of computation on read operations. In mainframe environments with powerful servers and relatively few users, this made sense. Database normalization prevents redundancy, and data can be joined together when needed to produce the desired results.</p>
<p>In a web application with 1,000,000 users, the normalized transactional model does not perform. Generally speaking it is way faster to make two queries to a small subset of data rather than attempt expensive joins in a client-facing web site.</p>
<p>Enter memcache: by storing the result of our SQL queries in memory, we improve the speed of subsequent requests by pulling the data from memory as well as avoiding a hit to the database entirely, freeing it to process urgent or real-time requests.</p>
<p><strong>Anatomy of an SQL Query</strong><br />
When we run an SQL query, we are actually asking the server to perform a lot of work:</p>
<ol>
<li>Break down the query into object references: The DBMS needs to understand which tables, columns, and filters you are using by tokenizing your SQL (by breaking out names from the keywords like SELECT, FROM, WHERE).</li>
<li>Identify which indexes (if any) are most relevant to the data: This is harder with more complex queries which must be broken down or which depend on outer tables for their values.</li>
<li>Read the source tables from the hard drive: Most DBMS implementations include some kind of memory caching which partially avoids this expensive read step, but some disk IO is a normal part of operation
<li>Join Columns: If we specify a join, especially a LEFT or RIGHT join, the DBMS has to create a pseudo table from the joined sources in memory before it can do any additional processing.</li>
<li>Sub-selects: Any sub-select statements need to be processed. Depending on how the statement was written, this needs to be done for every row returned in the result set.</li>
<li>Filter and sort: Anything in the &#8216;WHERE&#8217; clause needs to be filtered out of the result set. This is where we are going to start seeing performance improvements by narrowing our result set.</li>
<li>Aggregation: Once the database has its final result set it can do all of the aggregation we ask of it, both calculations and grouping</li>
</ol>
<p>As we can imagine, this can be a time-consuming process. If it is repeated thousands of times in a short period, we will see significant performance loss.</p>
<p><strong>Anatomy of a Cache Request</strong><br />
By contrast, when we perform a request for data from a cache like memcache, we do this:</p>
<ol>
<li>Check the index for the presence of the supplied key</li>
<li>If a result exists, return it</li>
</ol>
<p>In the case of memcache, this happens entirely in memory with no hit to the database whatsoever, resulting in a blindingly fast result set.</p>
<p><strong>Speed Over Persistance</strong><br />
The reason <a href="<a href="http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/">&#8220;>memcache and MySQL work well</a> as a pair is because they provide the tools needed to have reliable, persistent transactional storage (through MySQL) along with lightning fast data retrieval (through Memcache) especially for rarely-changing results.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/' rel='bookmark' title='Drizzle &#8211; MySQL for the Cloud'>Drizzle &#8211; MySQL for the Cloud</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/' rel='bookmark' title='MySQL Pulled From Mac OS X Lion'>MySQL Pulled From Mac OS X Lion</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Drizzle &#8211; MySQL for the Cloud</title>
		<link>http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 02:14:45 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Drizzle]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[Percona]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=356</guid>
		<description><![CDATA[photo credit: nimishgogri Drizzle &#8211; a lightweight fork of MySQL &#8211; has reached general availability. Drizzle&#8217;s design goals are to create a highly performant and module database engine tailored for cloud computing. Some of the interesting features this database has to offer are: No Views, Triggers, or Stored Procedures &#8211; I consider this a huge [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/' rel='bookmark' title='MySQL Pulled From Mac OS X Lion'>MySQL Pulled From Mac OS X Lion</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class=alignright><a href="http://www.flickr.com/photos/25146185@N04/5162821107/" title="No individual raindrop ever considers itself responsible for the flood" target="_blank"><img src="http://farm5.static.flickr.com/4124/5162821107_312a59bd97_m.jpg" alt="No individual raindrop ever considers itself responsible for the flood" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://www.alwaysgetbetter.com/blog/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/25146185@N04/5162821107/" title="nimishgogri" target="_blank">nimishgogri</a></small></div>
<p>Drizzle &#8211; a lightweight fork of MySQL &#8211; has reached general availability. Drizzle&#8217;s design goals are to create a highly performant and module database engine tailored for cloud computing.</p>
<p>Some of the interesting features this database has to offer are:</p>
<ul>
<li><strong>No Views, Triggers, or Stored Procedures</strong> &#8211; I consider this a huge advantage. Stored Procedures were once a good thing when query optimizers were all but non-existant, but modern database systems perform this task extremely efficiently. Using stored procedures adds a second level of APIs to your application for what I would consider to be a rather dubious potential security benefit.</li>
<li><strong>Sharding</strong> &#8211; The client protocol is designed to decide which database server to target based upon a hashing key. This is similar to how memcached handles its linear horizontal scaling &#8211; leaving this calculation to the client is a huge advantage to systems hosting a high number of concurrent visitors</li>
<li><strong>Gearman Support</strong> &#8211; This is a terrific tool for spreading workload across machines. Use Gearman to handle logging in Drizzle, keeping the actual DB server available for database work</li>
</ul>
<p><strong>How Does Drizzle Compare to Percona?</strong><br />
Percona is a high-performance build of MySQL which promises to offer better performance than &#8220;stock&#8221; MySQL. It is built from a branch of publicly-available MySQL source code and enhanced by the folks at Percona. Percona maintains the same functionality as &#8220;real&#8221; MySQL, but attempts to achieve faster speeds.</p>
<p><strong>How Does Drizzle Compare to MariaDB?</strong><br />
MariaDB is a different database engine started by the original creator of MySQL intended to replace MySQL using non-Oracle licensing. Anyone concerned with Oracle&#8217;s development practices or plans for the future of MySQL should consider switching to MariaDB. MariaDB implements all of the existing MySQL functionality plus more and provides a drop-in replacement for MySQL.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/' rel='bookmark' title='MySQL Pulled From Mac OS X Lion'>MySQL Pulled From Mac OS X Lion</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2011/03/24/drizzle-mysql-cloud/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>VISTA: How to fix SQL Server Express Error &#8211; CREATE DATABASE permission denied in database &#8216;master&#8217;</title>
		<link>http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 13:22:16 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Windows Vista]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=265</guid>
		<description><![CDATA[If you&#8217;re using SQL Server Management Studio Express under Windows Vista and see either of these errors: CREATE DATABASE permission denied in database 'master' or The database [Name] is not accessible. (Microsoft.SqlServer.Express.ObjectExplorer) Here&#8217;s the fix: Close SQL Server Management Studio Express Open your start menu and locate that program. Right-click on the Management Studio and [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/' rel='bookmark' title='Database Migrations'>Database Migrations</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/26/protect-ssh-server-rsa-keys/' rel='bookmark' title='Protect Your SSH Server with RSA Keys'>Protect Your SSH Server with RSA Keys</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using SQL Server Management Studio Express under Windows Vista and see either of these errors:</p>
<p><code>CREATE DATABASE permission denied in database 'master'</code></p>
<p>or</p>
<p><code>The database [Name] is not accessible. (Microsoft.SqlServer.Express.ObjectExplorer)</code></p>
<p>Here&#8217;s the fix:</p>
<ol>
<li>Close SQL Server Management Studio Express</li>
<li>Open your start menu and locate that program.</li>
<li>Right-click on the Management Studio and choose &#8216;Run as Administrator&#8217;</li>
<li>Fixed!</li>
</ol>
<p>I swear the simplest solutions can be the hardest to find &#8211; hopefully this saves someone (or my forgetful self!) some aggravation.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/' rel='bookmark' title='Database Migrations'>Database Migrations</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/26/protect-ssh-server-rsa-keys/' rel='bookmark' title='Protect Your SSH Server with RSA Keys'>Protect Your SSH Server with RSA Keys</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2009/11/05/vista-fix-sql-server-express-error-create-database-permission-denied-database-master/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Optimize SQL Queries by Using Aliases</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/06/30/optimize-sql-queries-by-using-aliases/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/06/30/optimize-sql-queries-by-using-aliases/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 02:22:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[efficiency]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=35</guid>
		<description><![CDATA[When joining tables in SQL, we often use aliases to shorten table names. Consider this query joining order lines (details) with orders inside the database: The above may work, but behind the scenes the database&#8217;s query analyzer has to associate each of the selected columns with their respective tables. Although this is a fast process, [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When joining tables in SQL, we often use aliases to shorten table names.  Consider this query joining order lines (details) with orders inside the database:</p>
<p>SELECT O.OrderID, FirstName, LastName, ItemName, Quantity<br />
FROM Orders O<br />
INNER JOIN OrderDetails OD<br />
ON O.OrderID = OD.OrderID;</p>
<p>The above may work, but behind the scenes the database&#8217;s query analyzer has to associate each of the selected columns with their respective tables.  Although this is a fast process, it can be skipped entirely by simply fleshing out the query like this:</p>
<p>SELECT O.OrderID, O.FirstName, O.LastName, OD.ItemName, OD.Quantity<br />
FROM Orders O<br />
INNER JOIN OrderDetails OD<br />
ON O.OrderID = OD.OrderID;</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/06/30/optimize-sql-queries-by-using-aliases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SELECT TOP N in Oracle</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/04/11/select-top-n-in-oracle/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/04/11/select-top-n-in-oracle/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 16:30:54 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=20</guid>
		<description><![CDATA[Being used to SQL Server, I get messed up when moving to Oracle. For reference, here are equivalent top N queries in both environments: SQL Server: Oracle: No related posts.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Being used to SQL Server, I get messed up when moving to Oracle.  For reference, here are equivalent top N queries in both environments:</p>
<p><strong>SQL Server</strong>:</p>
<p>SELECT TOP 10 book_name, price*sales<br />
FROM tblBooks;</p>
<p><strong>Oracle</strong>:</p>
<p>SELECT book_name, price*sales<br />
FROM tblBooks<br />
WHERE ROWNUM <= 10;</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/04/11/select-top-n-in-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to SUM Bit Fields in SQL</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 12:49:55 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/</guid>
		<description><![CDATA[By default, SQL Server doesn&#8217;t allow an operation like this: In order to achieve this result, you must first convert the bit column to a numeric type: This counts the number of times the bit is true. If you want to get the flip-side of that to see how many times the bit is false, [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>By default, SQL Server doesn&#8217;t allow an operation like this:</p>
<p>SELECT SUM(blnBitColumn) FROM tblTable;</p>
<p>In order to achieve this result, you must first convert the bit column to a numeric type:</p>
<p>SELECT SUM(CONVERT(int,blnBitColumn)) FROM tblTable;</p>
<p>This counts the number of times the bit is <strong>true</strong>.</p>
<p>If you want to get the flip-side of that to see how many times the bit is <strong>false</strong>, just subtract the total number of bits from the positive:</p>
<p>SELECT COUNT(blnBitColumn)-SUM(CONVERT(int,blnBitColumn)) FROM tblTable;</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/04/06/how-to-sum-bit-fields-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Connections in ASP.NET &#8211; What you learned is WRONG!</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 19:22:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[interfaces]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/</guid>
		<description><![CDATA[When we learn how to open and use a database connection with ASP.NET, as with any other programming concept in any other programming language, the simplified version used to explain what’s going on is not truly representative of the quality professional code we will one day be expected to write. Opening and Closing Connections Case [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/' rel='bookmark' title='Database Migrations'>Database Migrations</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When we learn how to open and use a database connection with ASP.NET, as with any other programming concept in any other programming language, the simplified version used to explain what’s going on is not truly representative of the quality professional code we will one day be expected to write.<br />
Opening and Closing Connections</p>
<p>Case in point: managing of sql database connection resources. How many of us learned to write something like this:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Close the database connection<br />
conn.Close();<br />
</code></p>
<p>Sure it’s easy to follow, but if you deploy that on a moderately busy server you are going to make your client very unhappy.</p>
<h2>Dispose Resources</h2>
<p>SQLConnection and SQLCommand objects reference unmanaged resources, meaning the C# garbage collector has no framework knowledge about your object. Since these classes both implement the disposable interface it is important to call the Dispose() method in order to correctly free your application’s used memory.</p>
<p>So our code gets updated to look like this:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Dispose of the command<br />
cmd.Dispose();</p>
<p>// Close the database connection<br />
conn.Close();</p>
<p>// Dispose of the connection object<br />
conn.Dispose();<br />
</code></p>
<h2>Trap for Errors</h2>
<p>What happens if there’s a problem, and your code fails to complete? If your application crashes before your objects are disposed, you are left with the same effect as if you had never disposed your objects at all!</p>
<p>Fortunately, C# includes the try … finally reserved words. If anything within the try { } block fails, the finally { } still executes. We can easily apply this to our program code:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>try<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>try<br />
{<br />
// Execute the command<br />
cmd.ExecuteNonQuery();<br />
}<br />
finally<br />
{<br />
// Dispose of the command<br />
cmd.Dispose();<br />
}</p>
<p>// Close the database connection<br />
conn.Close();<br />
}<br />
finally<br />
{<br />
// Dispose of the connection object<br />
conn.Dispose();<br />
}<br />
</code></p>
<p>For my own part, I prefer the using keyword. We can include a using call anywhere we would ordinarily use a disposal object. When the code is compiled, it behaves the same as try … catch, but leaves our program code much more readable.</p>
<p>Even better, we don’t even have to bother calling Dispose() because it does it for us!</p>
<p><code><br />
// Create a new SQL Connection object<br />
using ( SqlConnection conn = new SqlConnection( connectionString ) )<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
using ( SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn ) )<br />
{<br />
// Execute the command<br />
cmd.ExecuteNonQuery();<br />
}</p>
<p>// Close the database connection<br />
conn.Close();<br />
}<br />
</code></p>
<p>Slick.</p>
<h2>Open Late, Close Early (like a bank)</h2>
<p>There is one more thing I would add to this. Creating objects in memory takes time. Although it happens in fractions of a second too fast to be detectable by us, it’s important not to waste processing time wherever possible.</p>
<p>Whenever we Open() a database connection, we expect to use the database right away. If we then create an SqlCommand, we’re wasting the open connection’s time. It’s as if we pick up the phone and listen to the dial tone while we then flip through the white pages looking for the number we want to call.</p>
<p>Let’s change our example code so we will now Open() at the last possible opportunity, and Close() right away when we’ve made our call:</p>
<p><code><br />
// Create a new SQL Connection object<br />
using ( SqlConnection conn = new SqlConnection( connectionString ) )<br />
{<br />
// Create a new SQL Command<br />
using ( SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn ) )<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Close the connection to the database<br />
conn.Close();<br />
}<br />
}<br />
</code></p>
<p>In the end, the program code we wrote is very similar to the newbie code we started with. However, we’re now protecting our system from memory leaks, and we’re protecting our database from wasted clock cycles. Our code is easy to read and stable.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/' rel='bookmark' title='Database Migrations'>Database Migrations</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Create Full Text Search Using mySQL</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 14:40:04 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[e-commerce]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/</guid>
		<description><![CDATA[Search is one of the most basic features visitors expect when they come to a web site. This is especially true in e-commerce where your ability to make a sale is directly related to your customer’s ability to find the product they’re looking for. Using Third-Party Solutions Many first-time site owner choose to go with [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/' rel='bookmark' title='MySQL Pulled From Mac OS X Lion'>MySQL Pulled From Mac OS X Lion</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Search is one of the most basic features visitors expect when they come to a web site.  This is especially true in e-commerce where your ability to make a sale is directly related to your customer’s ability to find the product they’re looking for.</p>
<h2>Using Third-Party Solutions</h2>
<p>Many first-time site owner choose to go with Google Custom Search because of its easy setup and because of Google’s incredible indexing reach and results.  I don’t like the standard edition of the Custom Search because of the branding – your search results advertise Google and provide links to competing content.  For an e-commerce site to lose control of such a critical function, the results can be costly.</p>
<h2>Don’t Give Up Control</h2>
<p>Especially in e-commerce, it is best to never give up control over any content.  Advanced users may choose to ignore your site’s search tool and use Google to get at your content anyway (via the site: directive) but in the general case there is great potential to keep selling useful products to your potential customers even while they search your site for other items.</p>
<p>Incorporating a decent search tool into a web site using PHP is dead simple.  All it involves is a database table with 3 or more rows and a little bit of an eye for layout.  Even if you don’t consider yourself a designer, having a look around other search engines will give you a feel for how results should display.</p>
<h2>Creating the Search Tables</h2>
<p>Let’s get started.  Our simplistic database table (PRODUCTS) will consist of the following columns:</p>
<table>
<tr>
<th>Column Name</th>
<th>Data Type</th>
<th>Description</th>
</tr>
<tr>
<td>intID</td>
<td>int</td>
<td>Product ID and Primary Key</td>
</tr>
<tr>
<td>vcrName</td>
<td>varchar(25)</td>
<td>Product Name</td>
</tr>
<tr>
<td>txtDescription</td>
<td>text</td>
<td>Product Description</td>
</tr>
<tr>
<td>vcrPhoto</td>
<td>varchar(40)</td>
<td>Path (URL) to product photo</td>
</tr>
</table>
<p>Obviously this is just a simplified example, but the product ID, name, description and photo should be enough for the purposes of our demonstration.</p>
<p>The SQL to create the table looks like this:</p>
<p>CREATE TABLE PRODUCTS<br />
(<br />
intID int auto_increment,<br />
vcrName varchar(25),<br />
txtDescription text,<br />
vcrPhoto varchar(40),<br />
CONSTRAINT PRODUCTS_pk PRIMARY KEY ( intID )<br />
);</p>
<h2>Add the Full-Text Index</h2>
<p>In this example, we’re interested in searching the name and description fields of our products.  In order to add the full-text index to our table, we use the ALTER TABLE command:</p>
<p>ALTER TABLE PRODUCTS ADD FULLTEXT( vcrName, txtDescription );</p>
<p>Alternatively, we could have created the index along with the table in our original CREATE statement like this:</p>
<p>CREATE TABLE PRODUCTS<br />
(<br />
intID int auto_increment,<br />
vcrName varchar(25),<br />
txtDescription text,<br />
vcrPhoto varchar(40),<br />
CONSTRAINT PRODUCTS_pk PRIMARY KEY ( intID ),<br />
FULLTEXT( vcrName, txtDescription )<br />
);</p>
<h2>Searching For Text</h2>
<p>Now that the index has been created, we can go ahead and search the database.  To activate full-text search, we use the MATCH () AGAINST () syntax like this:</p>
<p>SELECT intID, vcrName, txtDescription, vcrPhoto<br />
FROM PRODUCTS<br />
WHERE MATCH( vcrName, txtDescription ) AGAINST ( ‘search terms here’ );</p>
<p>That’s all there is to it!  Anyone with access to a mySQL database should be able to incorporate search into their sites without too much difficulty.  Of course this is a very basic introduction, but should be more the sufficient to get going with.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/10/memcache-mysqls-hero/' rel='bookmark' title='Memcache as MySQL&#8217;s Hero'>Memcache as MySQL&#8217;s Hero</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/05/mysql-pulled-mac-os-lion/' rel='bookmark' title='MySQL Pulled From Mac OS X Lion'>MySQL Pulled From Mac OS X Lion</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/18/memcache-mysql/' rel='bookmark' title='Using Memcache with MySQL'>Using Memcache with MySQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/02/14/how-to-create-full-text-search-using-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: alwaysgetbetter.com @ 2012-02-07 18:06:46 -->
