0% found this document useful (0 votes)
3 views

Practice+34+ +Optimizing+Database+Connection

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practice+34+ +Optimizing+Database+Connection

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Practice 34 - Optimizing Database Connection P a g e |1

Practice 34

Optimizing Database Connection

Practice Target
In this practice, you will perform the recommended steps to optimize database connections.

In high level, in this practice, you will perform the following:


• Run a query in the current database state and gather its entire elapsed time.
• Perform the changes to optimize the connection to the database
• Run the same query and compare the elapsed time with the elapsed time noted in the first step.

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |2

Preparing for the Practice


In the following steps, you will take actions to prepare the environment for this practice.

1. If srv1 is not running, start it up.

2. Open Putty, login as oracle to srv1. In the rest of this practice, this session will be referred to as
the monitoring window.

3. Run the following code to create a SQL*Plus script file named as display_sqlnet.sql
The script retrieves the total 'SQL*Net%' wait events for specific current sessions.

cat > display_sqlnet.sql <<EOL


col EVENT for a30
col WAIT_CLASS for a10
SELECT E.EVENT,
TIME_WAITED TIME_CS,
E.WAIT_CLASS
FROM V\$SESSION_EVENT E, V\$SESSION S
WHERE E.SID=S.SID
AND S.MODULE='TEST' AND E.EVENT LIKE 'SQL*Net%'
ORDER BY TIME_WAITED;
EOL

4. In the hosting PC, open a command-line window. In the rest of this practice, this window will be
referred to as the client window.

5. In the client window, issue the following command to open notepad and create a SQL*Plus script
file named as run_query.sql. Enter the code that follows to the script file.
The script runs a single query. It also sets the session module/action, enables tracing on the
query, and displays the trace file of the session that runs the script file.

notepad run_query.sql

EXEC DBMS_APPLICATION_INFO.SET_MODULE ('TEST','TEST')


col ORDER_DATE for a30
ALTER SESSION SET EVENTS '10046 TRACE NAME CONTEXT FOREVER, LEVEL 12';
SET TIMING ON
SELECT ORDER_ID, ORDER_TOTAL, ORDER_DATE FROM ORDERS WHERE ORDER_ID <=110000;
SET TIMING OFF
ALTER SESSION SET EVENTS '10046 TRACE NAME CONTEXT OFF';

SELECT P.TRACEFILE
FROM V$SESSION S
JOIN V$PROCESS P ON S.PADDR = P.ADDR
WHERE S.USERNAME = 'SOE';

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |3

6. In the monitoring window, open the sqlnet.ora file and set the parameter
TRACE_LEVEL_SERVER to 16.

cat $TNS_ADMIN/sqlnet.ora

# if the parameter is not already set in the file:


echo "TRACE_LEVEL_SERVER=16" >> $TNS_ADMIN/sqlnet.ora

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |4

Optimizing Database Connection from SQL*Plus


In the following steps, you will perform the recommended steps on optimizing Oracle database
connection in SQL*Plus. You will examine the impact of the procedure on enhancing query retrieval
time.

7. In the client window, invoke SQL*Plus, connect to ORADB as soe

sqlplus soe/soe@oradb

8. In the client window, check out the value of the SQL*Plus parameter ARRAYSIZE
ARRAYSIZE sets the number of rows fetched at one time. The default value is 15.

SHOW ARRAYSIZE

9. In the client window, run the query script.


Take a note of the query execution time.
@ run_query

10. In the monitoring window, determine the SDU size used by the connection between the client
SQL*Plus and the database listener.
Obtain the trace file full name from the output of the preceding step.
The default SDU is 8KB.
cat <trace_file> | grep nsconneg

11. In the monitoring window, use tkprof utility to convert the trace file into a text file.
tkprof <trace_file> output1.log SYS=no waits=yes

12. Open the generated text file and observe the count value of the fetch operation for the query.
It should be very close to the value 6667. Can you explain why?
Observe the 'elapsed seconds in trace file' is nearly the same as the execution time noted
earlier.
vi output1.log

13. In the monitoring window, invoke SQL*Plus, connect to the database as sysdba. Then run the
script display_sqlnet.sql. Take a note of the output.

sqlplus / as sysdba
@ display_sqlnet

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |5

In the previous steps, you ran the query and gather statistics about it. In the following steps, you will
perform changes on the database connection parameters.

14. In the monitoring window, exit from SQL*Plus then obtain the bandwidth of the network
adapter eth0
In our practice environment, the bandwidth is 1000Mb/s (this is Mb/s not MB/s).
ethtool eth0 | grep Speed

15. In the client window, exit from SQL*Plus then obtain the latency of its connection to the
database machine. Use either of the following commands.
Substitute the IP address with the IP address in your environment.
The round trip time is less than 1 ms. Consider the latency is 1 ms in this case.
ping -l 9000 192.168.1.133
tracert -4 192.168.1.133

16. Using the following formula, calculate the recommended socket size.
The recommended socket size = bandwidth * latency * 3

The calculation should go as follows:


The recommended socket size = 1000 * 1000000 (b/s) * 0.001 (s) * 3 = 3,000,000 bit =
3,000,000 / 8 byte = 375,0000 byte

17. In the hosting PC, open the tnsnames.ora file with the notepad. Make the changes on the
ORADB connection descriptor so that is appears as follows.
The SDU size is increased to 512 KB.
The socket size increased to 375,000 byte.
Note: Do not copy the code from the PDF file. Take it from the attached tnsnames.ora file or
manually add them.
ORADB =
(DESCRIPTION =
(SDU=524288)
(SEND_BUF_SIZE=375000)
(RECV_BUF_SIZE=375000)
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.159)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORADB.localdomain)
))

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |6

18. In the monitoring window, open the sqlnet.ora file and add the following lines to it.
Note: Do not copy the code from the PDF file. Take it from the attached sqlnet.ora file or
manually add them.

vi $TNS_ADMIN/sqlnet.ora
DEFAULT_SDU_SIZE=524288
SEND_BUF_SIZE=375000
RECV_BUF_SIZE=375000

TRACE_LEVEL_SERVER=16

19. Restart the listener.


lsnrctl stop
lsnrctl start

20. Register the database service in the listener.


sqlplus / as sysdba
ALTER SYSTEM REGISTER;
exit

21. In the client window, invoke SQL*Plus, connect to ORADB as soe, then set the value of the
SQL*Plus parameter ARRAYSIZE to 100.

sqlplus soe/soe@oradb
set ARRAYSIZE 100

In the following steps, you will run the query and gather statistics about its execution. You will
compare them with the statistics noted before the changes.

22. In the client window, run the query script.


Compare the elapsed time value to its noted value. The query runs slightly faster after the
implementing the changes.
@ run_query

23. In the monitoring window, verify that the SDU size used by the connection between the client
SQL*Plus is 524288 byte.
cat <trace_file> | grep nsconneg

24. In the monitoring window, use tkprof utility to convert the trace file into a text file.
tkprof <trace_file> output2.log SYS=no waits=yes

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |7

25. Open the generated text file and observe the count value of the fetch operation for the query.
It should be very close to the value 1000.
vi output2.log

26. In the monitoring window, invoke SQL*Plus, connect to the database as sysdba. Then run the
script display_sqlnet.sql. Compare the value of 'SQL*Net message to client' with its noted
value.
SQL*Net message to client is less after increasing the array size.
sqlplus / as sysdba
@ display_sqlnet

Cleanup

27. In the monitoring window, delete the created files.


rm display_sqlnet.sql
rm output1.log
rm output2.log

28. In the monitoring window, delete the created file


del run_query.sql

29. In the monitoring window, open the sqlnet.ora file and remove the following added code from
it.
vi $TNS_ADMIN/sqlnet.ora
DEFAULT_SDU_SIZE=524288
SEND_BUF_SIZE=375000
RECV_BUF_SIZE=375000
TRACE_LEVEL_SERVER=16

30. In the hosting PC, open the tnsnames.ora file with the notepad and remove the following added
code from it.
(SDU=524288)
(SEND_BUF_SIZE=375000)
(RECV_BUF_SIZE=375000)

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 34 - Optimizing Database Connection P a g e |8

Summary

• Tuning database connection includes tuning the following setting


o Setting the SDU at the database side and at the client side as well.
o Setting the buffer socket size
o Setting the batch size at the application side

• Tuning database connection introduces enhancement in data retrieval time

Oracle Database Performance Tuning, a course by Ahmed Baraka

You might also like