<?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>Hackerdude &#187; SQL</title>
	<atom:link href="http://www.hackerdude.com/category/dev-platforms/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hackerdude.com</link>
	<description>Software Development Blog by David Martinez</description>
	<lastBuildDate>Fri, 17 Sep 2010 18:31:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Stupid monitoring trick: Watch mysql queries fly</title>
		<link>http://www.hackerdude.com/2009/05/14/stupid-monitoring-trick-watch-mysql-queries-fly/</link>
		<comments>http://www.hackerdude.com/2009/05/14/stupid-monitoring-trick-watch-mysql-queries-fly/#comments</comments>
		<pubDate>Thu, 14 May 2009 22:09:58 +0000</pubDate>
		<dc:creator>David Martinez</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hackerdude.com/2009/05/14/stupid-monitoring-trick-watch-mysql-queries-fly/</guid>
		<description><![CDATA[Put this somewhere in your ~/bin: watch 'echo "show processlist" &#124; mysql -u whateveruser --password=mypassword &#124; grep -v "show processlist" ' Now run it and you will have a poor man's monitor, kind of like top but for MySQL. That coupled with screen (or multiple terminals) may give you some quick and easy piece of [...]]]></description>
			<content:encoded><![CDATA[<p>Put this somewhere in your ~/bin:</p>
<p><code>watch 'echo "show processlist" | mysql -u whateveruser --password=mypassword | grep -v "show processlist" '</code></p>
<p>Now run it and you will have a poor man's monitor, kind of like top but for MySQL. That coupled with screen (or multiple terminals) may give you some quick and easy piece of mind.</p>
<p>This should give you *a lot* of monitoring automation ideas. It should be easy to put together a shell script that puts it all in a little "important things panel" to use watch on. Sometimes that's all you need.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackerdude.com/2009/05/14/stupid-monitoring-trick-watch-mysql-queries-fly/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Oracle tuning: Using Hints</title>
		<link>http://www.hackerdude.com/2005/06/20/oracle-tuning-using-hints/</link>
		<comments>http://www.hackerdude.com/2005/06/20/oracle-tuning-using-hints/#comments</comments>
		<pubDate>Mon, 20 Jun 2005 14:29:38 +0000</pubDate>
		<dc:creator>David Martinez</dc:creator>
				<category><![CDATA[CBBTR]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hackerdude.com/2005/06/20/oracle-tuning-using-hints/</guid>
		<description><![CDATA[Every once in a while you will get a query that no matter what you try, it doesn't use the indices. This is typically because the cost-based optimizer decides that an approach that is slower has a total cost that is lower than using the index and making it faster. To solve this, you would [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while you will get a query that no matter what you try, it doesn't use the indices. This is typically because the cost-based optimizer decides that an approach that is <b>slower</b> has a <b>total cost</b> that is lower than using the index and making it faster.</p>
<p>To solve this, you would use a hint. You can find <a href="http://www.dbasupport.com/oracle/ora9i/index_hints.shtml">an article on how to do this on dbasupport</a>. But the basics are: <span id="more-318"></span> The way to use a hint is as follows:</p>
<pre class="sql">&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span>  <span style="color: #808080; font-style: italic;">/*+ INDEX(a, MY_INDEX1) */</span>
  *
<span style="color: #993333; font-weight: bold;">FROM</span>
  MY_TABLE a
<span style="color: #993333; font-weight: bold;">WHERE</span>
  FUNKY_ID= <span style="color: #ff0000;">'3455'</span>
&nbsp;</pre>
<p>You can use <a href="http://www.hackerdude.com/2005/06/20/oracle-tuning-using-explain-plan/">Explain Plan</a> to verify that the index was used. Typical problems include:</p>
<ul>
<li>Check the spaces. Plus sign <b>must</b> be next to the comment opening. No spaces in between.</li>
<li>Make sure the table or alias is correct. When in doubt, use an alias.</li>
<li>Note the comma and space betwen alias and index name</li>
</ul>
<p>Hints are not very well documented. Google around for oracle hints and you will find <a href="http://www.adp-gmbh.ch/ora/sql/hints.html">some interesting articles</a> however..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackerdude.com/2005/06/20/oracle-tuning-using-hints/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle tuning: Using explain plan</title>
		<link>http://www.hackerdude.com/2005/06/20/oracle-tuning-using-explain-plan/</link>
		<comments>http://www.hackerdude.com/2005/06/20/oracle-tuning-using-explain-plan/#comments</comments>
		<pubDate>Mon, 20 Jun 2005 14:18:54 +0000</pubDate>
		<dc:creator>David Martinez</dc:creator>
				<category><![CDATA[CBBTR]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hackerdude.com/2005/06/20/oracle-tuning-using-explain-plan/</guid>
		<description><![CDATA[In order to make queries run faster, you should try to have oracle explain to you how the query should be performed. The whole explanation for oracle tuning can be found on the Oracle Tuning guide, but here are the basics. First, you need a PLAN_TABLE: &#160; CREATE TABLE plan_table &#40; statement_id VARCHAR2&#40;30&#41;, timestamp DATE, [...]]]></description>
			<content:encoded><![CDATA[<p>In order to make queries run faster, you should try to have oracle explain to you how the query should be performed.</p>
<p>The whole explanation for oracle tuning can be found on the <a href="http://www.csee.umbc.edu/help/oracle8/server.815/a67775/ch13_exp.htm">Oracle Tuning guide</a>, but here are the basics. <span id="more-319"></span> First, you need a PLAN_TABLE:</p>
<pre class="sql">&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> plan_table <span style="color: #66cc66;">&#40;</span>
    statement_id     VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>,
    timestamp        DATE,
    remarks          VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&#41;</span>,
    operation        VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>,
    options          VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>,
    object_node      VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span>,
    object_owner     VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>,
    object_name      VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>,
    object_instance  NUMERIC,
    object_type      VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>,
    optimizer        VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>,
    search_columns   NUMERIC,
    id               NUMERIC,
    parent_id        NUMERIC,
    position         NUMERIC,
    cost             NUMERIC,
    cardinality      NUMERIC,
    bytes            NUMERIC,
    other_tag        VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
    other            LONG <span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Next, you need to output the SQL</p>
<pre class="sql">&nbsp;
<span style="color: #993333; font-weight: bold;">EXPLAIN</span> PLAN <span style="color: #993333; font-weight: bold;">SET</span> STATEMENT_ID = <span style="color: #ff0000;">'MyPlan'</span> <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> myTable <span style="color: #993333; font-weight: bold;">WHERE</span> myName <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'Dude%'</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> PLAN_TABLE <span style="color: #993333; font-weight: bold;">WHERE</span> STATEMENT_ID = <span style="color: #ff0000;">'MyPlan'</span>
&nbsp;</pre>
<p>Now look at the output. Take a special look at the cost and any full table scans.</p>
<p>Full table scans may or may not be a problem (depending on the size of the table).</p>
<p>Note: If using <a href="http://www.toadsoft.com/">TOAD</a>, you only need to switch to the "Explain" tab. You can set the name of the plan table to use by using <b>View->Options->Oracle->Explain Table Name</b>. You will probably need to do this since it defaults to TOAD_PLAN_TABLE..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackerdude.com/2005/06/20/oracle-tuning-using-explain-plan/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Create/Drop DB and user</title>
		<link>http://www.hackerdude.com/2005/04/05/mysql-createdrop-db-and-user/</link>
		<comments>http://www.hackerdude.com/2005/04/05/mysql-createdrop-db-and-user/#comments</comments>
		<pubDate>Tue, 05 Apr 2005 21:02:09 +0000</pubDate>
		<dc:creator>David Martinez</dc:creator>
				<category><![CDATA[CBBTR]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hackerdude.com/2005/04/05/mysql-createdrop-db-and-user/</guid>
		<description><![CDATA[This tip contains a couple of MySQL scripts that allow me to create and drop a database and a user that will "own" said database. They are accompanied by a couple of ant tasks that will allow you to replace the tokens for dbname, etc. By themselves, the SQL scripts illustrate how you need to [...]]]></description>
			<content:encoded><![CDATA[<p>This tip contains a couple of MySQL scripts that allow me to create and drop a database and a user that will "own" said database. <span id="more-327"></span> </p>
<p>They are accompanied by a couple of <a href="http://ant.apache.org">ant tasks</a> that will allow you to replace the tokens for dbname, etc.</p>
<p>By themselves, the SQL scripts illustrate how you need to provide the appropriate grants (and how to clear the grants when bouncing the db from scratch.</p>
<p>Note: Listing 3 also runs a ficticious "schema.sql" file.</p>
<p><b>Listing 1.- Create Database and user</b><br />
<code lang="sql"></p>
<p>-- DO NOT RUN THIS FILE DIRECTLY.<br />
-- Use the accompanying ant script to create the database.</p>
<p>-- Those ant scripts will set up replace the tokens for<br />
-- dbname, dbuser and dbpassword.</p>
<p>CREATE DATABASE _DBNAME_;</p>
<p>GRANT ALL ON _DBNAME_.* TO _DBUSER_@'%' IDENTIFIED BY '_DBPASSWORD_';</p>
<p>FLUSH PRIVILEGES</p>
<p></code></p>
<p><b>Listing 2.- Drop Database and user</b></p>
<p><code lang="sql"></p>
<p>-- DO NOT RUN THIS FILE DIRECTLY.<br />
-- Use the accompanying ant script to drop the database.</p>
<p>-- Those ant scripts will set up replace the tokens for<br />
-- dbname, dbuser and dbpassword.</p>
<p>REVOKE ALL ON _DBNAME_.* FROM _DBUSER_;</p>
<p>DROP DATABASE _DBNAME_;</p>
<p>DROP USER _DBUSER_;</p>
<p>FLUSH PRIVILEGES;</p>
<p></code></p>
<p><b>Listing 3: Ant taks for create and drop</b><br />
<code lang="xml"></p>
<p>    <!-- JDBC.USER properties are used when the tables are created and at application runtime. --><br />
    < property name="jdbc.user.databasename" value="homeinv"/><br />
    < property name="jdbc.user.name" value="homeinv"/><br />
    < property name="jdbc.user.password" value="inventory"/><br />
    < property name="jdbc.user.url" value="jdbc:mysql://localhost/${jdbc.user.databasename}"/><br />
    <!-- JDBC.SUPERUSER properties are used when the database is created or dropped. --><br />
    < property name="jdbc.superuser.name" value="root"/><br />
    < property name="jdbc.superuser.password" value="change this password"/><br />
    < property name="jdbc.superuser.url" value="jdbc:mysql://localhost/"/><br />
    < property name="jdbc.driver.classname" value="org.gjt.mm.mysql.Driver"/><br />
    < property name="jdbc.jar" value="${lib.dir}/mysql-connector-java-3.1.7-bin.jar"/><br />
    < property name="database.type" value="mysql"/><br />
    < property name="tmp.dir" value="${java.io.tmpdir}/${project.name}"/></p>
<p>    <!-- - - - - - - - - - - - - - - - - -<br />
          target: create_database<br />
         - - - - - - - - - - - - - - - - - --><br />
    < target name="create_database" description="Creates the database schema"><br />
      < delete dir="${tmp.dir}/${database.type}"/><br />
        < mkdir dir="${tmp.dir}/${database.type}"/><br />
        < copy file="${database.dir}/${database.type}/_createdb.sql" tofile="${tmp.dir}/${database.type}/_createdb.sql"/><br />
        < replace file="${tmp.dir}/${database.type}/_createdb.sql"><br />
          < replacefilter token="_DBNAME_" value="${jdbc.user.databasename}"/><br />
          < replacefilter token="_DBUSER_" value="${jdbc.user.name}"/><br />
          < replacefilter token="_DBPASSWORD_" value="${jdbc.user.password}"/><br />
        < /replace></p>
<p>        < copy file="${database.dir}/${database.type}/schema.sql" tofile="${tmp.dir}/${database.type}/schema.sql"/><br />
            < replace file="${tmp.dir}/${database.type}/schema.sql"><br />
            <!-- TODO: The schema will also have some other replacements, like the superuser name for the actual app, etc --><br />
            < replacefilter token="_DBNAME_" value="${jdbc.user.databasename}"/><br />
            < replacefilter token="_DBUSER_" value="${jdbc.user.name}"/><br />
            < replacefilter token="_DBPASSWORD_" value="${jdbc.user.password}"/><br />
          < /replace></p>
<p>          < sql url="${jdbc.superuser.url}"  userid="${jdbc.superuser.name}" password="${jdbc.superuser.password}" driver="${jdbc.driver.classname}"<br />
    	classpath="${jdbc.jar}" src="${tmp.dir}/${database.type}/_createdb.sql"/><br />
          < echo>DATABASE CREATED< /echo></p>
<p>          < sql url="${jdbc.user.url}"  userid="${jdbc.user.name}" password="${jdbc.user.password}" driver="${jdbc.driver.classname}"<br />
		classpath="${jdbc.jar}" src="${tmp.dir}/${database.type}/schema.sql"><br />
          < /sql><br />
          < echo>SCHEMA CREATED< /echo><br />
    < /target></p>
<p>    <!-- - - - - - - - - - - - - - - - - -<br />
          target: drop_database<br />
         - - - - - - - - - - - - - - - - - --><br />
    < target name="drop_database" description="Drops the database schema"><br />
      < delete dir="${tmp.dir}/${database.type}"/><br />
      < mkdir dir="${tmp.dir}/${database.type}"/><br />
      < copy file="${database.dir}/${database.type}/_dropdb.sql" tofile="${tmp.dir}/${database.type}/_dropdb.sql"/><br />
        < replace file="${tmp.dir}/${database.type}/_dropdb.sql"><br />
          < replacefilter token="_DBNAME_" value="${jdbc.user.databasename}"/><br />
          < replacefilter token="_DBUSER_" value="${jdbc.user.name}"/><br />
          < replacefilter token="_DBPASSWORD_" value="${jdbc.user.password}"/><br />
        < /replace><br />
        < sql url="${jdbc.superuser.url}"  userid="${jdbc.superuser.name}" password="${jdbc.superuser.password}" driver="${jdbc.driver.classname}"<br />
    	classpath="${jdbc.jar}" src="${tmp.dir}/${database.type}/_dropdb.sql"><br />
        < /sql><br />
        < echo>DATABASE DROPPED< /echo><br />
    < /target></p>
<p>    < target name="rebuild_database" depends="drop_database,create_database" description="Rebuilds the database (drops it and recreates it again)"><br />
    < /target></p>
<p></code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackerdude.com/2005/04/05/mysql-createdrop-db-and-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

