<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/styles/XMLStylesDoc_xhtml11_en.xsl"?><!DOCTYPE document SYSTEM "/schemas/XMLStyles10.dtd">
<document xmlns="http://XMLStyles.com/namespaces/styles" xmlns:xst="http://XMLStyles.com/namespaces/styles" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:date="http://exslt.org/dates-and-times">
   <noxml>
      <p xmlns="http://www.w3.org/1999/xhtml"/>
      <!-- You are viewing the source.  The following message should be ignored if you did "View Source" in your browser. -->
      <p xmlns="http://www.w3.org/1999/xhtml">
ATTENTION: XML WEB PAGES NOT SUPPORTED
   If you see this message, your current browser does not support the
   1999 XSLT 1.0 (or later) standard for XML web pages such as this one.
   Please upgrade your browser to a newer version
   that supports 1999 or later standards such as:
      Mozilla Firefox version 1.0.2 or later (GetFirefox -&gt; http://www.GetFirefox.com/)
      Netscape version 8 or later
      Safari version 1.3 or later
      Opera version 9 or later
      Microsoft Internet Explorer (MSIE) version 5 or later
   For further assistance, contact the software vendor for your browser.
   To go to the X<!-- extended HTML -->HTML version of this page click the following link:
<a href="index.html">index.html</a>
      </p>
      <p xmlns="http://www.w3.org/1999/xhtml">&#160;</p>
   </noxml>
   <path>/computers/databases/</path>
   <site>How To Guides</site>
   <logo xlink:type="simple" xlink:href="/images/howtohome.jpg" media="screen" width="240" height="34">How To Guides</logo>
   <logo xlink:type="simple" xlink:href="/images/howtoguides.jpg" media="print" width="240" height="34">How To Guides</logo>
   <logo xlink:type="simple" xlink:href="/images/howtoguidesmobile.jpg" media="handheld" width="150" height="17">How To Guides</logo>
   <navigation where="sections">
      <label>How To Guides</label>
      <link xlink:type="simple" xlink:href="/business/index.xml">Business</link>
      <link xlink:type="simple" xlink:href="/computers/index.xml">Computers</link>
      <link xlink:type="simple" xlink:href="/computers/databases/index.xml">Databases</link>
      <link xlink:type="simple" xlink:href="/internet/index.xml">Internet</link>
      <link xlink:type="simple" xlink:href="/mobile/index.xml">Mobile</link>
      <link xlink:type="simple" xlink:href="/money/index.xml">Money</link>
      <link xlink:type="simple" xlink:href="/movies/index.xml">Movies</link>
      <link xlink:type="simple" xlink:href="/computers/os/index.xml">Operating Systems</link>
   </navigation>
   <navigation where="up">
      <link xlink:type="simple" xlink:href="../">Computers</link>
   </navigation>
   <navigation where="subsections">
      <label>How To Guides for Databases</label>
      <link xlink:type="simple" xlink:href="design.xml">Design</link>
      <link xlink:type="simple" xlink:href="tuning.xml">Tuning</link>
      <link xlink:type="simple" xlink:href="derby/">Apache Derby</link>
      <link xlink:type="simple" xlink:href="ibmdb2/">IBM DB2</link>
      <link xlink:type="simple" xlink:href="mysql/">MySQL</link>
      <link xlink:type="simple" xlink:href="oracle/">Oracle</link>
      <link xlink:type="simple" xlink:href="sqlserver/">SQL Server</link>
   </navigation>
   <section id="body" type="body">
      <pages id="tuning" name="tuning">
         <title>Database Tuning</title>
         <label>Tuning</label>
         <navigation where="pages">
            <label>Tuning</label>
            <link xlink:type="simple" xlink:href="tuning.xml">Summary</link>
            <link xlink:type="simple" xlink:href="functions.xml">Functions</link>
         </navigation>
         <navigation where="up">
            <link xlink:type="simple" xlink:href="../">Databases</link>
         </navigation>
         <page id="N10246" name="functions">
            <title>Functions</title>
            <label>Functions</label>
            <subpage id="keycolfn" name="keycolfn">
               <label>Functions</label>
               <h1>Avoid functions on index columns</h1>
               <p>Applying a function to a key column will prevent the
                  <acronym term="Database Management System">&gt;DBMS</acronym>
                  from being able to search the index using the column value.
                  For example, given these two queries to retrieve a particular row:
               </p>
               <blockcode>
                  <l>select * from t where keycol = :hostvar;</l>
                  <l>select * from t where rtrim(keycol) = :hostvar;</l>
               </blockcode>
               <p>The first query can use the index to find the row very quickly.
                  For the second query, the database must retrieve every value of the key column
                  from either the table or the index, in order to apply the function to those values.
               </p>
               <p>If possible, move the function to the other side of the expression:
               </p>
               <blockcode>
                  <l>select * from t where keycol = rpad(:hostvar, <i>n</i>);</l>
               </blockcode>
            </subpage>
            <subpage id="N10268" name="fnindex">
               <label>Function Indexes</label>
               <h1>When to use a function index</h1>
               <p>Some databases allow creating an index on a function of a column.
                  Using a function index becomes necessary when there is no inverse
                  function that allows moving the function to a non-indexed value.
                  For example, most databases always use case-sensitive comparisons,
                  and there is no inverse function for a <code>lower</code> function.
                  Say an application requires a query that ignores case and padding, as in:
               </p>
               <blockcode>
                  <l>select ... from <i>schema</i>.<i>tablename</i> where lower(trim(email_address)) = lower(trim(:emailaddr));</l>
               </blockcode>
               <p>To avoid a table or index pagespace scan to convert all of the email_address
                  values to lower case, a function index could be created on that column.
               </p>
               <blockcode>
                  <l>create index <i>schema</i>.<i>indexname</i>​</l>
                  <l>   on <i>schema</i>.<i>tablename</i> (</l>
                  <l>      lower(trim(email_address))</l>
                  <l>   ) ...;</l>
               </blockcode>
            </subpage>
            <subpage id="N10293" name="coersion">
               <label>Implied Coersion</label>
               <h1>Watch out for implied coersion of key columns</h1>
               <p>Implied coersion of key columns can result in the same type of
                  <link xlink:type="simple" xlink:href="#keycolfn">problems that are caused by explicit functions</link>.
                  For example, when the fixed length <code>CHAR</code> data type is being used,
                  one side of a comparison will be padded automatically if they are different lengths.
                  Therefore, a key column should be compared only with constants or variables of an equal or shorter length.
                  Here are some examples that use the same <code>CHAR(6)</code>
                  "gender" column that was used in the discussion of
                  <link xlink:type="simple" xlink:href="charvarchar.xml#usewhich">when to use CHAR vs. VARCHAR</link>.
                  These examples will not cause automatic padding of the <code>GENDER</code> column:
               </p>
               <blockcode>
                  <l>GENDER = 'MALE'</l>
                  <l>GENDER = 'MALE  '</l>
                  <l>GENDER = 'FEMALE'</l>
               </blockcode>
               <p>These examples should be avoided because they will cause automatic padding of
                  the <code>GENDER</code> column to expand its length from 6 to 7 characters:
               </p>
               <blockcode>
                  <l>GENDER = 'UNKNOWN'</l>
                  <l>GENDER = 'FEMALE '</l>
               </blockcode>
            </subpage>
            <updated local="2006-11-16">Thursday November 16, 2006</updated>
         </page>
      </pages>
   </section>
   <copyright>Copyright © 2006 How To Guides .com. Alteration of content, including addition of any function such as hypertext links or pop-up advertising, or interference with the hypertext links or other functions of this site is expressly prohibited.</copyright>
   <disclaimer>All information, links, forms, applications and other items on this site or obtained from it are provided <b>AS IS</b>, WITHOUT WARRANTY OF ANY KIND EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.</disclaimer>
   <ids urchin="UA-779578-2"/>
</document>

