Friday, October 29, 2010

Oracle database FAQ

Find out 5th highest salary from emp table
SELECT DISTINCT(a.sal)
FROM EMP A WHERE 5 =
(SELECT COUNT(DISTINCT(b.sal))
FROM EMP B
WHERE a.sal <= b.sal);

How to find out Oracle version
select * from v$version

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Solaris: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

How to find out last update time in a table
select scn_to_timestamp( max(ora_rowscn) ) from tableName;

How to login to SQLPLUS from command line
sqlplus user/password@sid

How to see execution plan for an SQL
SQL> set autotrace on
SQL> select count(*) from table_name;

SQL> explain plan set statement_id = '111' for select * from tableName;
SQL> select * from plan_table;

How to find out cpu time for an SQL?
SQL> TIMING START select_emp
SQL> SELECT * FROM employee ;
SQL> TIMING SHOW
select_emp timing for:
select_emp real: 1760

Solaris commands

Get the full command-line including arguments for a process on Solaris
/usr/ucb/ps auxww

Disk space
df -hF zfs
du -sk dir

List zones
zfs list
zoneadm list -cv
df -h /zfs/zones/*

Mail env to someone
env | /usr/bin/mailx -v xyz@xxx.com

CPU and Memory information
connect to global zone as root and run this:
prtdiag -v <= for Memory and CPU
prtconf -pv | head
uname -a
df -hF zfs <= for disk space (make sure to locate your zone from the list)

Process tree
ptree 28811

Print process arguments and environment variables
pargs -e 24272

> top
load averages: 3.34, 3.07, 3.01 16:14:17
102 processes: 100 sleeping, 2 on cpu
CPU states: 76.4% idle, 16.4% user, 7.2% kernel, 0.0% iowait, 0.0% swap
Memory: 32G real, 2116M free, 18G swap in use, 30G swap free

PID USERNAME LWP PRI NICE SIZE RES STATE TIME CPU COMMAND
28811 adatdce 409 0 0 13G 12G cpu/3 209:29 10.19% java
26243 adatdce 92 59 0 257M 245M sleep 252:53 5.88% TestManager
29824 adatdce 1 50 0 4248K 3656K sleep 147:23 4.52% prstat
4743 adatdce 506 59 0 2293M 2266M sleep 104:32 0.19% java
13970 adatdce 45 56 1 119M 56M sleep 10:09 0.02% java
16194 adatdce 1 59 0 4376K 3768K sleep 8:06 0.02% prstat
4901 adatdce 1 49 0 3616K 2264K cpu/14 0:00 0.02% top-SunOS-5.10-
18895 adatdce 33 56 1 296M 254M sleep 1:51 0.02% BIBusTKServerMa
22690 adatdce 29 56 1 99M 52M sleep 3:34 0.01% BIBusTKServerMa
13827 adatdce 126 56 1 828M 667M sleep 10:49 0.01% java
13805 adatdce 30 56 1 98M 40M sleep 2:35 0.00% rmiregistry
14226 adatdce 1 56 1 64M 46M sleep 2:15 0.00% db_server
5684 noaccess 18 59 0 222M 119M sleep 2:00 0.00% java
3934 adatdce 23 59 0 113M 45M sleep 1:20 0.00% java
28833 root 32 59 0 9840K 5568K sleep 3:55 0.00% nscd


> prstat -s size -n 5

PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP
28811 adatdce 13G 12G sleep 59 0 3:27:36 2.9% java/405
4743 adatdce 2293M 2266M sleep 59 0 1:44:29 0.1% java/505
13827 adatdce 828M 667M sleep 56 1 0:10:49 0.0% java/126
18895 adatdce 296M 254M sleep 56 1 0:01:50 0.0% BIBusTKServerMa/33
26243 adatdce 256M 244M sleep 59 0 4:11:18 5.0% TestManager/78
Total: 103 processes, 1478 lwps, load averages: 2.98, 3.00, 2.98

"SIZE" == Memory allocated, is always bigger than RSS
RSS includes shared space
MEM includes swap + RAM

prstat -Z <= for process status and resource consumptions
iostat
vmstat


General technical interview questions

What is the Unix command for listening to ports?
netstat -an (local, remote, state)

What is "having" in reference to databases?
when using group by, to aggregate

What are the two flavors of Unix?
i have used mostly solaris and linux a little..but in general, there are system V and BSD

Give an overview of normalization
Normalization is a physical data modelling process where we can avoid update-delete anamolies in db design.

What is denormalization and why would you use it?
some times, we may use denormalization to access data faster for aggregation, material views

What are the ACID properties of a transaction?
atomicity(all or nothing)
consistency (takes db from one consistent state to other)
isolaton (no concurrent updates to data)
durability(once txn commits, it is durable and can be restored)

How would you improve a poor performing query?
use indexes, improve h/w, caching, tune it, hints,

Write an SQL query that returns department name, and the number of employees in each department (it's a single table query)
select dept_name, count(*) from dept group by dept_name

How do you tune a sql statement?
use explain plan or sql analyzer to see the execution path
add hints
rewrite sql
replace sql with pl/sql

What is a prepared statement?
this is compiled and runs faster

What is a hash join?
in memory join

What is a nested loop join?
two for loops

What is a merge sort join?
you can optimize a join by eliminating some row according to sort order

understand row level lock and how it migrates to table level lock

understand user security authentication in unix and location of password files
/etc/passwd

what is chmode command in unix?
file permissions....421 (user group other)

give a jdbc example in a class
load driver, drivermanager.getconnection, conn.preparestatement, statement.execute(exeUpdate)

what is the difference between vector and array and what is best in implementation?
vector can grow, holds objects, threadsafe

what is an inode and what is missing (unix)
inode contains file metadata

where are passwords stored in unix?
/etc/passwd

What is a stored procedure?
sql block

about himself, how long has he been with the company, what he does and who does he report to general question. talk about general architecture related work, specific contributions to the project, achievements, with information about application and database tuning, J2EE, industry standards and best practices, common design patterns, software development methodology (like releases, bug fixes, marketing requirements, technical specifications, application-business-data modeling etc)

is your current role and some of the projects you have worked on?

how did you hear about xxx?

what are some of the benefits of data virtualization?

1) primarily to bring together data from disparate data sources (such as legacy apps, databases, xml-flat files, other datasources) and provide real time view of the data in a much faster way than db warehouse.
2) provide an abstraction layer on top of real datasources for application developers to integrate their apps)

how long does it take to implement a change in the data warehouse and what is the process followed?
generally many days...need to change several batch jobs that may run during night and follow a enterprise configuration change procedure (depending on corp IT change policies)

how long did it took for developing an etl process?
generally several days...need to find what data needs to extracted by gathering business requirements and identify where data exists.

what is the process followed to deploy code to production?
follow corporate change control procedures...typically involves build, test, release cycle with appropriate approvals at each stage

how do you test before deploying in production, do you prepare and execute test cases?
yes. However, most developers only do unit testing..Testing beyond unit testing is done by specialized testing teams such as QA group that focuses on integration-system-performance testing

what was the most challenging situation you have faced and how did you solve it?
Most challenging situation is to understand at a high level the system requirements and design the system without focusing on too much details first

what is a stored procedure, why would you use it instead of say another application program?
A block of code that encapsulates a group of SQL statements and some logic...A stored procedure is preferred in some situations because it is executed on the database server, pre-compiled, and most work can be delegated to DB server

what is a prepared statement?
compiled sql statement

how does database locks work, what is row level lock and table level lock and where is table level lock used?
row level locks are very fine grained....table level locks are to be used sparingly..

what are the different databases you have worked on?
i have mostly worked on oracle...early during my career, I have worked a lot with DB2

imagine yourself as the dba and user complained the report query is not performing well, what do you do?
I will look at what SQL is executed for the report, try gethering execution plan data and tune it for performance

what are the commands for optimizer, query plan generator?
explain plan, tkprof

what are the database statistics tools that you used?
tkprof...oracle enterprize manager has some graphical user interfaces that can aid in viewing database statistics

what kind of java programming have you done?
primarily enterprise application development using j2ee based technologies...core java, jdbc, jms, xml parsing, UI (jsf, jsp, servlets), middleware development

what are the key differences between java and other programming languages such as c++?
for ex, java is for "write once, run anywhere" paradigm which is based on java virtual machine...with C++, you have to compile the same program for different operating systems..there are several other differences in terms of language constructs and syntax..

what is garbage collection?
garbage collection is freeing up space allocated on heap for storing java objects created during program execution

what is the difference between interface and abstract class?
interface defines what a system can do without specifying exact implementation...
Abstract classes can have some implementation and some interface definition

what is synchronization?
synchronization is to allow access for data by multiple threads in a non-destructive way

what is web services and what is your experience in the web services area?
a webservice is a platform independent way to communicate over http with many disparate systems. I have developed many interafces using web services both on producing and consuming side.

left outer join
- returns all rows from left side
equals, hashcode
- Equal objects have equal hashcode

exception hierarchy
- throwable is at the top with two subclasses: Errors, Exceptions
- Exceptions are two types: checked and unchecked(Runtime)

Access modifiers
- protected means within package and subclasses outside
- nothing means within package only