Wednesday, August 10, 2016

creating sql baselines in oracle 11g

First create a test table

CREATE TABLE test (
  id           NUMBER,
  description  VARCHAR2(50)
);



Insert 10000 rows into it using the following code

BEGIN
  FOR v_LoopCounter IN 1..10000 LOOP
 INSERT INTO test (id)
 VALUES (v_LoopCounter);
END LOOP;
END;
 /


Gather stats on the test table

EXEC DBMS_STATS.gather_table_stats(USER, 'test', cascade=>TRUE);


Check the explain plan for the following query.. It should do a full table scan

SELECT description
FROM   test
WHERE  id = 13;


Find the sql_id for the above sql

SELECT *
FROM   v$sql
WHERE  sql_text LIKE '%test%'
AND    sql_text NOT LIKE '%dba_sql_plan_baselines%'
AND    sql_text NOT LIKE '%EXPLAIN%';


 Since we are loading manually in this scenario it is called manual baseline.. There is a parameter which enables system wide automatic loading of baselines.. To enable it you have to set OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES to True. Default is False. Please dont enable on production systems without proper testing

 Manual method

 SET SERVEROUTPUT ON
DECLARE
  l_plans_loaded  PLS_INTEGER;
BEGIN
  l_plans_loaded := DBMS_SPM.load_plans_from_cursor_cache(
    sql_id => 'gat6z1bc6nc2d');
 
  DBMS_OUTPUT.put_line('Plans Loaded: ' || l_plans_loaded);
END;
/

After the above baseline will be loaded..

You can check the loaded baselines using the following query

SELECT sql_handle, plan_name, enabled, accepted
FROM   dba_sql_plan_baselines

You can see in the output.. It will be yes for both enabled and accepted.


This completes the manual loading of baselines.

To test if baseline is working.. Flush shared pool( not recommended on production again) this is for hard parsing..

ALTER SYSTEM FLUSH SHARED_POOL;

Now create index on the table

CREATE INDEX test_idx ON test(id);

Gather stats

EXEC DBMS_STATS.gather_table_stats(USER, 'test', cascade=>TRUE);

Check the explain plan for the following query.. It should do a full table scan even after creating index..

SELECT description
FROM   test
WHERE  id = 13;

Now run the following query..

SELECT sql_handle, plan_name, enabled, accepted
FROM   dba_sql_plan_baselines

you will see a new baseline but it says not enabled.. This is the plan for new index we created but it wont be used as it is not tested.. Oracle uses new baseline only if performance is tested and better than previous baseline.

So to test that and evolve the new plan we grab the sql_handle from dba_sql_baselines and run the following..
SELECT DBMS_SPM.evolve_sql_plan_baseline(sql_handle => 'SQL_7b76323ad90440b9') FROM   dual;


Now if you run the above select query and check explain plan it should be using the plan with index.

Wednesday, August 3, 2016

ORA-02327: cannot create index on expression with datatype LOB

I got this error when i am trying to rebuild an index on a different tablespace.

Here is the query i ran..

Alter index  xxx.SYS_IL0000194446C00010$$ REBUILD tablespace xxx_data
Error at line 1
ORA-02327: cannot create index on expression with datatype LOB

Previously these tablespaces are in Users tablespace.

So in order to move them we should do the following since these are LOB's


ALTER TABLE xxx.TBL_REQUEST_STATUS_DETAIL MOVE LOB (TECH_ERROR)
STORE AS (TABLESPACE xxx_DATA);

After running this i was able to move them successfully



my reference:

ALTER TABLE xxx.TBL_REQUEST_STATUS_DETAIL MOVE LOB (TECH_ERROR)
STORE AS (TABLESPACE xxx_DATA);
ALTER TABLE xxx.TBL_REQUEST_STATUS_DETAIL MOVE LOB (REQUEST_BODY)
STORE AS (TABLESPACE xxx_DATA);
ALTER TABLE xxx.TBL_REQUEST_STATUS_DETAIL MOVE LOB (RESPONSE_BODY)
STORE AS (TABLESPACE xxx_DATA);
ALTER TABLE xxx.TBL_REQUEST_STATUS_DETAIL MOVE LOB (ERROR_MESSAGE)
STORE AS (TABLESPACE xxx_DATA);

Friday, July 29, 2016

Configure and Install Oracle Application express for 11g database

Installing oracle application express

Usually oracle express will come pre installed in the oracle database

To check if it is installed you need to query dba_registry

select * from dba_registry where comp_id='APEX';

If it is installed note the version and all you need to do is configure it.

For configuring follow these steps

Go to the $ORACLE_HOME/apex directory.

sqlplus / as sysdba

@apxconf.sql

Enter values below for the XDB HTTP listener port and the password for the Application Express ADMIN user.
Default values are in brackets [ ].
Press Enter to accept the default value.

Enter a password for the ADMIN user              []admin_password
Enter a port for the XDB HTTP listener [8080]
...changing HTTP Port

ALTER USER ANONYMOUS ACCOUNT UNLOCK;

Check port usage using the following command.

select dbms_xdb.gethttpport from dual;

Usually the above gives 0. If thats the case run the below to change port to 8080

Enable Oracle XML DB HTTP server

EXEC DBMS_XDB.SETHTTPPORT(8080);
COMMIT;

This completes setup. You can login to admin console from following location.

http://host:port/apex
http://host:port/apex/apex_admin — Admin console


To upgrade Apex to latest version download from oracle support. In my case i am upgrading from version 3 to version 5.

apex_5.0.4_en.zip

unzip apex_5.0.4_en.zip

I unzipped the file to /opt/app/oracle


Sqlplus / as sysdba

Make sure anonymos, apex_public_user and APEX_030000 users are unlocked

Check default tablespace and temporary tablespace of the users which are usually sysaux and temporary


Once this is done, Navigate to unzipped folder and run the following

cd /opt/app/oracle/apex

sqlplus / as sysdba

@apexins.sql SYSAUX SYSAUX TEMP /i/


This takes some time to complete

After installation is complete you need to setup admin password. You need to do this again even if you did it earlier because user needs to be configured based on new version

@apxchpwd.sql

================================================================================
This script can be used to change the password of an Application Express
instance administrator. If the user does not yet exist, a user record will be
created.
================================================================================
Enter the administrator's username [ADMIN]
User "ADMIN" exists.
Enter ADMIN's email [ADMIN]
Enter ADMIN's password []
Changed password of instance administrator ADMIN.


After this is done there is one more final step to load images

To load images give the directory without apex as below.

sqlplus / as sysdba

@apex_epg_config.sql /opt/app/oracle

This takes around 2 minutes.

This completes the upgrade. You may login using the web address. Sometimes you might need to restart db for the changes to take effect



Friday, July 8, 2016

[Off topic] How to calculate your lease payment and negotiate lease deal

First visit all the dealerships and test drive the vehicles you like. Just take basic quotes from the dealers and do not buy at that point.

Once you have driven all the cars narrow down based on what you like. After you know what car you liked the most start research on the car.

Hit to edmunds forums and go to the vehicle specific forums and see what others are paying. Also request money factor and residual value of the model you would like to buy and one of the moderator will post the values for you.

Now visit specific car related forums. In my case i am planning to get a 2016 Honda civic touring.

So i went to civicx forums to see what are the possible issues people are facing and see if there are any TSB's to fix the issue. Make sure you check these issues before you finalize your purchase.

In addition to that see what prices people are paying around your area as they have state specific forums in civicx for example.

Once you get an idea just go to truecar and get the price for your model. Just use this as reference.

Also in edmunds you can check the invoice price of the vehicle.

The sticker price for civic touring $27335 ( Including destination fee)

Invoice price is around $24600 + $835 destination fee

Keeping these in mind i want to target price less than invoice including destination for $24500

From here i started requesting quotes online from all dealerships around me in 30 mile radius.

After negotiations over the phone and emails i am left with prices ranging from 24100 to 24900

I send this 24100 quote i got from the dealer who is quite far from my place to nearest dealer to see if he can beat it and i said i will be coming in today for purchase.

Instantly i got a call saying to bring the printed quote and they will beat it by $50 bucks.

So i headed to the dealership and when i told them this is for a lease they said for lease they make calculations differently and some crap. Dont hesitate to walkout. They will call you back in most of the cases. They will try to comeup with some numbers and force you but be firm and ask them the money factor (multiply this value with 2400 to get interest rate) and residual value( this is the value finance company decides what the value of car at the end of 3 yr lease)

So For honda civic touring i was told residual is 57% and MF is 0.00070(only if credit score above 760)


Do the math dont panic and let dealer match the price. This is how you calculate

Residual value is always calculated on MSRP or sticker price

Since dealer said 57%

27335*(57/100) = 15580.95

So your car is worth 15580.95 at the end of the lease. If you want you can buy your car if you want for that price at the ened of lease.

Money factor i was told is 0.00070

multiply money factor by 2400 to get interest rate. This is just for your reference,

In this case it is 0.00070*2400=1.68%



For lease you pay the depreciation of the car and the interest

MSRP=27335
Negotiated price= 24050
Residual Value= 15580.95
Lease acquisition fee =$595

So Total price you negotiated= 24050+595=$24645


Lease acquisition fee is mandatory when you are leasing


So first calculate this if its for 36 month lease

Depreciation = (24645-15581)/36 = $251.77

Interest = (24645+15581)*0.00070 = $28.15

Add these two

$251.77 + $28.15 = $279.92 + tax

These are the monthly payments you need to pay for 36 months

Our tax rate is 8% so it is $22.39


So total including tax per month is $302.31 for 36 months including tax.

Dont panic and be clear and dont overpay.

In addition to this when you drive of you need to pay dealer, title and documentation fee. In California it is less than $400

Happy Leasing. Leave comments if you have any questions.





Wednesday, July 6, 2016

Basic procedures to give db level grants to users

Some times we need to grant a particular db user to kill sessions and for that we need to  grant alter system access which is not a best practice. So to over come we create a simple procedure to do the required actions and grant execute permissions to the user on that procedure.

Here are few examples..

Killing session procedure

CREATE OR REPLACE procedure kill_db_session
 ( v_sid number, v_serial number )  as
 v_varchar2 varchar2(100);
begin
execute immediate 'ALTER SYSTEM KILL SESSION '''   || v_sid || ',' || v_serial || ''' IMMEDIATE';
end;
/

Refresh materialized view under a different user

CREATE OR REPLACE PROCEDURE SSS.REFRESH_MV AS
BEGIN
    DBMS_MVIEW.REFRESH('xxx','C');
END REFRESH_MV;
/


Here xxx is tablename and C means complete refresh

Make sure you give grants on tables explicitly for mviews or else they will throw table not found error

Friday, June 17, 2016

Exadata IORM (Incomplete)

alter iormplan dbplan=((name='DDWH', level=1, allocation=10),(name=other, level=1, allocation=90))


alter iormplan active


LIST IORMPLAN detail

alter iormplan objective=auto;

alter iormplan objective=basic;


Example 6-12 Resetting Default Values in an Interdatabase Plan

CellCLI> ALTER IORMPLAN dbPlan="", catPlan=""
CellCLI> ALTER IORMPLAN dbPlan=""
CellCLI> ALTER IORMPLAN catPlan=""


[root@xxx01 ~]# cellcli
CellCLI: Release 12.1.2.3.1 - Production on Mon Jun 13 16:33:04 PDT 2016

Copyright (c) 2007, 2016, Oracle.  All rights reserved.

CellCLI> list iormplan detail;
         name:                   xxx01_IORMPLAN
         catPlan:
         dbPlan:
         objective:              basic
         status:                 active






CellCLI> list iormplan detail;
         name:                   xxx01_IORMPLAN
         catPlan:
         dbPlan:
         objective:              basic
         status:                 active


Manage IO distribution across databases

alter iormplan dbplan=((name='DDWH', level=1, allocation=1),(name=other, level=1, allocation=99))

alter iormplan objective=auto;

alter iormplan active




Reset back to default

alter iormplan objective=basic;


CellCLI> ALTER IORMPLAN dbPlan="", catPlan=""

alter iormplan active

**************************************************************************
To set a plan

change objective from basic to auto

Here allocation is ( Only during contention )
xxxdr will get 35% io
xxdr will get 22% io
xwh will get 22% io

Remaining all db's will be in level 2

alter iormplan objective=auto;


alter iormplan dbplan=((name='XXXDR', level=1, allocation=35),(name='XXDR', level=1, allocation=22),(name='XWH',level=1,allocation=22),(name=other,level=2,allocation=100))


Another scenario from oracle note

suppose we have 3 databases sharing the Exadata storage cells in a hosted environment. Databases 'sales' and 'finance' are more important than 'hr', so we'd like to give them more I/O bandwidth when there is I/O contention. We therefore set the shares in a 4-4-1 ratio. This means that when there is I/O contention, 'sales' and 'finance' will get equal I/O bandwidth. They will each also get 4x the bandwidth of 'hr'. If only one database is active, then it can consume all of the I/O bandwidth.

alter iormplan dbplan = -
  ((name=sales,   share=4), -
   (name=finance, share=4), -
   (name=hr,      share=1), -
   (name=default, share=1));


Thursday, June 2, 2016

root account gets locked on exadata

On Exadata after failed login accounts even root account gets locked. Play with values below to increase hits

$  grep -i pam_tally2 /etc/pam.d/sshd
auth       required     pam_tally2.so deny=5 onerr=fail lock_time=600 serialize                        

$ grep -i pam_tally2 /etc/pam.d/login
auth       required     pam_tally2.so deny=5 onerr=fail lock_time=600 serialize

Meaning of options:
1) deny=5 <<<<<< user account will be locked after 5 in-correct wrong passwords
2) lock_time=600   <<<< user account will be locked for 600 seconds i.e. for next 10mins

When Input Wrong Root Password Login Exadata System, We Have To Wait For 10mins ( Doc ID 1541862.1 )

use vi editor to change it to 10 seconds

vi /etc/pam.d/sshd

auth       required     pam_tally2.so deny=5 onerr=fail lock_time=10 serialize



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


To reset ssh for a user and allow logins

pam_tally2 -r -u oracle

Featured Post

Apply Patch 22191577 latest GI PSU to RAC and DB homes using Opatch auto or manual steps

Patch 22191577: GRID INFRASTRUCTURE PATCH SET UPDATE 11.2.0.4.160119 (JAN2016) Unzip the patch 22191577 Unzip latest Opatch Version in or...