<?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>brettdargan.com &#187; db</title>
	<atom:link href="http://brettdargan.com/blog/tag/db/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettdargan.com/blog</link>
	<description>&#955; Thoughts and rants</description>
	<lastBuildDate>Fri, 28 May 2010 01:35:46 +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>database driver Character Set hassles are not just a pain in Oracle</title>
		<link>http://brettdargan.com/blog/2006/07/15/database-driver-character-set-hassles-are-not-just-a-pain-in-oracle/</link>
		<comments>http://brettdargan.com/blog/2006/07/15/database-driver-character-set-hassles-are-not-just-a-pain-in-oracle/#comments</comments>
		<pubDate>Fri, 14 Jul 2006 22:19:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://brettdargan.com/blog/?p=72</guid>
		<description><![CDATA[To use utf8 in mysql from a client you must execute: set names "utf8" query. There are a number of character_set properties that can be set, issuing the set names query will ensure the 3 appropriate character_set_* properties are changed to your character set. Then you can insert values without getting them converted to latin [...]]]></description>
			<content:encoded><![CDATA[<p>To use <a href="http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html">utf8 in mysql</a> from a client you must execute: set names "utf8" query.<br />
There are a number of character_set properties that can be set, issuing the set names query will ensure the 3 appropriate character_set_* properties are changed to your character set.</p>
<p>Then you can insert values without getting them converted to latin on the client.</p>
<p>Too bad ActiveRecord::Base doesn't already have a hook for these on start queries, since you need to issue some query to set character set properties for just about every database anyway.</p>
<div class="tweetthis" style="text-align:left;"><p> <a class="tt" href="http://twitter.com/home/?status=database+driver+Character+Set+hassles+are+not+just+a+pain+in+Oracle+http%3A%2F%2Ftinyurl.com%2F6d8jct5" title="Post to Twitter"><img class="nothumb" src="http://brettdargan.com/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=database+driver+Character+Set+hassles+are+not+just+a+pain+in+Oracle+http%3A%2F%2Ftinyurl.com%2F6d8jct5" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://brettdargan.com/blog/2006/07/15/database-driver-character-set-hassles-are-not-just-a-pain-in-oracle/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Update policy, Optimistic Locking vs. Last in Wins</title>
		<link>http://brettdargan.com/blog/2005/05/30/update-policy-optimistic-locking-vs-last-in-wins/</link>
		<comments>http://brettdargan.com/blog/2005/05/30/update-policy-optimistic-locking-vs-last-in-wins/#comments</comments>
		<pubDate>Sun, 29 May 2005 22:39:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[locking]]></category>
		<category><![CDATA[transactions]]></category>

		<guid isPermaLink="false">http://brettdargan.com/blog/?p=45</guid>
		<description><![CDATA[I cringe at the thought of using a Last In Wins update policy, it just smells bad. Maybe because I've been involved in alot of database development, or maybe I've worked on systems with reasonable levels of concurrent access that I hate leaving things to chance. An argument for Last In Wins, is that the [...]]]></description>
			<content:encoded><![CDATA[<p>I cringe at the thought of using a Last In Wins update policy, it just smells bad. Maybe because I've been involved in alot of database development, or maybe I've worked on systems with reasonable levels of concurrent access that I hate leaving things to chance. </p>
<p>An argument for Last In Wins, is that the user may not know why there update has failed etc. We could approach this through useful messages or improved usability.</p>
<p>Another would be that why build optimistic locking when the simplest thing that could possibly work should suffice in most cases.</p>
<p>Well the cost of building it in is not high, it has good support in most orm including hibernate.</p>
<p>In reality the actual number of cases where multiple user transactions may attempt to update the same row should be very rare, but that is no reason to give up on optimisitc locking. </p>
<p>Databases are there to provide <a href="http://en.wikipedia.org/wiki/ACID">ACID</a> proof transactions, Atomicity, Consistency, Isolation and Durable.</p>
<p>If we choose Last In Wins we are at a much higher risk of corrupting the data, if updates on tables can occur that don't update all columns the way is open for inconsistent updates. It feels so wrong to have a database that provides ACID transactions yet we can partially thwart it's goodness by not using  pessimistic or optimistic locking policy.</p>
<p>For some systems or certain areas of a domain it may be fine. eg. updating a user's profile. </p>
<p>If you are going to mix it up, at a minimum ensure that Aggregate Root Entities use optimistic locking since they are more likely to have concerns (state or behaviour) that depend on it's children. eg. summarised data that needs to be updated, or invariants that need to be validated before transaction completion.</p>
<p>In general I would prefer to standardise on optimistic locking.</p>
<div class="tweetthis" style="text-align:left;"><p> <a class="tt" href="http://twitter.com/home/?status=Update+policy%2C+Optimistic+Locking+vs.+Last+in+Wins+http%3A%2F%2Ftinyurl.com%2F63cst2c" title="Post to Twitter"><img class="nothumb" src="http://brettdargan.com/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Update+policy%2C+Optimistic+Locking+vs.+Last+in+Wins+http%3A%2F%2Ftinyurl.com%2F63cst2c" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://brettdargan.com/blog/2005/05/30/update-policy-optimistic-locking-vs-last-in-wins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle, not using a simple Index in a multilingual db</title>
		<link>http://brettdargan.com/blog/2005/03/24/oracle-not-using-a-simple-index-in-a-multilingual-db/</link>
		<comments>http://brettdargan.com/blog/2005/03/24/oracle-not-using-a-simple-index-in-a-multilingual-db/#comments</comments>
		<pubDate>Wed, 23 Mar 2005 18:54:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[multilingual]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://brettdargan.com/blog/?p=41</guid>
		<description><![CDATA[So Oracle's Cost Based Optimizer can often decide not to use an index. We had a case this week where it decided not to use an index on a single column varchar2 using a string bind variable? After eliminating statistics as an issue, we discovered the problem was only affecting linux from either sqlplus, or [...]]]></description>
			<content:encoded><![CDATA[<p>So Oracle's Cost Based Optimizer can often decide not to use an index. We had a case this week where it decided not to use an index on a single column varchar2 using a string bind variable?</p>
<p>After eliminating statistics as an issue, we discovered the problem was only affecting linux from either sqlplus, or jdbc using a type 4 thin driver, windows clients were ok.</p>
<p>So not sounding like a driver issue, we hunted further and foud that after  <code>select * from nls_session_parameters</code> it was obvious that some we had some very important nls settings that were incorrect. For Oracle on *nix you have to ensure that an NLS_LANG environment variable is setup, like  "ENGLISH_AUSTRALIA.UTF8"</p>
<p>Since we have been porting from sqlserver (case-insensitive) to oracle (case sensitive) there were certain nls settings that need to be set.</p>
<p>In Particular nls_comp=ansi and nls_sort=binary_ai. There are some articles around for doing this, just <a href="http://asktom.oracle.com">ask tom</a>.</p>
<h4>Warning</h4>
<p>It should be stated however that this will only allow case insensitive searching for relational operators like '=', '&lt;', and '&gt;'. <em>LIKE</em> searches will still be sensitive, so for many cases you will still need to do standard uppercasing of literal search value and creation of function based index.</p>
<p>Back to our problem, why was asp working and the good guys not, well there was an existing after logon trigger that was setting up a single nls_sort=binary_ai.</p>
<p>Now we were getting this setting on connection, but asp were not, other regional settings must have been interfering and the nls_sort value was the standard "binary". So asp was still working fast due to the use of a standard <em>NOT CASE INSENSITIVE</em> index. </p>
<p>Meanwhile the cost based optimizer had no index to choose for us, because the creation of a function based index with that nls_sort setting had been missed. If you had a need to create one yourself it would be something like this:<br />
<code>create index blah on tbl (nlssort(columnName, 'NLS_SORT=BINARY_AI')) </code></p>
<p>BTW, when adjusting some of these parameters you may need to spend time on checking the order, as there are implicit dependencies with no apparent documentation on which order you should override them. </p>
<p>So if you are working in a multilingual db, make sure you check out nls_session_parameters to confirm your changes. The other tables  nls_instance_parameters or nls_database_parameters are only relevant for checking the character set(s) of your database since that isn't session specific.</p>
<h4>The Test</h4>
<p>So you may be wondering how this was tested, well there was a test, it was very simple, using the explain plan feature of oracle asserting that there was an index scan present. Here are the interesting bits:</p>
<pre>
    public void testOracleWillUseIndexForQuery() throws SQLException {
        String problemQuery = "select * from test where name = &apos;2423423&apos;";
        assertQueryUsesIndex(problemQuery);
    }

    private void assertQueryUsesIndex(String problemQuery) throws SQLException {
        assertTrue(explainPlan(problemQuery).indexOf("INDEX (RANGE SCAN") > 0);
    }

    private String explainPlan(final String query) throws SQLException {
        String queryPlan = "explain plan for ";
        getConnection().createStatement().execute(queryPlan + query);
        return explainPlan;
    }

    private String getExplainPlan() throws SQLException {
        String queryPlan = "select n" +
        "  substr (lpad(&apos; &apos;, level-1) || operation || &apos; (&apos; || options ||&apos;)&apos;,1,30 ) "Operation", n" +
           "  object_name "Object"n" +
           "from n" +
           "  plan_table n" +
           "start with id = 0 n" +
           "connect by prior id=parent_id";
        ResultSet rs = getConnection().createStatement().executeQuery(queryPlan);
        return accumulateColumn1(rs);
    }

    private String accumulateColumn1(ResultSet rs) throws SQLException {
        StringBuffer result = new StringBuffer();
        while (rs.next()) {
            result.append(rs.getString(1));
            result.append("n");
        }
        return result.toString();
    }
</pre>
<div class="tweetthis" style="text-align:left;"><p> <a class="tt" href="http://twitter.com/home/?status=Oracle%2C+not+using+a+simple+Index+in+a+multilingual+db+http%3A%2F%2Ftinyurl.com%2F6drdszj" title="Post to Twitter"><img class="nothumb" src="http://brettdargan.com/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Oracle%2C+not+using+a+simple+Index+in+a+multilingual+db+http%3A%2F%2Ftinyurl.com%2F6drdszj" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://brettdargan.com/blog/2005/03/24/oracle-not-using-a-simple-index-in-a-multilingual-db/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle &#8220;IN&#8221; condition limits</title>
		<link>http://brettdargan.com/blog/2004/10/13/oracle-in-condition-limits/</link>
		<comments>http://brettdargan.com/blog/2004/10/13/oracle-in-condition-limits/#comments</comments>
		<pubDate>Tue, 12 Oct 2004 22:17:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://brettdargan.com/blog/?p=16</guid>
		<description><![CDATA[In Oracle 8i it use to be that the "IN" condition was limited to 254 expressions, in total so stringing a few together with "OR" didn't work. But now in 10g you have alot more freedom, for starters the expression list is now limited to 1000 plus you can have multiple "IN" conditions in the [...]]]></description>
			<content:encoded><![CDATA[<p>In Oracle 8i it use to be that the "IN" condition was limited to 254 expressions,  in total so stringing a few together with "OR" didn't work.  </p>
<p>But now in 10g you have alot more freedom, for starters the expression list is now limited to 1000 plus you can have multiple "IN" conditions in the query to extend that and if you want something less smelly, create a collection and use the CAST and TABLE commands to join an in memory collection of predefined type to real tables. </p>
<p>When I first tried the latter method in 8i it was too unreliable to use, but now it seems better, thanks to Gary Mandelkow for doing the leg work and testing 10g, we can reliably do this with atleast a 3000 member collection.</p>
<div class="tweetthis" style="text-align:left;"><p> <a class="tt" href="http://twitter.com/home/?status=Oracle+%E2%80%9CIN%E2%80%9D+condition+limits+http%3A%2F%2Ftinyurl.com%2F6b43snh" title="Post to Twitter"><img class="nothumb" src="http://brettdargan.com/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Oracle+%E2%80%9CIN%E2%80%9D+condition+limits+http%3A%2F%2Ftinyurl.com%2F6b43snh" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://brettdargan.com/blog/2004/10/13/oracle-in-condition-limits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

