24 May 2010

SHRINK DATA FILE

In order to regain unused space on your database you need to use SHRINK command on Oracle. I will try to illustrate this feature with a very simple example:
First create a table:

CREATE TABLE SHRING_TEST2_TAB(
aa number,
tt varchar2(1000)
)
/

Then fill this table with some dummy data:

Fill it more and make it a big sized table;

Now we have a big table in USERS tablespace.

Attention please, We have not committed the data yet. If we issue a rollback statement all of the inserts we have done will be gone and this table will be empty. Normally, we shall expect what we have done would not cost a space to us. But this is so wrong in Oracle terms. Oracle already allocated that space (segments) and even if you didn't commit it, it wont release it until you do something for releasing.

Now lets see how SHRINK command works:

Did you see the FREE(MB) column, it was 2MB before and after shrink operation it is 25 MB. Don't worry about the bar chart, it will show the right result soon, it is due to OEM’s weird and lazy behavior.

Now we have a smaller table but still have a big datafile. Small table was not our main aim, we want to resize the physical file on OS. After this shrink operation we can resize it because we have free space on that file, lets use OEM for that:

Before I resize the USERS01.dbf, it has a 51.25 MB size as below:

I changed the File Size value from 51.25 MB to 28 MB as below:

ATTENTION: If I entered a value lower than 26.25 MB, it would raise an error because there is still 26.25 MB data on this datafile.

* If you receive an ORA-03297 error when trying to apply the changes, you need to REORGANIZE the tablespace. How you can reorganize a tablespace is illustrated at the end of this document?

And this is how it looks like now;

Lets see the result on OS view too, pay attention to the size value of USERS01.dbf file:

This is a great feature if you had a lot of delete operations on your tables before, you can free your unused spaces by using this feature. By the way, you need to enable row movement on the table you want to make shrink operation and also the tablespace you are working on must have auto segment management.

ORA-03297- File Contains used data beyond requested RESIZE value

If you receive above error when you try to resize your datafile, it means that your datafile is fragmented and it is needed to be reorganized. You can reorganize your datafiles by reorganizing your tablespaces. Below screen shows how can you reorganize tablespaces through OEM:

When you choose the Reorganize action, basically oracle will create a job which will create a new tablespace with a new datafile and move all your tables to your new tablespace from old fragmented tablespace. After move operations complete, it will drop your old tablespace and rename fresh tablespace to deleted one’s name. Finally you will have a unfragmented tablespace with new datafile name. Oracle does not delete the old fragmented datafile after reorganizing operation, you need to manually delete it from OS file structure you need to shutdown and startup your db to be able to delete that file from OS otherwise it gives in use error).

working of plan table!!!

column options format a30
column optimizer format a30
column statement_id format a10

delete from plan_table where statement_id = ‘m1′;

explain plan set statement_id = ‘m1′
for
select sales_town.town_descr,sum(sm_sales_exp.net_sales_val) as sum_net_sales_val_24 from sales_town,sm_sales_exp where ((sm_sales_exp.prd_code >= ‘200804′) and (sm_sales_exp.prd_code <= ‘200810′) and (sm_sales_exp.terr_code in (select territory_code from sales_station
where territory_descr like ‘nellore%’ and (town_descr like ‘tirupati%’ or town_descr like ‘madana%’)) ) and (sm_sales_exp.item_ser = ‘rd’) ) group by sales_town.town_descr ;

select statement_id,operation,options,object_name,object_type,optimizer,search_columns,position
from plan_table where statement_id = ‘m1′;

batch files!!!

The key to automated backup is the use of a batch file. This is a text file that contains commands that are executed whenever the file is run. Creating a batch file is easy. In Windows, you can create one using Notepad. The thing to remember is that it must be a pure ASCII text file. Using a Word Processor, even Wordpad, will not produce a pure text file unless you are very careful. I recommend sticking to Notepad to be sure.

Microsoft first introduced batch files in DOS, where they used one (autoexec.bat) as one of the main startup files for booting DOS. Since then, the graphical environment of Windows has taken over, and a lot of people either forgot about batch files or never learned about them. But they still have their uses, and backing up files is one of them. I will do an example that assumes you have a Zip drive that is the G: drive on your system. If you want to backup to a different hard drive, or to a network drive, you will need to adjust this accordingly.

Start by opening Notepad. Type the following command:

copy c:\temp g:

That is all, just the one line. Save it to your Desktop, but be careful to name it Test.bat. You need to have the *.bat extension for it to work as a batch file. Now, if you look at the icon on your desktop, you will see that it is not the usual text file icon. It will have gear on it. That is the sign that it is recognized as a batch file. If you have a C:\Temp directory, make sure it has a couple of files in it so you can see how this works. If you don’t have such a directory, create one and put a couple of files in it. Now, just double-click on your batch file, and you should see a MS-DOS window open, with a black background, and you will see your files being copied. Each file will be listed, and at the end if will say “X” file(s) copied, where “X” is the number of files in your C:\Temp directory. Now, go to your G: drive (or whichever drive you used for this test), and verify that your files were indeed copied. Congratulations! You have just written and run a batch file.

Now, this is the essence of how to use a batch file to backup important files. But we need to refine it a little. First, the “copy” command is just not powerful enough for our purposes. Fortunately, there is another command, called “xcopy”, that will do very nicely. You can get information on this command by opening an MS-DOS window (also known as a Command Prompt in Windows NT/2000), and entering the command:

help xcopy

Here is what you get back in Windows 2000, for instance:

****************************************************

Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W] [/C] [/I] [/Q] [/F] [/L] [/H] [/R] [/T] [/U] [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/EXCLUDE:file1[+file2][+file3]…]

source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set, doesn’t change the attribute.
/M Copies only files with the archive attribute set, turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
/EXCLUDE:file1[+file2][+file3]… Specifies a list of files containing strings. When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied. For example, specifying a string like \obj\ or .obj will exclude all files underneath the directory obj or all files with the .obj extension respectively.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/V Verifies each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attribute
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite an existing destination file.
/Z Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable. This may be overridden with /-Y on the command line.

****************************************************

Now, this may be a bit much when you first look at it, but in most cases you won’t need all of these options in your batch file. Here is a copy of my own backup batch file, which I call “backup.bat”

xcopy /e /v /y E:\Agent \\NTSERVER\NTServerC\KevinBackup\Agent
xcopy /e /v /y E:\Agent M:\Backup\Agent
xcopy /e /v /y J:\Netscape\Users\KOB1 \\NTSERVER\NTServerC\KevinBackup\Netscape
xcopy /e /v /y J:\Netscape\Users\KOB1 M:\Backup\Netscape
xcopy /e /v /y F:\MyDocuments \\NTSERVER\NTServerC\KevinBackup\MyDocuments
xcopy /e /v /y F:\MyDocuments M:\Backup\MyDocuments
xcopy /e /v /y F:\Eudora \\NTSERVER\NTServerC\KevinBackup\Eudora
xcopy /e /v /y F:\Eudora M:\Backup\Eudora

Let us take this a piece at a time. First, the command “xcopy”. This is followed by three “switches” as they are called. The first, /e, says to copy all of the subdirectories and their contents as well. So I only need to name a directory, and all of the contents get copied. The second switch, /v, tells the computer to verify the copy it makes. The third switch, /y, tells the computer to go ahead and overwrite the remote copy of the file without asking me if I am sure. Following these switches, first comes the source, followed by the destination. So in line one, all of the contents of the E:\Agent directory are being copied to a place on the Server. In my case, I am actually doing two backups here. After copying to the server, I also make a copy to a second hard drive (Drive M: on my machine).

So, take your Test file, and try creating an xcopy command to back up one of your directories. When you know it works, just create additional xcopy commands for as many directories as you think you need to backup. I think you will find that creating a batch file to do your backups is not hard at all. And next I will show you how to make it automatic.

##############———-##############

Use ECHO %DATE% at a prompt and it returns the
data–you can also use %date% in a .cmd file with just about any
other command. You just have to remember, depending on the
regional settings of the machines, to replace / ir you’re
gonna use it in a file name

the basics of FTP!!!

Basic Order of Operations:

1. Change to your local directory where most (if not all) of the files you will be transferring are kept.
2. Open a connection to the remote host via the ftp command.
3. Once connected to the remote host, change to the directory (cd command) where the files are that you are going to get or to the location where you are going to put files.
4. Set the transfer mode (ascii or binary).
5. Transfer the files (get, mget, put, mput).
6. Repeat steps 1, 3, 4, 5 as necessary.
7. Exit ftp with the bye command.

Commands:

* ftp [host] – open an ftp session with the specified host machine.

Examples:
C:\> ftp neserve0
C:\> ftp erols.erols.com

* open [host] – Establish a connection to the specified host when you’re already at an ftp prompt.

Examples:
ftp> open neserve0
ftp> open erols.erols.com

* user [username] – Log into an ftp server when you’re already connected in an ftp session.

Examples:
ftp> user dlozinsk
ftp> user anonymous

* ls [remote-directory] – Print a listing of the contents of remote-directory on the remote machine. The listing includes any system-dependent information that the server chooses to include.

Examples:
ftp> ls
ftp> ls /usr/local/bin

* dir [remote-directory] [local-file] – Print a listing of the contents in the directory remote-directory, and optionally, placing the output in local-file.

Examples:
ftp> dir
ftp> dir /usr/local/bin

* help [command] – Print an informative message about the meaning of command. If no argument is given, ftp prints a list of the known commands.

Examples:
ftp> help
ftp> help dir

* ? – synonym for help.

Examples:
ftp> ?
ftp> ? dir

* pwd – Print the name of the current working directory on the remote machine.Often times this includes printing the full path.

Example:
ftp pwd>

* cd [remote-directory] – Change the working directory on the remote machine to remote-directory.

Examples:
ftp> cd /tmp
ftp> cd ../..

* lcd [directory] – Change the working directory to directory on the local machine. If no directory is specified, the user’s home directory is used.

Examples:
ftp> lcd c:\temp
ftp> lcd ../..

* ascii – Set the file transfer type to ASCII . Only use this transfer method for text-files. That is, files ending in .txt, html files, and/or perl programs.

Example:
ftp> ascii

* binary – Set the file transfer type to support binary file transfer. Use this transfer method for anything other than a textfile. For example, Word documents, pdf files, gifs, jpgs, java class files, etc.

Example:
ftp> binary

* put [local-file] – Put (upload) local-file to the remote machine. No wildcards!

Examples:
ftp> put index.html
ftp> put test.txt

* get [remote-file] – Retrieve (download) remote-file and store it on the local machine. No wildcards! Can only get one file at a time.

Examples:
ftp> get index.html
ftp> get /tmp/readme.txt

* mput [local-files] – Expand wild cards in the list of local-files given as arguments and do a put for each file in the resulting list. The list of files should be separated by spaces.

Examples:
ftp> mput *
ftp> mput *.html
ftp> mput *.html test.txt README

* mget [multiple files and/or wildcards] – Expand wild cards in the list of remote files given as arguments and do a get for each file in the resulting list. The list of files should be separated by spaces.

Examples:
ftp> mget *
ftp> mget *.gif
ftp> mget *.doc image.gif salaries*

* prompt – Toggle interactive prompting. Interactive prompting occurs during multiple file transfers to allow the user to selectively retrieve or store files. If prompting is turned off, any mget or mput will transfer all files, and any mdelete will delete all files.

Example:
ftp> prompt

* bell – Arrange that a bell be sounded after each file transfer command is completed.

Example:
ftp> bell

* delete [remote-file] – Delete the remote-file on the remote machine.

Examples:
ftp> delete test.doc
ftp> delete /tmp/temporary_file.txt

* mkdir [new-directory-name] – create a directory new-directory-name on the remote machine.

Examples:
ftp> mkdir temp
ftp> mkdir /tmp/dave

* rmdir [directory-name] – Delete the directory entitled directory-name on the remote machine.

Examples:
ftp> rmdir temporary_directory
ftp> rmdir /tmp/test_dir

* rename [old-file-name] [new-file-name] – Rename the file old-file-name on the remote machine, to the file new-file-name.

Examples:
ftp> rename index.htm homepage.html
ftp> rename /tmp/readme.txt /tmp/README_NOW.txt

* bye – Terminate the FTP session with the remote server and exit ftp. On Unix, an end of file should also terminate the session and exit.

Example:
ftp> bye

* quote site chmod xxx [file name] – Change the permission modes of the file file-name on the remote system to xxx mode. Note that the chmod command is not always implemented.

* get [file-name] “|more” – Instead of downloading and saving the file file-name on the local machine, you view its contents. Only recommended to use with text files.