Changeset 3811


Ignore:
Timestamp:
Oct 9, 2007, 2:15:16 PM (16 years ago)
Author:
Nicklas Nordborg
Message:

References #796: Don't create indexes on primary key columns for tables in the dynamic database

Added tool to remove indexes from dynamic db.

Location:
branches/2.4-stable
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.4-stable/doc/src/docbook/admindoc/installation_upgrade.xml

    r3744 r3811  
    6767    <title>Upgrade instructions</title>
    6868
    69     <important>
    70       <title>Note to PostgreSQL users</title>
    71       Upgrading BASE to versions earlier
    72       than v2.2 is not be safe with an PostgreSQL database engine due
    73       to a bug in Hibernate. The problem is reported to Hibernate
    74       developers. There now exists a workaround for this problem. Read
    75       more about the workaround on
    76       the <ulink
    77       url="http://base.thep.lu.se/wiki/UpgradePostgres">Upgrading a
    78       Postgres database prior to BASE 2.2</ulink> page. This fix is
    79       not needed for the release which this document is a part of.
     69    <important id="dropindexes">
     70      <title>Upgrading from BASE 2.4.3 or lower to 2.4.4 or higher</title>
     71      <para>
     72        Older releases of BASE 2 used to create indexes for many columns
     73        in the dynamic database. The same columns are part of the primary
     74        key for the tables so the indexes are not really needed. The
     75        result is very bad performance since the database engine sometimes
     76        get stuck in "index update" mode making the entire server
     77        very slow. BASE 2.4.4 no longer creates the indexes. Indexes on
     78        existing tables should be dropped to increase the performance.
     79        Tests have shown a 50-90% decrease in execution time for some
     80        plug-ins
     81        (<ulink url="http://base.thep.lu.se/ticket/294">http://base.thep.lu.se/ticket/294</ulink>).
     82      </para>
     83      <para>
     84        Removing the indexes is very simple. <emphasis>After the server
     85        has been upgraded</emphasis> following the usual instructions below, issue the
     86        the following commands:
     87      </para>
     88   
     89      <programlisting>
     90cd &lt;basedir&gt;/bin
     91./dynamicdb.sh -v -dropindexes
     92</programlisting>
     93
     94      <para>
     95        Skip the <option>-dropindexes</option> option to do a dry
     96        run without actually deleting the indexes.
     97      </para>
    8098    </important>
    8199
  • branches/2.4-stable/src/core/net/sf/basedb/core/HibernateUtil.java

    r3808 r3811  
    4646import java.util.HashMap;
    4747import java.util.HashSet;
     48import java.util.LinkedList;
    4849import java.util.List;
    4950import java.util.Iterator;
     
    16921693 
    16931694  /**
     1695    Prints a lot of useful stuff about the dynamic database to standard output.
     1696    For each table the output includes information about columns, indexes,
     1697    primary keys and foreign keys as found in the current database.
     1698   
     1699    @param verbose If true, lots of information will be printed
     1700    @param silent If true, no information will be printed
     1701    @param dropIndexes If true, all indexes will be dropped
     1702  */
     1703  @SuppressWarnings("unchecked")
     1704  public static void dynamicDbIndexes(boolean verbose, boolean silent, boolean dropIndexes)
     1705  {
     1706    if (silent) verbose = false;
     1707    Session session = newSession();
     1708    Connection connection = HibernateUtil.getConnection(session);
     1709    DatabaseMetaData metaData = null;
     1710    List<Table> tables = new LinkedList<Table>();
     1711   
     1712    try
     1713    {
     1714      metaData = connection.getMetaData();
     1715      ResultSet result = metaData.getTables(Application.getDynamicCatalog(),
     1716          Application.getDynamicSchema(), null, null);
     1717     
     1718      while (result.next())
     1719      {
     1720        String tableName = result.getString("TABLE_NAME");
     1721        String tableCatalog = result.getString("TABLE_CAT");
     1722        String tableSchema = result.getString("TABLE_SCHEM");
     1723        Table table = new Table();
     1724        table.setName(tableName);
     1725        table.setCatalog(tableCatalog);
     1726        table.setSchema(tableSchema);
     1727        tables.add(table);
     1728      }
     1729    }
     1730    catch (SQLException ex)
     1731    {
     1732      throw new BaseException(ex);
     1733    }
     1734   
     1735    for (Table table : tables)
     1736    {
     1737
     1738      if (!silent)
     1739      {
     1740        System.out.println("=================");
     1741        System.out.println("Table   : " + table.getName());
     1742      }
     1743      if (verbose)
     1744      {
     1745        System.out.println("Catalog : " + table.getCatalog());
     1746        System.out.println("Schema  : " + table.getSchema());
     1747      }
     1748
     1749      Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>();
     1750      Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>();
     1751     
     1752      String pkName = null;
     1753      Set<String> fkNames = new HashSet<String>();
     1754
     1755      if (verbose)
     1756      {
     1757        System.out.println("Database information");
     1758        System.out.println("--------------------");
     1759      }
     1760     
     1761      TableInfo tiDb = null;
     1762      try
     1763      {
     1764        tiDb = new TableInfo(table, metaData);
     1765      }
     1766      catch (SQLException ex)
     1767      {
     1768        throw new BaseException(ex);
     1769      }
     1770
     1771      if (tiDb != null)
     1772      {
     1773        if (verbose)
     1774        {
     1775          // Write all columns
     1776          for (ColumnInfo ci : tiDb.getColumns())
     1777          {
     1778            System.out.println("  Column       : " + ci);
     1779          }
     1780         
     1781          // Write primary key
     1782          System.out.println("  Primary key  : " + tiDb.getPrimaryKey());
     1783       
     1784          // Write foreign keys
     1785          for (ForeignKeyInfo fk : tiDb.getForeignKeys())
     1786          {
     1787            System.out.println("  Foreign key  : " + fk);
     1788          }
     1789        }
     1790     
     1791        // Write indexes and unique constraints
     1792        for (IndexInfo ii : tiDb.getIndexes())
     1793        {
     1794          if (!silent)
     1795          {
     1796            if (ii.isUnique())
     1797            {
     1798              System.out.println("  Unique       : " + ii);
     1799            }
     1800            else
     1801            {
     1802              System.out.println("  Index        : " + ii);
     1803            }
     1804          }
     1805         
     1806          boolean safeToDrop = tiDb.safeToDrop(ii);
     1807
     1808          String dropSql = dbEngine.getDropIndexSql(
     1809            table.getCatalog(), table.getSchema(), table.getName(),
     1810            ii.getName(), ii.isUnique());
     1811          boolean actionDrop = dropIndexes && safeToDrop;
     1812
     1813          if (!silent)
     1814          {
     1815            System.out.println("    Safe drop  : " + safeToDrop);
     1816            System.out.println("    DROP-SQL   : " + dropSql);
     1817            System.out.println("    Actions    : " + (actionDrop ? "DROP " : ""));
     1818          }
     1819         
     1820          if (actionDrop )
     1821          {
     1822            Statement st = null;
     1823            try
     1824            {
     1825              st = connection.createStatement();
     1826              if (actionDrop)
     1827              {
     1828                log.info("Dropping index: + " + dropSql);
     1829                st.executeUpdate(dropSql);
     1830              }
     1831              connection.commit();
     1832              st.close();
     1833              st = null;
     1834            }
     1835            catch (SQLException ex)
     1836            {
     1837              log.error("Exception", ex);
     1838              throw new BaseException(ex);
     1839            }
     1840            finally
     1841            {
     1842              if (st != null)
     1843              {
     1844                try
     1845                {
     1846                  st.close();
     1847                }
     1848                catch (Throwable t)
     1849                {
     1850                  log.error("Exception", t);
     1851                }
     1852              }
     1853            }
     1854          }
     1855         
     1856        }
     1857      }
     1858     
     1859      if (!silent)
     1860      {
     1861        System.out.println("=================");
     1862        System.out.println("");
     1863      }
     1864    }
     1865  }
     1866 
     1867 
     1868  /**
    16941869    Prints a lot of useful stuff about the database to standard output.
    16951870    For each table the output includes information about columns, indexes,
     
    17331908        System.out.println("Schema  : " + table.getSchema());
    17341909      }
    1735 
     1910 
    17361911      Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>();
    17371912      Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>();
     
    17391914      String pkName = null;
    17401915      Set<String> fkNames = new HashSet<String>();
    1741 
     1916 
    17421917      if (verbose)
    17431918      {
     
    17551930        throw new BaseException(ex);
    17561931      }
    1757 
     1932 
    17581933      if (verbose && tiDb != null)
    17591934      {
     
    18121987        }
    18131988      }
    1814 
     1989 
    18151990      // Write indexes and unique constraints
    18161991      for (IndexInfo ii : tiHib.getIndexes())
     
    18312006        boolean exists = dbName != null;
    18322007        boolean safeToDrop = exists && tiDb.safeToDrop(ii);
    1833 
     2008 
    18342009        String dropSql = exists ?
    1835           dbEngine.getDropIndexSql(null, null, table.getName(), dbName, ii.isUnique()) : "";
     2010          dbEngine.getDropIndexSql(table.getCatalog(), table.getSchema(),
     2011            table.getName(), dbName, ii.isUnique()) : "";
    18362012        String createSql =
    1837           dbEngine.getCreateIndexSql(null, null, table.getName(), table.getName() + "_" + ii.getName(),
     2013          dbEngine.getCreateIndexSql(table.getCatalog(), table.getSchema(),
     2014            table.getName(), table.getName() + "_" + ii.getName(),
    18382015          ii.getColumns(), ii.isUnique());
    18392016        boolean actionDrop = dropIndexes && exists && safeToDrop;
     
    18992076    }
    19002077  }
    1901  
     2078
    19022079
    19032080}
  • branches/2.4-stable/src/install/net/sf/basedb/install/InitDB.java

    r3679 r3811  
    2424*/
    2525package net.sf.basedb.install;
     26
     27import java.util.Arrays;
     28import java.util.List;
    2629
    2730import net.sf.basedb.core.Application;
     
    9497        Application.stop();
    9598      }
     99      else if ("dynamicdb".equals(cmd))
     100      {
     101        List<String> opt = Arrays.asList(args);
     102        boolean verbose = opt.contains("-v");
     103        boolean drop = opt.contains("-dropindexes");
     104        Application.start(false);
     105        HibernateUtil.dynamicDbIndexes(verbose, false, drop);
     106        Application.stop();
     107      }
    96108      else
    97109      {
    98110        System.out.println("Unknown command: " + cmd);
    99         System.out.println("Usage: InitDB cmd");
    100         System.out.println("       cmd: install | update | info | updateindexes | dropindexes");
     111        System.out.println("Usage: InitDB cmd <options>");
     112        System.out.println("    cmd: install | update | info | updateindexes | dropindexes | dynamicdb");
     113        System.out.println("options: ");
     114        System.out.println("           -v : Verbose output (if cmd = updateindexes, dropindexs or dynamicdb)");
     115        System.out.println(" -dropindexes : Drop indexes when cmd = dynamicdb");
    101116      }
    102117    }
Note: See TracChangeset for help on using the changeset viewer.