Oracle and Expert Systems Technology
Show
Based on my personal DBA experience, I highly recommend that you store all SQL in packages in you have enough shared_pool memory. Storing SQL in packages has several benefits. It ensures that all SQL is uniform and reusable, and it makes the SQL "pin-able". To prevent paging, packages can be marked as nonswappable. Marking a package as nonswappable tells a database that after the package is initially loaded into the shared pool, the package must always remain in memory. This procedure is sometimes called pinning or memory fencing. Oracle provides a procedure called dbms_shared_pool.keep to pin a package, and packages can be unpinned with dbms_shared_pool.unkeep. Only packages can be pinned. Stored procedures cannot be pinned unless they are placed into a package. The choice of whether to pin a package in memory is a function of the size of the object and the frequency of its use. Very large packages that are called frequently might benefit from pinning, but any difference might go unnoticed because the frequent calls to the procedure have kept it loaded into memory anyway. Therefore, because the object never pages out in the first place, pinning has no effect. Also, the way procedures are grouped into packages can have some influence. Some Oracle DBAs identify high-impact procedures and group them into a single package, and then pin this package in the shared pool library cache. In an ideal world, the shared_pool parameter of the init.ora should be large enough to accept every package, stored procedure, and trigger that can be used by the applications. However, reality dictates that the shared pool cannot grow indefinitely, and wise choices must be made in terms of which packages are pinned. Unix users might want to add code to their database startup script ensure that the packages are re-pinned after each database startup, guaranteeing that all packages are re-pinned with each bounce of the box. A script might look like this: ORACLE_SID=mydata EXECUTE dbms_shared_pool.keep('DBMS_ALERT'); exit; The database administrator also needs to remember to run pin.sqlwhenever restarting a database. This is done by reissuing the PIN command from inside SQL*DBA immediately after the database has been restarted. If you have large procedures or large anonymous PL/SQL blocks in your application, you may also want to put these into packages and pin them in the shared pool. You can determine what large stored objects are in the shared pool by selecting from the v$db_object_cache fixed view. This will also tell you which objects have been marked kept. This can be done with the following query: select * from v$db_object_cache where sharable_mem > 10000; If you have plenty of free memory in the shared pool and you wish to mark all packages in the system 'kept', you can execute the following PL/SQL snippet: declare cursor pkgs is select begin open pkgs; end; Now, let's look at how we can identify packages that should be pinned. If you like Oracle tuning, see the book "Oracle Tuning: The Definitive Reference", with 950 pages of tuning tips and scripts. You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts. Click here for more books by Donald K. Burleson.Packages in PL/SQLA package is a way of logically storing the subprograms like procedures, functions, exception or cursor into a single common unit. A package can be defined as an oracle object that is compiled and stored in the database. Once it is compiled and stored in the database it can be used by all the users of database who have executable permissions on Oracle database. Components of PackagePackage has two basic components:
Benefits of using PackageFollowing are some of the benefits of packages in PL/SQL:
How to create a PL/SQL Package?Following are the steps to declare and use a package in PL/SQL code block: STEP 1: Package specification or declarationIt mainly comprises of the following:
Here is the syntax:
where,
<package_name>, <function_name>, <procedure_name> are user-defined names.
STEP 2: Package BodyIt mainly comprises of the following:
Here is the syntax:
Where,
<package_name>, <function_name>, <procedure_name> are user-defined.
Note: Creating a package only defines it, to use it we must refer it using the package object. Following is the syntax for referring a package object:
The Object can be a function, procedure, cursor, exception that has been declared in the package specification and defined in the package body and to access their executable statements above syntax is used. Time for an Example!We have a STUDENT table as specified below:
Let's write a simple program to demonstrate the use of Package in PL/SQL. PL/SQL code for package specification:
Package Created PL/SQL code for package body: Package Body Created Now let's write the PL/SQL code for calling the Procedure and Function used in Package. Enter value for sno: 12 Enter value for snm: Neha RECORD UPDATED RECORD NOT FOUND PL/SQL procedure successfully completed. Note: If the package specification or package body has been created with compilation errors then a following warning message is displayed on the screen: WARNING: Package Body created with compilation errors. In that case, the errors can be seen by executing following statement: In the next tutorial we will cover transactions in PL/SQL. Why do you have functions and procedures together in a package?Procedures and functions permit the caller to provide parameters that can be input only, output only, or input and output values. Procedures and functions allow you to combine the ease and flexibility of SQL with the procedural functionality of a structured programming language.
What are procedures functions and packages in Oracle?A package is a group of related procedures and functions, together with the cursors and variables they use, stored together in the database for continued use as a unit. Similar to standalone procedures and functions, packaged procedures and functions can be called explicitly by applications or users.
How can we create procedure inside package in Oracle?Procedure. In the Data Project Explorer, right-click the PL/SQL Packages folder in a project, and click New > PL/SQL Package. ... . Complete the steps of the wizard. ... . In the specification, add the stored procedure name and variables. ... . Click the Body tab and edit the PL/SQL package body, to add the stored procedure.. Which of the following is true about Plsql package body?Which of the following is true about PL/SQL package body? The package body has methods declared in package specification and private. It is created using the CREATE PACKAGE Statement.
|