Friday, 31 January 2014

when to create index

You should create an index if:

• A column contains a wide range of values
• A column contains a large number of null values
• One or more columns are frequently used together  in a WHERE clause or a join condition
• The table is large and most queries are expected to retrieve less than 2 to 4 percent of the rows


When Not to Create an Index

It is usually not worth creating an index if:
• The table is small
• The columns are not often used as a condition in the query
• Most queries are expected to retrieve more than 2 to 4 percent of the rows in the table
• The table is updated frequently
• The indexed columns are referenced as part of an expression

Nth highest salary

--highest sal

select ENAME,sal from emp where sal=(select max(sal) from emp)

2nd highest sal
select ename,sal from emp a where 2=(select count(distinct(sal)) from emp b where A.SAL<=B.SAL )

3rd highest sal
select ename,sal from emp a where 3=(select count(distinct(sal)) from emp b where A.SAL<=B.SAL )

1st highest sal in each department
select * from emp a where 1=(select count(distinct(sal))from emp b where A.DEPTNO=B.DEPTNO and A.SAL<=B.SAL  )

 highest sal in specific department
select * from emp a where 1=(select count(distinct(sal))from emp b where A.DEPTNO=B.DEPTNO and A.SAL<=B.SAL and a.deptn0=20  )

NOCOPY Hint to Improve Performance of OUT and IN OUT Parameters in PL/SQL Code

Oracle has two methods of passing passing OUT and IN OUT parameters in PL/SQL code:
  • Pass By Value : The default action is to create a temporary buffer (formal parameter), copy the data from the parameter variable (actual parameter) to that buffer and work on the temporary buffer during the lifetime of the procedure. On successful completion of the procedure, the contents of the temporary buffer are copied back into the parameter variable. In the event of an exception occurring, the copy back operation does not happen.
  • Pass By Reference : Using the NOCOPY hint tells the compiler to use pass by reference, so no temporary buffer is needed and no copy forward and copy back operations happen. Instead, any modification to the parameter values are written directly to the parameter variable (actual parameter).
Under normal circumstances you probably wouldn't notice the difference between the two methods, but once you start to pass large or complex data types (LOBs, XMLTYPEs, collections etc.) the difference between the two methods can be come quite considerable. The presence of the temporary buffer means pass by value requires twice the memory for every OUT and IN OUT parameter, which can be a problem when using large parameters. In addition, the time it takes to copy the data to the temporary buffer and back to the parameter variable can be quite considerable.

Why Views are required ?

view is a named and validated SQL query which is stored in the Oracle data dictionary. Views do not contain any data - it is just a stored query in the database that can be executed when called. One can think of a view as a virtual table or mapping of data from one or more tables.
Views are useful for security and information hiding, but can cause problems if nested too deeply. Some of the advantages of using views:

  To restrict data access
• To make complex queries easy
• To provide data independence
• To present different views of the same data

Tuesday, 28 January 2014

Duplicate Rows and coloumns in Oracle

How to find the duplicate rows in oracle?

SELECT  a.deptno,a.empno,a.sal from emp a,emp b 
where  a.deptno=b.deptno
and      a.empno=b.deptno
and      a.sal=b.sal
and      a.rowid<>b.rowid;

Below query is used to delete the duplicate rows from the table
Note := we should give the all the column names in groupby clause ..else only duplicate columns gets deleted

DELETE FROM emp
WHERE rowid not in
(SELECT MIN(rowid)
FROM emp
GROUP BY empno, ENAME, JOB);

How to find the duplicate coloumns in oracle?

select empno from emp group by empno having count(*)>1;

Difference between ROWNUM and ROW_NUMBER?


 
ted
ROWNUM is a "pseudocolumn" that assigns a number to each row returned by a query:
SQL>selectrownum, ename, deptno
2fromemp;
 
    ROWNUM ENAME          DEPTNO
---------- ---------- ----------
1 SMITH              99
2 ALLEN              30
3 WARD               30
4 JONES              20
5 MARTIN             30
6 BLAKE              30
7 CLARK              10
8 SCOTT              20
9 KING               10
10 TURNER             30
11 FORD               20
12 MILLER             10
ROW_NUMBER is an analytic function that assigns a number to each row according to its ordering within a group of rows:
SQL>selectename, deptno, row_number() over (partitionbydeptnoorderbyename) rn
2fromemp;
 
ENAME          DEPTNO         RN
---------- ---------- ----------
CLARK              101
KING               102
MILLER             103
FORD               201
JONES              202
SCOTT              203
ALLEN              301
BLAKE              302
MARTIN             303
TURNER             304
WARD               305
SMITH              991
 

Monday, 27 January 2014

View vs Materialized View in SQL


 
When to Use View vs Materialized View in SQL
Mostly in application we use views because they are more feasible, only logical representation of table data no extra space needed. We easily get replica of data and we can perform our operation on that data without affecting actual table data but when we see performance which is crucial for large application they use materialized view where Query Response time matters so Materialized views are used mostly with data ware housing or business intelligence application

< please post your comments and reviews on this>
Venugopal Basam
09543457785

Saturday, 25 January 2014

Advantage of trigger over procedure

We can do in all things in store procedure which can be done by trigger. what is more advantage beyond this by using trigger at application level?

Procedure runs only when one call them manually whereas a trigger runs when there is any activity(insert,update,delete) on table on which the trigger is written.

This is the main advantage of triggers over procedures is if you want to perform any action on the DML event of any table you should use triggers as you dont need to make a seperate call for your code.

Triggers are fired implicitly where as procedure are called explicitly by procedure name when required.

< please post your comments and reviews on this>
Venugopal Basam
09543457785

What happens if we rename the table which is having the primary key values.

We  have the parent and child relationship.

Dept2.depno (Primary key)
Emp2.deptno( foreign key)


What happens if we rename the table which is having the primary key values.

Solution:
Answer :  Automatically the foreign key relation exists with new table which is rename..hence no impact.

 Please provide your comments and feedback >

Venugopal Basam
9543457785

multiple constraints for single column in a table

Can we have multiple constraints for single column in a table

Answer : Yes we can have multiple constrains for single column