Thursday, May 22, 2014

How to increase performance of PL/SQL code using IN/OUT Parameters and NOCOPY directive?

While wring PL/SQL code, keep in mind that 
  • if I need any calculations, create a function. 
  • if any DB operations which does not return any values from it, create a procedure.

Following ways we can use the parameters passed and returned from functions and procedures:
  • All IN parameters of procedure are passed by pointer. Variable is not copied when program block is called.However PL/SQL block is not allowed to modify the variable value.
  • All OUT and IN/OUT parameters are passed by value. Variable is copied when program block is called, so the pl/sql code can modify a copy of the variable.When pl/sql block completes without error, the original variable is overwritten with the modified copy of variable. If the program block raises exception, the original value remains unchanged.
  • The OUT and IN/OUT parameters can be passed by reference if we use the NOCOPY directive for them.However its not available for function return value.
A function that returns variable as pass by value.

CREATE OR REPLACE FUNCTION func_VarIN(p1 IN VARCHAR2) RETURN VARCHAR2 IS
BEGIN
  RETURN p1;
END;
/
An empty procedure using the IN/OUT parameter.

CREATE OR REPLACE PROCEDURE prc_VarINOUT(p2 IN OUT VARCHAR2) IS
BEGIN
  NULL;
END;
/

An empty procedure using IN/OUT parameter with NOCOPY directive.

CREATE OR REPLACE PROCEDURE prc_VarINOUT_NOCOPY(p3 IN OUT NOCOPY VARCHAR2) IS
BEGIN
  NULL;
END;
/

PL/SQL Code Observations: 
  • Function calls perform similar to procedure calls with IN/OUT parameters.
  • NOCOPY directive reduces the copy overhead and causes the call to be twice as fast then the function or regular procedure.
  • using NOCOPY, bigger value of variable gives bigger boost on performance.
Using of procedures with NOCOPY rather than functions, it pefroms really good - upto 20 -25 times faster and gives us good practices for PL/SQL code clarity.

Wednesday, May 21, 2014

Rsync Command to Sync files in Unix/Linux.

Rsync (Remote Sync) command is used for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. With the help of rsync command you can copy and synchronize data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux/Unix machines.
  • It supports copying links, devices, owners, groups and permissions.
  • It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows us to transfer just the differences between two sets of files. 
  • First time, it copies the whole content of a file or a directory from source to destination. From next time. it copies only the changed blocks and bytes to the destination.
  • It consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.
  • It copy files from locally, to/from another host over any remote shell, or to/from a remote rsync daemon.
  • Its widely used for backups and mirroring and as an improved copy command for everyday use.
  • It can use exclude and exclude-from options similar to GNU tar.
  • It does not require root privileges.
  • It supports anonymous or authenticated rsync servers which is ideal for mirroring.
Syntax

Local use:
rsync [OPTION...] SRC... [DEST]

Access via remote shell (PULL):
rsync [OPTION...] [USER@]HOST:SRC... [DEST]

Access via remote shell (PUSH):
rsync [OPTION...] SRC... [USER@]HOST:DEST

Access via rsync daemon (PULL):
rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

Access via rsync daemon (PUSH):
rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

Common Examples

Copy/Sync Files and Directory Locally

rsync -zvh aaa.tar /tmp/aaa/ destination_folder

destination_folder is current folder by default.

Copy a Directory from Local Server to a Remote Server

rsync -avz anand_files/ anand@192.168.0.1:/home/anand_files/
rsync -avzh anand@192.168.0.1:/home/anand_file /tmp/anand_files

Copy a File from a Remote Server to a Local Server with SSH

rsync -avzhe ssh anand@192.168.0.1:/anand/A.log /tmp/anand/
rsync -avzhe ssh /tmp/a.log anand@192.168.0.1:/backup/anand/

Show Progress While Transferring Data with rsync

rsync -avzhe ssh --progress /home/soft anand@192.168.0.1:/anand/soft

Use of –include and –exclude Options

It excludes/includes particular char/string file names while copying.

rsync -avze ssh --include 'RAJ*' --exclude '*' anand@192.168.0.1:/anand/unix/ /root/anand/unix/

Set the Max/Min Size of Files to be Transferred

rsync -avzhe ssh --min-size='5k' --max-size='2048k' /anand/unix/ anand@192.168.0.1:/anand/unix

Automatically Delete source Files after successful Transfer

rsync --remove-source-files -zvh aaa.tar /tmp/anand/

Set Bandwidth Limit (KBPS) and Transfer File

rsync --bwlimit=1024 -avzhe ssh  /anand/unix/  anand@192.168.0.100:/anand/unix/temp/