Tag Archives: 9i

How Oracle handles the distinct clause between 10gR1 and 10g R2

Oracle has changed the algorithm used to perform “select distinct” operations from 10gR1 and earlier version to 10gR2 and new versions. This has caught a lot of custom application and script writers by surprise, when they depended on the ordering of returned values from 10gR1 and earlier versions. The problem is caused by the way in which the Oracle parser handles a “select distinct” operation: From Oracle 8 to 10gR1, the parser used a sort algorithm to determine distinct values and from 10gR2 and later, the parser uses a hash algorithm to determine distinct values. The end result is that 8 to 10gR1 will return “select distinct” operations in sorted order and new version will not. So to resolve this issue you must use the “order by” clause to guarantee values are returned in sorted order. This article shows the plan differences between the two parsed statements in Oracle 10gR1 and 10gR2.

1. Logon to Oracle 10gR1 and execute “select version from v$instance to show the exact version of this release.

SQL> select version from v$instance;

VERSION
—————–
10.1.0.3.0

SQL>

2. Create the table test1 with the following DDL and insert ten numerical values.

create table test1(v_number number(10));

declare

v_ct number:=1;

begin

while v_ct<10
loop
insert into test1(v_number) values(v_ct);

v_ct := v_ct+1;

end loop;
end;
/

3. Select from this table with “select distinct” clause, as you can see the values are returned in sorted order even though we did not specify the order by clause.

select distinct v_number from test1;

SQL> select distinct v_number from test1;

V_NUMBER
———-
1
2
3
4
5
6
7
8
9

9 rows selected.

SQL>

4. Execute explain plan on the previous statement and you can see that the parser is performing a sort to find all distinct values in the table, thus returning the values in sorted order.

SQL> explain plan for select distinct v_number from test1;

Explained.

SQL> @?/rdbms/admin/utlxpls.sql

PLAN_TABLE_OUTPUT
——————————————————————

Plan hash value: 1260548514

——————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
——————————————————————–
| 0 | SELECT STATEMENT | | 9 | 117 | 4 (25)| 00:00:01 |
| 1 | SORT UNIQUE | | 9 | 117 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| TEST1 | 9 | 117 | 3 (0)| 00:00:01 |
——————————————————————–

Note

PLAN_TABLE_OUTPUT
———————————————————————
– dynamic sampling used for this statement

13 rows selected.

SQL>

5. Logon to 10gR2 server and select version from v$instance to show the exact RDBMS version.

SQL> select version from v$instance;

VERSION
—————–
10.2.0.4.0

SQL>

6. Create the table test1 and insert ten numerical values.

SQL> create table test1(v_number number(10));

Table created.

SQL>
SQL> declare
2
3 v_ct number:=1;
4
5 begin
6
7 while v_ct<10
8 loop
9 insert into test1(v_number) values(v_ct);
10
11 v_ct := v_ct+1;
12
13 end loop;
14 end;
15 /

PL/SQL procedure successfully completed.

SQL>

7. Perform select distinct operation on test1 table and note that the values are returned in an unsorted order.

SQL> select distinct v_number from test1;

V_NUMBER
———-
1
6
2
4
5
8
3
7
9

9 rows selected.

SQL>

8. Perform a explain plain on your “select distinct” statement and note that the sort operation has been replaced by a hash operation, thus values will not be returned in a sorted format.

SQL> explain plan for select distinct v_number from test1;

Explained.

SQL> @?/rdbms/admin/utlxpls.sql

PLAN_TABLE_OUTPUT
——————————————————————–

Plan hash value: 255531131

——————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
———————————————————————
| 0 | SELECT STATEMENT | | 9 | 117 | 4 (25)| 00:00:01 |
| 1 | HASH UNIQUE | | 9 | 117 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| TEST1 | 9 | 117 | 3 (0)| 00:00:01 |
———————————————————————-

Note

PLAN_TABLE_OUTPUT
——————————————————————-
– dynamic sampling used for this statement

13 rows selected.

SQL>

9. The reason for the change in the parser’s behavior is speed of execution; the hash algorithm is much more efficient then the sort operation and thus execution time decreases. In order to have a guaranteed sorting of your value, you must us an “order by” clause.

Larry J Catt, OCP 9i, 10g
oracle@allcompute.com
www.allcompute.com

Upgrade of Oracle 9i RDBMS to 10g RDBMS

Oracle provides upgrade paths for most RDBMS versions, however they are not as simple as some would like. I have ran into several Junior to Mid-level DBA’s who appear to have a difficult time grasping the general steps required to perform this task or just want a Senior person present for them to perform sanity checks with. Thus, I have decided to outline the general steps required to perform the upgrade from 9i to 10g.

1. Downloading the following software from www.oracle.com if you do not have the media available:

• Oracle 10.2.0.1 (this is the base release of Oracle 10g R2)
• Oracle 10.2.0.1 Companion CD (if needed)
• Oracle 10.2.0.4 (This is patch set 6810189)
• Latest Oracle CPU Patch

2. Shutdown your current Oracle 9i database system and listener. Do not delete any of the 9i binaries or database objects.

$ sqlplus ” / as sysdba”
SQL> shutdown immediate
SQL> exit
$
$ cd network/admin
$ lsnrctl stop LISTENER

3. Unpack or Unzip (dependent on OS) your 10.2.0.1 database installation. Execute the runInstaller. Note: It is most likely that your OS will be of a higher version, than is supported by this installer. You may have to use the tags “–jreLoc –ignoreSysPrereqs” to bypass the OS support version. Example:

./runInstaller -jreLoc /opt/java1.4/jre -ignoreSysPrereqs

4. At the Welcome to the Oracle Database 10g Installation page, check the Advanced Installation option and press Next.

5. At the Select Installation Type page, select Enterprise Addition and press Next.

6. At the Specify Home Details page, enter OraDb10g for Name and /app/oracle/10.2.0 for the path and press Next.

7. At the Product-Specific Prerequisite Checks page the Installer will verify the system is ready for installation.

8. At the Product-Specific Prerequisite Checks, review any warning received and press Next.

9. At the Upgrade an Existing Database page, select No and press next.

10. At the Select Configuration Options page, select Install Database Software Only and press Next.

11. At the Summary page, press Install.

12. Once the installer request for you to execute shell script root.sh, contact the SA support for your system and have them execute, then click OK.

13. Record the following output in a safe place and click Exit.

The following J2EE Applications have been deployed and are accessible at the
URLs listed below.
iSQL*Plus URL:
http://linux1:5560/isqlplus
iSQL*Plus DBA URL:
http://linux1:5560/isqlplus/dba

14. This completes Installation of 10.2.0.1 software.

15. Install Oracle companion component. (if desired)

16. Upgrade to 10.2.0.4, unzip the Oracle patch set 6810189 and execute the runInstaller utility. NOTE: You may again have to user the –jreLoc and –ignoreSysPrereqs tags. Example

./runInstaller -jreLoc /opt/java1.4/jre -ignoreSysPrereqs

17. At the Welcome page, select Next.

18. At the Specify Home Details page, enter OraDb10g in Name and /app/oracle/10.2.0 in Path and press Next

19. At the Product-Specific Prerequisite Checks page, press Next.

20. At the Oracle Configuration Manager Registration page, press Next.

21. At the Summary page, press Install.

22. Once requested to execute the script: app/oracle/10.2.0/root.sh, contact the SA and have them execute as root, then press OK.

23. At the End of Installation page, record the below information and press Exit.

The iSQL*Plus URL is:
http://linux1:5560/isqlplus

The iSQL*Plus DBA URL is:
http://linux1:5560/isqlplus/dba

24. At the screen ‘Do you really want to exit?’, press Yes.

25. This completes the upgrade to 10.2.0.4

26. Apply latest CPU patch. Logon to Metalink at metalink.oracle.com and download the latest CPU patch for Oracle 10.2.0.4. In this example, we will download CPU Jan 2009 – patch number 7592346

27. Unzip the patch file.

28. Ensure that the ORACLE_HOME parameter is point to 10.2.0.4 install by setting the parameter again.

Linux1:/oracle_software/7592346:>echo $ORACLE_HOME
/app/oracle/10.2.0
Linux1:/oracle_software/7592346:>

29. Edit your .profile file to ensure that OPATCH parameter is pointing at the correct OPatch directory and your PATH parameter is specifying the correct $ORACLE_HOME/bin directory. NOTE: The OPatch utility installed with 10.2.0.1 maybe too old for your CPU patch, see the README in your CPU to determine the correct OPatch version to use. You can navigate to $ORACLE_HOME/OPatch and execute the command “opatch version” to determine you current version.

30. Note: you may have to user the -jre tag to make the patch work.

opatch napply -skip_subset -skip_duplicate -jre /opt/java1.4/jre

or
$ORACLE_HOME/OPatch/opatch napply -skip_subset -skip_duplicate -jre
/opt/java1.4/jre

31. This completes installation of the latest CPU patch.

Post installation Steps

32. Update the .profile file to point to the new Oracle installation and make sure the following environment variables point to the new ORACLE installation.

ORACLE_HOME
PATH
LD_LIBRARY_PATH
SH_LIB_PATH

33. Logout and login to reset your environment variables.

34. Copy the init.ora file from existing 9.2.0.8 $ORACLE_HOME/dbs to 10g $ORACLE_HOME/dbs folder

Linux1:/app/oracle/9.2.0.8.0/dbs:>cp initorcl.ora ../../10.2.0/dbs/.
Linux1:/app/oracle/9.2.0.8.0/dbs:>

35. NOTE: See listing at bottom of this article for all non-supported 9i init parameters which must be removed or commented out.

36. Increase large pool size=200M in init.ora file.

37. Change folders to $ORACLE_HOME/rdbms/admin/. This is the folder where the SQL files for upgrade are stored.

$ cd $ORACLE_HOME/rdbms/admin

38. Start the database in upgrade mode

$>sqlplus / as sysdba
SQL>startup upgrade

39. Create sysaux tablespace

Ex:CREATE TABLESPACE sysaux DATAFILE
‘/orcl/oradata/orcl/data1/sysaux02.dbf’ SIZE 4096M REUSE EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ONLINE;

40. Issue unlimited quota for system

ALTER USER SYSTEM QUOTA UNLIMITED ON system;

41. Start the upgrade by running catupgrd.sql

SQL> spool /scripts/upgrade.log
SQL> @catupgrd.sql
Turn off the spooling of script results to the log file:
SQL> SPOOL OFF

Check the spool file and verify that the packages and procedures compiled successfully. You named the spool file earlier in this step; the suggested name was upgrade.log. Correct any problems you find in this file and rerun the appropriate upgrade script if necessary. You can rerun any of the scripts described in this note as many times as necessary.

42. Run utlu102s.sql, specifying the TEXT option:

SQL> @utlu102s.sql TEXT

43. Insure that all temp files from the production system are attached to temporary tablespace in converted database. If not recreate temp files.

Example:
alter tablespace TEMP add tempfile ‘/orcl/oradata/orcl/data1/temp_01.dbf’
size 2048m;

44. Run utlrp.sql to recompile any remaining stored PL/SQL and Java code.

SQL> @utlrp.sql

Verify that all expected packages and classes are valid:

45. If there are still objects which are not valid after running the script run
the following:

spool invalid_post.lst
Select substr(owner,1,12) owner,
substr(object_name,1,30) object,
substr(object_type,1,30) type, status
from dba_objects where status <>‘VALID’;
spool off

46. Copy old listeners.ora and tnsnames.ora from the previous 9.2.0.8 oracle installation and update their ORACLE_HOME references.
• Copy files to new ORACLE_HOME/network/admin folder
cp listener.ora $ORACLE_HOME/network/admin
cp tnsnames.ora $ORACLE_HOME/network/admin
• Change folders to $ORACLE_HOME/network/admin
cd $ORACLE_HOME/network/admin
• Update the newly copied files to point to the new ORACLE_HOME location

47. Restart the database in a normal mode with “startup open”

48. It is a good idea to know rebuild all of your indexes and gather statistics.

49. NOTE: You will not get all of the benefits of Oracle 10g until you set the COMPATIBILITY parameter to a minimum of 10.1

Larry J Catt, OCP 9i, 10g
oracle@allcompute.com
www.allcompute.com

Appendix A: Initialization Parameters Obsolete in 10g
—————————————————–

ENQUEUE_RESOURCES
DBLINK_ENCRYPT_LOGIN
HASH_JOIN_ENABLED
LOG_PARALLELISM
MAX_ROLLBACK_SEGMENTS
MTS_CIRCUITS
MTS_DISPATCHERS
MTS_LISTENER_ADDRESS
MTS_MAX_DISPATCHERS
MTS_MAX_SERVERS
MTS_MULTIPLE_LISTENERS
MTS_SERVERS
MTS_SERVICE
MTS_SESSIONS
OPTIMIZER_MAX_PERMUTATIONS
ORACLE_TRACE_COLLECTION_NAME
ORACLE_TRACE_COLLECTION_PATH
ORACLE_TRACE_COLLECTION_SIZE
ORACLE_TRACE_ENABLE
ORACLE_TRACE_FACILITY_NAME
ORACLE_TRACE_FACILITY_PATH
PARTITION_VIEW_ENABLED
PLSQL_NATIVE_C_COMPILER
PLSQL_NATIVE_LINKER
PLSQL_NATIVE_MAKE_FILE_NAME
PLSQL_NATIVE_MAKE_UTILITY
ROW_LOCKING
SERIALIZABLE
TRANSACTION_AUDITING
UNDO_SUPPRESS_ERRORS

Appendix B: Initialization Parameters Deprecated in 10g
——————————————————-

LOGMNR_MAX_PERSISTENT_SESSIONS
MAX_COMMIT_PROPAGATION_DELAY
REMOTE_ARCHIVE_ENABLE
SERIAL_REUSE
SQL_TRACE
BUFFER_POOL_KEEP (replaced by DB_KEEP_CACHE_SIZE)
BUFFER_POOL_RECYCLE (replaced by DB_RECYCLE_CACHE_SIZE)
GLOBAL_CONTEXT_POOL_SIZE
LOCK_NAME_SPACE
LOG_ARCHIVE_START
MAX_ENABLED_ROLES
PARALLEL_AUTOMATIC_TUNING
PLSQL_COMPILER_FLAGS (replaced by PLSQL_CODE_TYPE and PLSQL_DEBUG)

Larry Catt
oracle@allcompute.com
www.allcompute.com