Register now and start sharing your code snippets.
-->

How to write UTF-8 data to an Oracle BLOB column with Java and JDBC

Java posted about 1 year ago by christian

This example works with Oracle:

   1  private Blob getBlob(Connection connection, String data)
   2  {
   3    BLOB blob = BLOB.createTemporary(connection, true, BLOB.DURATION_SESSION);
   4  
   5    try
   6    {
   7        blob.open(BLOB.MODE_READWRITE);
   8        blob.putBytes(1, data.getBytes("UTF-8")); // Consider streaming, if data size is unknown. Note that setBytes doesn't work
   9    }
  10    catch(UnsupportedEncodingException ex)
  11    {
  12        throw new RuntimeException("Unable to get a blob for '" + data + "'", ex);
  13    }
  14    catch(SQLException ex)
  15    {
  16        throw new RuntimeException("Unable to get a blob for '" + data + "'", ex);
  17    }
  18    finally
  19    {
  20        try { if(blob != null) blob.close(); } catch(Exception ex) {};
  21    }
  22  }

Then use the method like this:

   1  Connection connection = getConnection();
   2  PreparedStatement statement = getPreparedStatement(yer sequel);
   3  
   4  statement.setBlob(1, getBlob(connection,  <Mao's Little Red Book>));

Tagged oracle, blob, jdbc, java, utf-8

Find the amount of CPU time and total time runtime for Oracle SQL queries

SQL posted about 1 year ago by christian

There’s no equivalent to the MySQL slow query log in Oracle but these queries and the statspack reports are helpful when trying to find slow queries:

   1  -- Order queries by how many cpu seconds they consume
   2  SELECT   hash_value, executions, ROUND (elapsed_time / 1000000, 2) total_time,
   3           ROUND (cpu_time / 1000000, 2) cpu_seconds
   4      FROM (SELECT   *
   5                FROM v$sql
   6            ORDER BY elapsed_time DESC)
   7  ORDER BY cpu_seconds DESC;
   8  
   9  -- Find the total cpu seconds consumed
  10  SELECT SUM (cpu_seconds)
  11    FROM (SELECT   hash_value, executions,
  12                   ROUND (elapsed_time / 1000000, 2) total_time,
  13                   ROUND (cpu_time / 1000000, 2) cpu_seconds
  14              FROM (SELECT   *
  15                        FROM v$sql
  16                    ORDER BY elapsed_time DESC)
  17          ORDER BY cpu_seconds DESC);
  18  
  19  
  20  -- Find the total time queries have taken
  21  SELECT SUM (total_time)
  22    FROM (SELECT   hash_value, executions,
  23                   ROUND (elapsed_time / 1000000, 2) total_time,
  24                   ROUND (cpu_time / 1000000, 2) cpu_seconds
  25              FROM (SELECT   *
  26                        FROM v$sql
  27                    ORDER BY elapsed_time DESC)
  28          ORDER BY cpu_seconds DESC);
  29  
  30  -- Find execution plan for the damned query you should fix
  31  SELECT *
  32    FROM v$sql_plan
  33   WHERE hash_value = 2967942512;

Tagged oracle, performance

How to generate an Oracle statspack report and an explain plan for a query

SQL posted about 1 year ago by christian

   1  -- Connect
   2  
   3  sqlplus <username>/<password>@<database>
   4  
   5  -- Generate statspack report
   6  
   7  @?/rdbms/admin/spreport.sql
   8  
   9  -- Find execution plan for a specific query
  10  @?/rdbms/admin/sprepsql.sql
  11  
  12  Specify the Begin and End Snapshot Ids
  13  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14  Enter value for begin_snap: 1
  15  Begin Snapshot Id specified: 1
  16  
  17  Enter value for end_snap: 2
  18  End   Snapshot Id specified: 2
  19  
  20  
  21  Specify the Hash Value
  22  ~~~~~~~~~~~~~~~~~~~~~~
  23  Enter value for hash_value: 2967942512
  24  Hash Value specified is: 2967942512

Tagged oracle, explain plan, statspack