<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8455543779867801145</atom:id><lastBuildDate>Thu, 16 Feb 2012 13:59:44 +0000</lastBuildDate><category>performance</category><category>apex</category><category>active directory</category><category>postgresql</category><category>heterogeneous</category><category>javascript</category><category>sql</category><category>mysql</category><category>cluster</category><category>xdb</category><category>oracle</category><title>Ilmar Kerm DB blog</title><description>Technical blog about databases, mainly Oracle technology.</description><link>http://ilmarkerm.blogspot.com/</link><managingEditor>noreply@blogger.com (Ilmar Kerm)</managingEditor><generator>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ilmarkerm" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="ilmarkerm" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>59.429</geo:lat><geo:long>24.720</geo:long><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-7253434623551569848</guid><pubDate>Wed, 09 Nov 2011 14:14:00 +0000</pubDate><atom:updated>2011-11-09T16:14:24.689+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">cluster</category><title>Clustering MySQL instances with Oracle Clusterware 11gR2</title><description>&lt;p&gt;I've been doing lately quite many database clustering implementations; Oracle RAC and since we have many MySQL instances in production, had to find a good way to make MySQL instances highly available also.&lt;/p&gt;

&lt;p&gt;One good solution for this is managing MySQL instances with clusterware and since we are planning to use Oracle RAC on Oracle Enterprise Linux anyway, then Oracle Clusterware is an excellent candidate for this task. Also... &lt;a href="http://www.oracle.com/us/technologies/linux/support-439829.html" target="_blank"&gt;Oracle Clusterware is included with Oracle Enterprise Linux at no additional charge&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Requirements I had:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Multiple MySQL instances running in the same cluster, in case of node failure affected MySQL instances are moved to any other surviving node (least loaded)&lt;/li&gt;
&lt;li&gt;Different MySQL instances may run different MySQL RDBMS software versions&lt;/li&gt;
&lt;li&gt;Each MySQL instance is listening to its own dedicated and clusterware managed IP address (so MySQL can always respond on default 3306 port and can move independently around nodes without any dependencies to other MySQL instances)&lt;/li&gt;
&lt;li&gt;Clusterware monitors each MySQL instance and in case of trouble automatically moves IP and starts instance on another cluster node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this task I wrote some Bash scripts to help managing MySQL instances in Oracle Clusterware environment. All these scripts are available here: &lt;a href="http://code.google.com/p/mysql-with-oracle-clusterware-scripts/" target="_blank"&gt;Google Code project page&lt;/a&gt;, also with documentation of the whole setup and how to use scripts: &lt;a href="http://code.google.com/p/mysql-with-oracle-clusterware-scripts/w/list" target="_blank"&gt;Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All comments welcome!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-7253434623551569848?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=H5fY_s3c2AY:XS9RmsXG5jE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=H5fY_s3c2AY:XS9RmsXG5jE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=H5fY_s3c2AY:XS9RmsXG5jE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=H5fY_s3c2AY:XS9RmsXG5jE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=H5fY_s3c2AY:XS9RmsXG5jE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/H5fY_s3c2AY" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2011/11/clustering-mysql-instances-with-oracle.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-6068018573516023343</guid><pubDate>Wed, 09 Feb 2011 18:10:00 +0000</pubDate><atom:updated>2011-02-09T20:56:44.848+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">postgresql</category><title>Script to import a table to another schema in PostgreSQL</title><description>&lt;p&gt;Tested with Centos 5 and PostgreSQL 8.4.&lt;/p&gt;
&lt;p&gt;Sadly PostgreSQL does not have remap_schema parameter in its data import tool pg_restore, so importing tables to another schema in destination database is a little bit tricky.&lt;/p&gt;
&lt;p&gt;Here is a Bash script I use to connect to the source database over SSH tunnel, export two tables (&lt;i&gt;srv.stat_export&lt;/i&gt; and &lt;i&gt;disp.trip&lt;/i&gt;) and then import them to the destination database under schema &lt;i&gt;etl&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;Download the script: &lt;a href="http://content.wuala.com/contents/ilmarkerm/Database/PostgreSQL/import_table_to_another_schema.sh"&gt;import_table_to_another_schema.sh&lt;/a&gt;&lt;/p&gt;

&lt;pre class="brush: bash;"&gt;
#!/bin/bash -l

# Open SSH tunnel to remote database
# Make sure, that passwordless authentication is enabled
ssh -C -N -L 15432:livedb2:5432 username@live.site.com &amp;
sleep 10s

# Set source PostgreSQL connection parameters
# Make sure that PostgreSQL tools (psql, pg_dump) can connect with these parameters without password
# if necessary use ~/.pgpass file
export PGDATABASE=db_live
export PGHOST=localhost
export PGPORT=15432
export PGUSER=exportuser

# Generate temporary filename
SCHEMAFILE=`mktemp`

# Dump the data
pg_dump -t srv.stat_export -t disp.trip --file=$SCHEMAFILE -O --no-acl
RESULT=$?

# Kill SSH tunnel
kill %+

# If export was successfull, then start importing
if [ $RESULT -eq 0 ]; then
  # Replace source schema name with destination schema name
  sed -i '/^SET search_path = /s/disp,\|srv,/etl,/g' $SCHEMAFILE

  # Set destination PostgreSQL connection parameters
  # Make sure that PostgreSQL tools (psql, pg_dump) can connect with these parameters without password
  export PGDATABASE=db_dev
  export PGHOST=localhost
  export PGPORT=5432
  export PGUSER=postgres

  # Drop old tables
  echo 'drop table etl.stat_export; drop table etl.trip; ' | psql -q
  # Load new tables
  psql -q &amp;lt; $SCHEMAFILE
  # Fix access privileges
  echo 'alter table etl.stat_export owner to etl; alter table etl.trip owner to etl;' | psql -q
fi

# Remove temporary file
rm $SCHEMAFILE
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-6068018573516023343?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=3EB37xPHrY4:VRcVRbLRYww:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=3EB37xPHrY4:VRcVRbLRYww:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=3EB37xPHrY4:VRcVRbLRYww:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=3EB37xPHrY4:VRcVRbLRYww:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=3EB37xPHrY4:VRcVRbLRYww:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/3EB37xPHrY4" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2011/02/script-to-import-table-to-another.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-6774777753306451747</guid><pubDate>Wed, 20 Oct 2010 13:19:00 +0000</pubDate><atom:updated>2010-10-20T16:59:05.494+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">apex</category><title>Geolocation API</title><description>&lt;p&gt;There is an interesting W3C Draft, that enables websites to just simply ask web browser to report the users geographical location, and then the web browser will try the best available location method, like GeoIP, WIFI location or GPS. I have currently tested it on Firefox 3.6 and Google Chrome; Internet Explorer 8.0 does not support it yet.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dev.w3.org/geo/api/spec-source.html" target="_blank"&gt;W3C Geolocation API Draft&lt;/a&gt;&lt;br/&gt;
&lt;a href="https://developer.mozilla.org/En/Using_geolocation" target="_blank"&gt;Mozilla documentation for Geolocation&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;How to use it in APEX?&lt;/h3&gt;
&lt;p&gt;If you are just interested in recording the users location, then using an on-demand application process should be the easiest solution:&lt;/p&gt;
&lt;p&gt;First, create two application items: &lt;b&gt;USER_LOC_LATITUDE&lt;/b&gt; and &lt;b&gt;USER_LOC_LONGITUDE&lt;/b&gt;. They are used for storing users location.&lt;/p&gt;
&lt;p&gt;Then, create an &lt;b&gt;On Demand&lt;/b&gt; application process &lt;b&gt;SAVE_USER_LOCATION&lt;/b&gt;. Create your necessary application logic in that process to handle the user location. The user location is available through application items USER_LOC_LATITUDE and USER_LOC_LONGITUDE.&lt;/p&gt;
&lt;p&gt;And finally, include the following HTML code to your page. This uses APEX AJAX JavaScript API to call the created application process as soon as the users location becomes available for the browser. Please note also, that the browser asks for users permission for reporting the location.&lt;/p&gt;

&lt;pre class="brush: html;"&gt;
&amp;lt;script type="text/javascript"&amp;gt;

  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {

      var get = new htmldb_Get(null, $x('pFlowId').value, 
        'APPLICATION_PROCESS=SAVE_USER_LOCATION', 0);
      get.add('USER_LOC_LATITUDE', position.coords.latitude);
      get.add('USER_LOC_LONGITUDE', position.coords.longitude);
      gReturn = get.get();
      get = null;

    });
  }

&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;p&gt;To continuously monitor user position, use the function &lt;b&gt;navigator.geolocation.watchPosition&lt;/b&gt; instead of &lt;b&gt;navigator.geolocation.getCurrentPosition&lt;/b&gt;.&lt;/p&gt;

&lt;h3&gt;Resolving coordinates to location name&lt;/h3&gt;

&lt;p&gt;Here is one package, that uses GeoNames.org database for resolving the location name. The package requires Oracle 11.2.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://content.wuala.com/contents/ilmarkerm/Database/Oracle/GEOLOCATION.sql?dl=1"&gt;The geolocation package&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://content.wuala.com/contents/ilmarkerm/Database/Oracle/HTTP_UTIL.sql?dl=1"&gt;One helper package, HTTP_UTIL, for downloading XML over HTTP&lt;/a&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-6774777753306451747?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=o-yB7YFVsSQ:XfKsrjYUyMw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=o-yB7YFVsSQ:XfKsrjYUyMw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=o-yB7YFVsSQ:XfKsrjYUyMw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=o-yB7YFVsSQ:XfKsrjYUyMw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=o-yB7YFVsSQ:XfKsrjYUyMw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/o-yB7YFVsSQ" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/10/geolocation-api.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-1460359479608037854</guid><pubDate>Sat, 28 Aug 2010 15:47:00 +0000</pubDate><atom:updated>2010-12-21T10:09:57.161+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">active directory</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>Authenticate database user against Active Directory - the poor man's version</title><description>&lt;p&gt;My use case for this: one big database, where applications have some shemas. But also, quite a lot of developers need access to the database, to see the production data and security requires each user to have his/her own personal account and of course also requires password expiration and complexity. Authenticating database users against organizations central Active Directory would be just perfect for this case.&lt;/p&gt;

&lt;p&gt;Oracle offers this solution for Enterprise Edition, but its an extra cost option - Oracle Advanced Security. If these are not an option for you, then its also possible to achieve the same task, in its simplest form, using user profile password verification function and database logon trigger.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update: &lt;/b&gt;Thanks to Job Miller for pointing out that password authentication does not require licensing ASO option anymore, but requires licensing Oracle Internet Directory: &lt;a href="http://download.oracle.com/docs/cd/B19306_01/license.102/b14199/options.htm#CIHFDJDG"&gt;Link to 10.2 Oracle® Database Licensing Information - 2 Options and Packs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, the procedure works as follows:&lt;br/&gt;
&lt;ul&gt;
&lt;li&gt;Create a new profile for AD-authenticated users with password verification function.&lt;/li&gt;
&lt;li&gt;Assign this new profile to a database user.&lt;/li&gt;
&lt;li&gt;When the database user changes password, the password verification function will try to connect to AD with the user specified password, if AD authentication is successful, then the password is correct and can be changed and stored by Oracle.&lt;/li&gt;
&lt;li&gt;Every time the user connects to the database, after logon trigger will query AD for the user status: does the user still exist, is the user disabled or expired, when did the user last change password. If the user status is changed in AD, the trigger will lock or expire the database user accordingly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;

&lt;h3&gt;The code&lt;/h3&gt;

&lt;p&gt;My solution consists of a PL/SQL package, that I create under my database administration account, named DBAUSER. The package will be called from password verification function and the logon trigger. The DBAUSER account needs network ACLs to permit it to connect to the AD server (11g+!) and the following system privileges:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
GRANT create job TO dbauser;
GRANT alter user TO dbauser;
GRANT select ON sys.user$ TO dbauser;
&lt;/pre&gt;

&lt;p&gt;Now the package:&lt;br&gt;
&lt;a href="http://content.wuala.com/contents/ilmarkerm/Database/Oracle/profile_ad_auth/PROFILE_AD_AUTH.pks"&gt;Package&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://content.wuala.com/contents/ilmarkerm/Database/Oracle/profile_ad_auth/PROFILE_AD_AUTH.pkb"&gt;Package Body&lt;/a&gt;&lt;br/&gt;
&lt;b&gt;NB! Look into the package body script!&lt;/b&gt; There are configuration parameters on top, that you MUST look over and change!
&lt;/p&gt;

&lt;p&gt;Now, create the password verification function under SYS schema, that will only call out the created package:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE OR REPLACE FUNCTION sys.ad_profile_verify_function
  (username varchar2, password varchar2, old_password varchar2)
  RETURN boolean IS
BEGIN
  RETURN DBAUSER.PROFILE_AD_AUTH.VERIFY_PASSWORD(username, password);
END;
/
&lt;/pre&gt;

&lt;p&gt;Next, create a new profile setting the password verification function and also would be good to set parameter PASSWORD_LIFE_TIME to the same value AD has for password expiration. An example profile:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE PROFILE ad_user LIMIT 
SESSIONS_PER_USER 2 
FAILED_LOGIN_ATTEMPTS 4
PASSWORD_LIFE_TIME 90
PASSWORD_VERIFY_FUNCTION ad_profile_verify_function;
&lt;/pre&gt;

&lt;p&gt;And finally the logon trigger, that will fire the user verification function for all users having the ad_user profile. Modify it for your own needs before executing. The following procedure also needs SELECT privilege on SYS.DBA_USERS granted to SYSTEM.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
GRANT execute ON dbauser.profile_ad_auth TO system;

CREATE OR REPLACE TRIGGER system.AD_USER_LOGON AFTER LOGON ON DATABASE
DECLARE
  i NUMBER;
BEGIN
  IF user NOT IN ('SYS','SYSTEM','APPUSER1','APPUSER2') THEN
    -- Check if user belongs to a specific profile
    SELECT count(*) INTO i 
    FROM sys.dba_users
    WHERE username = user AND profile = 'AD_USER';
    -- Execute verification procedure
    IF i &gt; 0 THEN
      dbauser.profile_ad_auth.verify_user(user);
    END IF;
  END IF;
END;
/
&lt;/pre&gt;

&lt;p&gt;To create a user that will be authenticated from AD, first create a regular user with a temporary password, then issue ALTER USER to set the profile. This is needed because if profile is set in CREATE USER command, then the initial password will also be validated with the verification function.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE USER ilmar IDENTIFIED BY test123;
ALTER USER ilmar PROFILE ad_user PASSWORD EXPIRE;
&lt;/pre&gt;

&lt;p&gt;NB! 10g passwords are not case sensitive, so subsequent logins will be allowed with passwords using the wrong case. In 11g, be sure to enable initialization parameter SEC_CASE_SENSITIVE_LOGON, to benefit from case sensitive passwords.&lt;/p&gt;

&lt;p&gt;One big problem... SQL Developer still does not support changing passwords and user must change the password from SQLPlus. What do you think may be a good way around it? When the procedure detects, that AD password has changed, then execute ALTER USER to expire the password, but do not raise an exception? Then the user must remember to change the password using ALTER USER, but of course, it means allowing authentication with the old password once...&lt;/p&gt;

&lt;p&gt;I have not yet used it in production, so all feedback is welcome! I've tested the code on 10.2 EE and 11.2 EE, should work on standard edition also.&lt;/p&gt;

&lt;a href="http://www.wuala.com/ilmarkerm/Database/Oracle/profile_ad_auth"&gt;Download all the code from here&lt;/a&gt;

&lt;p&gt;&lt;b&gt;Update: &lt;/b&gt;Take a look at Oracle-L mailing list thread about the same subject: &lt;a href="http://www.freelists.org/post/oracle-l/Database-authentication-and-Active-Directory"&gt;Click here&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-1460359479608037854?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=MBjyQkV4btw:Ip55LEug6-U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=MBjyQkV4btw:Ip55LEug6-U:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=MBjyQkV4btw:Ip55LEug6-U:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=MBjyQkV4btw:Ip55LEug6-U:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=MBjyQkV4btw:Ip55LEug6-U:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/MBjyQkV4btw" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/08/authenticate-database-user-against.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-7397964080794054838</guid><pubDate>Wed, 26 May 2010 06:25:00 +0000</pubDate><atom:updated>2010-05-26T10:39:58.808+03:00</atom:updated><title>FUSE Linux kernel module for DBFS</title><description>&lt;p&gt;I started to play around with &lt;a href="http://download.oracle.com/docs/cd/E14072_01/appdev.112/e10645/adlob_fs.htm"&gt;DBFS (Database Filesystem)&lt;/a&gt;, a new interesting feature in Oracle 11.2.&lt;/p&gt;
&lt;p&gt;There are some excellent guides on how to set it up:&lt;br/&gt;
&lt;a href="http://www.oracle-base.com/articles/11g/DBFS_11gR2.php"&gt;http://www.oracle-base.com/articles/11g/DBFS_11gR2.php&lt;/a&gt;&lt;br&gt;
&lt;a href="http://blog.ronnyegner-consulting.de/2009/10/08/the-oracle-database-file-system-dbfs/"&gt;http://blog.ronnyegner-consulting.de/2009/10/08/the-oracle-database-file-system-dbfs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But both of them had one "not very clean" part in them - compiling FUSE kernel driver from source. Actually this is not necessary and there are precompiled FUSE rpm packages available:&lt;br/&gt;
&lt;a href="http://dag.wieers.com/rpm/packages/fuse/"&gt;http://dag.wieers.com/rpm/packages/fuse/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After RPM package is installed, you need to add the OS user who will mount the filesystem (for example oracle) to OS group fuse.&lt;/p&gt;

&lt;pre class="brush: bash;"&gt;
usermod -a -G fuse oracle
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-7397964080794054838?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=kGJiDWNM9B0:XDgFPVbC598:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=kGJiDWNM9B0:XDgFPVbC598:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=kGJiDWNM9B0:XDgFPVbC598:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=kGJiDWNM9B0:XDgFPVbC598:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=kGJiDWNM9B0:XDgFPVbC598:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/kGJiDWNM9B0" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/05/fuse-linux-kernel-module-for-dbfs.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-7658581803310667398</guid><pubDate>Thu, 20 May 2010 17:38:00 +0000</pubDate><atom:updated>2010-05-20T21:25:15.633+03:00</atom:updated><title>Reading RSS with SQL</title><description>&lt;p&gt;A small SQL code sample this time, how to read RSS file in SQL. I'm using it to display and aggregate some RSS feeds in APEX application.&lt;br/&gt;
All columns are directly read from XML (plus some additinal code to get the time zone correct), except &lt;i&gt;pubdate_local&lt;/i&gt; that is &lt;i&gt;pubdate&lt;/i&gt; converted to date datatype in local time zone. The first example is reading from a local file localnews.rss under directory DIR1.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SELECT title,
       link,
       description,
       author,
       pubdate,
       CAST (pubdate AT TIME ZONE SESSIONTIMEZONE AS DATE) pubdate_local
  FROM (    SELECT title,
                   link,
                   description,
                   author,
                   TO_TIMESTAMP_TZ (
                      REPLACE(pubdate, 'PDT', 'PST PDT'),
                      CASE
                         WHEN REGEXP_LIKE (pubdate, '[[:digit:]]{4}$')
                         THEN
                            'Dy, dd fmMonth YYYY HH24:MI:SS TZHTZM'
                         ELSE
                            'Dy, dd fmMonth YYYY HH24:MI:SS TZR TZD'
                      END,
                      'NLS_DATE_LANGUAGE = American')
                      pubdate
              FROM XMLTABLE (
                      '/rss/channel/item'
                      PASSING xmltype (BFILENAME ('DIR1', 'localnews.rss'),
                                       NLS_CHARSET_ID ('AL32UTF8'))
                      COLUMNS title VARCHAR2 (250 CHAR) PATH 'title',
                              link VARCHAR2 (250 CHAR) PATH 'link',
                              author VARCHAR2 (250 CHAR) PATH 'author',
                              description CLOB PATH 'description',
                              pubdate VARCHAR2 (50) PATH 'pubDate'));
&lt;/pre&gt;

&lt;p&gt;And the same SQL when reading RSS directly from internet over HTTP (BFile is replaced with HttpUriType).&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SELECT title,
       link,
       description,
       author,
       pubdate,
       CAST (pubdate AT TIME ZONE SESSIONTIMEZONE AS DATE) pubdate_local
  FROM (    SELECT title,
                   link,
                   description,
                   author,
                   TO_TIMESTAMP_TZ (
                      REPLACE(pubdate, 'PDT', 'PST PDT'),
                      CASE
                         WHEN REGEXP_LIKE (pubdate, '[[:digit:]]{4}$')
                         THEN
                            'Dy, dd fmMonth YYYY HH24:MI:SS TZHTZM'
                         ELSE
                            'Dy, dd fmMonth YYYY HH24:MI:SS TZR TZD'
                      END,
                      'NLS_DATE_LANGUAGE = American')
                      pubdate
              FROM XMLTABLE (
                      '/rss/channel/item'
                      PASSING HttpUriType('http://feeds.feedburner.com/DougsOracleBlog').getXML()
                      COLUMNS title VARCHAR2 (250 CHAR) PATH 'title',
                              link VARCHAR2 (250 CHAR) PATH 'link',
                              author VARCHAR2 (250 CHAR) PATH 'author',
                              description CLOB PATH 'description',
                              pubdate VARCHAR2 (50) PATH 'pubDate'));
&lt;/pre&gt;


&lt;p&gt;Thanks to Daniel Morgans session today at EMEA Harmony, I now have a personal goal to get the Oracle ACE title :) Need to get working towards it and become more active in the community.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-7658581803310667398?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=cAu_m9DQeJo:NBx8um5wcns:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=cAu_m9DQeJo:NBx8um5wcns:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=cAu_m9DQeJo:NBx8um5wcns:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=cAu_m9DQeJo:NBx8um5wcns:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=cAu_m9DQeJo:NBx8um5wcns:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/cAu_m9DQeJo" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/05/reading-rss-with-sql.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-1881008695708393911</guid><pubDate>Thu, 11 Mar 2010 07:03:00 +0000</pubDate><atom:updated>2010-03-11T10:31:44.778+02:00</atom:updated><title>20. - 21. May EMEA Harmony Conference in Tallinn</title><description>&lt;p&gt;An excellent joint conference for Estonian, Finnish, Latvian and Russian Oracle User Groups in Tallinn, 20. - 21. May 2010.&lt;br/&gt;
Speakers also include Tom Kyte, Tanel Põder, Chris J. Date and Steven Feuerstein.&lt;/p&gt;

&lt;p&gt;Read the agenda &lt;a href="http://ougf.fi/index.php?option=com_docman&amp;task=doc_download&amp;gid=305&amp;Itemid=42%E2%8C%A9%3Dfi&amp;lang=en"&gt;here&lt;/a&gt; and register in &lt;a href="http://ougf.fi/index.php?lang=en"&gt;OUGF home page&lt;/a&gt; (250€+VAT registration fee).&lt;/p&gt;

&lt;p&gt;In addition, just before the conference, 17.-18. May, Chris Date will perform his "How to Write Correct SQL and Know It: A Relational Approach to SQL" seminar in Helsinki. &lt;a href="http://ougf.fi/index.php?option=com_content&amp;view=article&amp;id=85%3Acj-date-seminaari&amp;catid=923&amp;Itemid=2&amp;lang=en"&gt;More info here&lt;/a&gt; and more detailed information &lt;a href="http://www.kantamestarit.fi/Files/Chris_Date_Seminar_Helsinki_2010.pdf"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-1881008695708393911?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=xFbAr31Ds9Q:4E9TTGhnW7c:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=xFbAr31Ds9Q:4E9TTGhnW7c:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=xFbAr31Ds9Q:4E9TTGhnW7c:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=xFbAr31Ds9Q:4E9TTGhnW7c:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=xFbAr31Ds9Q:4E9TTGhnW7c:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/xFbAr31Ds9Q" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/03/20-21-may-emea-harmony-conference-in.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-3150737770295002394</guid><pubDate>Wed, 20 Jan 2010 13:10:00 +0000</pubDate><atom:updated>2010-01-23T08:51:46.696+02:00</atom:updated><title>The easy way of working with XML in Oracle database (part 2)</title><description>&lt;p&gt;In this part I'll look at some features of Oracle Database for getting the data out in XML format, with SQL code only. In &lt;a href="/2010/01/easy-way-of-working-with-xml-in-oracle.html"&gt;part 1&lt;/a&gt; I looked at some features for reading/parsing XML with pure SQL.&lt;/p&gt;

&lt;h2&gt;Populating the tables for examples&lt;/h2&gt;

Loading the departments into table DEPARTMENTS.

&lt;pre class="brush: sql;"&gt;
CREATE TABLE departments AS
   SELECT dep.*
     FROM xml, 
     XMLTABLE ('/company/employees/item[1]/departments/item'
        PASSING x
        COLUMNS id FOR ORDINALITY,
                name VARCHAR2 (100 CHAR) PATH 'name') dep;
&lt;/pre&gt;

&lt;p&gt;Employee data to table EMPLOYEES.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE TABLE employees AS
   SELECT emp.id,
          emp.first_name,
          emp.last_name,
          emp.country,
          dep.id active_department_id
     FROM xml,
          departments dep,
          XMLTABLE (
             '/company/employees/item'
                PASSING xml.x
                COLUMNS id NUMBER PATH '@id',
                        first_name VARCHAR2 (10 CHAR) PATH 'first_name',
                        last_name VARCHAR2 (10 CHAR) PATH 'last_name',
                        country VARCHAR2 (10 CHAR) PATH 'country',
                        active_department VARCHAR2 (20 CHAR) PATH 'departments/item[@active="true"]/name') emp
    WHERE dep.name = emp.active_department;
&lt;/pre&gt;

&lt;h2&gt;Automatic XML generation + XML Stylesheet&lt;/h2&gt;

&lt;p&gt;Oracle provides functions sys_xmlgen and sys_xmlagg to automatically generate XML document based on table data. sys_xmlgen adds XML tags around a single value (returns XMLType data type from a single value) and sys_xmlagg aggregates/encloses rows into one XML document. Both of these functions also accept XMLFormat formatting parameter, that can be used to override the default enclosing XML tag name.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SELECT SYS_XMLAGG (x, XMLFormat (enclTag =&gt; 'COMPANY')) company_xml
  FROM (SELECT SYS_XMLAGG (emp_xml, XMLFormat (enclTag =&gt; 'EMPLOYEES')) x
          FROM (  SELECT tmp_id,
                         SYS_XMLAGG (xml_col, XMLFormat (enclTag =&gt; 'ITEM')) emp_xml
                    FROM (SELECT *
                            FROM (SELECT e.id tmp_id,
                                         SYS_XMLGEN (e.id) id,
                                         SYS_XMLGEN (e.first_name) first_name,
                                         SYS_XMLGEN (e.last_name) last_name,
                                         SYS_XMLGEN (e.country) country,
                                         SYS_XMLGEN (d.name, XMLFormat (enclTag =&gt; 'ACTIVE_DEPARTMENT')) active_department
                                    FROM employees e, departments d
                                   WHERE e.active_department_id = d.id) UNPIVOT ((xml_col)
                                                                        FOR tmp_col
                                                                        IN (id, first_name, last_name, 
                                                                          country, active_department)))
                GROUP BY tmp_id)
        UNION ALL
        SELECT SYS_XMLGEN ('Some Comany name', XMLFormat (enclTag =&gt; 'NAME')) x
          FROM DUAL)
&lt;/pre&gt;

&lt;p&gt;The query is quite long, but the idea behind it is pretty simple - generate a XMLType row for every data attribute and use SYS_XMLAGG to group and aggregate the generated XMLType rows into desirable XML format.&lt;/p&gt;

&lt;p&gt;And the result is one XML document.&lt;/p&gt;

&lt;pre class="brush: xml;"&gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;COMPANY&amp;gt;
&amp;lt;EMPLOYEES&amp;gt;
  &amp;lt;ITEM&amp;gt;
    &amp;lt;ID&amp;gt;1&amp;lt;/ID&amp;gt;
    &amp;lt;ACTIVE_DEPARTMENT&amp;gt;DBA&amp;lt;/ACTIVE_DEPARTMENT&amp;gt;
    &amp;lt;COUNTRY&amp;gt;Estonia&amp;lt;/COUNTRY&amp;gt;
    &amp;lt;LAST_NAME&amp;gt;Kerm&amp;lt;/LAST_NAME&amp;gt;
    &amp;lt;FIRST_NAME&amp;gt;Ilmar&amp;lt;/FIRST_NAME&amp;gt;
  &amp;lt;/ITEM&amp;gt;
  &amp;lt;ITEM&amp;gt;
    &amp;lt;ID&amp;gt;2&amp;lt;/ID&amp;gt;
    &amp;lt;ACTIVE_DEPARTMENT&amp;gt;Development&amp;lt;/ACTIVE_DEPARTMENT&amp;gt;
    &amp;lt;COUNTRY&amp;gt;Estonia2&amp;lt;/COUNTRY&amp;gt;
    &amp;lt;LAST_NAME&amp;gt;Kerm2&amp;lt;/LAST_NAME&amp;gt;
    &amp;lt;FIRST_NAME&amp;gt;Ilmar2&amp;lt;/FIRST_NAME&amp;gt;
  &amp;lt;/ITEM&amp;gt;
&amp;lt;/EMPLOYEES&amp;gt;
&amp;lt;NAME&amp;gt;Some Comany name&amp;lt;/NAME&amp;gt;
&amp;lt;/COMPANY&amp;gt;
&lt;/pre&gt;

&lt;p&gt;If the XML is still not at the correct format, then &lt;a href="http://www.w3schools.com/xsl/"&gt;XSL Transformation&lt;/a&gt; can be applied to convert the output to another XML format.&lt;/p&gt;

&lt;p&gt;XSL Transformation is an XML document, that describes the rules how to convert one XML document to another XML. For this example I wrote the following XSLT document:&lt;/p&gt;

&lt;pre class="brush: xml;"&gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;

&amp;lt;xsl:template match="COMPANY"&amp;gt;
  &amp;lt;company&amp;gt;
    &amp;lt;name&amp;gt;&amp;lt;xsl:value-of select="NAME"/&amp;gt;&amp;lt;/name&amp;gt;
  &amp;lt;/company&amp;gt;
  &amp;lt;employees&amp;gt;
    &amp;lt;xsl:for-each select="EMPLOYEES/ITEM"&amp;gt;
      &amp;lt;item&amp;gt;
        &amp;lt;xsl:attribute name="id"&amp;gt;
          &amp;lt;xsl:value-of select="ID" /&amp;gt;
        &amp;lt;/xsl:attribute&amp;gt;
        &amp;lt;first_name&amp;gt;&amp;lt;xsl:value-of select="FIRST_NAME" /&amp;gt;&amp;lt;/first_name&amp;gt;
        &amp;lt;last_name&amp;gt;&amp;lt;xsl:value-of select="LAST_NAME" /&amp;gt;&amp;lt;/last_name&amp;gt;
        &amp;lt;country&amp;gt;&amp;lt;xsl:value-of select="COUNTRY" /&amp;gt;&amp;lt;/country&amp;gt;
        &amp;lt;departments&amp;gt;
          &amp;lt;item active="true"&amp;gt;
            &amp;lt;name&amp;gt;&amp;lt;xsl:value-of select="ACTIVE_DEPARTMENT" /&amp;gt;&amp;lt;/name&amp;gt;
          &amp;lt;/item&amp;gt;
        &amp;lt;/departments&amp;gt;
      &amp;lt;/item&amp;gt;
    &amp;lt;/xsl:for-each&amp;gt;
  &amp;lt;/employees&amp;gt;
&amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Save it to XML table named comp_xml_cslt.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE TABLE comp_xml_xslt of xmltype;

INSERT INTO comp_xml_xslt VALUES ('&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;
...
&amp;lt;/xsl:stylesheet&amp;gt;
');

COMMIT;
&lt;/pre&gt;

&lt;p&gt;To apply the XSL Transformation to source XML, Oracle has a function &lt;b&gt;XMLTransform(source_xml, transformation_xml)&lt;/b&gt;. Both arguments are XML documents of XMLType and this function returns transformed XML document.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
WITH company AS 
  (SELECT SYS_XMLAGG (x, XMLFormat (enclTag =&gt; 'COMPANY')) xml
     FROM (SELECT SYS_XMLAGG (
                     emp_xml,
                     XMLFormat (enclTag =&gt; 'EMPLOYEES'))
                     x
             FROM (  SELECT tmp_id,
                            SYS_XMLAGG (
                               xml_col,
                               XMLFormat (enclTag =&gt; 'ITEM'))
                               emp_xml
                       FROM (SELECT *
                               FROM (SELECT e.id tmp_id,
                                            SYS_XMLGEN (e.id) id,
                                            SYS_XMLGEN (e.first_name) first_name,
                                            SYS_XMLGEN (e.last_name) last_name,
                                            SYS_XMLGEN (e.country) country,
                                            SYS_XMLGEN (d.name,
                                               XMLFormat (enclTag =&gt; 'ACTIVE_DEPARTMENT')) active_department
                                       FROM employees e,
                                            departments d
                                      WHERE e.active_department_id =
                                               d.id) UNPIVOT ((xml_col)
                                                     FOR tmp_col
                                                     IN (id, first_name, last_name, country, active_department)))
                   GROUP BY tmp_id)
           UNION ALL
           SELECT SYS_XMLGEN ('Some Comany name',
                              XMLFormat (enclTag =&gt; 'NAME'))
                     x
             FROM DUAL))
SELECT XMLTRANSFORM (company.xml, comp_xml_xslt.object_value)
  FROM company, comp_xml_xslt
&lt;/pre&gt;

&lt;p&gt;In this query, COMPANY inline view is the same XML generating query as in the example before, but the XMLTRANSFORM function in main query is used apply the transformation. Result is below:&lt;/p&gt;

&lt;pre class="brush: xml;"&gt;
&amp;lt;company&amp;gt;
 &amp;lt;name&amp;gt;Some Comany name&amp;lt;/name&amp;gt;
&amp;lt;/company&amp;gt;
&amp;lt;employees&amp;gt;
 &amp;lt;item id="1"&amp;gt;
  &amp;lt;first_name&amp;gt;Ilmar&amp;lt;/first_name&amp;gt;
  &amp;lt;last_name&amp;gt;Kerm&amp;lt;/last_name&amp;gt;
  &amp;lt;country&amp;gt;Estonia&amp;lt;/country&amp;gt;
  &amp;lt;departments&amp;gt;
   &amp;lt;item active="true"&amp;gt;
    &amp;lt;name&amp;gt;DBA&amp;lt;/name&amp;gt;
   &amp;lt;/item&amp;gt;
  &amp;lt;/departments&amp;gt;
 &amp;lt;/item&amp;gt;
 &amp;lt;item id="2"&amp;gt;
  &amp;lt;first_name&amp;gt;Ilmar2&amp;lt;/first_name&amp;gt;
  &amp;lt;last_name&amp;gt;Kerm2&amp;lt;/last_name&amp;gt;
  &amp;lt;country&amp;gt;Estonia2&amp;lt;/country&amp;gt;
  &amp;lt;departments&amp;gt;
   &amp;lt;item active="true"&amp;gt;
    &amp;lt;name&amp;gt;Development&amp;lt;/name&amp;gt;
   &amp;lt;/item&amp;gt;
  &amp;lt;/departments&amp;gt;
 &amp;lt;/item&amp;gt;
&amp;lt;/employees&amp;gt;
&lt;/pre&gt;

&lt;h2&gt;XQuery&lt;/h2&gt;

&lt;p&gt;Oracle also implements the standard XQuery language, that can be used for querying XML data and Oracle has provided some XQuery views to access relational database tables.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SELECT XMLQuery(
         'for $emp in ora:view("EMPLOYEES")/ROW
          return &amp;lt;item id="{$emp/ID}"&amp;gt;
                 {$emp/FIRST_NAME}
                 {$emp/LAST_NAME}
                 {$emp/COUNTRY}
                 &amp;lt;departments&amp;gt;
                   {for $dep in ora:view("DEPARTMENTS")/ROW 
                    return &amp;lt;item active=&amp;quot;{if ($dep/ID eq $emp/ACTIVE_DEPARTMENT_ID) then &amp;quot;true&amp;quot; else (&amp;quot;false&amp;quot;)}&amp;quot;&amp;gt;{$dep/NAME}&amp;lt;/item&amp;gt;}
                &amp;lt;/departments&amp;gt;
                &amp;lt;/item&amp;gt;'
          RETURNING CONTENT) x FROM DUAL
&lt;/pre&gt;

&lt;p&gt;And the XML result is:&lt;/p&gt;

&lt;pre class="brush: xml;"&gt;
&amp;lt;item id="1"&amp;gt;&amp;lt;FIRST_NAME&amp;gt;Ilmar&amp;lt;/FIRST_NAME&amp;gt;&amp;lt;LAST_NAME&amp;gt;Kerm&amp;lt;/LAST_NAME&amp;gt;&amp;lt;COUNTRY&amp;gt;Estonia&amp;lt;/COUNTRY&amp;gt;&amp;lt;departments&amp;gt;&amp;lt;item active="false"&amp;gt;&amp;lt;NAME&amp;gt;Development&amp;lt;/NAME&amp;gt;&amp;lt;/item&amp;gt;&amp;lt;item active="true"&amp;gt;&amp;lt;NAME&amp;gt;DBA&amp;lt;/NAME&amp;gt;&amp;lt;/item&amp;gt;&amp;lt;/departments&amp;gt;&amp;lt;/item&amp;gt;&amp;lt;item id="2"&amp;gt;&amp;lt;FIRST_NAME&amp;gt;Ilmar2&amp;lt;/FIRST_NAME&amp;gt;&amp;lt;LAST_NAME&amp;gt;Kerm2&amp;lt;/LAST_NAME&amp;gt;&amp;lt;COUNTRY&amp;gt;Estonia2&amp;lt;/COUNTRY&amp;gt;&amp;lt;departments&amp;gt;&amp;lt;item active="true"&amp;gt;&amp;lt;NAME&amp;gt;Development&amp;lt;/NAME&amp;gt;&amp;lt;/item&amp;gt;&amp;lt;item active="false"&amp;gt;&amp;lt;NAME&amp;gt;DBA&amp;lt;/NAME&amp;gt;&amp;lt;/item&amp;gt;&amp;lt;/departments&amp;gt;&amp;lt;/item&amp;gt;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-3150737770295002394?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=lQ07lZj0168:_ZiTiHiw8xU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=lQ07lZj0168:_ZiTiHiw8xU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=lQ07lZj0168:_ZiTiHiw8xU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=lQ07lZj0168:_ZiTiHiw8xU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=lQ07lZj0168:_ZiTiHiw8xU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/lQ07lZj0168" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/01/easy-way-of-working-with-xml-in-oracle_20.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-5324400499738558775</guid><pubDate>Tue, 19 Jan 2010 16:42:00 +0000</pubDate><atom:updated>2010-01-20T11:11:41.471+02:00</atom:updated><title>The easy way of working with XML in Oracle database (part 1)</title><description>&lt;p&gt;I have tried to read Oracle XMLDB Developer Guide and my first reaction was that parsing XML is very complicated in Oracle... And lately I got a task to rewrite some PL/SQL code to use a different SOAP service and the existing code had a few hundred rows just to parse XML (with XML DOM API)! Actually, starting from 10.2, this job is much easier...&lt;/p&gt;

&lt;p&gt;Here I will go over some methods for working with XML, that are usable directly from SQL. In this first part, reading and extracting data from XML.&lt;/p&gt;

&lt;h2&gt;XML for the examples&lt;/h2&gt;

&lt;pre class="brush: xml;"&gt;
&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&lt;company&gt;
  &lt;name&gt;Some Company name&lt;/name&gt;
  &lt;employees&gt;
    &lt;item id="1"&gt;
      &lt;first_name&gt;Ilmar&lt;/first_name&gt;
      &lt;last_name&gt;Kerm&lt;/last_name&gt;
      &lt;country&gt;Estonia&lt;/country&gt;
      &lt;departments&gt;
        &lt;item active="false"&gt;
          &lt;name&gt;Development&lt;/name&gt;
        &lt;/item&gt;
        &lt;item active="true"&gt;
          &lt;name&gt;DBA&lt;/name&gt;
        &lt;/item&gt;
      &lt;/departments&gt;
    &lt;/item&gt;
    &lt;item id="2"&gt;
      &lt;first_name&gt;Ilmar2&lt;/first_name&gt;
      &lt;last_name&gt;Kerm2&lt;/last_name&gt;
      &lt;country&gt;Estonia2&lt;/country&gt;
      &lt;departments&gt;
        &lt;item active="true"&gt;
          &lt;name&gt;Development&lt;/name&gt;
        &lt;/item&gt;
        &lt;item active="false"&gt;
          &lt;name&gt;DBA&lt;/name&gt;
        &lt;/item&gt;
      &lt;/departments&gt;
    &lt;/item&gt;
  &lt;/employees&gt;
&lt;/company&gt;
&lt;/pre&gt;

&lt;p&gt;Load the XML data to a table, to special XMLTYPE data type.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
create table xml (
  x xmltype
);

insert into xml values (
xmltype.createxml('&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&lt;company&gt;
  &lt;name&gt;Some Company name&lt;/name&gt;
  &lt;employees&gt;
...
  &lt;/employees&gt;
&lt;/company&gt;'));

COMMIT;
&lt;/pre&gt;

&lt;h2&gt;Extracting a single value&lt;/h2&gt;

&lt;p&gt;&lt;b&gt;extractValue&lt;/b&gt; can be used to extract a single value from XML using &lt;a href="http://www.w3schools.com/Xpath/"&gt;XPath expression&lt;/a&gt;.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; SELECT extractValue(x, '/company/name') FROM xml;

EXTRACTVALUE(X,'/COMPANY/NAME')
-------------------------------
Some Company name
&lt;/pre&gt;

extractValue only works with single values, otherwise exception will be raised.

&lt;pre class="brush: sql;"&gt;
SQL&gt; SELECT extractValue(x, '/company/employees/item') FROM xml;
SELECT extractValue(x, '/company/employees/item') FROM xml
*
ERROR at line 1:
ORA-19025: EXTRACTVALUE returns value of only one node
&lt;/pre&gt;

&lt;h2&gt;Query XML as relational object&lt;/h2&gt;

&lt;p&gt;Oracle Database, since 10g, has a very easy way to map XML to a relational object and query it with SQL, so no PL/SQL code is needed for parsing XML - the XMLTABLE function.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; SELECT emp.*
  FROM xml,
       XMLTABLE (
         '/company/employees/item'
         PASSING x
         COLUMNS id NUMBER PATH '@id',
                 first_name VARCHAR2 (10 CHAR) PATH 'first_name',
                 last_name VARCHAR2 (10 CHAR) PATH 'last_name',
                 country VARCHAR2 (10 CHAR) PATH 'country',
                 active_department VARCHAR2 (20 CHAR) PATH 'departments/item[@active="true"]/name'
       ) emp;

        ID FIRST_NAME LAST_NAME  COUNTRY    ACTIVE_DEPARTMENT
---------- ---------- ---------- ---------- --------------------
         1 Ilmar      Kerm       Estonia    DBA
         2 Ilmar2     Kerm2      Estonia2   Development
&lt;/pre&gt;

&lt;p&gt;XmlTable takes the following arguments:&lt;/p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;&lt;td&gt;'/company/employees/item'&lt;/td&gt;&lt;td&gt;XPath expression of the "row"&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;PASSING x&lt;/td&gt;&lt;td&gt;Source of XML data - XMLTYPE data type&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;COLUMNS&lt;br/&gt;id NUMBER PATH '@id',&lt;br/&gt;
first_name VARCHAR2 (10 CHAR) PATH 'first_name',&lt;br/&gt;
last_name VARCHAR2 (10 CHAR) PATH 'last_name',&lt;br/&gt;
country VARCHAR2 (10 CHAR) PATH 'country',&lt;br/&gt;
active_department VARCHAR2 (20 CHAR) PATH 'departments/item[@active="true"]/name'
&lt;/td&gt;&lt;td&gt;Column definitions with the corresponding Oracle data type and XPath expression&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;After XML is readable as a relational table, all the power of Oracle SQL can be used for querying.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-5324400499738558775?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=2LOJpd3SoCg:APiu65z5M2U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=2LOJpd3SoCg:APiu65z5M2U:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=2LOJpd3SoCg:APiu65z5M2U:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=2LOJpd3SoCg:APiu65z5M2U:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=2LOJpd3SoCg:APiu65z5M2U:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/2LOJpd3SoCg" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2010/01/easy-way-of-working-with-xml-in-oracle.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-5740422133324166096</guid><pubDate>Fri, 06 Nov 2009 11:40:00 +0000</pubDate><atom:updated>2009-11-06T14:21:27.608+02:00</atom:updated><title>Oracle Enterprise Manager Desktop Widgets</title><description>&lt;p&gt;I love database performance visualization tools and now Oracle has released small desktop widgets that give a quick overview of the targets in OEM Grid Control.&lt;/p&gt;

&lt;p&gt;The one I really like is High-Load Databases widget. That shows a quick overview of how the top databases are performing at the moment - total number of average active sessions and a graph how this load is divided between the CPU/IO/Other wait classes.&lt;/p&gt;

&lt;img src="http://blogs.oracle.com/emwidgets/Highload.png" border="0"/&gt;

&lt;p&gt;With a click of a button, the screen changes to the new rectangular style performance graph, also showing the latest ADDM findings.&lt;/p&gt;

&lt;img src="http://ilmar.laksrecordings.com/blog/widget2.png" border="0"/&gt;

&lt;p&gt;One feature request to Oracle - please add more lightweight skin and allow the user to configure how many databases the widget shows. I'd like to set that widget "stay on top", but the view with TOP 5 databases takes too much screen space.&lt;/p&gt;

&lt;p&gt;Download the widgets here: &lt;a href="http://www.oracle.com/technology/products/oem/widgets/index.html"&gt;http://www.oracle.com/technology/products/oem/widgets/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Blog for these widgets: &lt;a href="http://blogs.oracle.com/emwidgets/"&gt;http://blogs.oracle.com/emwidgets/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Actually, this is the first application I'd like to have running on my mobile device...&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-5740422133324166096?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=vVLtfu7E45c:gJJGE8sDYIA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=vVLtfu7E45c:gJJGE8sDYIA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=vVLtfu7E45c:gJJGE8sDYIA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=vVLtfu7E45c:gJJGE8sDYIA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=vVLtfu7E45c:gJJGE8sDYIA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/vVLtfu7E45c" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/11/oracle-enterprise-manager-desktop.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-5496263692641473281</guid><pubDate>Tue, 27 Oct 2009 09:34:00 +0000</pubDate><atom:updated>2009-10-27T13:47:50.365+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">xdb</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>Hello World with Oracle XE</title><description>&lt;p&gt;I needed to develop one small application that communicates with external mobile devices, being like a central server where mobile clients send their data and receive configuration parameters. Reporting will be added later using Oracle APEX.
It's a small application for a customer demo, so it sounded like a perfect opportunity to have my first look at the Oracle XE database :)&lt;/p&gt;

&lt;p&gt;Oracle XE comes with a pre-configured XDB HTTP listener for APEX.
I don't need the full APEX engine for my simple request server, just a PL/SQL package using PL/SQL Web Toolkit will do just fine and there is no need to use PHP or some other scripting engine outside the database.&lt;/p&gt;

&lt;p&gt;Here are the steps I used to configure Oracle XE XDB HTTP listener with additional path (DAD) to my custom procedure.&lt;/p&gt;

Make HTTP listener to listen on all network interfaces, not only localhost:
&lt;pre class="brush: sql;"&gt;
EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);
&lt;/pre&gt;

&lt;p&gt;Oracle XE has pre-defined DAD for APEX - /apex/&lt;br/&gt;
The following code will create a new DAD /mobileapp/, associate it with schema MOBILEAPP and authorize it to run without asking the user for a password.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
BEGIN
  DBMS_EPG.create_dad (
    dad_name =&gt; 'mobileapp',
    path     =&gt; '/mobileapp/*');

  DBMS_EPG.set_dad_attribute (
    dad_name   =&gt; 'mobileapp',
    attr_name  =&gt; 'default-page',
    attr_value =&gt; 'home');

  DBMS_EPG.set_dad_attribute (
    dad_name   =&gt; 'mobileapp',
    attr_name  =&gt; 'database-username',
    attr_value =&gt; 'MOBILEAPP');

  DBMS_EPG.set_dad_attribute (
    dad_name   =&gt; 'mobileapp',
    attr_name  =&gt; 'nls-language',
    attr_value =&gt; 'american_america.al32utf8');

  DBMS_EPG.authorize_dad (
    dad_name =&gt; 'mobileapp',
    user     =&gt; 'MOBILEAPP');
END;
/
&lt;/pre&gt;

&lt;p&gt;I used the following DAD attributes:&lt;/p&gt;

&lt;table border="1"&gt;
&lt;tr&gt;&lt;th&gt;default-page&lt;/th&gt;&lt;td&gt;specifies the default procedure name, that will be used when the procedure name is not specified in a HTTP URL&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;database-username&lt;/th&gt;&lt;td&gt;associated database schema name&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;nls-language&lt;/th&gt;&lt;td&gt;NLS client value, for a web application the important part is the charset. al32utf8 means the result page is in UTF-8.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;DBMS_EPG.authorize_dad&lt;/th&gt;&lt;td&gt;This will disable the HTTP basic authentication&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;There are other interesting DAD attributes and the current XDB HTTP configuration and APEX DAD configuration can be views using this SQL:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; SELECT   DBMS_XDB.CFG_GET().getClobVal() FROM DUAL;
&lt;/pre&gt;

&lt;pre class="brush: xml;"&gt;
...
              &lt;servlet xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;
                &lt;servlet-name&gt;APEX&lt;/servlet-name&gt;
                &lt;servlet-language&gt;PL/SQL&lt;/servlet-language&gt;
                &lt;display-name&gt;APEX&lt;/display-name&gt;
                &lt;plsql xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;
                  &lt;database-username xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;ANONYMOUS&lt;/database-username&gt;
                  &lt;default-page xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;apex&lt;/default-page&gt;
                  &lt;document-table-name xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;wwv_flow_file_objects$&lt;/document-table-name&gt;
                  &lt;document-path xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;docs&lt;/document-path&gt;
                  &lt;document-procedure xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;wwv_flow_file_mgr.process_download&lt;/document-procedure&gt;
                  &lt;nls-language xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;american_america.al32utf8&lt;/nls-language&gt;
                  &lt;request-validation-function xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"&gt;wwv_flow_epg_include_modules.authorize&lt;/request-validation-function&gt;
                &lt;/plsql&gt;
              &lt;/servlet&gt;
...
&lt;/pre&gt;

&lt;p&gt;Now all MOBILEAPP PL/SQL procedures and packages can be accessed over HTTP and the HTTP URL has the following form:&lt;br/&gt;
&lt;b&gt;http://oraclexeserver:8080/mobileapp/procedurename&lt;/b&gt;&lt;br/&gt;
or&lt;br/&gt;
&lt;b&gt;http://oraclexeserver:8080/mobileapp/packagename.procedurename&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;So, a simple hello world procedure using PL/SQL Web Toolkit could look like this:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE OR REPLACE PROCEDURE mobileapp.home IS
BEGIN
  HTP.htmlopen;
  HTP.headopen;
  HTP.title('Page title');
  HTP.headclose;
  HTP.bodyopen;
  HTP.print('Hello world');
  HTP.bodyclose;
  HTP.htmlclose;
END;
/
&lt;/pre&gt;

&lt;p&gt;HTTP URL for this procedure will be &lt;b&gt;http://oraclexeserver:8080/mobileapp/home&lt;/b&gt; or just &lt;b&gt;http://oraclexeserver:8080/mobileapp/&lt;/b&gt; (note the &lt;b&gt;default-page&lt;/b&gt; DAD attribute).&lt;br/&gt;
The proceudres like &lt;b&gt;HTP.htmlopen&lt;/b&gt; will just generate HTML tags.&lt;/p&gt;

&lt;p&gt;Passing HTTP GET parameters is very easy and they will be set as a procedure parameters with the same name. Parameter data type is VARCHAR2 or if HTTP request has more than one parameter with the same name, then TABLE OF VARCHAR2 data type is used.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE OR REPLACE PROCEDURE mobileapp.paramtest(p1 IN VARCHAR2 DEFAULT NULL,
  p2 IN VARCHAR2 DEFAULT NULL) IS
BEGIN
  IF p1 IS NULL THEN
    HTP.p('Hello world');
  ELSE
    HTP.p('Hello p1='||p1||' p2='||p2);
  END IF;
END;
/
&lt;/pre&gt;

&lt;p&gt;This procedure can be called with HTTP URL:&lt;br/&gt;
&lt;b&gt;http://oraclexeserver:8080/mobileapp/paramtest?p1=value1&amp;p2=value2&lt;/b&gt;&lt;br/&gt;
Both parameters are optional (procedure parameters have DEFAULT value set).&lt;/p&gt;

&lt;p&gt;If the procedure raises an exception or suitable procedure name with correct argument list cannot be found, then HTTP error code 404 (page not found) is the result.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-5496263692641473281?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=daWBZwl01NU:5dZTa-TNt4Y:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=daWBZwl01NU:5dZTa-TNt4Y:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=daWBZwl01NU:5dZTa-TNt4Y:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=daWBZwl01NU:5dZTa-TNt4Y:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=daWBZwl01NU:5dZTa-TNt4Y:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/daWBZwl01NU" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/10/hello-world-with-oracle-xe.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-1616621791838135474</guid><pubDate>Tue, 04 Aug 2009 20:20:00 +0000</pubDate><atom:updated>2009-08-15T20:06:43.411+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">heterogeneous</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>High performance data unloading from Oracle to ODBC database</title><description>I got a task some time ago, to copy a-lot-of-millions of rows from Oracle to MySQL. As fast as possible.&lt;br /&gt;
In the last year I have used Oracle Heterogeneous Services (HSODBC) a lot to select data from different non-Oracle databases, but I had yet to try to unload data from Oracle with best performance.&lt;br /&gt;&lt;br /&gt;

My first thought was, of course, that the process needs to run in parallel. Here I'll first take a look at the overall HSODBC performance and then look how can I make this process run in parallel.&lt;br /&gt;&lt;br /&gt;

In this test I use Oracle Database 11.1.0.7 as source, Oracle Heterogeneous Services 10.2.0.4 with MyODBC 5.1 driver, MySQL 5.1 destination database. In my setup, I have all these three components running on separate servers.&lt;br /&gt;&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; CREATE TABLE hsperftest
AS
   SELECT   * FROM all_objects
   UNION ALL
   SELECT   * FROM all_objects
   UNION ALL
   SELECT   * FROM all_objects;

SQL&gt; DESC hsperftest
 Name                                        Null?    Type
 ------------------------------------------- -------- ---------------------
 OWNER                                                VARCHAR2(30)
 OBJECT_NAME                                          VARCHAR2(30)
 SUBOBJECT_NAME                                       VARCHAR2(30)
 OBJECT_ID                                            NUMBER
 DATA_OBJECT_ID                                       NUMBER
 OBJECT_TYPE                                          VARCHAR2(19)
 CREATED                                              DATE
 LAST_DDL_TIME                                        DATE
 TIMESTAMP                                            VARCHAR2(19)
 STATUS                                               VARCHAR2(7)
 TEMPORARY                                            VARCHAR2(1)
 GENERATED                                            VARCHAR2(1)
 SECONDARY                                            VARCHAR2(1)
 NAMESPACE                                            NUMBER
 EDITION_NAME                                         VARCHAR2(30)
 ID                                          NOT NULL NUMBER(10)
&lt;/pre&gt;
&lt;br /&gt;
And I create the same table in MySQL.&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
CREATE TABLE `hsperftest` (
  `OWNER` varchar(30) DEFAULT NULL,
  `OBJECT_NAME` varchar(30) DEFAULT NULL,
  `SUBOBJECT_NAME` varchar(30) DEFAULT NULL,
  `OBJECT_ID` int(11) DEFAULT NULL,
  `DATA_OBJECT_ID` int(11) DEFAULT NULL,
  `OBJECT_TYPE` varchar(19) DEFAULT NULL,
  `CREATED` datetime DEFAULT NULL,
  `LAST_DDL_TIME` datetime DEFAULT NULL,
  `TIMESTAMP` varchar(19) DEFAULT NULL,
  `STATUS` varchar(7) DEFAULT NULL,
  `TEMPORARY` varchar(1) DEFAULT NULL,
  `GENERATED` varchar(1) DEFAULT NULL,
  `SECONDARY` varchar(1) DEFAULT NULL,
  `NAMESPACE` int(11) DEFAULT NULL,
  `EDITION_NAME` varchar(30) DEFAULT NULL,
  `ID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
&lt;/pre&gt;
&lt;br /&gt;
Set up HSODBC to connect to the MySQL database and create a database link on Oracle side. When setting up HSODBC, do not enable logging/tracing (HS_FDS_TRACE_LEVEL = off)! Tracing will serialize all database calls to the remote database.&lt;br /&gt;&lt;br /&gt;

I tested this with HSODBC running on Windows Vista and Linux (using unixODBC). The results were similar.&lt;br /&gt;&lt;br /&gt;

Test that MySQL table is accessible for Oracle:&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
SQL&gt; DESC "hsperftest"@hsdb
 Name                                                  Null?    Type
 ----------------------------------------------------- -------- -----------------
 OWNER                                                          VARCHAR2(30)
 OBJECT_NAME                                                    VARCHAR2(30)
 SUBOBJECT_NAME                                                 VARCHAR2(30)
 OBJECT_ID                                                      NUMBER(10)
 DATA_OBJECT_ID                                                 NUMBER(10)
 OBJECT_TYPE                                                    VARCHAR2(19)
 CREATED                                                        DATE
 LAST_DDL_TIME                                                  DATE
 TIMESTAMP                                                      VARCHAR2(19)
 STATUS                                                         VARCHAR2(7)
 TEMPORARY                                                      VARCHAR2(1)
 GENERATED                                                      VARCHAR2(1)
 SECONDARY                                                      VARCHAR2(1)
 NAMESPACE                                                      NUMBER(10)
 EDITION_NAME                                                   VARCHAR2(30)
 ID                                                    NOT NULL NUMBER(10)
&lt;/pre&gt;
&lt;br /&gt;
Before continuing, check that all MySQL data types are mapped correctly to Oracle. And make sure, that none of the columns are using deprecated LONG datatype.&lt;br /&gt;&lt;br /&gt;

First of all... the following SQL will not work:&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
SQL&gt; insert into "hsperftest"@hsdb select * from hsperftest;
insert into "hsperftest"@hsdb select * from hsperftest
                                     *
ERROR at line 1:
ORA-02025: all tables in the SQL statement must be at the remote database
&lt;/pre&gt;
&lt;br /&gt;

Some PL/SQL is needed to first select from Oracle cursor and then bind the values to the remote database INSERT statement. The easiest way is:&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
BEGIN
   FOR rec IN (SELECT   * FROM HSPERFTEST)
   LOOP
      INSERT INTO "hsperftest"@hsdb
        VALUES   rec;
   END LOOP;
   COMMIT;
END;
/

PL/SQL procedure successfully completed.

Elapsed: 00:14:28.38
&lt;/pre&gt;
&lt;br /&gt;

When running this anonymous block I noticed, that the load on Oracle database and MySQL database was very light, but the HSODBC CPU usage was at the maximum. So HSODBC seems to be the bottleneck. What is it doing? With this kind of CPU usage its not going to scale much in parallel. If its performing some mappings between Oracle SQL and MySQL SQL, then can I remove this bottleneck by using DBMS_HS_PASSTHROUGH, that sends the query unmodified to the remote database?&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
DECLARE
   p_c   BINARY_INTEGER;
   nr    INTEGER;
BEGIN
   p_c := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@hsdb;
   DBMS_HS_PASSTHROUGH.PARSE@hsdb (
      p_c,
      'insert into hsperftest (OWNER,OBJECT_NAME,SUBOBJECT_NAME,OBJECT_ID,
      DATA_OBJECT_ID,OBJECT_TYPE,CREATED,LAST_DDL_TIME,TIMESTAMP,STATUS,
      TEMPORARY,GENERATED,SECONDARY,NAMESPACE,EDITION_NAME,ID)
      VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
   );

   FOR rec IN (SELECT   * FROM HSPERFTEST)
   LOOP
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 1, rec.owner);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 2, rec.object_name);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 3, rec.SUBOBJECT_NAME);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 4, rec.OBJECT_ID);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 5, rec.DATA_OBJECT_ID);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 6, rec.OBJECT_TYPE);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 7, rec.CREATED);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 8, rec.LAST_DDL_TIME);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 9, rec.TIMESTAMP);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 10, rec.STATUS);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 11, rec.TEMPORARY);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 12, rec.GENERATED);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 13, rec.SECONDARY);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 14, rec.NAMESPACE);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 15, rec.EDITION_NAME);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 16, rec.ID);
      nr := DBMS_HS_PASSTHROUGH.EXECUTE_NON_QUERY@hsdb (p_c);
   END LOOP;

   DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@hsdb (p_c);
   COMMIT;
END;
/

PL/SQL procedure successfully completed.

Elapsed: 00:07:37.77
&lt;/pre&gt;
&lt;br /&gt;

That is a huge difference in execution time! And with DBMS_HS_PASSTHROUGH, the HSODBC process CPU usage is much less, making it more scalable running in parallel.&lt;br /&gt;&lt;br /&gt;

Now, how to run it in parallel?&lt;br /&gt;&lt;br /&gt;

Since the destination database is non-Oracle, the database built-in parallel execution cannot be used, so "DIY parallelism" is needed: split/partition the input data with some kind of criteria and start a new process for each partition.&lt;br /&gt;&lt;br /&gt;

In the real project I used each records CREATED_DATE value for partitioning, because underlying table was already partitioned by that column, but its also possible to use table ROWID values for partitioning. A good example of it is on book &lt;a href="http://books.google.com/books?id=TmPoYfpeJAUC&amp;printsec=frontcover#v=onepage&amp;q=&amp;f=false"&gt;Expert Oracle database architecture By Thomas Kyte&lt;/a&gt;, chapter 14, Parallel Execution, Do-It-Yourself Parallelism.&lt;br /&gt;&lt;br /&gt;

Here I'm going to split the table just into 3 pieces to make it simple. To get the maximum IO performance from Oracle, Tom Kytes example needs to be followed. But I'll make my example just simple.&lt;br /&gt;
Table hsperftest_job will keep the metadata for every parallel slave.&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
CREATE TABLE hsperftest_job
(
   job_no       NUMBER NOT NULL,
   low_id       ROWID NOT NULL,
   hi_id        ROWID NOT NULL,
   max_in       NUMBER NOT NULL,
   start_date   DATE,
   end_date     DATE,
   rows_processed   NUMBER
);
CREATE SEQUENCE seq_hsperftest_job;

DECLARE
  p_min rowid;
  p_max rowid;
  p_row1 rowid;
  p_row2 rowid;
  p_count number;
BEGIN
  SELECT MIN(rowid), MAX(rowid), COUNT(*) INTO p_min, p_max, p_count
    FROM hsperftest;
  SELECT MAX(a) INTO p_row1 FROM (
    SELECT rowid a FROM hsperftest WHERE rownum &lt; CEIL(p_count/3)
  );
  SELECT MAX(a) INTO p_row2 FROM (
    SELECT rowid a, rownum rn FROM hsperftest WHERE rowid &gt; p_row1 
  ) WHERE rn &lt; CEIL(p_count/3);
  INSERT INTO hsperftest_job (job_no, low_id, hi_id, max_in) VALUES (seq_hsperftest_job.nextval, p_min, p_row1, 0);
  INSERT INTO hsperftest_job (job_no, low_id, hi_id, max_in) VALUES (seq_hsperftest_job.nextval, p_row1, p_row2, 0);
  INSERT INTO hsperftest_job (job_no, low_id, hi_id, max_in) VALUES (seq_hsperftest_job.nextval, p_row2, p_max, 1);
  COMMIT;
END;
/

  SELECT   j.job_no, COUNT (1)
    FROM   hsperftest_job j, hsperftest h
   WHERE   h.ROWID BETWEEN j.low_id AND j.hi_id
GROUP BY   j.job_no;

    JOB_NO   COUNT(1)
---------- ----------
         1      59313
         2      59314
         3      59317
&lt;/pre&gt;
&lt;br /&gt;

Looks like my very simple ROWID hack is good enough for this test.&lt;br /&gt;&lt;br /&gt;

Now I'll create a procedure that will be executed in parallel.&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
CREATE OR REPLACE PROCEDURE hsperftest_proc(p_job_no hsperftest_job.job_no%TYPE) IS
  p_min ROWID;
  p_max ROWID;
  p_max_in  NUMBER;
  p_c   BINARY_INTEGER;
  nr    INTEGER;
  p_count NUMBER:= 0;
BEGIN
  -- Read the job metadata
  SELECT low_id, hi_id, max_in INTO p_min, p_max, p_max_in
  FROM hsperftest_job
  WHERE job_no = p_job_no;
  UPDATE hsperftest_job SET start_date = sysdate WHERE job_no = p_job_no;
  COMMIT;
  -- Do the work
   p_c := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@hsdb;
   DBMS_HS_PASSTHROUGH.PARSE@hsdb (
      p_c,
      'insert into hsperftest (OWNER,OBJECT_NAME,SUBOBJECT_NAME,OBJECT_ID,DATA_OBJECT_ID,OBJECT_TYPE,CREATED,LAST_DDL_TIME,TIMESTAMP,STATUS,
      TEMPORARY,GENERATED,SECONDARY,NAMESPACE,EDITION_NAME,ID)
      VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
   );
   FOR rec IN (SELECT  * FROM HSPERFTEST WHERE rowid BETWEEN p_min AND p_max AND (p_max_in = 1 OR (p_max_in = 0 AND rowid &lt; p_max)))
   LOOP
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 1, rec.owner);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 2, rec.object_name);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 3, rec.SUBOBJECT_NAME);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 4, rec.OBJECT_ID);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 5, rec.DATA_OBJECT_ID);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 6, rec.OBJECT_TYPE);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 7, rec.CREATED);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 8, rec.LAST_DDL_TIME);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 9, rec.TIMESTAMP);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 10, rec.STATUS);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 11, rec.TEMPORARY);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 12, rec.GENERATED);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 13, rec.SECONDARY);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 14, rec.NAMESPACE);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 15, rec.EDITION_NAME);
      DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsdb (p_c, 16, rec.ID);
      nr := DBMS_HS_PASSTHROUGH.EXECUTE_NON_QUERY@hsdb (p_c);
      p_count:= p_count + 1;
   END LOOP;

   DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@hsdb (p_c);
   COMMIT;  
  -- Write the job end time
  UPDATE hsperftest_job SET end_date = sysdate, rows_processed = p_count WHERE job_no = p_job_no;
  COMMIT;
END;
/
&lt;/pre&gt;
&lt;br /&gt;

And finally I'll execute the jobs by putting them on the job queue.&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
DECLARE
   x   PLS_INTEGER;
BEGIN
   FOR rec IN (SELECT   job_no FROM hsperftest_job)
   LOOP
      DBMS_JOB.submit (job =&gt; x, what =&gt; 'hsperftest_proc(' || rec.job_no || ');');
   END LOOP;

   COMMIT;
END;
/

SQL&gt; SELECT   job, total_time, broken, what FROM   user_jobs;
       JOB TOTAL_TIME B WHAT
---------- ---------- - -----------------
     10967         95 N hsperftest_proc(1);
     10968         95 N hsperftest_proc(2);
     10969         95 N hsperftest_proc(3);
&lt;/pre&gt;
&lt;br /&gt;

When the jobs are running, monitor the HSODBC processes CPU and memory usage. If there are too much parallel slaves running, then they may become a bottleneck.
If HSODBC memory usage becomes a problem, then partition the source data into smaller chunks.&lt;br /&gt;&lt;br /&gt;

When everything is done, look at the results.&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
SQL&gt; SELECT SUM(rows_processed) total_rows, (MAX(end_date)-MIN(start_date))*24*60 runtime_min FROM hsperftest_job;

TOTAL_ROWS RUNTIME_MIN
---------- -----------
    177942  3.58333333
&lt;/pre&gt;
&lt;br /&gt;
3.58 min = 3 min 34.8 sec. Nice :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-1616621791838135474?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=AGai3tuTMqQ:DIXcdumvlls:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=AGai3tuTMqQ:DIXcdumvlls:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=AGai3tuTMqQ:DIXcdumvlls:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=AGai3tuTMqQ:DIXcdumvlls:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=AGai3tuTMqQ:DIXcdumvlls:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/AGai3tuTMqQ" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/08/high-performance-data-unloading-from.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-6580877714675463969</guid><pubDate>Mon, 27 Jul 2009 19:15:00 +0000</pubDate><atom:updated>2009-08-15T20:11:39.477+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">postgresql</category><title>Using linguistic indexes for sorting in open source databases</title><description>Here I'm following up my previous post &lt;a href="http://ilmarkerm.blogspot.com/2009/07/using-linguisting-indexes-for-sorting.html"&gt;Using linguistic indexes for sorting in Oracle&lt;/a&gt;. I don't much like the Oracle solution, that requires creating a special index to speed up sorting, but... at the same time its very powerful, allows to index in many languages and no database changes are needed.&lt;br /&gt;&lt;br /&gt;

In this post I’ll take a look at the two popular open source databases MySQL and PostgreSQL. I'll take a look only at features, that the database has included and that can be used without any special application changes.&lt;br /&gt;&lt;br /&gt;

&lt;h3&gt;PostgreSQL 8.4&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;

Starting from 8.4, collation (sorting) rules can be defined per database and there is no possibility to set it in session level. All sorting and all indexes are ordered according to the database collation locale. In previous versions there was only one collation locale allowed for the entire database cluster.&lt;br /&gt;&lt;br /&gt;

For my example I create two databases, one with Estonian locale and one with German.&lt;br /&gt;
&lt;pre class="brush: bash;"&gt;
$ createdb -E utf8 -l 'et_EE.utf8' -O ilmar ilmar_ee
$ createdb -E utf8 -l 'de_DE.utf8' -O ilmar -T template0 ilmar_de
&lt;/pre&gt;

Currently PostgreSQL relies on the underlying OS to do the collations, so the OS must also support the specified locale. Check it with:&lt;br /&gt;
&lt;pre class="brush: bash;"&gt;
$ locale -a | grep et_EE
et_EE
et_EE.iso88591
et_EE.iso885915
et_EE.utf8
&lt;/pre&gt;

To change the collation you need to dump the entire database to a text file (pg_dump), create a new database and reload the data. So, a pretty painful procedure for large databases.&lt;br /&gt;&lt;br /&gt;

A small test if it really works. In ilmar_ee and ilmar_de I create table test_coll and load it with 4 rows:&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
CREATE TABLE test_coll (
  t character varying(100) NOT NULL
);

begin;
insert into test_coll values ('a');
insert into test_coll values ('o');
insert into test_coll values ('õ');
insert into test_coll values ('ä');
commit;


ilmar_ee=&gt; select t from test_coll order by t;
a
o
õ
ä

ilmar_de=&gt; select t from test_coll order by t;
a
ä
o
õ
&lt;/pre&gt;


Now can index be used for sorting?&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
CREATE TABLE t (
  t character varying(100) NOT NULL
);

CREATE INDEX idxt
  ON t
  USING btree
  (t);

CREATE OR REPLACE FUNCTION fill_t(p_num_rows bigint) RETURNS bigint AS
$BODY$
declare
  s t.t%type;
  i integer;
begin
  for i in 1..p_num_rows loop
    s:= case mod(i, 4) when 0 then 'a' when 1 then 'ä' when 2 then 'o' when 3 then 'õ' end;
    if mod(i,2) = 0 then
      s:= upper(s);
    end if;
    s:= s ||' wqe wqe wqe wqeqwdsa asd asdasd sasss we qwewq dssas';
    insert into t (t) values (s);
  end loop;
  return 0;
end;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

select fill_t(10000);
vacuum analyze t;
&lt;/pre&gt;

After the test data is created, some tests. Oracle users will find the following very strange:&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
ilmar=&gt; explain select t from t order by t;
                          QUERY PLAN
--------------------------------------------------------------
 Sort  (cost=868.39..893.39 rows=10000 width=55)
   Sort Key: t
   -&gt;  Seq Scan on t  (cost=0.00..204.00 rows=10000 width=55)

ilmar=&gt; explain select t from t where t between 'a' and 'b' order by t;
                               QUERY PLAN
-------------------------------------------------------------------------
 Sort  (cost=395.10..401.35 rows=2500 width=55)
   Sort Key: t
   -&gt;  Seq Scan on t  (cost=0.00..254.00 rows=2500 width=55)
         Filter: (((t)::text &gt;= 'a'::text) AND ((t)::text &lt;= 'b'::text))

ilmar=&gt; explain select t from t where t between 'a' and 'ak' order by t;
                               QUERY PLAN
------------------------------------------------------------------------
 Index Scan using idxt on t  (cost=0.00..8.27 rows=1 width=55)
   Index Cond: (((t)::text &gt;= 'a'::text) AND ((t)::text &lt;= 'ak'::text))
&lt;/pre&gt;

It seems that Postgres optimizer only consideres using index for sorting, when there is only a small fraction of the table filtered. A reason for this in the &lt;a href="http://www.postgresql.org/docs/8.4/interactive/indexes-ordering.html"&gt;documentation&lt;/a&gt; is:&lt;br /&gt;
&lt;blockquote&gt;
The planner will consider satisfying an ORDER BY specification either by scanning an available index that matches the specification, or by scanning the table in physical order and doing an explicit sort. For a query that requires scanning a large fraction of the table, an explicit sort is likely to be faster than using an index because it requires less disk I/O due to following a sequential access pattern. Indexes are more useful when only a few rows need be fetched.
&lt;/blockquote&gt;
&lt;br /&gt;
So it seems that Postgres cannot fast full scan an index. If I fill the table up even more, then finally optimizer is costing the sort operation higher than index scan. But the query is slow, 4125ms on my system.&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
ilmar=&gt; select fill_t(90000);
ilmar=&gt; vacuum analyze t;

ilmar=&gt; explain select t from t order by t;
                              QUERY PLAN
-----------------------------------------------------------------------
 Index Scan using idxt on t  (cost=0.00..9771.11 rows=100000 width=55)
&lt;/pre&gt;

A note from &lt;a href="http://www.postgresql.org/docs/8.4/interactive/locale.html"&gt;documentation&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
The drawback of using locales other than C or POSIX in PostgreSQL is its performance impact. It slows character handling and prevents ordinary indexes from being used by LIKE. For this reason use locales only if you actually need them.
&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;

Better collation supports seems to be a work in progress:&lt;br /&gt;
&lt;a href="http://wiki.postgresql.org/wiki/Todo:Collate"&gt;Todo:Collate&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://wiki.postgresql.org/wiki/Todo:ICU"&gt;Todo:IDU&lt;/a&gt;&lt;br /&gt;

Its also possible to use third party &lt;a href="http://orafce.projects.postgresql.org/"&gt;Orafce&lt;/a&gt; package, that enables the use on NLSSORT function, that is similar to Oracle.&lt;br /&gt;&lt;br /&gt;

&lt;h3&gt;MySQL 5.1&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;

MySQL supports collations at the column level.&lt;br /&gt;
All available collations for the given charset can be queried like this:&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
mysql&gt; show collation where charset = 'utf8';
+--------------------+---------+-----+---------+----------+---------+
| Collation          | Charset | Id  | Default | Compiled | Sortlen |
+--------------------+---------+-----+---------+----------+---------+
| utf8_general_ci    | utf8    |  33 | Yes     | Yes      |       1 |
| utf8_bin           | utf8    |  83 |         | Yes      |       1 |
| utf8_unicode_ci    | utf8    | 192 |         | Yes      |       8 |
| utf8_icelandic_ci  | utf8    | 193 |         | Yes      |       8 |
| utf8_latvian_ci    | utf8    | 194 |         | Yes      |       8 |
| utf8_romanian_ci   | utf8    | 195 |         | Yes      |       8 |
| utf8_slovenian_ci  | utf8    | 196 |         | Yes      |       8 |
| utf8_polish_ci     | utf8    | 197 |         | Yes      |       8 |
| utf8_estonian_ci   | utf8    | 198 |         | Yes      |       8 |
| utf8_spanish_ci    | utf8    | 199 |         | Yes      |       8 |
| utf8_swedish_ci    | utf8    | 200 |         | Yes      |       8 |
| utf8_turkish_ci    | utf8    | 201 |         | Yes      |       8 |
| utf8_czech_ci      | utf8    | 202 |         | Yes      |       8 |
| utf8_danish_ci     | utf8    | 203 |         | Yes      |       8 |
| utf8_lithuanian_ci | utf8    | 204 |         | Yes      |       8 |
| utf8_slovak_ci     | utf8    | 205 |         | Yes      |       8 |
| utf8_spanish2_ci   | utf8    | 206 |         | Yes      |       8 |
| utf8_roman_ci      | utf8    | 207 |         | Yes      |       8 |
| utf8_persian_ci    | utf8    | 208 |         | Yes      |       8 |
| utf8_esperanto_ci  | utf8    | 209 |         | Yes      |       8 |
| utf8_hungarian_ci  | utf8    | 210 |         | Yes      |       8 |
+--------------------+---------+-----+---------+----------+---------+
21 rows in set (0.04 sec)
&lt;/pre&gt;

I'll create a table test, in where column e uses Estonian sorting and column h uses hungarian sorting.&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
mysql&gt; create table test (
  e varchar(100) not null collate 'utf8_estonian_ci',
  h varchar(100) not null collate 'utf8_hungarian_ci'
) charset=utf8;

mysql&gt; insert into test values ('a','a');
mysql&gt; insert into test values ('ä','ä');
mysql&gt; insert into test values ('o','o');
mysql&gt; insert into test values ('õ','õ');
mysql&gt; select * from test;
+---+---+
| e | h |
+---+---+
| a | a |
| ä | ä |
| o | o |
| õ | õ |
+---+---+
4 rows in set (0.02 sec)

mysql&gt; select * from test order by e;
+---+---+
| e | h |
+---+---+
| a | a |
| o | o |
| õ | õ |
| ä | ä |
+---+---+
4 rows in set (0.04 sec)

mysql&gt; select * from test order by h;
+---+---+
| e | h |
+---+---+
| a | a |
| ä | ä |
| o | o |
| õ | õ |
+---+---+
4 rows in set (0.01 sec)
&lt;/pre&gt;

Perfect, now what about indexes?&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
mysql&gt; create index idxe on test (e);
Query OK, 4 rows affected (0.71 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql&gt; create index idxh on test (h);
Query OK, 4 rows affected (0.18 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql&gt; explain select e from test order by e;
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test  | index | NULL          | idxe | 302     | NULL |    4 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; explain select h from test order by h;
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test  | index | NULL          | idxh | 302     | NULL |    4 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
&lt;/pre&gt;

If I force a different collation on a column, then values are read from index, but extra filesort step is needed:
&lt;pre class="brush: sql;"&gt;
mysql&gt; explain select h from test order by h collate utf8_estonian_ci;
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------------+
| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                       |
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | test  | index | NULL          | idxh | 302     | NULL |    4 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)
&lt;/pre&gt;

I'll add more rows to see how much difference there is when reading the order from index and when doing extra sorting.&lt;br /&gt;

&lt;pre class="brush: sql;"&gt;
mysql&gt; delimiter //
mysql&gt; create procedure load_data(p_num_rows INT)
    -&gt; BEGIN
    -&gt;   SET @i = 0;
    -&gt;   REPEAT
    -&gt;     SET @i = @i + 1;
    -&gt;     INSERT INTO test (e, h) VALUES (CONCAT('eeeaad sadsa dasd asd', @i),
    -&gt;       CONCAT('213aad sadsa dasd asd', @i));
    -&gt;   UNTIL @i &gt; p_num_rows END REPEAT;
    -&gt; END
    -&gt; //
Query OK, 0 rows affected (0.44 sec)

mysql&gt; delimiter ;
mysql&gt; call load_data(10000);
mysql&gt; call load_data(20000);
mysql&gt; ANALYZE TABLE test;

mysql&gt; select sql_no_cache count(*) from (select h from test order by h collate utf8_estonian_ci) a;
+----------+
| count(*) |
+----------+
|    30002 |
+----------+
1 row in set (1.22 sec)

mysql&gt; select sql_no_cache count(*) from (select h from test order by h) a;
+----------+
| count(*) |
+----------+
|    30002 |
+----------+
1 row in set (0.09 sec)
&lt;/pre&gt;

It is also possible to set collation at connection level, but this does not change the row sorting order like in Oracle.&lt;br /&gt;
&lt;pre class="brush: sql;"&gt;
mysql&gt; truncate table test;
Query OK, 0 rows affected (0.07 sec)

mysql&gt; insert into test values ('a','a');
Query OK, 1 row affected (0.03 sec)

mysql&gt; insert into test values ('ä','ä');
Query OK, 1 row affected (0.08 sec)

mysql&gt; insert into test values ('o','o');
Query OK, 1 row affected (0.03 sec)

mysql&gt; insert into test values ('õ','õ');
Query OK, 1 row affected (0.03 sec)

mysql&gt; set collation_connection=utf8_estonian_ci;
Query OK, 0 rows affected (0.01 sec)

mysql&gt; select h from test order by h;
+---+
| h |
+---+
| a |
| ä |
| o |
| õ |
+---+
4 rows in set (0.00 sec)

mysql&gt; set collation_connection=utf8_hungarian_ci;
Query OK, 0 rows affected (0.00 sec)

mysql&gt; select h from test order by h;
+---+
| h |
+---+
| a |
| ä |
| o |
| õ |
+---+
4 rows in set (0.00 sec)
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-6580877714675463969?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=3Yc-rtB0qyI:DqlCbOi0yw8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=3Yc-rtB0qyI:DqlCbOi0yw8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=3Yc-rtB0qyI:DqlCbOi0yw8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=3Yc-rtB0qyI:DqlCbOi0yw8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=3Yc-rtB0qyI:DqlCbOi0yw8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/3Yc-rtB0qyI" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/07/using-linguistic-indexes-for-sorting-in.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-5943710538254679830</guid><pubDate>Sat, 25 Jul 2009 13:00:00 +0000</pubDate><atom:updated>2009-08-15T20:22:56.495+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>Refreshing test database from production using incremental backups</title><description>&lt;p&gt;It's common to use a production database copy for testing. But how to keep it fresh? If production database is large and located on a remote site (even on another country/continent), then copying the full database backup over network may be too unreasonable and if the test database needs to be open for a longer time, then transporting and storing production archived logs to test site may be too unreasonable also.&lt;br/&gt;
In this situation Oracle has three great technologies that can help: physical standby database, flashback database and RMAN.&lt;/p&gt;

&lt;p&gt;Here I'm using Oracle Database 10.2.0.4 EE.&lt;/p&gt;

&lt;h3&gt;Initial setup&lt;/h3&gt;

&lt;p&gt;This is just to create a test database as physical standby database. Do it in any way you like. Just make sure you create a guaranteed restore point before activating the standby.&lt;/p&gt;

&lt;p&gt;Create backup and standby controlfile. Give backupsets some tag, that can later be used for cleaning up the backups from primary database.&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;
RMAN&gt; backup device type disk format '/home/oracle/backup/%U' as compressed backupset
  tag 'testdb_init' database INCLUDE CURRENT CONTROLFILE FOR STANDBY
  plus archivelog;

Starting backup at 24-JUL-09
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=159 devtype=DISK
channel ORA_DISK_1: starting compressed full datafile backupset
... and so on
Finished backup at 24-JUL-09
&lt;/pre&gt;

&lt;p&gt;I also need to take spfile and password file.&lt;/p&gt;

&lt;pre class="brush: bash;"&gt;
$ cp $ORACLE_HOME/dbs/spfiletest1.ora /home/oracle/backup/
$ cp $ORACLE_HOME/dbs/orapwtest1 /home/oracle/backup/
&lt;/pre&gt;

&lt;p&gt;Now, copy everything over to the test database. I'm leaving all file locations exactly the same as in primary database.&lt;/p&gt;

&lt;pre class="brush: bash;"&gt;
$ scp /home/oracle/backup/* oracle@testdb:/home/oracle/backup/
&lt;/pre&gt;

&lt;p&gt;On test server:&lt;/p&gt;

&lt;pre class="brush: bash;"&gt;
$ cd backup/
$ cp spfiletest1.ora $ORACLE_HOME/dbs/
$ cp orapwtest1 $ORACLE_HOME/dbs/
&lt;/pre&gt;

&lt;p&gt;Start up the test database instance and change service name to be different from the one used in production.&lt;/p&gt;

&lt;pre class="brush: plain;"&gt;
SQL&gt; startup nomount
ORACLE instance started.

Total System Global Area  608174080 bytes
Fixed Size                  1268920 bytes
Variable Size             167773000 bytes
Database Buffers          436207616 bytes
Redo Buffers                2924544 bytes
SQL&gt; show parameter service

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      test1

SQL&gt; alter system set service_names='test_test1' scope=both;

System altered.
&lt;/pre&gt;

&lt;p&gt;Restore controlfile and restore the datafiles.&lt;/p&gt;

&lt;pre class="brush: plain;"&gt;
RMAN&gt; restore controlfile from '/home/oracle/backup/0tkkuvob_1_1';

Starting restore at 24-JUL-09
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK

channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
output filename=/u01/app/oracle/oradata/TEST1/controlfile/o1_mf_56lyof8g_.ctl
output filename=/u01/app/oracle/flash_recovery_area/TEST1/controlfile/o1_mf_56lyofm4_.ctl
Finished restore at 24-JUL-09

RMAN&gt; alter database mount;
RMAN&gt; sql "alter database flashback off";

RMAN&gt; catalog start with '/home/oracle/backup';

searching for all files that match the pattern /home/oracle/backup

List of Files Unknown to the Database
=====================================
File Name: /home/oracle/backup/0ukkuvok_1_1
File Name: /home/oracle/backup/0tkkuvob_1_1

Do you really want to catalog the above files (enter YES or NO)? yes
cataloging files...
cataloging done

List of Cataloged Files
=======================
File Name: /home/oracle/backup/0ukkuvok_1_1
File Name: /home/oracle/backup/0tkkuvob_1_1

RMAN&gt; restore database;

Starting restore at 24-JUL-09
...
Finished restore at 24-JUL-09

RMAN&gt; recover database;

Starting recover at 24-JUL-09
using channel ORA_DISK_1

starting media recovery
unable to find archive log
archive log thread=1 sequence=51
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 07/24/2009 17:09:50
RMAN-06054: media recovery requesting unknown log: thread 1 seq 51 lowscn 360057
&lt;/pre&gt;

&lt;p&gt;Recover database will fail at the end, but this is only because it didn't find the current log. This doesn't matter here.&lt;/p&gt;

&lt;p&gt;Test that media recovery process will start.&lt;/p&gt;

&lt;pre class="brush: plain;"&gt;
SQL&gt; alter database recover managed standby database disconnect from session;

Database altered.

SQL&gt; select process, status, sequence# from v$managed_standby where process like 'MRP%';

PROCESS   STATUS        SEQUENCE#
--------- ------------ ----------
MRP0      WAIT_FOR_LOG         51

SQL&gt; alter database recover managed standby database cancel;

Database altered.
&lt;/pre&gt;

&lt;p&gt;If MRP* is started and waiting for log (and no error in alert.log), then everything is good.&lt;/p&gt;

&lt;p&gt;Set up flashback. Make sure that you have enough diskspace in FRA (flashback recovery area) for all flashback logs generated while the test database is open.
After flashback has been enabled, create a guaranteed restore point.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; alter database flashback on;

Database altered.

SQL&gt; create restore point prod_point guarantee flashback database;

Restore point created.
&lt;/pre&gt;

&lt;p&gt;Now, just activate standby database and open it.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; alter database activate standby database;
SQL&gt; startup mount force
SQL&gt; alter database set standby database to maximize performance;
SQL&gt; alter database open;
&lt;/pre&gt;

&lt;p&gt;Delete the backups from both databases. Just for cleaning up.&lt;/p&gt;

&lt;pre class="brush: plain;"&gt;
RMAN&gt; delete backup tag 'testdb_init';
&lt;/pre&gt;

&lt;h3&gt;Syncing with production&lt;/h3&gt;

&lt;p&gt;To make it more interesting, I'll add some new datafiles to the primary database.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; alter tablespace sysaux add datafile size 10m autoextend on next 10m maxsize 1g;

Tablespace altered.

SQL&gt; create tablespace test5891 datafile size 10m autoextend on next 10m maxsize 1g;

Tablespace created.
&lt;/pre&gt;

&lt;p&gt;First, in test database find out the restore point SCN. This is the point from where to create the incremental backup.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; select to_char(scn) from v$restore_point where name = 'PROD_POINT';

TO_CHAR(SCN)
---------------
390703
&lt;/pre&gt;

&lt;p&gt;Find out the current production database log sequence.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; select sequence# from v$log where status = 'CURRENT';

 SEQUENCE#
----------
        57

SQL&gt; alter system checkpoint;
&lt;/pre&gt;

&lt;p&gt;Create incremental backup from production and transport it to the test site. Also include all archived logs that were generated during backup.&lt;/p&gt;

&lt;pre class="brush: plain;"&gt;
RMAN&gt; BACKUP INCREMENTAL FROM SCN=390703 device type disk format '/home/oracle/backup/%U'
  as compressed backupset database INCLUDE CURRENT CONTROLFILE FOR STANDBY;

RMAN&gt; sql "alter system archive log current";

RMAN&gt; backup device type disk format '/home/oracle/backup/%U' as compressed backupset archivelog from sequence 57;
&lt;/pre&gt;

&lt;p&gt;Close test database and flash it back to the restore point.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; shutdown immediate
SQL&gt; startup mount
SQL&gt; flashback database to restore point PROD_POINT;
SQL&gt; drop restore point PROD_POINT;
SQL&gt; alter database flashback off;
SQL&gt; ALTER DATABASE CONVERT TO PHYSICAL STANDBY;
SQL&gt; startup mount force
&lt;/pre&gt;

&lt;p&gt;Perform any cleanups, like added datafiles while the test database was open.&lt;/p&gt;

&lt;p&gt;Register backup and then apply the changes.&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;
RMAN&gt; catalog start with '/home/oracle/backup';

searching for all files that match the pattern /home/oracle/backup

List of Files Unknown to the Database
=====================================
File Name: /home/oracle/backup/13kl0v38_1_1
File Name: /home/oracle/backup/12kl0uuv_1_1
File Name: /home/oracle/backup/14kl0v87_1_1

Do you really want to catalog the above files (enter YES or NO)? yes
cataloging files...
cataloging done

List of Cataloged Files
=======================
File Name: /home/oracle/backup/13kl0v38_1_1
File Name: /home/oracle/backup/12kl0uuv_1_1
File Name: /home/oracle/backup/14kl0v87_1_1

RMAN&gt; recover database;

Starting recover at 25-JUL-09
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental datafile backupset restore
...

starting media recovery

channel ORA_DISK_1: starting archive log restore to default destination
...
unable to find archive log
archive log thread=1 sequence=59
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 07/25/2009 12:10:43
RMAN-06054: media recovery requesting unknown log: thread 1 seq 59 lowscn 391208
&lt;/pre&gt;

&lt;p&gt;At the end, recover database will fail, because it cant find the current log, but it doesn't matter here. Looking at the datafiles, RMAN has automatically created all newly added datafiles. That's nice and easy.&lt;/p&gt;

&lt;p&gt;Now, I'll check that standby database is working, create a new restore point and open the database.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; alter database recover managed standby database disconnect from session;

Database altered.

SQL&gt; select process, status, sequence# from v$managed_standby where process like 'MRP%';

PROCESS   STATUS        SEQUENCE#
--------- ------------ ----------
MRP0      WAIT_FOR_LOG         59

SQL&gt; alter database recover managed standby database cancel;

Database altered.

SQL&gt; alter database flashback on;

Database altered.

SQL&gt; create restore point prod_point guarantee flashback database;

Restore point created.

SQL&gt; alter database activate standby database;

Database altered.

SQL&gt; startup mount force
ORACLE instance started.
...
Database mounted.
SQL&gt; alter database set standby database to maximize performance;

Database altered.

SQL&gt; alter database open;

Database altered.
&lt;/pre&gt;

&lt;p&gt;That's it. Pretty easy actually :)&lt;/p&gt;

&lt;p&gt;So if you think this method can be useful to you, then try it out. I needed to work this out, because test database is on another continent from primary and physical standbys and the network link is pretty slow. Test database is open usually about a month so using incremental backups for refreshing is pretty much the only thing reasonable.&lt;/p&gt;

&lt;p&gt;Actually I use this method with backups taken from physical standby database, so in that case it's not necessary to take the archivelogs also, just shut down media recovery process before starting the backup.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-5943710538254679830?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=G2MHHiYCUuk:NiqYw159Mkc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=G2MHHiYCUuk:NiqYw159Mkc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=G2MHHiYCUuk:NiqYw159Mkc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=G2MHHiYCUuk:NiqYw159Mkc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=G2MHHiYCUuk:NiqYw159Mkc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/G2MHHiYCUuk" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/07/refreshing-test-database-from.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-1984712198147272375</guid><pubDate>Wed, 22 Jul 2009 11:42:00 +0000</pubDate><atom:updated>2009-08-15T23:32:29.688+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>Using linguistic indexes for sorting in Oracle</title><description>&lt;p&gt;In Estonia we have our own language and Estonian language has its own alphabet that is a little bit different from English :) And it would be good if a database application sorts text fields according to the correct language rules (I have seen many bug reports from clients, that "the sort order is wrong").&lt;/p&gt;

&lt;p&gt;In Oracle database its quite easy to sort according to the correct language rules, you just need to set the correct database session language from the client. This can be done using ALTER SESSION SET NLS_LANGUAGE or quite often the database client picks up the correct language from OS locale.&lt;/p&gt;

&lt;p&gt;Small example to demonstrate this (I use Oracle Database 11.1.0.7 for these examples):&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
create table t (
  t varchar2(100) not null
);

insert into t values ('a');
insert into t values ('ä');
insert into t values ('o');
insert into t values ('õ');
commit;

SQL&gt; alter session set nls_language='estonian';

Session altered.

SQL&gt; select t from t order by t;

T
---------
a
o
õ
ä

SQL&gt; alter session set nls_language='german';

Session altered.

SQL&gt; select t from t order by t;

T
---------
a
ä
o
õ
&lt;/pre&gt;

&lt;p&gt;But what happens if I create an index to speed up the sorting?&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; truncate table t;

SQL&gt; create index idxt on t(t);

SQL&gt; 
declare
  s t.t%type;
begin
  for i in 1..100000 loop
    s:= case mod(i,4) 
          when 0 then 'a'
          when 1 then 'ä'
          when 2 then 'o'
          when 3 then 'õ'
        end;
    if mod(i,3) = 0 then
      s:= upper(s);
    end if;
    s:= s||dbms_random.string('x',50);
    insert into t values (s);
  end loop;
  commit;
end;
/
SQL&gt; exec dbms_stats.gather_table_stats(user,'t',cascade=&gt;true);

PL/SQL procedure successfully completed.

SQL&gt; alter session set nls_language='estonian';

Session altered.

SQL&gt; select t from t order by t;

100000 rows selected.

Elapsed: 00:00:02.89

-----------------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |   100K|  5175K|       |  1482   (2)| 00:00:18 |
|   1 |  SORT ORDER BY     |      |   100K|  5175K|  5896K|  1482   (2)| 00:00:18 |
|   2 |   TABLE ACCESS FULL| T    |   100K|  5175K|       |   162   (2)| 00:00:02 |
-----------------------------------------------------------------------------------

SQL&gt; alter session set nls_language='american';

Session altered.

SQL&gt; select t from t order by t;

100000 rows selected.

Elapsed: 00:00:01.56

-------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------
|   0 | SELECT STATEMENT |      |   100K|  5175K|   895   (1)| 00:00:11 |
|   1 |  INDEX FULL SCAN | IDXT |   100K|  5175K|   895   (1)| 00:00:11 |
-------------------------------------------------------------------------
&lt;/pre&gt;

&lt;p&gt;First I did a query with Estonian language and then for comparison with American language. If you compare these two, the query in American language works as expected, it reads the correct sort order directly from index. But query in Estonian language requires extra sorting. This is also clearly demonstrated in the query elapsed times. Usually this is not a problem at all, but if there is a need for more performance, what if the data sets are really large and could benefit a sorting help from an index?&lt;/p&gt;

&lt;p&gt;This is because normal indexes in oracle are always built with BINARY sort mode. This is the fastest sorting mode that orders the characters by their numerical value in the character set. But this is only good for the English language.&lt;/p&gt;

&lt;p&gt;Here come the linguistic indexes.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; create index idx_ling on t (nlssort(t, 'nls_sort=estonian'));

Index created.

SQL&gt; alter session set nls_language='estonian';

Session altered.

SQL&gt; select /*+index(t idx_ling)*/ t from t order by t;

----------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |   100K|    14M|   101K  (1)| 00:20:20 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T        |   100K|    14M|   101K  (1)| 00:20:20 |
|   2 |   INDEX FULL SCAN           | IDX_LING |   100K|       |  1646   (1)| 00:00:20 |
----------------------------------------------------------------------------------------
&lt;/pre&gt;

&lt;p&gt;So it works, there is no extra sorting step anymore. But it's useless in this case, because the index contains only the sorting value, not the actual column value and the slow TABLE ACCESS BY INDEX ROWID needs to be performed.&lt;/p&gt;

&lt;p&gt;This isn't a problem when I do not need to return the actual value of column t and only need to do the sorting:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; select 1 from t order by t;

-----------------------------------------------------------------------------
| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT |          |   100K|    14M|  1646   (1)| 00:00:20 |
|   1 |  INDEX FULL SCAN | IDX_LING |   100K|    14M|  1646   (1)| 00:00:20 |
-----------------------------------------------------------------------------
&lt;/pre&gt;

Or I could include the actual column value in the index itself:
&lt;pre class="brush: sql;"&gt;
SQL&gt; create index idx_ling2 on t (nlssort(t, 'nls_sort=estonian'), t);

SQL&gt; select t from t order by t;

------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |   100K|    14M|  2388   (1)| 00:00:29 |
|   1 |  INDEX FULL SCAN | IDX_LING2 |   100K|    14M|  2388   (1)| 00:00:29 |
------------------------------------------------------------------------------
&lt;/pre&gt;

&lt;p&gt;If I change the language, the index is not used anymore.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; drop index idxt;

SQL&gt; alter session set nls_language='american';

Session altered.

SQL&gt; select 1 from t order by t;

-----------------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |   100K|  5175K|       |  1482   (2)| 00:00:18 |
|   1 |  SORT ORDER BY     |      |   100K|  5175K|  5896K|  1482   (2)| 00:00:18 |
|   2 |   TABLE ACCESS FULL| T    |   100K|  5175K|       |   162   (2)| 00:00:02 |
-----------------------------------------------------------------------------------
&lt;/pre&gt;

&lt;p&gt;The same linguistic index can also be used for searching, but for that NLS_COMP needs to be set to LINGUISTIC:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; alter session set nls_language='estonian';

Session altered.

SQL&gt; select t from t where t = 'x';

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    53 |   163   (3)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |    53 |   163   (3)| 00:00:02 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("T"='x')

SQL&gt; alter session set nls_comp='linguistic';

Session altered.

SQL&gt; select t from t where t = 'x';

------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |     1 |   157 |     3   (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| IDX_LING2 |     1 |   157 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access(NLSSORT("T",'nls_sort=''ESTONIAN''')=HEXTORAW('80000100')
              )
&lt;/pre&gt;

&lt;p&gt;It is also possible to combine search and sorting:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
SQL&gt; select t from t where t between 'a' and 'b' order by t;

------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |   250 | 39250 |    13   (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| IDX_LING2 |   250 | 39250 |    13   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access(NLSSORT("T",'nls_sort=''ESTONIAN''')&gt;=HEXTORAW('14000100')
                AND NLSSORT("T",'nls_sort=''ESTONIAN''')&lt;=HEXTORAW('19000100') )
&lt;/pre&gt;

&lt;p&gt;In conclusion, its possible to use index for sorting in non-English languages, but the solution is not very straight-forward and simple. But at the same time it is powerful, you can index the same column in different languages at the same time! But I'm not yet sure, that I like this FBI kind of solution by Oracle, because when doing SQL tuning I really like the possibility of reading the actual column value from index and not doing the always painfully slow TABLE ACCESS BY INDEX ROWID, if at all possible.&lt;br/&gt;
In one of the next posts I will take a look at other databases, how have they solved sorting according to national language rules.&lt;/p&gt;

&lt;p&gt;More examples about Linguistic Indexes can be found from Richard Foote blog, he focuses on the case-insensitive search feature:&lt;br/&gt;
&lt;a href="http://richardfoote.wordpress.com/2008/01/03/introduction-to-linguistic-indexes-part-i/"&gt;Introduction To Linguistic Indexes – Part I&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also take a look at Metalink note 227335.1:&lt;br/&gt;
Linguistic Sorting - Frequently Asked Questions&lt;/p&gt;

&lt;p&gt;And of course... documentation:&lt;br/&gt;
Oracle® Database Globalization Support Guide - Linguistic Sorting and String Searching&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-1984712198147272375?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=IuRFjSfspjg:C3kyggn55sg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=IuRFjSfspjg:C3kyggn55sg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=IuRFjSfspjg:C3kyggn55sg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=IuRFjSfspjg:C3kyggn55sg:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=IuRFjSfspjg:C3kyggn55sg:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/IuRFjSfspjg" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/07/using-linguisting-indexes-for-sorting.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-7874053052127161772</guid><pubDate>Wed, 22 Jul 2009 09:55:00 +0000</pubDate><atom:updated>2009-08-15T23:24:51.051+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">apex</category><category domain="http://www.blogger.com/atom/ns#">heterogeneous</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>APEX application running on "another" database</title><description>&lt;p&gt;I have attended some APEX presentations/seminars and one question seems to be repeating and got my attention. Can APEX run on another database?&lt;/p&gt;

&lt;p&gt;Well, APEX itself or APEX application itself cannot run in any way on another database besides Oracle. APEX is PL/SQL application and no other database supports Oracle PL/SQL language.&lt;/p&gt;

&lt;p&gt;But it's possible to select and modify data from another database from your APEX application.&lt;/p&gt;

&lt;p&gt;First of all you need to set up a database link to the other database using Oracle Heterogeneous Services/Transparent Gateways. I'm not going to get into that right now, please use documentation/google for that.&lt;br/&gt;
I'm going to name my database link HSDB:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
create database link hsdb 
  connect to "lowercaseusername" 
  identified by "randomCasepaSSword"
  using 'MYSQL_DB_SERVICE';
&lt;/pre&gt;

&lt;p&gt;I'm going to use MySQL database and I'll create table employees on MySQL database:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE TABLE `employees` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `personal_code` varchar(20) NOT NULL,
  `birthday` date DEFAULT NULL,
  `salary` decimal(10,2) NOT NULL,
  `is_active` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
&lt;/pre&gt;

&lt;p&gt;Test that table employees is accessible from Oracle:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
select * from "employees"@hsdb;
&lt;/pre&gt;

&lt;p&gt;NB! I created the table in MySQL with a lower case table name, but Oracle uses upper case identifiers by default, unless you force the correct case with double-quotes.&lt;/p&gt;

&lt;p&gt;One other thing to look out for: check that ODBC/Oracle do not translate any of the columns to the deprecated LONG datatype. LONG columns seems to be used for long/unlimited text fields (*text in MySQL, text in PostgreSQL). You can check it by running DESC command from SQL*PLUS.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
desc "employees"@hsdb
 Name                       Null?    Type
 -------------------------- -------- -----------------
 id                                  NUMBER(10)
 first_name                 NOT NULL VARCHAR2(100)
 last_name                  NOT NULL VARCHAR2(100)
 personal_code              NOT NULL VARCHAR2(20)
 birthday                   DATE
 salary                     NOT NULL NUMBER(10,2)
 is_active                  NOT NULL NUMBER(3)
&lt;/pre&gt;

&lt;p&gt;Make sure, that none of the data types is LONG. If you have LONG column, then you need to modify the data type from the source database or create a "Oracle friendly" view on top of it.&lt;br/&gt;
In PostgreSQL ODBC driver its also possible to modify the maximum VARCHAR length before its mapped to LONG.&lt;br/&gt;
Anyway, this is the most critical part in my opinion. Make sure that you are happy with the data type mappings before continuing!&lt;/p&gt;

&lt;p&gt;Now create an Oracle view on top that MySQL table. This view will translate all column names to upper case and will simplify writing the SQL code.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
  CREATE VIEW EMPLOYEES_MYSQL AS 
  SELECT   "id" id,
            "first_name" first_name,
            "last_name" last_name,
            "personal_code" personal_code,
            "birthday" birthday,
            "salary" salary,
            "is_active" is_active
     FROM   "employees"@hsdb;

desc employees_mysql
 Name                    Null?    Type
 ----------------------- -------- ----------------
 ID                               NUMBER(10)
 FIRST_NAME              NOT NULL VARCHAR2(300)
 LAST_NAME               NOT NULL VARCHAR2(300)
 PERSONAL_CODE           NOT NULL VARCHAR2(60)
 BIRTHDAY                DATE
 SALARY                  NOT NULL NUMBER(10,2)
 IS_ACTIVE               NOT NULL NUMBER(3)
&lt;/pre&gt;

&lt;h3&gt;Simple report&lt;/h3&gt;

&lt;p&gt;Creating a simple report is very easy, just use the created EMPLOYEES_MYSQL view in APEX like any other normal Oracle table/view.&lt;/p&gt;

&lt;h3&gt;Complex report&lt;/h3&gt;

&lt;p&gt;Writing vanilla-SQL against non-Oracle table over database link usually works, when the SQL is really simple. But there are pretty serious limitations and in some cases the performance "may not be very good" (indexes not used for some data type conversions).&lt;br/&gt;
To solve that problem, Oracle has a package &lt;a href="http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_hspass.htm#ARPLS017"&gt;DBMS_HS_PASSTHROUGH&lt;/a&gt;, that sends unmodified native SQL query to the linked database.&lt;/p&gt;

&lt;p&gt;For this example I'll use the following native MySQL SQL statement for a report:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
select sql_cache id, first_name, last_name from employees limit 10
&lt;/pre&gt;

&lt;p&gt;To be able to use this result in a SQL statement, I'm going to use PIPELINED function and for that I first need to create TYPE, that will define the structure of the query output. And after that I'll create the actual function that will query the remote database.&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
create or replace type mysql_hstest_type as object (
  id number,
  first_name varchar2(250),
  last_name varchar2(250)
);
/

create or replace type tab_mysql_hstest_type is 
  table of mysql_hstest_type;
/

create or replace FUNCTION mysql_hstest_query 
  RETURN tab_mysql_hstest_type PIPELINED
IS
  p_row mysql_hstest_type:= mysql_hstest_type(null, null, null);
  p_c binary_integer;
BEGIN
  p_c:= DBMS_HS_PASSTHROUGH.OPEN_CURSOR@hsdb;
  DBMS_HS_PASSTHROUGH.PARSE@hsdb(p_c, 
    'select sql_cache id, first_name, last_name from employees limit 10');
  WHILE DBMS_HS_PASSTHROUGH.FETCH_ROW@hsdb(p_c) &gt; 0 LOOP
    DBMS_HS_PASSTHROUGH.GET_VALUE@hsdb(p_c, 1, p_row.id);
    DBMS_HS_PASSTHROUGH.GET_VALUE@hsdb(p_c, 2, p_row.first_name);
    DBMS_HS_PASSTHROUGH.GET_VALUE@hsdb(p_c, 3, p_row.last_name);
    PIPE ROW(p_row);
  END LOOP;
  DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@hsdb(p_c);
  RETURN;
END;
/
&lt;/pre&gt;

&lt;p&gt;Now, to use it in an APEX form just use the following SQL in report:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
select * from table(mysql_hstest_query)
&lt;/pre&gt;

&lt;h3&gt;Form&lt;/h3&gt;

&lt;p&gt;First create a form on a view EMPLOYEES_MYSQL with the wizard like for any normal table. This will just create all the necessary page elements quickly.&lt;/p&gt;

&lt;p&gt;Its necessary to write optimistic locking feature, because "Automatic Row Processing (DML)" process cannot be used and optimistic locking is a "silent" built in feature of that process. If for some weird reason you do not want the optimistic locking feature, then you can skip the checksum and validation steps.&lt;br/&gt;
For checksumming I'll create hashing function (this one needs execute privileges on DBMS_CRYPTO):&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE OR REPLACE FUNCTION form_md5_checksum(
  p1 varchar2 default null, p2 varchar2 default null,
  p3 varchar2 default null, p4 varchar2 default null,
  p5 varchar2 default null, p6 varchar2 default null,
  p7 varchar2 default null, p8 varchar2 default null,
  p9 varchar2 default null, p10 varchar2 default null)
RETURN varchar2 DETERMINISTIC IS
BEGIN
  RETURN rawtohex(DBMS_crypto.hash(UTL_RAW.CAST_TO_RAW(
    p1||'|'||p2||'|'||p3||'|'||p4||'|'||
    p5||'|'||p6||'|'||p7||'|'||p8||'|'||
    p9||'|'||p10), DBMS_CRYPTO.HASH_MD5));
END;
/
&lt;/pre&gt;

&lt;p&gt;Now create a new &lt;span style="font-weight:bold;"&gt;hidden and protected&lt;/span&gt; item in the APEX page, I'll call it P3_CHECKSUM.&lt;br/&gt;
Then create a PL/SQL anonymous block process:&lt;/p&gt;

&lt;pre&gt;
  Name: Calculate checksum
  Sequence: 20 (just after Automated Row Fetch)
  Process Point: On Load - After Header
  Process:
&lt;/pre&gt;

&lt;pre class="brush: sql;"&gt;
BEGIN
  :p3_checksum:= form_md5_checksum(
    :p3_ID, :p3_FIRST_NAME, :p3_LAST_NAME, :p3_PERSONAL_CODE, 
    :p3_BIRTHDAY, :p3_SALARY, :p3_IS_ACTIVE);
END;
&lt;/pre&gt;

&lt;p&gt;Then I removed "Database Action" from the form buttons and changed the Button Name (the page submit REQUEST value):&lt;/p&gt;
&lt;pre&gt;
Delete - DELETE
Apply Changes - UPDATE
Create - INSERT
&lt;/pre&gt;

&lt;p&gt;The default "Automatic Row Processing (DML)" process cannot be used for saving the chages back to the database, because the ODBC database/driver lacks the support for SELECT FOR UPDATE. Because of it, delete the existing "Automatic Row Processing (DML)" process.&lt;/p&gt;

&lt;p&gt;To save the changes, a new procedure is needed:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;
CREATE OR REPLACE PROCEDURE modify_employees_mysql(
  p_action IN VARCHAR2, p_row IN employees_mysql%rowtype,
  p_md5 varchar2)
IS
  PRAGMA AUTONOMOUS_TRANSACTION;
  p_new_md5 varchar2(50);
  p_new_row employees_mysql%rowtype;
BEGIN
  -- Calculate checksum
  IF p_action IN ('UPDATE','DELETE') AND p_row.id IS NOT NULL THEN
    -- Lock the row
    UPDATE employees_mysql SET first_name = first_name
      WHERE id = p_row.id;
    -- Calculate new checksum
    SELECT * INTO p_new_row FROM employees_mysql WHERE id = p_row.id;
    p_new_md5:= form_md5_checksum(p_new_row.ID, p_new_row.FIRST_NAME,
      p_new_row.LAST_NAME, p_new_row.PERSONAL_CODE, 
      p_new_row.BIRTHDAY, p_new_row.SALARY, p_new_row.IS_ACTIVE);
    -- Check if the checksum has changed
    IF NVL(p_new_md5, '-') &lt;&gt; NVL(p_md5, '-') THEN
      ROLLBACK;
      raise_application_error(-20000, 'Data has changed');
    END IF;
    --
  END IF;
  -- Do the data modifications
  IF p_action = 'INSERT' THEN
    INSERT INTO employees_mysql VALUES p_row;
  ELSIF p_action = 'UPDATE' AND p_row.id IS NOT NULL THEN
    UPDATE employees_mysql SET ROW = p_row WHERE id = p_row.id;
  ELSIF p_action = 'DELETE' AND p_row.id IS NOT NULL THEN
    DELETE FROM employees_mysql WHERE id = p_row.id;
  ELSE
    raise_application_error(-20099, 'Invalid action.');
  END IF;
  commit;
END;
/
&lt;/pre&gt;

&lt;p&gt;Note the "PRAGMA AUTONOMOUS_TRANSACTION" in the above code. I used the default open source MySQL ODBC driver that lacks the support for 2PC (Two Phase Commit). The symptom for this "ORA-02047: cannot join the distributed transaction in progress" when running the procedure inside APEX transaction.&lt;br/&gt;
If you are using some commercial ODBC driver with 2PC support or drivers supplied by Oracle HS or Oracle Transparent Gateways, then you don't need autonomous transaction for this procedure and you also need to remove commit/rollback statements from the procedure.&lt;/p&gt;

&lt;p&gt;And finally put this procedure to the APEX page flow.&lt;br/&gt;
Create a new PL/SQL anonymous block process:&lt;/p&gt;

&lt;pre&gt;
  Name: Save changes
  Sequence: 30
  Process Point: On Submit - After Computations and Validations
  Process:
&lt;/pre&gt;

&lt;pre class="brush: sql;"&gt;
DECLARE
  p_row employees_mysql%rowtype;
BEGIN
  p_row.id:= :P3_ID;
  p_row.first_name:= :P3_FIRST_NAME;
  p_row.last_name:= :P3_LAST_NAME;
  p_row.personal_code:= :P3_PERSONAL_CODE;
  p_row.birthday:= :P3_BIRTHDAY;
  p_row.salary:= :P3_SALARY;
  p_row.is_active:= :P3_IS_ACTIVE;
  modify_employees_mysql(:REQUEST, p_row, :p3_checksum);
END;
&lt;/pre&gt;

&lt;p&gt;And now you have it - APEX running on another database :)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-7874053052127161772?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=B5Op8exzrvQ:qUhbIaOXvT8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=B5Op8exzrvQ:qUhbIaOXvT8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=B5Op8exzrvQ:qUhbIaOXvT8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=B5Op8exzrvQ:qUhbIaOXvT8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=B5Op8exzrvQ:qUhbIaOXvT8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/B5Op8exzrvQ" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/07/apex-application-running-on-another.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-4959965296062465321</guid><pubDate>Tue, 21 Jul 2009 17:55:00 +0000</pubDate><atom:updated>2009-08-15T20:33:27.079+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">apex</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>APEX instrumentation</title><description>&lt;p&gt;For every Oracle database session it is always good to set MODULE, ACTION and CLIENT_ID values for instrumentation. This allows DBA to see and debug in database level what the session is/was doing in detail. Oracle diagnostic tools are all powered up for using these values and a lot of diagnostic power is just lost, when the application is not instrumented.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/endtoend.htm"&gt;For JDBC one can use end-to-end metrics support in JDBC driver&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_appinf.htm#i999290"&gt;For PL/SQL one can use DBMS_APPLICATION_INFO subprograms&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All this is just great, but what about &lt;a href="http://otn.oracle.com/apex/"&gt;APEX&lt;/a&gt;? I think it's the best database application development tool in the market today, but has it got this instrumentation already built in?&lt;br/&gt;
Yes it has!&lt;/p&gt;
&lt;pre class="brush: sql;"&gt;
SELECT   module,
         action,
         client_id
  FROM   v$active_session_history
 WHERE   module LIKE 'APEX%';

MODULE                  ACTION          CLIENT_ID
----------------------- --------------- ------------------------
APEX:APPLICATION 109    PAGE 7          ILMAR:2697049844839191
APEX:APPLICATION 109    PAGE 12         ILMAR:2697049844839191
APEX:APPLICATION 109    PAGE 6          ILMAR:2697049844839191
&lt;/pre&gt;

&lt;p&gt;MODULE is set to the application number&lt;br/&gt;
ACTION contains the page number&lt;br/&gt;
CLIENT_ID constains username and session id&lt;/p&gt;

&lt;p&gt;This example is from APEX 3.2 and Oracle 11.1.0.7 database.&lt;/p&gt;

&lt;p&gt;You can read more about using Oracle diagnostic tools and session tracing from Doug Burns blog:&lt;br/&gt;
&lt;a href="http://oracledoug.com/serendipity/index.php?/archives/1505-Session-Level-ASH-Reports.html"&gt;Session Level ASH Reports&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-4959965296062465321?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=os5_NIQ3GUw:OHxqzh2Ag5I:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=os5_NIQ3GUw:OHxqzh2Ag5I:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=os5_NIQ3GUw:OHxqzh2Ag5I:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=os5_NIQ3GUw:OHxqzh2Ag5I:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=os5_NIQ3GUw:OHxqzh2Ag5I:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/os5_NIQ3GUw" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/07/apex-instrumentation.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8455543779867801145.post-5276247454670597333</guid><pubDate>Tue, 21 Jul 2009 08:05:00 +0000</pubDate><atom:updated>2009-08-15T20:28:34.654+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">sql</category><category domain="http://www.blogger.com/atom/ns#">oracle</category><title>UNPIVOT with multi-column groups in Oracle 11g</title><description>&lt;p&gt;Welcome to my first blog :) And I'll start with something simple, but very cool at the same time.&lt;/p&gt;

&lt;p&gt;I really like Oracle 11g database and one of my favourite features is the PIVOT/UNPIVOT clause in SQL. PIVOT is used to present rows as columns and UNPIVOT is the opposite operation, columns are presented as rows. I have found them very useful for &lt;a href="http://otn.oracle.com/apex/"&gt;APEX&lt;/a&gt; applications, when I need to present data in table rows as columns in an APEX form for user input.&lt;/p&gt;

&lt;p&gt;PIVOT feature seems to have recieved a lot of bloggers attention, but I haven't read much about UNPIVOT. Using UNPIVOT with one column is pretty straight forward and easy, just a small example (I use Oracle Database 11.1.0.7 Enterprise Edition for all the examples here):&lt;/p&gt;

&lt;pre  class="brush: sql;"&gt;
create table unpivot_ex1 (
 id number primary key,
 jan number,
 feb number,
 mar number,
 apr number
);
&lt;/pre&gt;

&lt;p&gt;Fill it up with test data:&lt;/p&gt;

&lt;pre  class="brush: sql;"&gt;
insert into unpivot_ex1 (id, jan, feb, mar, apr)
 values (1, 1, 2, null, 4);
insert into unpivot_ex1 (id, jan, feb, mar, apr)
 values (2, 5, 6, null, 8);
insert into unpivot_ex1 (id, jan, feb, mar, apr)
 values (3, 9, 10, null, null);
commit;

SELECT * FROM unpivot_ex1;

    ID        JAN        FEB        MAR        APR
---------- ---------- ---------- ---------- ----------
     1          1          2                     4
     2          5          6                     8
     3          9         10
&lt;/pre&gt;

&lt;p&gt;Now, if you want to select JAN, FEB, MAR, APR columns as separate rows, then its possible to use the simplest UNPIVOT clause:&lt;/p&gt;

&lt;pre  class="brush: sql;"&gt;
SELECT * FROM unpivot_ex1 
  UNPIVOT INCLUDE NULLS
    (monthly_value FOR month IN (jan, feb, mar, apr));

        ID MON MONTHLY_VALUE
---------- --- -------------
         1 JAN             1
         1 FEB             2
         1 MAR
         1 APR             4
         2 JAN             5
         2 FEB             6
         2 MAR
         2 APR             8
         3 JAN             9
         3 FEB            10
         3 MAR
         3 APR

12 rows selected.
&lt;/pre&gt;

&lt;p&gt;Now, lets take a more interesting case. What if some of the columns are combined into logical groups and you want to preserve them in one row? For example:&lt;/p&gt;

&lt;pre  class="brush: sql;"&gt;
create table unpivot_ex2 (
  id number primary key,
  jan_income number,
  jan_expense number,
  feb_income number,
  feb_expense number,
  mar_income number,
  mar_expense number
);
&lt;/pre&gt;

&lt;p&gt;For every month there is income and expense column.&lt;/p&gt;

&lt;pre  class="brush: sql;"&gt;
insert into unpivot_ex2 values (1, 1, 1, 2, 3, 5, 2);
insert into unpivot_ex2 values (2, 5, 6, 4, 4, null, null);
commit;

SELECT * FROM unpivot_ex2;

        ID JAN_INCOME JAN_EXPENSE FEB_INCOME FEB_EXPENSE MAR_INCOME MAR_EXPENSE
---------- ---------- ----------- ---------- ----------- ---------- -----------
         1          1           1          2           3          5           2
         2          5           6          4           4
&lt;/pre&gt;

&lt;p&gt;UNPIVOT allows to create column groups, like this:&lt;/p&gt;

&lt;pre  class="brush: sql;"&gt;
SELECT * FROM unpivot_ex2
  UNPIVOT INCLUDE NULLS
    ((income, expense) FOR month IN (
     (jan_income, jan_expense), (feb_income, feb_expense), 
     (mar_income, mar_expense)));

        ID MONTH                      INCOME    EXPENSE
---------- ---------------------- ---------- ----------
         1 JAN_INCOME_JAN_EXPENSE          1          1
         1 FEB_INCOME_FEB_EXPENSE          2          3
         1 MAR_INCOME_MAR_EXPENSE          5          2
         2 JAN_INCOME_JAN_EXPENSE          5          6
         2 FEB_INCOME_FEB_EXPENSE          4          4
         2 MAR_INCOME_MAR_EXPENSE

6 rows selected.
&lt;/pre&gt;

&lt;p&gt;How cool is that? :)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8455543779867801145-5276247454670597333?l=ilmarkerm.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=a6Vk9DGGeyA:AP8W4EGKYLI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=a6Vk9DGGeyA:AP8W4EGKYLI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=a6Vk9DGGeyA:AP8W4EGKYLI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/ilmarkerm?a=a6Vk9DGGeyA:AP8W4EGKYLI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ilmarkerm?i=a6Vk9DGGeyA:AP8W4EGKYLI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ilmarkerm/~4/a6Vk9DGGeyA" height="1" width="1"/&gt;</description><link>http://ilmarkerm.blogspot.com/2009/07/unpivot-with-multi-column-groups-in.html</link><author>noreply@blogger.com (Ilmar Kerm)</author><thr:total>3</thr:total></item></channel></rss>

