Pages

Thursday, March 14, 2019

Unix Integer & Float calculations


Source: https://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks

Q:  How to do integer & float calculations, in bash or other languages/frameworks?
$ printf %.10f\\n "$((10**9 * 20/7))e-9"   # many shells. Not mksh.
$ echo "$((20.0/7))"                       # (ksh93/zsh/yash, not bash)
$ awk "BEGIN {print (20+5)/2}"
$ zcalc
$ bc <<< 20+5/2
$ bc <<< "scale=4; (20+5)/2"
$ dc <<< "4 k 20 5 + 2 / p"
$ expr 20 + 5
$ calc 2 + 4
$ node -pe 20+5/2  # Uses the power of JavaScript, e.g. : node -pe 20+5/Math.PI
$ echo 20 5 2 / + p | dc 
$ echo 4 k 20 5 2 / + p | dc 
$ perl -E "say 20+5/2"
$ python -c "print 20+5/2"
$ python -c "print 20+5/2.0"
$ clisp -x "(+ 2 2)"
$ lua -e "print(20+5/2)"
$ php -r 'echo 20+5/2;'
$ ruby -e 'p 20+5/2'
$ ruby -e 'p 20+5/2.0'
$ guile -c '(display (+ 20 (/ 5 2)))'
$ guile -c '(display (+ 20 (/ 5 2.0)))'
$ slsh -e 'printf("%f",20+5/2)'
$ slsh -e 'printf("%f",20+5/2.0)'
$ tclsh <<< 'puts [expr 20+5/2]'
$ tclsh <<< 'puts [expr 20+5/2.0]'
$ sqlite3 <<< 'select 20+5/2;'
$ sqlite3 <<< 'select 20+5/2.0;'
$ echo 'select 1 + 1;' | sqlite3 
$ psql -tAc 'select 1+1'
$ R -q -e 'print(sd(rnorm(1000)))'
$ r -e 'cat(pi^2, "\n")'
$ r -e 'print(sum(1:100))'
$ smjs
$ jspl

Monday, March 4, 2019

Oracle SQL Plan Baseline


Oracle SPM


select * from DBA_SQL_PLAN_BASELINEs --where ACCEPTED='YES'


Steps:

-- load baseline from sql cache
DECLARE
  l_plans_loaded  PLS_INTEGER;
BEGIN
  l_plans_loaded := DBMS_SPM.load_plans_from_cursor_cache(
  sql_id => 'dxt4r9mp8wvx9');
  DBMS_OUTPUT.PUT_LINE('dropped: '||l_plans_loaded);
END;
/

-- get plan from base line plan name
select * from TABLE(DBMS_XPLAN.display_sql_plan_baseline(plan_name=>'SQL_PLAN_5jbmma7s7tv2h17fb8113'));

---- drop a plan from baseline 
DECLARE
  l_plans_dropped  PLS_INTEGER;
BEGIN
  l_plans_dropped := DBMS_SPM.drop_sql_plan_baseline (
    sql_handle => NULL,
    plan_name  => 'SQL_PLAN_5jbmma7s7tv2h17fb8113');
    
  DBMS_OUTPUT.put_line('dropped: ' || l_plans_dropped);
END;
/

--- Export & Import (http://expertoracle.com/2016/05/17/move-sql-baseline-to-other-instance/)
-- 1. create staging table
DECLARE
  stgtbl number;
BEGIN
  stgtbl := DBMS_SPM.CREATE_STGTAB_BASELINE(table_name => 'stage2');
  DBMS_OUTPUT.PUT_LINE('stage:=' || stgtbl);
END;
/

-- 2. pack SQL plan baselines
DECLARE
  my_plans number;
BEGIN
  my_plans := DBMS_SPM.PACK_STGTAB_BASELINE(
    table_name => 'stage2',
    enabled => 'yes',
    ACCEPTED => 'yes',
    SQL_HANDLE => 'SQL_58ae7351f07cec50');
    DBMS_OUTPUT.PUT_LINE('packed: '|| my_plans);
END;
/

-- 3. export staging table
expdp sysadm tables=stage2 directory=DPDIR_FSSBX dumpfile=stage2.exp.dmp logfile=stage2.exp.log compression=all

-- 4. import stage into new DB
impdp sysadm file=stage2.dmp tables=stage2 directory=DPDIR_MYDB logfile=stage2.imp. log

-- unpack plan baselines
DECLARE
  l_plans_unpacked  PLS_INTEGER;
BEGIN
  l_plans_unpacked := DBMS_SPM.unpack_stgtab_baseline(
    table_name      => 'STAGE2',
    table_owner     => 'SYSADM',
    creator         => 'SYSADM');
DBMS_OUTPUT.put_line('Unpacked: ' || l_plans_unpacked);
END;
/


related Oracle parameters:


  1. SHOW PARAMETER OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES
  2. select name, value from  gV$SES_OPTIMIZER_ENV where sid=24 and upper(name)='OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES';
  3. select * from gv$parameter where upper(name) in select * from gv$parameter where upper(name) in ('OPTIMIZER_USE_SQL_PLAN_BASELINES', 'OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES')

Performance Qeries