├── 9781430232285.jpg ├── LICENSE.txt ├── Pro Oracle SQL ├── chapter01_scripts │ ├── list_depts.sql │ ├── lst-01-02.sql │ ├── lst-01-08.sql │ ├── lst-01-09.sql │ ├── lst-01-11.sql │ ├── lst-01-12.sql │ ├── lst-01-13.sql │ ├── lst-01-14.sql │ ├── lst-01-15.sql │ ├── lst-01-16.sql │ ├── lst-01-17.sql │ ├── lst-01-18.sql │ ├── lst-01-19.sql │ └── lst-01-20.sql ├── chapter02_scripts │ ├── lst-02-01.sql │ ├── lst-02-02.sql │ ├── lst-02-03.sql │ ├── lst-02-05.sql │ ├── lst-02-06.sql │ ├── lst-02-07.sql │ ├── lst-02-08.sql │ ├── lst-02-09.sql │ ├── lst-02-10.sql │ ├── lst-02-12.sql │ ├── st-all.sql │ ├── st-col-body.sql │ ├── st-col-virtual-body.sql │ ├── st-col-virtual.sql │ ├── st-col.sql │ ├── st-hist-body.sql │ ├── st-hist.sql │ ├── st-idx-body.sql │ ├── st-idx-part-body.sql │ ├── st-idx-part.sql │ ├── st-idx.sql │ ├── st-main.sql │ ├── st-part-body.sql │ ├── st-part.sql │ ├── st-tab-body.sql │ └── st-tab.sql ├── chapter03_scripts │ ├── lst-03-01.sql │ ├── lst-03-02.sql │ ├── lst-03-05.sql │ ├── lst-03-06.sql │ ├── lst-03-07.sql │ ├── lst-03-08.sql │ ├── lst-03-09.sql │ ├── lst-03-10.sql │ ├── lst-03-11.sql │ ├── lst-03-12.sql │ ├── lst-03-13.sql │ ├── lst-03-14.sql │ ├── lst-03-15.sql │ ├── lst-03-16.sql │ ├── lst-03-17.sql │ ├── lst-03-18.sql │ ├── lst-03-19.sql │ ├── lst-03-20.sql │ ├── lst-03-21.sql │ ├── lst-03-22.sql │ ├── lst-03-23.sql │ ├── lst-03-24.sql │ ├── lst-03-25.sql │ ├── lst-03-26.sql │ └── space_usage.sql ├── chapter04_scripts │ ├── lst-04-01.sql │ ├── lst-04-02.sql │ ├── lst-04-03.sql │ ├── lst-04-04.sql │ ├── lst-04-05.sql │ ├── lst-04-06.sql │ ├── lst-04-07.sql │ ├── lst-04-08.sql │ ├── lst-04-09.sql │ ├── lst-04-10.sql │ └── lst-04-11.sql ├── chapter05_scripts │ ├── lst-05-01.sql │ ├── lst-05-02.sql │ ├── lst-05-04.sql │ ├── lst-05-05.sql │ ├── lst-05-06.sql │ └── pln.sql ├── chapter06_scripts │ ├── lst-06-01.sql │ ├── lst-06-03.sql │ ├── lst-06-04.sql │ ├── lst-06-06.sql │ ├── lst-06-07.sql │ ├── lst-06-08.sql │ ├── lst-06-09.sql │ ├── lst-06-11.sql │ ├── lst-06-12.sql │ ├── lst-06-13.sql │ ├── lst-06-14.sql │ └── lst-06-15.sql ├── chapter07_scripts │ ├── l_7_1.sql │ ├── l_7_10.sql │ ├── l_7_11.sql │ ├── l_7_12.sql │ ├── l_7_13.sql │ ├── l_7_14.sql │ ├── l_7_15.sql │ ├── l_7_16.sql │ ├── l_7_17.sql │ ├── l_7_18.sql │ ├── l_7_19.sql │ ├── l_7_2.sql │ ├── l_7_20.sql │ ├── l_7_21.sql │ ├── l_7_3.sql │ ├── l_7_4.sql │ ├── l_7_5.sql │ ├── l_7_6.sql │ ├── l_7_7.sql │ ├── l_7_8.sql │ ├── l_7_9.sql │ └── showplan_last.sql ├── chapter08_scripts │ ├── Listing_8_1.sql │ ├── Listing_8_10.sql │ ├── Listing_8_11.sql │ ├── Listing_8_12.sql │ ├── Listing_8_13.sql │ ├── Listing_8_14.sql │ ├── Listing_8_15.sql │ ├── Listing_8_16.sql │ ├── Listing_8_17.sql │ ├── Listing_8_18.sql │ ├── Listing_8_19.sql │ ├── Listing_8_2.sql │ ├── Listing_8_20.sql │ ├── Listing_8_21.sql │ ├── Listing_8_22.sql │ ├── Listing_8_24.sql │ ├── Listing_8_25.sql │ ├── Listing_8_3.sql │ ├── Listing_8_4.sql │ ├── Listing_8_5.sql │ ├── Listing_8_6.sql │ ├── Listing_8_7.sql │ ├── Listing_8_8.sql │ ├── Listing_8_9.sql │ ├── README_chapter8.txt │ └── x.sql ├── chapter09_scripts │ ├── Listing_9_10.sql │ ├── Listing_9_11.sql │ ├── Listing_9_12.sql │ ├── Listing_9_13.sql │ ├── Listing_9_14.sql │ ├── Listing_9_15.sql │ ├── Listing_9_16.sql │ ├── Listing_9_17.sql │ ├── Listing_9_18.sql │ ├── Listing_9_19.sql │ ├── Listing_9_20.sql │ ├── Listing_9_21.sql │ ├── Listing_9_22.sql │ ├── Listing_9_23.sql │ ├── Listing_9_24.sql │ ├── Listing_9_25.sql │ ├── Listing_9_26.sql │ ├── Listing_9_27.sql │ ├── Listing_9_28.sql │ ├── Listing_9_29.sql │ ├── Listing_9_2_cr_table.sql │ ├── Listing_9_3.sql │ ├── Listing_9_30.sql │ ├── Listing_9_31.sql │ ├── Listing_9_31_partition.sql │ ├── Listing_9_32.sql │ ├── Listing_9_33.sql │ ├── Listing_9_34.sql │ ├── Listing_9_6.sql │ ├── Listing_9_7.sql │ ├── Listing_9_8.sql │ ├── Listing_9_9.sql │ ├── README_Chapter09.txt │ └── x.sql ├── chapter10_scripts │ ├── exercises │ │ ├── l_10_exercise_1.sql │ │ ├── l_10_exercise_2.sql │ │ └── showplan_last.sql │ ├── l_10_1.sql │ ├── l_10_10.sql │ ├── l_10_11.sql │ ├── l_10_12.sql │ ├── l_10_13.sql │ ├── l_10_14.sql │ ├── l_10_15.sql │ ├── l_10_16.sql │ ├── l_10_17.sql │ ├── l_10_18.sql │ ├── l_10_19.sql │ ├── l_10_2.sql │ ├── l_10_20.sql │ ├── l_10_21.sql │ ├── l_10_22.sql │ ├── l_10_23.sql │ ├── l_10_24.sql │ ├── l_10_25.sql │ ├── l_10_26.sql │ ├── l_10_27.sql │ ├── l_10_28.sql │ ├── l_10_3.sql │ ├── l_10_4.sql │ ├── l_10_5.sql │ ├── l_10_6.sql │ ├── l_10_7.sql │ ├── l_10_8.sql │ ├── l_10_9.sql │ ├── sf_run_stats │ │ ├── README │ │ ├── child_wait.sh │ │ ├── create.sql │ │ ├── defaults.sql │ │ ├── drop.sql │ │ ├── get_epoch_microseconds.sql │ │ ├── insert_results.sql │ │ ├── mse.sql │ │ ├── mse2.sql │ │ ├── mview.sql │ │ ├── my_sess_events.sql │ │ ├── response_time.txt │ │ ├── response_time_2.txt │ │ ├── run_stats.sql │ │ ├── se.sql │ │ ├── sqlnet.log │ │ ├── sr.sql │ │ ├── test_harness_m.sql │ │ ├── th.conf │ │ ├── thc1.sh │ │ ├── thc1.sql │ │ ├── thc2.sh │ │ └── thc2.sql │ └── sf_run_stats_load │ │ ├── README │ │ ├── afiedt.buf │ │ ├── child_wait.sh │ │ ├── create.sql │ │ ├── d1.sql │ │ ├── defaults.sql │ │ ├── drop.sql │ │ ├── insert_results.sql │ │ ├── j │ │ ├── mview.sql │ │ ├── query.txt │ │ ├── run_stats.sql │ │ ├── sqlnet.log │ │ ├── sr.sql │ │ ├── test_harness_m.sql │ │ ├── th.conf │ │ ├── thc1.sh │ │ ├── thc1.sql │ │ ├── thc2.sh │ │ ├── thc2.sql │ │ ├── thm.sql │ │ ├── w1.txt │ │ ├── w2.txt │ │ ├── x.sql │ │ ├── x1.sql │ │ ├── x2.sql │ │ ├── x3.sql │ │ ├── x4.sql │ │ └── x5.sql ├── chapter11_scripts │ ├── anti_ex.sql │ ├── anti_ex1.sql │ ├── anti_ex1a.sql │ ├── anti_ex1b.sql │ ├── anti_ex2.sql │ ├── anti_ex2a.sql │ ├── anti_ex3.sql │ ├── anti_ex4.sql │ ├── anti_ex5.sql │ ├── anti_ex7.sql │ ├── anti_hints2.sql │ ├── anti_odd1.sql │ ├── fsaj.sql │ ├── fsp.sql │ ├── fssj.sql │ ├── parms.sql │ ├── semi1.sql │ ├── semi2.sql │ ├── semi3.sql │ ├── semi_ex.sql │ ├── semi_ex1.sql │ ├── semi_ex1a.sql │ ├── semi_ex1b.sql │ ├── semi_ex2.sql │ ├── semi_ex3.sql │ ├── semi_ex4.sql │ ├── semi_ex5.sql │ ├── semi_ex6.sql │ ├── semi_ex7.sql │ ├── semi_ex9.sql │ ├── semi_hash.sql │ ├── semi_hint.sql │ ├── semi_inter.sql │ ├── semi_tom1.sql │ ├── semi_tom2.sql │ └── valid_parms.sql ├── chapter12_scripts │ ├── Listing_12_1.sql │ ├── Listing_12_10.sql │ ├── Listing_12_11.sql │ ├── Listing_12_12.sql │ ├── Listing_12_14.sql │ ├── Listing_12_18.sql │ ├── Listing_12_19.sql │ ├── Listing_12_2.sql │ ├── Listing_12_20.sql │ ├── Listing_12_22.sql │ ├── Listing_12_23.sql │ ├── Listing_12_24.sql │ ├── Listing_12_3.sql │ ├── Listing_12_4.sql │ ├── Listing_12_5.sql │ ├── Listing_12_6.sql │ ├── Listing_12_7.sql │ ├── Listing_12_8.sql │ ├── Listing_12_9.sql │ ├── Listing_12_cr_table.sql │ ├── analyze_sf.sql │ ├── analyze_sfp.sql │ ├── analyze_table.sql │ └── x.sql ├── chapter13_scripts │ ├── compare_insert_merge_ctas.sql │ ├── constraints.sql │ ├── create_people.sql │ ├── create_skew.sql │ ├── find_sql_stats.sql │ ├── flush_pool.sql │ ├── fss2.sql │ ├── mystats.sql │ └── recreate_table.sql ├── chapter14_scripts │ ├── 14.10_TransactionIsolation.sql │ ├── 14.11_AutonomousTransaction.sql │ ├── 14.12_OrderTransaction.sql │ ├── 14.2_OE_schema_changes.sql │ ├── 14.3_VerifyAvailableInventory.sql │ ├── 14.4_OrderTransaction_hardcoded.sql │ ├── 14.5_ConfirmTransactionUpdates.sql │ ├── 14.6_OrderWithTransactionIDandSCN.sql │ ├── 14.7_ReturnToASavepoint.sql │ ├── 14.8_VerifyDataAfterRollback.sql │ ├── 14.9_SerializableTransactionFail.sql │ ├── credit_request.prc │ ├── formats.sql │ ├── get_listprice.fnc │ ├── get_orderitemcount.fnc │ ├── get_ordertotal.fnc │ └── order_detail_views.sql ├── chapter15_scripts │ ├── 15.10_ILOExecInAProcedure.sql │ ├── 15.11_ExecutionScript.sql │ ├── 15.11_IncorportateILOinProcedure.sql │ ├── 15.1_CheckInvalidObjects.sql │ ├── 15.4_SchemaChangesAndProductData.sql │ ├── 15.5_InvalidObjectsAndRecompile.sql │ ├── 15.7_OrderReportExecPlanBefore.sql │ ├── 15.8_OrderReportExecPlanAfter.sql │ ├── 15.9_OrderReportExecPlanCorrected.sql │ ├── order_detail_views.sql │ ├── order_detail_views2.sql │ ├── order_detail_w_supplier_id.sql │ ├── order_report.sql │ ├── order_reports_all.sql │ ├── pln.sql │ └── purchasing_report.sql └── chapter16_scripts │ ├── as.sql │ ├── awr_plan_change.sql │ ├── awr_plan_stats.sql │ ├── baseline_hints.sql │ ├── baselines.sql │ ├── col_stats.sql │ ├── create_1_hint_sql_profile.sql │ ├── create_baseline.sql │ ├── create_baseline_awr.sql │ ├── create_outline.sql │ ├── create_skew.sql │ ├── create_sql_profile.sql │ ├── create_sql_profile_awr.sql │ ├── dba_tables.sql │ ├── display_raw.sql │ ├── dplan.sql │ ├── dplan_alias.sql │ ├── dplan_awr.sql │ ├── find_sql.sql │ ├── find_sql_awr.sql │ ├── find_sql_using_baseline.sql │ ├── find_sql_using_outline.sql │ ├── find_sql_using_profile.sql │ ├── move_sql_profile.sql │ ├── optim_parms.sql │ ├── outline_hints.sql │ ├── outlines.sql │ ├── restore_table_stats.sql │ ├── set_col_stats.sql │ ├── set_col_stats_max.sql │ ├── snapper.sql │ ├── sql_hints.sql │ ├── sql_profile_hints.sql │ ├── sql_profiles.sql │ ├── tab_stats_history.sql │ ├── unstable_plans.sql │ ├── valid_hints.sql │ └── whats_changed.sql ├── README.md └── contributing.md /9781430232285.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/pro-oracle-sql/bbcb26062e09482a655acdb2dd02422152265b20/9781430232285.jpg -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/pro-oracle-sql/bbcb26062e09482a655acdb2dd02422152265b20/LICENSE.txt -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/list_depts.sql: -------------------------------------------------------------------------------- 1 | select * from scott.dept; 2 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-02.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-2 */ 2 | 3 | select salary 4 | from hr.employees 5 | where employee_id = 108; 6 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-08.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-8 */ 2 | 3 | select empno, deptno from scott.emp where ename = 'SMITH' ; 4 | 5 | select empno, deptno from scott.emp where ename = 'SMITH' 6 | ; 7 | 8 | select empno, deptno from scott.emp where ename = 'SMITH' 9 | / 10 | 11 | select empno, deptno from scott.emp where ename = 'SMITH' 12 | 13 | / 14 | 15 | select empno, deptno from scott.emp where ename = 'SMITH'/ 16 | 17 | l 18 | 19 | / 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-09.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-9 */ 2 | 3 | @list_depts 4 | 5 | start list_depts 6 | 7 | l 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-11.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-11 */ 2 | 3 | select c.customer_id, count(o.order_id) as orders_ct 4 | from oe.customers c 5 | join oe.orders o 6 | on c.customer_id = o.customer_id 7 | where c.gender = 'F' 8 | group by c.customer_id 9 | having count(o.order_id) > 4 10 | order by orders_ct, c.customer_id 11 | / 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-12.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-12 */ 2 | 3 | select c.customer_id cust_id, o.order_id ord_id, c.gender 4 | from oe.customers c 5 | join oe.orders o 6 | on c.customer_id = o.customer_id; 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-13.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-13 */ 2 | 3 | select c.customer_id, count(o.order_id) as orders_ct 4 | from oe.customers c 5 | join oe.orders o 6 | on c.customer_id = o.customer_id 7 | where c.gender = 'F' 8 | group by c.customer_id; 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-14.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-14 */ 2 | 3 | select c.customer_id, c.cust_first_name||' '||c.cust_last_name, 4 | (select e.last_name 5 | from hr.employees e 6 | where e.employee_id = c.account_mgr_id) acct_mgr 7 | from oe.customers c; 8 | 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-15.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-15 */ 2 | 3 | select c.customer_id, count(o.order_id) as orders_ct 4 | from oe.customers c 5 | join oe.orders o 6 | on c.customer_id = o.customer_id 7 | where c.gender = 'F' 8 | group by c.customer_id 9 | having count(o.order_id) > 4 10 | order by orders_ct, c.customer_id 11 | / 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-16.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-16 */ 2 | 3 | insert into hr.jobs (job_id, job_title, min_salary, max_salary) 4 | values ('IT_PM', 'Project Manager', 5000, 11000) ; 5 | 6 | 7 | insert into scott.bonus (ename, job, sal) 8 | select ename, job, sal * .10 9 | from scott.emp; 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-17.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-17 */ 2 | 3 | create table small_customers 4 | (customer_id number, 5 | sum_orders number) 6 | ; 7 | 8 | 9 | create table medium_customers 10 | (customer_id number, 11 | sum_orders number) 12 | ; 13 | 14 | 15 | create table large_customers 16 | (customer_id number, 17 | sum_orders number) 18 | ; 19 | 20 | 21 | select * from small_customers ; 22 | 23 | select * from medium_customers ; 24 | 25 | select * from large_customers ; 26 | 27 | insert all 28 | when sum_orders < 10000 then 29 | into small_customers 30 | when sum_orders >= 10000 and sum_orders < 100000 then 31 | into medium_customers 32 | else 33 | into large_customers 34 | select customer_id, sum(order_total) sum_orders 35 | from oe.orders 36 | group by customer_id ; 37 | 38 | 39 | select * from small_customers ; 40 | 41 | select * from medium_customers ; 42 | 43 | select * from large_customers ; 44 | 45 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-19.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-19 */ 2 | 3 | select employee_id, department_id, last_name, salary 4 | from employees2 5 | where department_id = 90; 6 | 7 | delete from employees2 8 | where department_id = 90; 9 | 10 | select employee_id, department_id, last_name, salary 11 | from employees2 12 | where department_id = 90; 13 | 14 | rollback; 15 | 16 | select employee_id, department_id, last_name, salary 17 | from employees2 18 | where department_id = 90; 19 | 20 | delete from (select * from employees2 where department_id = 90); 21 | 22 | select employee_id, department_id, last_name, salary 23 | from employees2 24 | where department_id = 90; 25 | 26 | rollback; 27 | 28 | select employee_id, department_id, last_name, salary 29 | from employees2 30 | where department_id = 90; 31 | 32 | delete from employees2 33 | where department_id in (select department_id 34 | from hr.departments 35 | where department_name = 'Executive'); 36 | 37 | select employee_id, department_id, last_name, salary 38 | from employees2 39 | where department_id = 90; 40 | 41 | rollback; 42 | 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter01_scripts/lst-01-20.sql: -------------------------------------------------------------------------------- 1 | /* Listing 1-20 */ 2 | 3 | create table dept60_bonuses 4 | (employee_id number 5 | ,bonus_amt number); 6 | 7 | insert into dept60_bonuses values (103, 0); 8 | 9 | insert into dept60_bonuses values (104, 100); 10 | 11 | insert into dept60_bonuses values (105, 0); 12 | 13 | commit; 14 | 15 | select employee_id, last_name, salary 16 | from hr.employees 17 | where department_id = 60 ; 18 | 19 | select * from dept60_bonuses; 20 | 21 | merge into dept60_bonuses b 22 | using ( 23 | select employee_id, salary, department_id 24 | from hr.employees 25 | where department_id = 60) e 26 | on (b.employee_id = e.employee_id) 27 | when matched then 28 | update set b.bonus_amt = e.salary * 0.2 29 | where b.bonus_amt = 0 30 | delete where (e.salary > 7500) 31 | when not matched then 32 | insert (b.employee_id, b.bonus_amt) 33 | values (e.employee_id, e.salary * 0.1) 34 | where (e.salary < 7500); 35 | 36 | select * from dept60_bonuses; 37 | 38 | rollback ; 39 | 40 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-01.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-1 2 | 3 | connect to hr demo schema 4 | 5 | */ 6 | 7 | 8 | select * from employees where department_id = 60; 9 | 10 | SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID = 60; 11 | 12 | select /* a_comment */ * from employees where department_id = 60; 13 | 14 | select sql_text, sql_id, child_number, hash_value, address, executions 15 | from v$sql where upper(sql_text) like '%EMPLOYEES%'; 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-02.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-2 */ 2 | 3 | variable v_dept number 4 | 5 | exec :v_dept := 10 6 | 7 | select * from hr.employees where department_id = :v_dept; 8 | 9 | exec :v_dept := 20 10 | 11 | select * from hr.employees where department_id = :v_dept; 12 | 13 | exec :v_dept := 30 14 | 15 | select * from hr.employees where department_id = :v_dept; 16 | 17 | select sql_text, sql_id, child_number, hash_value, address, executions 18 | from v$sql where sql_text like '%v_dept'; 19 | 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-03.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-3 */ 2 | 3 | 4 | alter system set events 'immediate trace name flush_cache'; 5 | 6 | alter system flush shared_pool; 7 | 8 | set autotrace traceonly statistics 9 | 10 | select * from hr.employees where department_id = 60; 11 | 12 | set autotrace off 13 | 14 | alter system set events 'immediate trace name flush_cache'; 15 | 16 | set autotrace traceonly statistics 17 | 18 | select * from hr.employees where department_id = 60; 19 | 20 | select * from hr.employees where department_id = 60; 21 | 22 | set autotrace off 23 | 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-05.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-5 */ 2 | 3 | 4 | set autotrace on 5 | 6 | SELECT e1.last_name, e1.salary, v.avg_salary 7 | FROM hr.employees e1, 8 | (SELECT department_id, avg(salary) avg_salary 9 | FROM hr.employees e2 10 | GROUP BY department_id) v 11 | WHERE e1.department_id = v.department_id AND e1.salary > v.avg_salary; 12 | 13 | SELECT /*+ MERGE(v) */ e1.last_name, e1.salary, v.avg_salary 14 | FROM hr.employees e1, 15 | (SELECT department_id, avg(salary) avg_salary 16 | FROM hr.employees e2 17 | GROUP BY department_id) v 18 | WHERE e1.department_id = v.department_id AND e1.salary > v.avg_salary; 19 | 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-06.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-6 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select * from hr.employees where department_id in (select department_id from hr.departments); 6 | 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-07.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-7 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select employee_id, last_name, salary, department_id 6 | from hr.employees 7 | where department_id in 8 | (select /*+ NO_UNNEST */department_id 9 | from hr.departments where location_id > 1700); 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-08.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-8 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select outer.employee_id, outer.last_name, outer.salary, outer.department_id 6 | from hr.employees outer 7 | where outer.salary > 8 | (select avg(inner.salary) 9 | from hr.employees inner 10 | where inner.department_id = outer.department_id) 11 | ; 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-09.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-9 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | SELECT e1.last_name, e1.salary, v.avg_salary 6 | FROM hr.employees e1, 7 | (SELECT department_id, avg(salary) avg_salary 8 | FROM hr.employees e2 9 | GROUP BY department_id) v 10 | WHERE e1.department_id = v.department_id 11 | AND e1.salary > v.avg_salary 12 | AND e1.department_id = 60; 13 | 14 | SELECT e1.last_name, e1.salary, v.avg_salary 15 | FROM hr.employees e1, 16 | (SELECT department_id, avg(salary) avg_salary 17 | FROM hr.employees e2 18 | WHERE rownum > 1 -- rownum prohibits predicate pushing! 19 | GROUP BY department_id) v 20 | WHERE e1.department_id = v.department_id 21 | AND e1.salary > v.avg_salary 22 | AND e1.department_id = 60; 23 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-10.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-10 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | SELECT p.prod_id, p.prod_name, t.time_id, t.week_ending_day, 6 | s.channel_id, s.promo_id, s.cust_id, s.amount_sold 7 | FROM sales s, products p, times t 8 | WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id; 9 | 10 | set autotrace off 11 | 12 | CREATE MATERIALIZED VIEW sales_time_product_mv 13 | ENABLE QUERY REWRITE AS 14 | SELECT p.prod_id, p.prod_name, t.time_id, t.week_ending_day, 15 | s.channel_id, s.promo_id, s.cust_id, s.amount_sold 16 | FROM sales s, products p, times t 17 | WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id; 18 | 19 | set autotrace traceonly explain 20 | 21 | SELECT p.prod_id, p.prod_name, t.time_id, t.week_ending_day, 22 | s.channel_id, s.promo_id, s.cust_id, s.amount_sold 23 | FROM sales s, products p, times t 24 | WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id; 25 | 26 | SELECT /*+ rewrite(sales_time_product_mv) */ 27 | p.prod_id, p.prod_name, t.time_id, t.week_ending_day, 28 | s.channel_id, s.promo_id, s.cust_id, s.amount_sold 29 | FROM sales s, products p, times t 30 | WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id; 31 | 32 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/lst-02-12.sql: -------------------------------------------------------------------------------- 1 | /* Listing 2-12 */ 2 | 3 | set arraysize 15 4 | 5 | set autotrace traceonly statistics 6 | 7 | select * from order_items ; 8 | 9 | set arraysize 45 10 | 11 | / 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-all.sql: -------------------------------------------------------------------------------- 1 | rem 2 | rem Displays all statistics information for owner/table 3 | rem 4 | 5 | @st-main 6 | @st-tab-body 7 | @st-part-body 8 | @st-col-body 9 | --@st-col-virtual-body 10 | @st-hist-body 11 | @st-idx-body 12 | @st-idx-part-body 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-col-virtual.sql: -------------------------------------------------------------------------------- 1 | @st-main 2 | @st-col-virtual-body 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-col.sql: -------------------------------------------------------------------------------- 1 | @st-main 2 | @st-col-body 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-hist.sql: -------------------------------------------------------------------------------- 1 | @st-main 2 | @st-hist-body 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-idx-part.sql: -------------------------------------------------------------------------------- 1 | @st-main 2 | @st-idx-part-body 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-idx.sql: -------------------------------------------------------------------------------- 1 | @st-main 2 | @st-idx-body 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-main.sql: -------------------------------------------------------------------------------- 1 | rem 2 | rem Main caller for retrieving statistics information by owner/table 3 | rem 4 | 5 | set echo off feed off 6 | 7 | set serveroutput on for wra 8 | 9 | exec dbms_output.enable(1000000); 10 | 11 | alter session set nls_date_format = 'mm/dd/yyyy hh24:mi:ss'; 12 | 13 | set termout on 14 | accept p_own prompt 'Enter the owner name: ' 15 | accept p_tab prompt 'Enter the table name: ' 16 | 17 | define p_owner = '&p_own' 18 | define p_table = '&p_tab' 19 | 20 | set termout on lines 500 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-part.sql: -------------------------------------------------------------------------------- 1 | @st-main 2 | @st-part-body 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter02_scripts/st-tab.sql: -------------------------------------------------------------------------------- 1 | 2 | @st-main 3 | @st-tab-body 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-01.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-1 */ 2 | 3 | create table t1 as 4 | select trunc((rownum-1)/100) id, 5 | rpad(rownum,100) t_pad 6 | from dba_source 7 | where rownum <= 10000; 8 | 9 | create index t1_idx1 on t1(id); 10 | 11 | exec dbms_stats.gather_table_stats(user,'t1',method_opt=>'FOR ALL COLUMNS SIZE 1',cascade=>TRUE); 12 | 13 | create table t2 as 14 | select mod(rownum,100) id, 15 | rpad(rownum,100) t_pad 16 | from dba_source 17 | where rownum <= 10000; 18 | 19 | create index t2_idx1 on t2(id); 20 | 21 | exec dbms_stats.gather_table_stats(user,'t2',method_opt=>'FOR ALL COLUMNS SIZE 1',cascade=>TRUE); 22 | 23 | 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-02.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-2 */ 2 | 3 | select count(*) ct from t1 where id = 1 ; 4 | 5 | select count(*) ct from t2 where id = 1 ; 6 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-05.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-5 */ 2 | 3 | select table_name, num_rows, blocks from user_tables where table_name = 'T2' ; 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-06.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-6 */ 2 | 3 | select blocks from user_segments where segment_name = 'T2'; 4 | 5 | select count(distinct (dbms_rowid.rowid_block_number(rowid))) block_ct from t2 ; 6 | 7 | select min(dbms_rowid.rowid_block_number(rowid)) min_blk, 8 | max(dbms_rowid.rowid_block_number(rowid)) max_blk from t2 ; 9 | 10 | get space_usage.sql 11 | 12 | @space_usage T2 13 | 14 | delete from t2 ; 15 | 16 | commit ; 17 | 18 | @space_usage T2 19 | 20 | select blocks from user_segments where segment_name = 'T2'; 21 | 22 | select count(distinct (dbms_rowid.rowid_block_number(rowid))) block_ct from t2 ; 23 | 24 | set autotrace traceonly 25 | 26 | select * from t2 ; 27 | 28 | set autotrace off 29 | 30 | truncate table t2 ; 31 | 32 | @space_usage T2 33 | 34 | select blocks from user_segments where segment_name = 'T2'; 35 | 36 | set autotrace traceonly 37 | 38 | select * from t2 ; 39 | 40 | set autotrace off 41 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-07.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-7 */ 2 | 3 | column filen format a50 head 'File Name' 4 | 5 | select e.rowid , 6 | (select file_name 7 | from dba_data_files 8 | where file_id = dbms_rowid.rowid_to_absolute_fno(e.rowid, user, 'EMP')) filen, 9 | dbms_rowid.rowid_block_number(e.rowid) block_no, 10 | dbms_rowid.rowid_row_number(e.rowid) row_no 11 | from emp e 12 | where e.ename = 'KING' ; 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-08.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-8 */ 2 | 3 | select t.table_name||'.'||i.index_name idx_name, 4 | i.clustering_factor, t.blocks, t.num_rows 5 | from user_indexes i, user_tables t 6 | where i.table_name = t.table_name 7 | and t.table_name in ('T1','T2') 8 | order by t.table_name, i.index_name; 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-09.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-9 */ 2 | 3 | select t.table_name||'.'||i.index_name idx_name, 4 | i.clustering_factor, t.blocks, t.num_rows 5 | from all_indexes i, all_tables t 6 | where i.table_name = t.table_name 7 | and t.table_name = 'EMPLOYEES' 8 | and t.owner = 'HR' 9 | and i.index_name = 'EMP_DEPARTMENT_IX' 10 | order by t.table_name, i.index_name; 11 | 12 | select department_id, last_name, blk_no, 13 | lag (blk_no,1,blk_no) over (order by department_id) prev_blk_no, 14 | case when blk_no != lag (blk_no,1,blk_no) over (order by department_id) 15 | or rownum = 1 16 | then '*** +1' 17 | else null 18 | end cluf_ct 19 | from ( 20 | select department_id, last_name, 21 | dbms_rowid.rowid_block_number(rowid) blk_no 22 | from hr.employees 23 | where department_id is not null 24 | order by department_id 25 | ); 26 | 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-10.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-10 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select * from hr.employees where employee_id = 100; 6 | 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-11.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-11 */ 2 | 3 | select * from hr.employees where department_id = 60 ; 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-12.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-12 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select * from hr.employees 6 | where department_id in (90, 100) 7 | order by department_id desc; 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-13.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-13 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select email from hr.employees ; 6 | 7 | select first_name, last_name from hr.employees 8 | where first_name like 'A%' ; 9 | 10 | select * from hr.employees order by employee_id ; 11 | 12 | select * from hr.employees order by employee_id desc ; 13 | 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-14.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-14 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select min(department_id) from hr.employees ; 6 | 7 | select min(department_id), max(department_id) from hr.employees ; 8 | 9 | select (select min(department_id) from hr.employees) min_id, 10 | (select max(department_id) from hr.employees) max_id 11 | from dual; 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-15.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-15 */ 2 | 3 | 4 | create index emp_jobfname_ix on employees(job_id, first_name, salary); 5 | 6 | set autotrace traceonly 7 | 8 | select * from employees where first_name = 'William'; 9 | 10 | select /*+ full(employees) */ * from employees where first_name = 'William'; 11 | 12 | select count(distinct job_id) ct from employees ; 13 | 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-16.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-16 */ 2 | 3 | alter table hr.employees modify (email null) ; 4 | 5 | set autotrace traceonly explain 6 | 7 | select email from hr.employees ; 8 | 9 | set autotrace off 10 | 11 | alter table hr.employees modify (email not null) ; 12 | 13 | set autotrace traceonly explain 14 | 15 | select email from hr.employees ; 16 | 17 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-17.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-17 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select empno, ename, dname, loc 6 | from emp, dept 7 | where emp.deptno = dept.deptno; 8 | 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-18.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-18 */ 2 | 3 | set autotrace traceonly statistics 4 | 5 | select empno, ename, dname, loc 6 | from scott.emp, scott.dept 7 | where emp.deptno = dept.deptno; 8 | 9 | select /*+ ordered use_nl (dept emp) */ empno, ename, dname, loc 10 | from scott.dept, scott.emp 11 | where emp.deptno = dept.deptno; 12 | 13 | 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-19.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-19 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select /*+ ordered */ empno, ename, dname, loc 6 | from scott.dept, scott.emp 7 | where emp.deptno = dept.deptno; 8 | 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-20.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-20 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select /*+ use_hash (dept, emp) */ empno, ename, dname, loc 6 | from scott.dept, scott.emp 7 | where emp.deptno = dept.deptno; 8 | 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-21.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-21 */ 2 | 3 | select distinct deptno, 4 | ora_hash(deptno,1000) hv 5 | from scott.emp 6 | order by deptno; 7 | 8 | select deptno 9 | from 10 | ( 11 | select distinct deptno, 12 | ora_hash(deptno,1000) hv 13 | from scott.emp 14 | order by deptno 15 | ) 16 | where hv between 100 and 500; 17 | 18 | select distinct deptno, 19 | ora_hash(deptno,1000,50) hv 20 | from scott.emp 21 | order by deptno; 22 | 23 | 24 | select deptno 25 | from 26 | ( 27 | select distinct deptno, 28 | ora_hash(deptno,1000,50) hv 29 | from scott.emp 30 | order by deptno 31 | ) 32 | where hv between 100 and 500; 33 | 34 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-22.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-22 */ 2 | 3 | set autotrace traceonly explain 4 | 5 | select empno, ename, dname, loc 6 | from scott.dept, scott.emp; 7 | 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-23.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-23 */ 2 | 3 | select c.cust_last_name, nvl(sum(o.order_total),0) tot_orders 4 | from customers c, orders o 5 | where c.customer_id = o.customer_id 6 | group by c.cust_last_name 7 | having nvl(sum(o.order_total),0) between 0 and 5000 8 | order by c.cust_last_name ; 9 | 10 | select count(*) ct 11 | from 12 | ( 13 | select c.cust_last_name, nvl(sum(o.order_total),0) tot_orders 14 | from customers c, orders o 15 | where c.customer_id = o.customer_id 16 | group by c.cust_last_name 17 | having nvl(sum(o.order_total),0) between 0 and 5000 18 | order by c.cust_last_name 19 | ); 20 | 21 | select count(*) ct 22 | from 23 | ( 24 | select c.cust_last_name, nvl(sum(o.order_total),0) tot_orders 25 | from customers c, orders o 26 | where c.customer_id = o.customer_id(+) 27 | group by c.cust_last_name 28 | having nvl(sum(o.order_total),0) between 0 and 5000 29 | order by c.cust_last_name 30 | ); 31 | 32 | set autotrace traceonly explain 33 | 34 | / 35 | 36 | 37 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-24.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-24 */ 2 | 3 | 4 | select count(*) ct 5 | from 6 | ( 7 | select c.cust_last_name, nvl(sum(o.order_total),0) tot_orders 8 | from customers c 9 | left outer join 10 | orders o 11 | on (c.customer_id = o.customer_id) 12 | group by c.cust_last_name 13 | having nvl(sum(o.order_total),0) between 0 and 5000 14 | order by c.cust_last_name 15 | ); 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-25.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-25 */ 2 | 3 | create table e1 as select * from emp where deptno in (10,20); 4 | 5 | create table e2 as select * from emp where deptno in (20,30); 6 | 7 | 8 | select e1.ename, e1.deptno, e1.job, e2.ename, e2.deptno, e2.job 9 | from e1 10 | full outer join 11 | e2 12 | on (e1.empno = e2.empno); 13 | 14 | set autotrace traceonly explain 15 | 16 | / 17 | 18 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter03_scripts/lst-03-26.sql: -------------------------------------------------------------------------------- 1 | /* Listing 3-26 */ 2 | 3 | select e1.ename, e1.deptno, e1.job, 4 | e2.ename, e2.deptno, e2.job 5 | from e1, 6 | e2 7 | where e1.empno (+) = e2.empno 8 | union 9 | select e1.ename, e1.deptno, e1.job, 10 | e2.ename, e2.deptno, e2.job 11 | from e1, 12 | e2 13 | where e1.empno = e2.empno (+); 14 | 15 | set autotrace traceonly explain 16 | 17 | / 18 | 19 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-01.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-1 */ 2 | 3 | set autotrace on 4 | 5 | select distinct employee_id 6 | from job_history j1 7 | where not exists 8 | (select null 9 | from job_history j2 10 | where j2.employee_id = j1.employee_id 11 | and round(months_between(j2.start_date,j2.end_date)/12,0) <> 12 | round(months_between(j1.start_date,j1.end_date)/12,0) ); 13 | 14 | select employee_id 15 | from job_history 16 | group by employee_id 17 | having min(round(months_between(start_date,end_date)/12,0)) = 18 | max(round(months_between(start_date,end_date)/12,0)); 19 | 20 | 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-02.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-2 */ 2 | 3 | select customer_id, order_date 4 | from orders 5 | where customer_id = 102 ; 6 | 7 | select customer_id, order_date, 8 | lag(order_date,1,order_date) 9 | over (partition by customer_id order by order_date) 10 | as prev_order_date 11 | from orders 12 | where customer_id = 102; 13 | 14 | select trunc(order_date) - trunc(prev_order_date) days_between 15 | from 16 | ( 17 | select customer_id, order_date, 18 | lag(order_date,1,order_date) 19 | over (partition by customer_id order by order_date) 20 | as prev_order_date 21 | from orders 22 | where customer_id = 102 23 | ); 24 | 25 | select avg(trunc(order_date) - trunc(prev_order_date)) avg_days_between 26 | from 27 | ( 28 | select customer_id, order_date, 29 | lag(order_date,1,order_date) 30 | over (partition by customer_id order by order_date) 31 | as prev_order_date 32 | from orders 33 | where customer_id = 102 34 | ); 35 | 36 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-03.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-3 */ 2 | 3 | select (max(trunc(order_date)) - min(trunc(order_date))) / count(*) as avg_days_between 4 | from orders 5 | where customer_id = 102 ; 6 | 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-05.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-5 */ 2 | 3 | 4 | select color from table1 5 | minus 6 | select color from table2; 7 | 8 | select distinct color from table1 9 | where not exists (select null from table2 where table2.color = table1.color) ; 10 | 11 | select color from table2 12 | minus 13 | select color from table1; 14 | 15 | select color from table1 16 | minus 17 | select color from table3; 18 | 19 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-06.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-6 */ 2 | 3 | 4 | select color from table1 5 | intersect 6 | select color from table2; 7 | 8 | 9 | select color from table1 10 | intersect 11 | select color from table3; 12 | 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-07.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-7 */ 2 | 3 | 4 | select * from scott.emp ; 5 | 6 | select * from scott.emp where deptno in (10, 20, 30) ; 7 | 8 | select * from scott.emp where deptno not in (10, 20, 30) ; 9 | 10 | select * from scott.emp where deptno not in (10, 20, 30) 11 | or deptno is null; 12 | 13 | 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-08.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-8 */ 2 | 3 | 4 | select * from scott.emp where deptno is null ; 5 | 6 | select * from scott.emp where deptno = null ; 7 | 8 | select sal, comm, sal + comm as tot_comp 9 | from scott.emp where deptno = 30; 10 | 11 | 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-09.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-9 */ 2 | 3 | 4 | select null from dual 5 | union 6 | select null from dual 7 | ; 8 | 9 | select null from dual 10 | union all 11 | select null from dual 12 | ; 13 | 14 | select null from dual 15 | intersect 16 | select null from dual 17 | ; 18 | 19 | select null from dual 20 | minus 21 | select null from dual 22 | ; 23 | 24 | select 1 from dual 25 | union 26 | select null from dual 27 | ; 28 | 29 | select 1 from dual 30 | union all 31 | select null from dual 32 | ; 33 | 34 | select 1 from dual 35 | intersect 36 | select null from dual 37 | ; 38 | 39 | select 1 from dual 40 | minus 41 | select null from dual 42 | ; 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-10.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-10 */ 2 | 3 | 4 | select comm, count(*) ctr 5 | from scott.emp 6 | group by comm ; 7 | 8 | select comm, count(*) ctr 9 | from scott.emp 10 | group by comm 11 | order by comm ; 12 | 13 | select comm, count(*) ctr 14 | from scott.emp 15 | group by comm 16 | order by comm 17 | nulls first ; 18 | 19 | select ename, sal, comm 20 | from scott.emp 21 | order by comm, ename ; 22 | 23 | 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter04_scripts/lst-04-11.sql: -------------------------------------------------------------------------------- 1 | /* Listing 4-11 */ 2 | 3 | 4 | select count(*) row_ct, count(comm) comm_ct, 5 | avg(comm) avg_comm, min(comm) min_comm, 6 | max(comm) max_comm, sum(comm) sum_comm 7 | from scott.emp ; 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter05_scripts/lst-05-01.sql: -------------------------------------------------------------------------------- 1 | /* Listing 5-1 */ 2 | 3 | select employee_id, count(*) job_ct 4 | from job_history 5 | group by employee_id 6 | having count(*) > 1; 7 | 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter05_scripts/lst-05-02.sql: -------------------------------------------------------------------------------- 1 | /* Listing 5-2 */ 2 | 3 | 4 | select employee_id, count(*) job_ct 5 | from 6 | ( 7 | select e.employee_id, e.job_id 8 | from employees e 9 | union all 10 | select j.employee_id, j.job_id 11 | from job_history j 12 | ) 13 | group by employee_id 14 | having count(*) > 1; 15 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter05_scripts/lst-05-04.sql: -------------------------------------------------------------------------------- 1 | /* Listing 5-4 */ 2 | 3 | select t.table_name||'.'||i.index_name idx_name, 4 | i.clustering_factor, t.blocks, t.num_rows 5 | from user_indexes i, user_tables t 6 | where i.table_name = t.table_name 7 | and t.table_name = 'SALES' 8 | order by t.table_name, i.index_name; 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter05_scripts/lst-05-06.sql: -------------------------------------------------------------------------------- 1 | /* Listing 5-6 */ 2 | 3 | select /* opt9 */ empno, ename 4 | from emp 5 | where :empno is null 6 | union all 7 | select empno, ename 8 | from emp 9 | where :empno = empno; 10 | 11 | @pln opt9 12 | 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter05_scripts/pln.sql: -------------------------------------------------------------------------------- 1 | SELECT xplan.* 2 | FROM 3 | ( 4 | select max(sql_id) keep 5 | (dense_rank last order by last_active_time) sql_id 6 | , max(child_number) keep 7 | (dense_rank last order by last_active_time) child_number 8 | from v$sql 9 | where upper(sql_text) like '%&1%' 10 | and upper(sql_text) not like '%FROM V$SQL WHERE UPPER(SQL_TEXT) LIKE %' 11 | ) sqlinfo, 12 | table(DBMS_XPLAN.DISPLAY_CURSOR(sqlinfo.sql_id, sqlinfo.child_number, 'ALLSTATS LAST')) xplan 13 | / 14 | 15 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-01.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-1 */ 2 | 3 | explain plan for 4 | select e.last_name || ', ' || e.first_name as full_name, 5 | e.phone_number, e.email, e.department_id, 6 | d.department_name, c.country_name, l.city, l.state_province, 7 | r.region_name 8 | from hr.employees e, hr.departments d, hr.countries c, 9 | hr.locations l, hr.regions r 10 | where e.department_id = d.department_id 11 | and d.location_id = l.location_id 12 | and l.country_id = c.country_id 13 | and c.region_id = r.region_id; 14 | 15 | select * from table(dbms_xplan.display); 16 | 17 | set autotrace traceonly explain 18 | 19 | select e.last_name || ', ' || e.first_name as full_name, 20 | e.phone_number, e.email, e.department_id, 21 | d.department_name, c.country_name, l.city, l.state_province, 22 | r.region_name 23 | from hr.employees e, hr.departments d, hr.countries c, 24 | hr.locations l, hr.regions r 25 | where e.department_id = d.department_id 26 | and d.location_id = l.location_id 27 | and l.country_id = c.country_id 28 | and c.region_id = r.region_id; 29 | 30 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-03.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-3 */ 2 | 3 | select id, parent_id, 4 | lpad(' ',level) || operation || ' ' || options || ' ' || 5 | object_name as operation 6 | from plan_table 7 | start with id = 0 8 | connect by prior id = parent_id ; 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-04.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-4 */ 2 | 3 | create table regions2 4 | (region_id varchar2(10) primary key, 5 | region_name varchar2(25)); 6 | 7 | 8 | insert into regions2 9 | select * from regions; 10 | 11 | 12 | variable regid number 13 | 14 | exec :regid := 1 15 | 16 | set autotrace traceonly explain 17 | 18 | select /* DataTypeTest */ * 19 | from regions2 20 | where region_id = :regid; 21 | 22 | set autotrace off 23 | 24 | select /* DataTypeTest */ * 25 | from regions2 26 | where region_id = :regid; 27 | 28 | @pln DataTypeTest 29 | 30 | 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-06.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-6 */ 2 | 3 | select id, parent_id, operation 4 | from ( 5 | select level lvl, id, parent_id, lpad(' ',level) || operation || ' ' || options 6 | || ' ' || object_name as operation 7 | from plan_table 8 | start with id = 0 9 | connect by prior id = parent_id 10 | ) 11 | order by lvl desc, id; 12 | 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-07.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-7 */ 2 | 3 | select /* recentsql */ sql_id, child_number, hash_value, address, executions, sql_text 4 | from v$sql 5 | where parsing_user_id = (select user_id 6 | from all_users 7 | where username = 'SCOTT') 8 | and command_type in (2,3,6,7,189) 9 | and UPPER(sql_text) not like UPPER('%recentsql%') 10 | / 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-08.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-8 */ 2 | 3 | 4 | select /*+ gather_plan_statistics */ empno, ename from scott.emp where ename = 'KING' ; 5 | 6 | set serveroutput off 7 | 8 | select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST')); 9 | 10 | 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-09.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-9 */ 2 | 3 | 4 | select ename from scott.emp where ename = 'KING' ; 5 | 6 | select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST')); 7 | 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-11.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-11 */ 2 | 3 | 4 | select /* KM-EMPTEST1 */ 5 | empno, ename 6 | from emp 7 | where job = 'MANAGER' ; 8 | 9 | 10 | select sql_id, child_number, sql_text 11 | from v$sql 12 | where sql_text like '%KM-EMPTEST1%'; 13 | 14 | 15 | select * from table(dbms_xplan.display_cursor('',0,'ALLSTATS LAST')); 16 | 17 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-12.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-12 */ 2 | 3 | 4 | select /* KM-EMPTEST2 */ 5 | empno, ename 6 | from emp 7 | where job = 'CLERK' ; 8 | 9 | 10 | get pln.sql 11 | 12 | @pln KM-EMPTEST2 13 | 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter06_scripts/lst-06-13.sql: -------------------------------------------------------------------------------- 1 | /* Listing 6-13 */ 2 | 3 | 4 | 5 | explain plan for 6 | select * from emp e, dept d 7 | where e.deptno = d.deptno 8 | and e.ename = 'JONES' ; 9 | 10 | 11 | select * from table(dbms_xplan.display(format=>'ALL')); 12 | 13 | select empno, ename from emp e, dept d 14 | where e.deptno = d.deptno 15 | and e.ename = 'JONES' ; 16 | 17 | 18 | select * from table(dbms_xplan.display_cursor(null,null,format=>'ALLSTATS LAST -COST -BYTES')); 19 | 20 | variable v_empno number 21 | 22 | exec :v_empno := 7566 ; 23 | 24 | select * from emp where empno = :v_empno ; 25 | 26 | select * from table(dbms_xplan.display_cursor(null,null,format=>'+PEEKED_BINDS')); 27 | 28 | select /*+ parallel(d, 4) parallel (e, 4) */ 29 | d.dname, avg(e.sal), max(e.sal) 30 | from dept d, emp e 31 | where d.deptno = e.deptno 32 | group by d.dname 33 | order by max(e.sal), avg(e.sal) desc; 34 | 35 | select * from table(dbms_xplan.display_cursor(null,null,'TYPICAL -BYTES -COST')); 36 | 37 | 38 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_1.sql: -------------------------------------------------------------------------------- 1 | 2 | -- showplan_last.sql 3 | 4 | set pause off 5 | set verify off 6 | set trimspool on 7 | set line 200 arraysize 1 8 | clear break 9 | clear compute 10 | -- serveroutput must be OFF for dbms_xplan.display_cursor to work. 11 | -- but do not turn it off here, or the SET statemeent will be the 'last' cursor 12 | 13 | select * 14 | from table(dbms_xplan.display_cursor( null,null,'TYPICAL LAST')) 15 | --from table(dbms_xplan.display_cursor( null,null,'ALLSTATS LAST')) 16 | --from table(dbms_xplan.display_cursor( null,null,'ALLSTATS PREDICATE LAST')) 17 | --from table(dbms_xplan.display_cursor( null,null,'ALLSTATS ALIAS PROJECTION PREDICATE LAST')) 18 | --from table(dbms_xplan.display_cursor( null,null,'ADVANCED LAST')) 19 | / 20 | 21 | 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_10.sql: -------------------------------------------------------------------------------- 1 | 2 | set autotrace on statistics 3 | 4 | with emps as ( 5 | select /*+ gather_plan_statistics */ 6 | last_name 7 | , first_name 8 | from hr.employees 9 | group by cube(first_name,last_name) 10 | ) 11 | select rownum 12 | , last_name 13 | , first_name 14 | from emps 15 | 16 | l 17 | 18 | / 19 | 20 | @showplan_last 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_11.sql: -------------------------------------------------------------------------------- 1 | 2 | 3 | with counts as ( 4 | select 5 | count(distinct first_name) first_name_count 6 | , count(distinct last_name) last_name_count 7 | , count(distinct(first_name||last_name)) full_name_count 8 | from hr.employees 9 | ) 10 | select 11 | first_name_count 12 | , last_name_count 13 | , full_name_count 14 | , first_name_count + last_name_count + full_name_count + 1 total_count 15 | from counts 16 | 17 | l 18 | 19 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_12.sql: -------------------------------------------------------------------------------- 1 | 2 | set autotrace on statistics 3 | 4 | with emps as ( 5 | select last_name, first_name from hr.employees 6 | ) , 7 | mycube as ( 8 | select last_name, first_name from emps 9 | union all 10 | select last_name, null first_name from emps 11 | union all 12 | select null last_name, first_name from emps 13 | union all 14 | select null last_name, null first_name from emps 15 | ) 16 | select /*+ gather_plan_statistics */ * 17 | from mycube 18 | group by last_name, first_name 19 | 20 | l 21 | 22 | / 23 | 24 | @showplan_last 25 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_17.sql: -------------------------------------------------------------------------------- 1 | 2 | -- demo bit vector created by grouping_id() 3 | 4 | col gid format 999 head 'GID' 5 | col gb_0 format 9999999 head 'GROUPING|BIT 0' 6 | col gb_1 format 9999999 head 'GROUPING|BIT 1' 7 | 8 | col bit_vector head 'BIT|VECTOR' format a6 9 | 10 | with rowgen as ( 11 | select 1 bit_1, 0 bit_0 12 | from dual 13 | ), 14 | cubed as ( 15 | select 16 | grouping_id(bit_1,bit_0) gid 17 | , to_char(grouping(bit_1)) bv_1 18 | , to_char(grouping(bit_0)) bv_0 19 | , decode(grouping(bit_1),1,'GRP BIT 1') gb_1 20 | , decode(grouping(bit_0),1,'GRP BIT 0') gb_0 21 | from rowgen 22 | group by cube(bit_1,bit_0) 23 | ) 24 | select 25 | gid 26 | , bv_1 || bv_0 bit_vector 27 | , gb_1 28 | , gb_0 29 | from cubed 30 | order by gid 31 | 32 | l 33 | 34 | / 35 | 36 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_2.sql: -------------------------------------------------------------------------------- 1 | 2 | with lobtest as ( 3 | select to_clob(d.dname ) dname 4 | from scott.emp e 5 | join scott.dept d on d.deptno = e.deptno 6 | ) 7 | select l.dname 8 | from lobtest l 9 | group by l.dname 10 | 11 | l 12 | 13 | / 14 | 15 | 16 | select d.dname, count(empno) empcount 17 | from scott.emp e 18 | join scott.dept d on d.deptno = e.deptno 19 | group by (select dname from scott.dept d2 where d2.dname = d.dname) 20 | order by d.dname 21 | 22 | l 23 | 24 | / 25 | 26 | 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_21.sql: -------------------------------------------------------------------------------- 1 | 2 | -- rollup 3 | 4 | col prod_category format a30 head 'PRODUCT CATEGORY' 5 | col amount_sold format $99,999,99.00 head 'AMT SOLD' 6 | col cust_name format a30 head 'CUSTOMER' 7 | 8 | spool l_7_21.txt 9 | 10 | with mysales as ( 11 | select 12 | c.cust_last_name ||',' || c.cust_first_name cust_name 13 | , p.prod_category 14 | , to_char(trunc(time_id,'YYYY'),'YYYY') sale_year 15 | , p.prod_name 16 | , s.amount_sold 17 | from sh.sales s 18 | join sh.products p on p.prod_id = s.prod_id 19 | join sh.customers c on c.cust_id = s.cust_id 20 | where c.cust_last_name like 'Sul%' 21 | --where s.time_id = to_date('01/01/2001','mm/dd/yyyy') 22 | ) 23 | select 24 | decode(grouping(m.cust_name),1,'GRAND TOTAL',m.cust_name) cust_name 25 | , decode(grouping(m.sale_year),1,'TOTAL BY YEAR',m.sale_year) sale_year 26 | , decode(grouping(m.prod_category),1,'TOTAL BY CATEGORY',m.prod_category) prod_category 27 | , sum(m.amount_sold) amount_sold 28 | from mysales m 29 | group by rollup(m.cust_name, m.prod_category, m.sale_year) 30 | order by grouping(m.cust_name), 1,2,3 31 | 32 | l 33 | 34 | / 35 | 36 | spool off 37 | 38 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_4.sql: -------------------------------------------------------------------------------- 1 | 2 | select d.dname, count(empno) empcount 3 | from scott.dept d 4 | left outer join scott.emp e on d.deptno = e.deptno 5 | group by d.dname 6 | order by d.dname 7 | 8 | l 9 | 10 | / 11 | 12 | --@showplan_last 13 | 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_5.sql: -------------------------------------------------------------------------------- 1 | select d.dname, count(empno) empcount 2 | from scott.emp e 3 | join scott.dept d on d.deptno = e.deptno 4 | order by d.dname 5 | 6 | l 7 | 8 | / 9 | 10 | --@showplan_last 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_6.sql: -------------------------------------------------------------------------------- 1 | select deptno, count(*) 2 | from scott.emp 3 | group by deptno 4 | / 5 | 6 | @showplan_last 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_7.sql: -------------------------------------------------------------------------------- 1 | select /*+ gather_plan_statistics */ 2 | distinct dname, decode( 3 | d.deptno, 4 | 10, (select count(*) from scott.emp where deptno= 10), 5 | 20, (select count(*) from scott.emp where deptno= 20), 6 | 30, (select count(*) from scott.emp where deptno= 30), 7 | (select count(*) from scott.emp where deptno not in (10,20,30)) 8 | ) dept_count 9 | from (select distinct deptno from scott.emp) d 10 | join scott.dept d2 on d2.deptno = d.deptno 11 | / 12 | 13 | --@showplan_last 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_8.sql: -------------------------------------------------------------------------------- 1 | 2 | select /*+ gather_plan_statistics */ 3 | d.dname 4 | , count(empno) empcount 5 | from scott.emp e 6 | join scott.dept d on d.deptno = e.deptno 7 | group by d.dname 8 | order by d.dname 9 | 10 | l 11 | 12 | / 13 | 14 | @showplan_last 15 | 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/l_7_9.sql: -------------------------------------------------------------------------------- 1 | 2 | select /*+ gather_plan_statistics */ 3 | d.dname 4 | , trunc(e.hiredate,'YYYY') hiredate 5 | , count(empno) empcount 6 | from scott.emp e 7 | join scott.dept d on d.deptno = e.deptno 8 | group by d.dname, trunc(e.hiredate,'YYYY') 9 | having 10 | count(empno) >= 5 11 | and trunc(e.hiredate,'YYYY') between 12 | (select trunc(add_months(min(hiredate),12),'YYYY') from scott.emp) 13 | and 14 | (select max(hiredate) from scott.emp) 15 | order by d.dname 16 | 17 | l 18 | 19 | / 20 | 21 | @showplan_last 22 | 23 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter07_scripts/showplan_last.sql: -------------------------------------------------------------------------------- 1 | 2 | -- showplan9i.sql 3 | -- works with 9.2+ 4 | 5 | SET PAUSE OFF 6 | SET VERIFY OFF 7 | set trimspool on 8 | set line 200 arraysize 1 9 | clear break 10 | clear compute 11 | 12 | 13 | select * 14 | from table(dbms_xplan.display_cursor( null,null,'ALLSTATS LAST')) 15 | / 16 | 17 | 18 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_1.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT FACT table for Model clause chapter. 3 | PROMPT 4 | 5 | drop table sales_fact; 6 | CREATE table sales_fact AS 7 | SELECT country_name country,country_subRegion region, prod_name product, calendar_year year, calendar_week_number week, 8 | SUM(amount_sold) sale, 9 | sum(amount_sold* 10 | ( case 11 | when mod(rownum, 10)=0 then 1.4 12 | when mod(rownum, 5)=0 then 0.6 13 | when mod(rownum, 2)=0 then 0.9 14 | when mod(rownum,2)=1 then 1.2 15 | else 1 16 | end )) receipts 17 | FROM sales, times, customers, countries, products 18 | WHERE sales.time_id = times.time_id AND 19 | sales.prod_id = products.prod_id AND 20 | sales.cust_id = customers.cust_id AND 21 | customers.country_id = countries.country_id 22 | GROUP BY 23 | country_name,country_subRegion, prod_name, calendar_year, calendar_week_number; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_10.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT NTH_VALUE function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week, sale, 13 | nth_value ( sale, 2) over ( 14 | partition by product,country, region, year 15 | order by sale desc 16 | rows between unbounded preceding and unbounded following 17 | ) sale_2nd_top 18 | from sales_fact 19 | where country in ('Australia') and product='Xtend Memory' 20 | order by product, country , year, week 21 | / 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_11.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT RANK function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select * from ( 13 | select year, week,sale, 14 | rank() over( 15 | partition by product, country, region ,year 16 | order by sale desc 17 | ) sales_rank 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by product, country,year, week 21 | ) where sales_rank<=10 22 | order by 1,4 23 | / 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_12.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT dense_rank 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select * from ( 13 | select year, week,sale, 14 | dense_rank() over( 15 | partition by product, country, region ,year 16 | order by sale desc 17 | ) sales_rank 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by product, country,year, week 21 | ) where sales_rank<=10 22 | order by 1,4 23 | / 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_13.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT row_number and rank function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | row_number() over( 14 | partition by product, country, region ,year 15 | order by sale desc 16 | ) sales_rn, 17 | rank() over( 18 | partition by product, country, region ,year 19 | order by sale desc 20 | ) sales_rank 21 | from sales_fact 22 | where country in ('Australia') and product ='Xtend Memory' 23 | order by product, country,year,sales_rank 24 | / 25 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_14.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT ratio_to_report 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | trunc(100* 14 | ratio_to_report(sale) over(partition by product, country, region ,year) 15 | ,2) sales_yr, 16 | trunc(100* 17 | ratio_to_report(sale) over(partition by product, country, region) 18 | ,2) sales_prod 19 | from sales_fact 20 | where country in ('Australia') and product ='Xtend Memory' 21 | order by product, country,year, week 22 | / 23 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_15.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT PERCENT_RANK function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select * from ( 13 | select year, week,sale, 14 | 100*percent_rank() over( 15 | partition by product, country, region , year 16 | order by sale desc 17 | ) pr 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | ) where pr <50 21 | order by year, sale desc 22 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_16.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT percentile_cont 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | percentile_cont (0.5) within group (order by sale desc) 14 | over( partition by product, country, region , year ) pc, 15 | percent_rank () over ( 16 | partition by product, country, region , year 17 | order by sale desc ) pr 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | / 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_17.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT percentile_disc function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | percentile_disc (0.5) within group (order by sale desc) 14 | over( partition by product, country, region , year ) pd_desc, 15 | percentile_disc (0.5) within group (order by sale ) 16 | over( partition by product, country, region , year ) pd_asc, 17 | percent_rank () over ( 18 | partition by product, country, region , year 19 | order by sale desc ) pr 20 | from sales_fact 21 | where country in ('Australia') and product ='Xtend Memory' 22 | / 23 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_18.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT NTILE 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | ntile (10) over( 14 | partition by product, country, region , year 15 | order by sale desc 16 | ) group# 17 | from sales_fact 18 | where country in ('Australia') and product ='Xtend Memory' 19 | 20 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_19.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT STDDEV function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | stddev (sale) over( 14 | partition by product, country, region , year 15 | order by Sale desc 16 | rows between unbounded preceding and unbounded following 17 | ) stddv 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by year, week 21 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_2.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT SUM function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | sum (sale) over( 14 | partition by product, country, region ,year 15 | order by week 16 | rows between unbounded preceding and current row 17 | ) running_sum_ytd 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by product, country,year, week 21 | / 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_20.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Listagg function 3 | PROMPT 4 | set lines 120 pages 100 5 | select listagg (country, ',') 6 | within group (order by country desc) 7 | from ( 8 | select distinct country from sales_fact 9 | order by country 10 | ) 11 | / 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_21.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Predicates in the execution plan 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | create or replace view max_5_weeks_vw as 13 | select country , product, region, year, week,sale, 14 | max (sale) over( 15 | partition by product, country, region ,year 16 | order by year, week 17 | rows between 2 preceding and 2 following 18 | ) max_weeks_5 19 | from sales_fact 20 | / 21 | 22 | select year, week, sale, max_weeks_5 from max_5_weeks_vw 23 | where country in ('Australia') and product ='Xtend Memory' and 24 | region='Australia' and year= 2000 and week <14 25 | order by year, week 26 | / 27 | 28 | set lines 120 pages 100 29 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_22.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Predicates.. 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | create or replace view max_5_weeks_vw as 13 | select country , product, region, year, week,sale, 14 | max (sale) over( 15 | partition by product, country, region ,year 16 | order by year, week 17 | rows between 2 preceding and 2 following 18 | ) max_weeks_5 19 | from sales_fact 20 | / 21 | create index sales_fact_i1 on sales_fact( country, product); 22 | select year, week, sale, max_weeks_5 from max_5_weeks_vw 23 | where country in ('Australia') and product ='Xtend Memory' and 24 | region='Australia' and year= 2000 and week <14 25 | order by year, week 26 | / 27 | 28 | set lines 120 pages 100 29 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 30 | drop index sales_fact_i1; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_3.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Max function - analytical mode 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | max (sale) over( 14 | partition by product, country, region ,year 15 | order by week 16 | rows between unbounded preceding and unbounded following 17 | ) max_sale_ytd 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by product, country,year, week 21 | / 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_4.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Max function - analytical mode - 5 weeks window 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | max (sale) over( 14 | partition by product, country, region ,year 15 | order by year, week 16 | rows between 2 preceding and 2 following 17 | ) max_weeks_5 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by product, country,year, week 21 | / 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_5.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Lag function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | lag(sale, 1,sale) over(partition by product, country, region order by year, week) prior_wk_sales 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | order by product, country,year, week 17 | / 18 | 19 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_6.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Lag function with offset of 10 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | lag(sale, 10,sale) over( 14 | partition by product, country, region 15 | order by year, week 16 | ) prior_wk_sales_10 17 | from sales_fact 18 | where country in ('Australia') and product ='Xtend Memory' 19 | order by product, country,year, week 20 | / 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_7.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Lead function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | lead(sale, 1,sale) over( 14 | partition by product, country, region 15 | order by year, week 16 | ) prior_wk_sales 17 | from sales_fact 18 | where country in ('Australia') and product ='Xtend Memory' 19 | order by product, country,year, week 20 | / 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_8.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT First_value function 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | first_value (sale) over( 14 | partition by product, country, region ,year 15 | order by sale desc 16 | rows between unbounded preceding and unbounded following 17 | ) top_sale_value, 18 | first_value (week) over( 19 | partition by product, country, region ,year 20 | order by sale desc 21 | rows between unbounded preceding and unbounded following 22 | ) top_sale_week 23 | from sales_fact 24 | where country in ('Australia') and product ='Xtend Memory' 25 | order by product, country,year, week 26 | / 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/Listing_8_9.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Last_value 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select year, week,sale, 13 | last_value (sale) over( 14 | partition by product, country, region ,year 15 | order by sale desc 16 | rows between unbounded preceding and unbounded following 17 | ) low_sale 18 | from sales_fact 19 | where country in ('Australia') and product ='Xtend Memory' 20 | order by product, country,year, week 21 | / 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter08_scripts/x.sql: -------------------------------------------------------------------------------- 1 | set lines 120 pages 100 2 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_10.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model with return updated rows 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, sale 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( sale) 20 | rules( 21 | sale [2002, 1] = 0 22 | ) 23 | / 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_11.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- Throws an error: should be ORA-32637 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | where country in ('Australia') 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , sale, receipts) 20 | rules ( 21 | inventory [year, week ] = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ] 25 | ) 26 | order by product, country,year, week 27 | / 28 | 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_12.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- This should work fine.. ORDER BY YEAR,WEEK added 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | where country in ('Australia') 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , sale, receipts) 20 | rules ( 21 | inventory [year, week ] order by year, week = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ] 25 | ) 26 | order by product, country,year, week 27 | / 28 | 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_13.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- This should work fine.. ORDER BY YEAR,WEEK added 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | where country in ('Australia') 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , sale, receipts) 20 | rules ( 21 | inventory [year, week ] order by year, week = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ] 25 | ) 26 | order by product, country,year, week 27 | / 28 | 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_16.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- Aggregation.. 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, inventory,avg_inventory, max_sale 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , 0 avg_inventory ,0 max_sale, sale, receipts) 20 | rules automatic order( 21 | inventory [year, week ] = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ], 25 | avg_inventory [ year,ANY ] = avg (inventory) [ cv(year), week ], 26 | max_Sale [ year, ANY ] = max( sale) [ cv(year), week ] 27 | ) 28 | order by product, country,year, week 29 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_17.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- Iteration example 3 | PROMPT 4 | PROMPT 5 | col product format A12 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select year, week,sale, sale_list 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( cast(' ' as varchar2(50) ) sale_list, sale) 20 | rules iterate (5) ( 21 | sale_list [ year, week ] order by year, week = 22 | sale [cv(year), CV(week)-ITERATION_NUMBER +2 ]|| 23 | case when iteration_number=0 then '' 24 | else ', ' 25 | end || 26 | sale_list [cv(year) ,cv(week)] 27 | ) 28 | order by year, week 29 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_18.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- Iteration and presentv example 3 | PROMPT 4 | PROMPT 5 | col product format A12 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | col sale_list format A40 13 | set lines 120 pages 100 14 | select year, week,sale, sale_list 15 | from sales_fact 16 | where country in ('Australia') and product ='Xtend Memory' 17 | model return updated rows 18 | partition by (product, country) 19 | dimension by (year, week) 20 | measures ( cast(' ' as varchar2(120) ) sale_list, sale, 0 tmp) 21 | rules iterate (5) ( 22 | sale_list [ year, week ] order by year, week = 23 | presentv( sale [cv(year), CV(week)-iteration_number + 2 ], 24 | sale [cv(year), CV(week)-iteration_number +2 ] || 25 | case when iteration_number=0 then '' else ', ' end || 26 | sale_list [cv(year) ,cv(week)] , 27 | sale_list [cv(year) ,cv(week)] ) 28 | ) 29 | order by year, week 30 | / 31 | 32 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_19.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- Reference clause example 3 | PROMPT 4 | PROMPT 5 | col product format A12 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select year, week,sale, prod_list_price 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | REFERENCE ref_prod on 18 | (select prod_name, max(prod_list_price) prod_list_price from products group by prod_name) 19 | dimension by (prod_name) 20 | measures (prod_list_price) 21 | MAIN main_section 22 | partition by (product, country) 23 | dimension by (year, week) 24 | measures ( sale, receipts, 0 prod_list_price ) 25 | rules ( 26 | prod_list_price[year,week] order by year, week = ref_prod.prod_list_price [ cv(product) ] 27 | ) 28 | order by year, week 29 | / 30 | 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_21.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT KEEP NAV example 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, sale 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model KEEP NAV return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( sale) 20 | rules automatic order( 21 | sale[2001,1] = sale[2001,1], 22 | sale [ 2002, 1] = sale[2001,1] + sale[2002,1] 23 | ) 24 | order by product, country,year, week 25 | / 26 | 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_22.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT IGNORE NAV 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, sale 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model IGNORE NAV return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( sale) 20 | rules sequential order( 21 | sale[2001,1] = sale[2001,1], 22 | sale [ 2002, 1] = sale[2001,1] + sale[2002,1] 23 | ) 24 | order by product, country,year, week 25 | / 26 | 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_23.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Execution plan- ACYCLIC 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select product, country, year, week, inventory, sale, receipts 13 | from sales_fact 14 | where country in ('Australia') and product='Xtend Memory' 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year, week) 18 | measures ( 0 inventory , sale, receipts) 19 | rules automatic order( 20 | inventory [year, week ] order by year, week= 21 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 22 | - sale[cv(year), cv(week) ] + 23 | + receipts [cv(year), cv(week) ] 24 | ) 25 | order by product, country,year, week 26 | / 27 | 28 | l 29 | set lines 120 pages 100 30 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_24.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Execution plan - ACYCLIC FAST 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select distinct product, country, year,week, sale_first_Week 13 | from sales_fact 14 | where country in ('Australia') and product='Xtend Memory' 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year,week) 18 | measures ( 0 sale_first_week ,sale ) 19 | rules automatic order( 20 | sale_first_week [2000,1] = 0.12*sale [2000, 1] 21 | ) 22 | order by product, country,year, week 23 | / 24 | 25 | l 26 | set lines 120 pages 100 27 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_25.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model - Cyclic 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select product, country, year, week, inventory, sale, receipts 13 | from sales_fact 14 | where country in ('Australia') and product='Xtend Memory' 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year, week) 18 | measures ( 0 inventory , sale, receipts) 19 | rules automatic order( 20 | inventory [year, week ] = 21 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 22 | - sale[cv(year), cv(week) ] + 23 | + receipts [cv(year), cv(week) ] 24 | ) 25 | order by product, country,year, week 26 | / 27 | 28 | l 29 | set lines 120 pages 100 30 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_26.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model - ORDERED 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select product, country, year, week, inventory, sale, receipts 13 | from sales_fact 14 | where country in ('Australia') and product='Xtend Memory' 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year, week) 18 | measures ( 0 inventory , sale, receipts) 19 | rules sequential order( 20 | inventory [year, week ] order by year, week = 21 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 22 | - sale[cv(year), cv(week) ] + 23 | + receipts [cv(year), cv(week) ] 24 | ) 25 | order by product, country,year, week 26 | / 27 | 28 | l 29 | set lines 120 pages 100 30 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_27.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model and predicate pushing 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select * from ( 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year, week) 18 | measures ( 0 inventory , sale, receipts) 19 | rules automatic order( 20 | inventory [year, week ] = 21 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 22 | - sale[cv(year), cv(week) ] + 23 | + receipts [cv(year), cv(week) ] 24 | ) 25 | ) where country in ('Australia') and product='Xtend Memory' 26 | order by product, country,year, week 27 | / 28 | @x 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_28.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Execution plan witout predicate push 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select * from ( 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year, week) 18 | measures ( 0 inventory , sale, receipts) 19 | rules automatic order( 20 | inventory [year, week ] = 21 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 22 | - sale[cv(year), cv(week) ] + 23 | + receipts [cv(year), cv(week) ] 24 | ) 25 | ) where year=2000 26 | order by product, country,year, week 27 | / 28 | @x.sql 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_2_cr_table.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT FACT table for Model clause chapter. 3 | PROMPT 4 | 5 | drop table sales_fact; 6 | CREATE table sales_fact AS 7 | SELECT country_name country,country_subRegion region, prod_name product, calendar_year year, calendar_week_number week, 8 | SUM(amount_sold) sale, 9 | sum(amount_sold* 10 | ( case 11 | when mod(rownum, 10)=0 then 1.4 12 | when mod(rownum, 5)=0 then 0.6 13 | when mod(rownum, 2)=0 then 0.9 14 | when mod(rownum,2)=1 then 1.2 15 | else 1 16 | end )) receipts 17 | FROM sales, times, customers, countries, products 18 | WHERE sales.time_id = times.time_id AND 19 | sales.prod_id = products.prod_id AND 20 | sales.cust_id = customers.cust_id AND 21 | customers.country_id = countries.country_id 22 | GROUP BY 23 | country_name,country_subRegion, prod_name, calendar_year, calendar_week_number; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_3.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Inventory formula calculation using Model clause. 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select product, country, year, week, inventory, sale, receipts 13 | from sales_fact 14 | where country in ('Australia') 15 | model return updated rows 16 | partition by (product, country) 17 | dimension by (year, week) 18 | measures ( 0 inventory , sale, receipts) 19 | rules automatic order( 20 | inventory [year, week ] = 21 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 22 | - sale[cv(year), cv(week) ] + 23 | + receipts [cv(year), cv(week) ] 24 | ) 25 | order by product, country,year, week 26 | / 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_30.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model and parallelism 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select /*+ parallel ( sf 4) */ 13 | product, country, year, week, inventory, sale, receipts 14 | from sales_fact sf 15 | where country in ('Australia') and product='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , sale, receipts) 20 | rules automatic order( 21 | inventory [year, week ] order by year, week = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ] 25 | ) 26 | 27 | / 28 | 29 | select * from table(dbms_xplan.display_cursor('','','ALLSTATS LAST')); -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_31.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model and partition pruning 3 | PROMPT 4 | col product format A30 5 | col country format A10 6 | col region format A10 7 | col year format 9999 8 | col week format 99 9 | col sale format 999999.99 10 | col receipts format 999999.99 11 | set lines 120 pages 100 12 | select * from ( 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact_part sf 15 | model return updated rows 16 | partition by (year, country ) 17 | dimension by (product, week) 18 | measures ( 0 inventory , sale, receipts ) 19 | rules automatic order( 20 | inventory [product, week ] order by product, week = 21 | nvl(inventory [cv(product), cv(week)-1 ] ,0) 22 | - sale[cv(product), cv(week) ] + 23 | + receipts [cv(product), cv(week) ] 24 | ) 25 | ) where year=2000 and country='Australia' and product='Xtend Memory' 26 | / 27 | 28 | select * from table(dbms_xplan.display_cursor('','','ALL')); -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_31_partition.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Test table for Model clause chapter. 3 | PROMPT 4 | 5 | drop table sales_fact_part; 6 | CREATE table sales_fact_part 7 | partition by list (year) 8 | ( 9 | partition p1 values ( 1998), 10 | partition p2 values ( 1999), 11 | partition p3 values (2000), 12 | partition p4 values (2001), 13 | partition p5 values ( default) 14 | ) 15 | AS 16 | SELECT country_name country,country_subRegion region, prod_name product, calendar_year year, calendar_week_number week, 17 | SUM(amount_sold) sale, 18 | sum(amount_sold* 19 | ( case 20 | when mod(rownum, 10)=0 then 1.4 21 | when mod(rownum, 5)=0 then 0.6 22 | when mod(rownum, 2)=0 then 0.9 23 | when mod(rownum,2)=1 then 1.2 24 | else 1 25 | end )) receipts 26 | FROM sales, times, customers, countries, products 27 | WHERE sales.time_id = times.time_id AND 28 | sales.prod_id = products.prod_id AND 29 | sales.cust_id = customers.cust_id AND 30 | customers.country_id = countries.country_id 31 | GROUP BY 32 | country_name,country_subRegion, prod_name, calendar_year, calendar_week_number; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_32.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model clause with no partition pruning 3 | PROMPT 4 | PROMPT Prerequisite: Listing_9_31_partition.sql 5 | PROMPT 6 | col product format A30 7 | col country format A10 8 | col region format A10 9 | col year format 9999 10 | col week format 99 11 | col sale format 999999.99 12 | col receipts format 999999.99 13 | set lines 120 pages 100 14 | select * from ( 15 | select product, country, year, week, inventory, sale, receipts 16 | from sales_fact_part sf 17 | model return updated rows 18 | partition by (product, country) 19 | dimension by (year, week) 20 | measures ( 0 inventory , sale, receipts) 21 | rules automatic order( 22 | inventory [year, week ] order by year, week = 23 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 24 | - sale[cv(year), cv(week) ] + 25 | + receipts [cv(year), cv(week) ] 26 | ) 27 | ) where year=2000 and country='Australia' and product='Xtend Memory' 28 | / 29 | 30 | select * from table(dbms_xplan.display_cursor('','','ALL')); -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_33.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Indexing with SQL access in mind 3 | PROMPT 4 | PROMPT Prerequisite: Listing_9_31_partition.sql 5 | PROMPT 6 | create index sales_fact_part_i1 on sales_fact_part (country, product) ; 7 | col product format A30 8 | col country format A10 9 | col region format A10 10 | col year format 9999 11 | col week format 99 12 | col sale format 999999.99 13 | col receipts format 999999.99 14 | set lines 160 pages 100 15 | select * from ( 16 | select product, country, year, week, inventory, sale, receipts 17 | from sales_fact_part sf 18 | model return updated rows 19 | partition by (product, country) 20 | dimension by (year, week) 21 | measures ( 0 inventory , sale, receipts) 22 | rules automatic order( 23 | inventory [year, week ] order by year, week = 24 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 25 | - sale[cv(year), cv(week) ] + 26 | + receipts [cv(year), cv(week) ] 27 | ) 28 | ) where year=2000 and country='Australia' and product='Xtend Memory' 29 | / 30 | 31 | select * from table(dbms_xplan.display_cursor('','','ALL')); -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_34.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT More complex variation of Model clause 3 | PROMPT 4 | with t1 as ( 5 | select product, country, year, week, inventory, sale, receipts 6 | from sales_fact sf 7 | where country in ('Australia') and product='Xtend Memory' 8 | model return updated rows 9 | partition by (product, country) 10 | dimension by (year, week) 11 | measures ( 0 inventory , sale, receipts) 12 | rules automatic order( 13 | inventory [year, week ] order by year, week = 14 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 15 | - sale[cv(year), cv(week) ] + 16 | + receipts [cv(year), cv(week) ] 17 | ) 18 | ) 19 | select product, country, year, week , inventory, sale,receipts, 20 | prev_sale 21 | from t1 22 | model return updated rows 23 | partition by (product, country) 24 | dimension by (year, week) 25 | measures (inventory, sale, receipts,0 prev_sale) 26 | rules sequential order ( 27 | prev_sale [ year, week ] order by year, week = 28 | nvl (sale [ cv(year) -1, cv(week)],0 ) 29 | ) 30 | / 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_6.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Positional reference: Adding cells for Calendar_week_number 3 | PROMPT 4 | PROMPT ..Retrieving only for product='CD-R Mini Discs' and region='Asia' to improve clarity 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , sale, receipts) 20 | rules automatic order( 21 | inventory [year, week ] = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ], 25 | sale [2002, 1] = 0, 26 | receipts [ 2002,1] =0 27 | ) 28 | order by product, country,year, week 29 | / 30 | 31 | 32 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_7.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Symbolic reference- updates only 3 | PROMPT 4 | PROMPT 5 | col product format A12 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, sale 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( sale) 20 | rules( 21 | sale [ year in (2000,2001), week in (1,52,53) ] order by year, week 22 | = sale [cv(year), cv(week)] * 1.10 23 | ) 24 | order by product, country,year, week 25 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_8.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT MODEL clause -- FOR Loops to add range of cells 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, inventory, sale, receipts 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model return updated rows 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( 0 inventory , sale, receipts) 20 | rules automatic order( 21 | inventory [year, week ] = 22 | nvl(inventory [cv(year), cv(week)-1 ] ,0) 23 | - sale[cv(year), cv(week) ] + 24 | + receipts [cv(year), cv(week) ], 25 | sale [2002, for week from 1 to 53 increment 1] = 0, 26 | receipts [ 2002,for week from 1 to 53 increment 1] =0 27 | ) 28 | order by product, country,year, week 29 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/Listing_9_9.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Model missing "return updated rows" clause 3 | PROMPT 4 | PROMPT 5 | col product format A30 6 | col country format A10 7 | col region format A10 8 | col year format 9999 9 | col week format 99 10 | col sale format 999999.99 11 | col receipts format 999999.99 12 | set lines 120 pages 100 13 | select product, country, year, week, sale 14 | from sales_fact 15 | where country in ('Australia') and product ='Xtend Memory' 16 | model 17 | partition by (product, country) 18 | dimension by (year, week) 19 | measures ( sale) 20 | rules( 21 | sale [ year in (2000,2001), week in (1,52,53) ] order by year, week 22 | = sale [cv(year), cv(week)] * 1.10 23 | ) 24 | order by product, country,year, week 25 | / 26 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/README_Chapter09.txt: -------------------------------------------------------------------------------- 1 | README for MODEL chapter 2 | ======================== 3 | Listing_9_2_cr_table.sql: FACT table creation for MODEL scripts 4 | Listing_9_3.sql : Inventory formula calculation using MODEL 5 | Listing_9_6.sql : Positional reference 6 | Listing_9_7.sql : Symbolic reference 7 | Listing_9_8.sql : FOR Loops 8 | Listing_9_9.sql : Model missing 'return updated rows' clause 9 | Listing_9_10.sql: Model with return updated rows clause 10 | Listing_9_11.sql: Model clause with ORA-32637 11 | Listing_9_12.sql: Model clause with 'order by ' to avoid ORA-32637 12 | Listing_9_13.sql: Evaluation order using the DESC keyword 13 | Listing_9_14.sql: Rule Evaluation order 14 | Listing_9_15.sql: Rule evaluation order with automatic order 15 | Listing_9_16.sql: Model and aggregation 16 | Listing_9_17.sql: Model and Iteration 17 | Listing_9_18.sql: Model, iteration and PRESENTV 18 | Listing_9_19.sql: Model and Reference 19 | Listing_9_20.sql: Model and more lookup tables 20 | Listing_9_21.sql: KEEP NAV example 21 | Listing_9_22.sql: -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter09_scripts/x.sql: -------------------------------------------------------------------------------- 1 | set lines 120 pages 100 2 | select * from table (dbms_xplan.display_cursor('','','ALLSTATS LAST')); 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/exercises/showplan_last.sql: -------------------------------------------------------------------------------- 1 | 2 | -- showplan9i.sql 3 | -- works with 9.2+ 4 | 5 | SET PAUSE OFF 6 | SET VERIFY OFF 7 | set trimspool on 8 | set line 200 arraysize 1 9 | clear break 10 | clear compute 11 | 12 | 13 | select * 14 | from table(dbms_xplan.display_cursor( null,null,'ALLSTATS LAST')) 15 | / 16 | 17 | 18 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_1.sql: -------------------------------------------------------------------------------- 1 | 2 | set timing on 3 | 4 | select * 5 | from ( 6 | select /*+ gather_plan_statistics */ 7 | product 8 | , channel 9 | , quarter 10 | , country 11 | , quantity_sold 12 | from 13 | ( 14 | select 15 | prod_name product 16 | , country_name country 17 | , channel_id channel 18 | , substr(calendar_quarter_desc, 6,2) quarter 19 | , sum(amount_sold) amount_sold 20 | , sum(quantity_sold) quantity_sold 21 | from 22 | sh.sales 23 | join sh.times on times.time_id = sales.time_id 24 | join sh.customers on customers.cust_id = sales.cust_id 25 | join sh.countries on countries.country_id = customers.country_id 26 | join sh.products on products.prod_id = sales.prod_id 27 | group by 28 | prod_name 29 | , country_name 30 | , channel_id 31 | , substr(calendar_quarter_desc, 6, 2) 32 | ) 33 | ) PIVOT ( 34 | sum(quantity_sold) 35 | FOR (channel, quarter) IN 36 | ( 37 | (5, '02') AS CATALOG_Q2, 38 | (4, '01') AS INTERNET_Q1, 39 | (4, '04') AS INTERNET_Q4, 40 | (2, '02') AS PARTNERS_Q2, 41 | (9, '03') AS TELE_Q3 42 | ) 43 | ) 44 | order by product, country 45 | / 46 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_12.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_12.txt 10 | 11 | with emp as ( 12 | select /*+ inline gather_plan_statistics */ 13 | e.last_name, e.first_name 14 | , e.employee_id, d.department_id 15 | , e.manager_id, d.department_name 16 | from hr.employees e 17 | left outer join hr.departments d on d.department_id = e.department_id 18 | ) 19 | select lpad(' ', level*2-1,' ') || emp.last_name last_name 20 | , emp.first_name 21 | , department_name 22 | from emp 23 | connect by prior emp.employee_id = emp.manager_id 24 | start with emp.manager_id is null 25 | order siblings by emp.last_name 26 | 27 | l 28 | 29 | / 30 | 31 | @showplan_last 32 | 33 | spool off 34 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_13.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_13.txt 10 | 11 | with emp as ( 12 | select /*+ inline gather_plan_statistics */ 13 | e.last_name, e.first_name, e.employee_id, e.manager_id, d.department_name 14 | from hr.employees e 15 | left outer join hr.departments d on d.department_id = e.department_id 16 | ), 17 | emp_recurse (last_name,first_name,employee_id,manager_id,department_name,lvl) as ( 18 | select e.last_name, e.first_name 19 | , e.employee_id, e.manager_id 20 | , e.department_name, 1 as lvl 21 | from emp e where e.manager_id is null 22 | union all 23 | select emp.last_name, emp.first_name 24 | , emp.employee_id, emp.manager_id 25 | ,emp.department_name, empr.lvl + 1 as lvl 26 | from emp 27 | join emp_recurse empr on empr.employee_id = emp.manager_id 28 | ) 29 | search depth first by last_name set order1 30 | select lpad(' ', lvl*2-1,' ') || er.last_name last_name 31 | , er.first_name 32 | , er.department_name 33 | from emp_recurse er 34 | 35 | l 36 | 37 | / 38 | 39 | @showplan_last 40 | 41 | spool off 42 | 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_14.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_13.txt 10 | 11 | with emp as ( 12 | select /*+ inline gather_plan_statistics */ 13 | e.last_name, e.first_name, e.employee_id, e.manager_id, d.department_name 14 | from hr.employees e 15 | left outer join hr.departments d on d.department_id = e.department_id 16 | ), 17 | emp_recurse (last_name,first_name,employee_id,manager_id,department_name,lvl) as ( 18 | select e.last_name, e.first_name 19 | , e.employee_id, e.manager_id 20 | , e.department_name, 1 as lvl 21 | from emp e where e.manager_id is null 22 | union all 23 | select emp.last_name, emp.first_name 24 | , emp.employee_id, emp.manager_id 25 | ,emp.department_name, empr.lvl + 1 as lvl 26 | from emp 27 | join emp_recurse empr on empr.employee_id = emp.manager_id 28 | ) 29 | search breadth first by last_name set order1 30 | select lpad(' ', lvl*2-1,' ') || er.last_name last_name 31 | , er.first_name 32 | , er.department_name 33 | from emp_recurse er 34 | 35 | l 36 | 37 | / 38 | 39 | @showplan_last 40 | 41 | spool off 42 | 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_15.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_15.txt 10 | 11 | select lpad(' ', level*2-1,' ') || e.last_name last_name, level 12 | from hr.employees e 13 | connect by prior e.employee_id = e.manager_id 14 | start with e.manager_id is null 15 | order siblings by e.last_name 16 | 17 | l 18 | 19 | / 20 | 21 | @showplan_last 22 | 23 | spool off 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_16.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_16.txt 10 | 11 | 12 | with emp_recurse(employee_id,manager_id,last_name,lvl) as ( 13 | select e.employee_id, null, e.last_name, 1 as lvl 14 | from hr.employees e 15 | where e.manager_id is null 16 | union all 17 | select e1.employee_id, e1.manager_id, e1.last_name, e2.lvl + 1 as lvl 18 | from hr.employees e1 19 | join emp_recurse e2 on e2.employee_id= e1.manager_id 20 | ) 21 | search depth first by last_name set last_name_order 22 | select lpad(' ', r.lvl*2-1,' ') || r.last_name last_name, r.lvl 23 | from emp_recurse r 24 | order by last_name_order 25 | 26 | l 27 | 28 | / 29 | 30 | @showplan_last 31 | 32 | spool off 33 | 34 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_17.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_17.txt 10 | 11 | select lpad(' ',2*(level-1)) || e.last_name last_name 12 | , sys_connect_by_path(last_name,':') path 13 | from hr.employees e 14 | start with e.manager_id is null 15 | connect by prior e.employee_id = e.manager_id 16 | order siblings by e.last_name 17 | 18 | l 19 | 20 | / 21 | 22 | @showplan_last 23 | 24 | spool off 25 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_18.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_18.txt 10 | 11 | 12 | with emp_recurse(employee_id,manager_id,last_name,lvl,path) as ( 13 | select e.employee_id, null, e.last_name 14 | , 1 as lvl 15 | ,':' || to_char(e.last_name) as path 16 | from hr.employees e 17 | where e.manager_id is null 18 | union all 19 | select e1.employee_id, e1.manager_id, e1.last_name 20 | ,e2.lvl + 1 as lvl 21 | ,e2.path || ':' || e1.last_name as path 22 | from hr.employees e1 23 | join emp_recurse e2 on e2.employee_id= e1.manager_id 24 | ) 25 | search depth first by last_name set last_name_order 26 | select lpad(' ', r.lvl*2-1,' ') || r.last_name last_name, r.path 27 | from emp_recurse r 28 | order by last_name_order 29 | 30 | l 31 | 32 | / 33 | 34 | @showplan_last 35 | 36 | spool off 37 | 38 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_19.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col last_name format a25 7 | col first_name format a20 8 | 9 | spool l_10_19.txt 10 | 11 | 12 | with emp_recurse(employee_id,manager_id,last_name,lvl,path) as ( 13 | select e.employee_id, null, e.last_name 14 | , 1 as lvl 15 | ,e.last_name as path 16 | from hr.employees e 17 | where e.manager_id is null 18 | union all 19 | select e1.employee_id, e1.manager_id, e1.last_name 20 | ,e2.lvl + 1 as lvl 21 | ,e2.path || ',' || e1.last_name as path 22 | from hr.employees e1 23 | join emp_recurse e2 on e2.employee_id= e1.manager_id 24 | ) 25 | search depth first by last_name set last_name_order 26 | select lpad(' ', r.lvl*2-1,' ') || r.last_name last_name, r.path 27 | from emp_recurse r 28 | order by last_name_order 29 | 30 | l 31 | 32 | / 33 | 34 | @showplan_last 35 | 36 | spool off 37 | 38 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_20.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a12 8 | col root format a12 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_20.txt 13 | 14 | update hr.employees set manager_id= null where last_name ='Kochhar' 15 | 16 | l 17 | 18 | / 19 | 20 | select /*+ inline gather_plan_statistics */ 21 | level 22 | , lpad(' ',2*(level-1)) || last_name last_name 23 | , first_name 24 | , CONNECT_BY_ROOT last_name as root 25 | , sys_connect_by_path(last_name,':') path 26 | from hr.employees 27 | where connect_by_root last_name = 'Kochhar' 28 | connect by prior employee_id = manager_id 29 | start with manager_id is null 30 | 31 | l 32 | 33 | / 34 | 35 | rollback; 36 | 37 | @showplan_last 38 | 39 | spool off 40 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_22.sql: -------------------------------------------------------------------------------- 1 | 2 | et linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a25 8 | col root format a15 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_22.txt 13 | 14 | 15 | update hr.employees set manager_id = 171 where employee_id = 100 16 | 17 | l 18 | 19 | / 20 | 21 | 22 | select lpad(' ',2*(level-1)) || last_name last_name 23 | ,first_name, employee_id, level 24 | from hr.employees 25 | start with employee_id = 100 26 | connect by prior employee_id = manager_id 27 | 28 | l 29 | 30 | / 31 | 32 | rollback 33 | 34 | l 35 | 36 | / 37 | 38 | --@showplan_last 39 | 40 | spool off 41 | 42 | 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_23.sql: -------------------------------------------------------------------------------- 1 | 2 | et linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a25 8 | col root format a15 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_23.txt 13 | 14 | 15 | update hr.employees set manager_id = 171 where employee_id = 100 16 | 17 | l 18 | 19 | / 20 | 21 | 22 | select lpad(' ',2*(level-1)) || last_name last_name 23 | ,first_name, employee_id, level 24 | , connect_by_iscycle 25 | from hr.employees 26 | start with employee_id = 100 27 | connect by nocycle prior employee_id = manager_id 28 | 29 | l 30 | 31 | / 32 | 33 | select last_name, first_name, employee_id, manager_id 34 | from hr.employees 35 | where employee_id = 171 36 | 37 | l 38 | 39 | / 40 | 41 | select last_name, first_name, employee_id, manager_id 42 | from hr.employees 43 | where manager_id = 171 44 | 45 | l 46 | 47 | / 48 | 49 | rollback 50 | 51 | l 52 | 53 | / 54 | 55 | --@showplan_last 56 | 57 | spool off 58 | 59 | 60 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_25.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a25 8 | col root format a15 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_25.txt 13 | 14 | select lpad(' ',2*(level-1)) || e.last_name last_name, connect_by_isleaf 15 | from hr.employees e 16 | start with e.manager_id is null 17 | connect by prior e.employee_id = e.manager_id 18 | order siblings by e.last_name 19 | 20 | l 21 | 22 | / 23 | 24 | @showplan_last 25 | 26 | spool off 27 | 28 | 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_26.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a25 8 | col root format a15 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_26.txt 13 | 14 | with leaves as ( 15 | select employee_id 16 | from hr.employees 17 | where employee_id not in ( 18 | select manager_id 19 | from hr.employees 20 | where manager_id is not null 21 | ) 22 | ), 23 | emp(manager_id,employee_id,last_name,lvl,isleaf) as ( 24 | select e.manager_id, e.employee_id, e.last_name, 1 as lvl, 0 as isleaf 25 | from hr.employees e 26 | where e.manager_id is null 27 | union all 28 | select e.manager_id, nvl(e.employee_id,null) employee_id, e.last_name, emp.lvl + 1 as lvl 29 | , decode(l.employee_id,null,0,1) isleaf 30 | from hr.employees e 31 | join emp on emp.employee_id = e.manager_id 32 | left outer join leaves l on l.employee_id = e.employee_id 33 | ) 34 | search depth first by last_name set order1 35 | select lpad(' ',2*(lvl-1)) || last_name last_name, isleaf 36 | from emp 37 | 38 | l 39 | 40 | / 41 | 42 | @showplan_last 43 | 44 | spool off 45 | 46 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_27.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a25 8 | col root format a15 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_27.txt 13 | 14 | with emp(manager_id,employee_id,last_name,lvl) as ( 15 | select e.manager_id, e.employee_id, e.last_name, 1 as lvl 16 | from hr.employees e 17 | where e.manager_id is null 18 | union all 19 | select e.manager_id, nvl(e.employee_id,null) employee_id 20 | , e.last_name, emp.lvl + 1 as lvl 21 | from hr.employees e 22 | join emp on emp.employee_id = e.manager_id 23 | ) 24 | search depth first by last_name set last_name_order 25 | select lpad(' ',2*(lvl-1)) || last_name last_name, 26 | lvl, 27 | lead(lvl) over (order by last_name_order) leadlvlorder, 28 | case 29 | when ( lvl - lead(lvl) over (order by last_name_order) ) < 0 30 | then 0 31 | else 1 32 | end isleaf 33 | from emp 34 | 35 | l 36 | 37 | / 38 | 39 | @showplan_last 40 | 41 | spool off 42 | 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_28.sql: -------------------------------------------------------------------------------- 1 | 2 | set linesize 200 3 | set serveroutput off 4 | set timing on 5 | 6 | col first_name format a12 7 | col last_name format a25 8 | col root format a15 9 | col level format 9999 10 | col path format a30 11 | 12 | spool l_10_28.txt 13 | 14 | with emp(manager_id,employee_id,last_name,lvl) as ( 15 | select e.manager_id, e.employee_id, e.last_name, 1 as lvl 16 | from hr.employees e 17 | where e.manager_id is null 18 | union all 19 | select e.manager_id, nvl(e.employee_id,null) employee_id 20 | , e.last_name, emp.lvl + 1 as lvl 21 | from hr.employees e 22 | join emp on emp.employee_id = e.manager_id 23 | ) 24 | search breadth first by last_name set last_name_order 25 | select lpad(' ',2*(lvl-1)) || last_name last_name, 26 | lvl, 27 | lead(lvl) over (order by last_name_order) leadlvlorder, 28 | case 29 | when ( lvl - lead(lvl) over (order by last_name_order) ) < 0 30 | then 0 31 | else 1 32 | end isleaf 33 | from emp 34 | 35 | l 36 | 37 | / 38 | 39 | @showplan_last 40 | 41 | spool off 42 | 43 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_3.sql: -------------------------------------------------------------------------------- 1 | 2 | set timing on 3 | set autotrace on statistics 4 | 5 | set pagesize 60 6 | set serveroutput off 7 | set line 200 8 | col cust_income_level format a20 head 'INCOME LEVEL' 9 | col country_name format a30 head 'COUNTRY' 10 | col country_cust_count format 999999 head 'CUSTOMER|COUNT' 11 | 12 | clear breaks 13 | 14 | spool l_10_3.txt 15 | 16 | with cust as ( 17 | select /*+ materialize gather_plan_statistics */ 18 | b.cust_income_level, 19 | a.country_name 20 | from sh.customers b 21 | join sh.countries a on a.country_id = b.country_id 22 | ) 23 | select country_name, cust_income_level, count(country_name) country_cust_count 24 | from cust c 25 | having count(country_name) > 26 | ( 27 | select count(*) * .01 28 | from cust c2 29 | ) 30 | or count(cust_income_level) >= 31 | ( 32 | select median(income_level_count) 33 | from ( 34 | select cust_income_level, count(*) *.25 income_level_count 35 | from cust 36 | group by cust_income_level 37 | ) 38 | ) 39 | group by country_name, cust_income_level 40 | order by 1,2 41 | 42 | l 43 | 44 | / 45 | 46 | @showplan_last 47 | 48 | spool off 49 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_4.sql: -------------------------------------------------------------------------------- 1 | 2 | set timing on 3 | set autotrace on statistics 4 | 5 | set pagesize 60 6 | set serveroutput off 7 | set line 200 8 | col cust_income_level format a20 head 'INCOME LEVEL' 9 | col country_name format a30 head 'COUNTRY' 10 | col country_cust_count format 999999 head 'CUSTOMER|COUNT' 11 | 12 | clear breaks 13 | 14 | spool l_10_4.txt 15 | 16 | with cust as ( 17 | select /*+ inline gather_plan_statistics */ 18 | b.cust_income_level, 19 | a.country_name 20 | from sh.customers b 21 | join sh.countries a on a.country_id = b.country_id 22 | ) 23 | select country_name, cust_income_level, count(country_name) country_cust_count 24 | from cust c 25 | having count(country_name) > 26 | ( 27 | select count(*) * .01 28 | from cust c2 29 | ) 30 | or count(cust_income_level) >= 31 | ( 32 | select median(income_level_count) 33 | from ( 34 | select cust_income_level, count(*) *.25 income_level_count 35 | from cust 36 | group by cust_income_level 37 | ) 38 | ) 39 | group by country_name, cust_income_level 40 | order by 1,2 41 | 42 | l 43 | 44 | / 45 | 46 | @showplan_last 47 | 48 | spool off 49 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/l_10_5.sql: -------------------------------------------------------------------------------- 1 | 2 | place holder. 3 | 4 | There is no script for this listing. 5 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/README: -------------------------------------------------------------------------------- 1 | 2 | similar to run_stats, but runs multiple processes to run the same code. 3 | collects and compares the results from 2 runs. 4 | 5 | edit defaults.sql 6 | change username to name of account you will be testing with. 7 | 8 | edit th.conf - sets the database, username and password 9 | 10 | edit thc1.sql and thc2.sql - the code to execute 11 | 12 | login as sys 13 | 14 | @../run_stats/grants.sql 15 | @mview.sql 16 | 17 | login as user 18 | 19 | @create.sql 20 | @../run_stats/get_epoch_microseconds.sql 21 | 22 | @test_harness_m.sql - run the code to compare 23 | 24 | it also runs: 25 | run_stats.sql - will compare the stats 26 | insert_results.sql - data for comparison 27 | 28 | sr.sql - compare results 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/child_wait.sh: -------------------------------------------------------------------------------- 1 | 2 | while : 3 | do 4 | if [ -f ./child.lock ]; then 5 | sleep 1; 6 | else 7 | exit 8 | fi 9 | done 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/create.sql: -------------------------------------------------------------------------------- 1 | 2 | @@drop 3 | 4 | -- create run_stats table 5 | @ /home/jkstill/oracle/dba/run_stats/table.sql 6 | 7 | create table semaphore ( systime date ); 8 | create table exit_semaphore ( systime date ); 9 | 10 | create table results( 11 | child_count number(6,0) not null, 12 | name varchar2(80) not null, 13 | run1 number, 14 | run2 number, 15 | diff number 16 | ) 17 | / 18 | 19 | 20 | create table my_sess_events ( 21 | id varchar2(10) not null, 22 | event varchar2(64) not null, 23 | time_waited_micro number 24 | ) 25 | / 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/defaults.sql: -------------------------------------------------------------------------------- 1 | 2 | define username='jkstill' 3 | 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/drop.sql: -------------------------------------------------------------------------------- 1 | 2 | drop table semaphore cascade constraints; 3 | drop table exit_semaphore cascade constraints; 4 | drop table results cascade constraints; 5 | drop table run_stats cascade constraints; 6 | drop table my_sess_events cascade constraints; 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/get_epoch_microseconds.sql: -------------------------------------------------------------------------------- 1 | 2 | -- get_epoch_microsends.sql 3 | 4 | -- return the time as the number of seconds since 1970 5 | -- plus the microseconds of the current second 6 | -- requires oracle 9i 7 | 8 | create or replace function get_epoch_microseconds return number 9 | is 10 | v_date date; 11 | v_epoch_secs number default 0; 12 | v_day_secs number default 0; 13 | v_hour_secs number default 0; 14 | v_10k_secs number(8,6) default 0; 15 | v_total_secs number(22,6) default 0; 16 | begin 17 | 18 | v_date := sysdate; 19 | 20 | v_epoch_secs := 86400*(trunc(v_date) - to_date('1970','yyyy')); 21 | v_day_secs := 86400*(trunc(v_date,'hh24') - trunc(v_date)); 22 | v_hour_secs := 86400*(trunc(v_date,'mi') - trunc(v_date,'hh24')); 23 | 24 | v_10k_secs := to_number(to_char(systimestamp, 'ss.ff6')) ; 25 | 26 | v_total_secs := v_epoch_secs + v_day_secs + v_hour_secs + v_10k_secs; 27 | 28 | return v_total_secs; 29 | 30 | end; 31 | / 32 | 33 | show errors function get_epoch_microseconds 34 | 35 | col get_epoch_microseconds format 9999999999.999999 36 | 37 | select get_epoch_microseconds from dual; 38 | 39 | 40 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/insert_results.sql: -------------------------------------------------------------------------------- 1 | 2 | truncate table results; 3 | 4 | insert into results( child_count, name, run1, run2, diff) 5 | select to_number('&&child_count') child_count, a.name name, b.value-a.value run1, c.value-b.value run2, 6 | ( (c.value-b.value)-(b.value-a.value)) diff 7 | from run_stats a, run_stats b, run_stats c 8 | where a.name = b.name 9 | and b.name = c.name 10 | and a.runid = 'before' 11 | and b.runid = 'after 1' 12 | and c.runid = 'after 2' 13 | and (c.value-a.value) > 0 14 | and (c.value-b.value) <> (b.value-a.value) 15 | order by abs( (c.value-b.value)-(b.value-a.value)) 16 | / 17 | 18 | commit; 19 | 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/mse.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/pro-oracle-sql/bbcb26062e09482a655acdb2dd02422152265b20/Pro Oracle SQL/chapter10_scripts/sf_run_stats/mse.sql -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/mse2.sql: -------------------------------------------------------------------------------- 1 | select 2 | av.event 3 | , nvl(r1.time_waited_seconds,0) time_waited_seconds_1 4 | , nvl(r2.time_waited_seconds,0) time_waited_seconds_2 5 | , nvl(r1.time_waited_seconds,0) - nvl(r2.time_waited_seconds,0) time_diff 6 | from 7 | ( 8 | select distinct event 9 | from my_sess_events 10 | ) av 11 | left outer join 12 | ( 13 | select s.id, s.event, s.time_waited_micro/1000000 time_waited_seconds 14 | from my_sess_events s 15 | where s.id = 'RUN_1' 16 | ) r1 on r1.event = av.event 17 | left outer join ( 18 | select s.id, s.event, s.time_waited_micro/1000000 time_waited_seconds 19 | from my_sess_events s 20 | where s.id = 'RUN_2' 21 | ) r2 on r2.event = av.event 22 | order by event 23 | / 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/mview.sql: -------------------------------------------------------------------------------- 1 | 2 | -- view.sql 3 | -- run as SYS 4 | 5 | @@defaults 6 | 7 | create or replace view allstats 8 | as select 'STAT...' || a.name name, value 9 | from v$statname a, v$sysstat b --, v$session c 10 | where a.statistic# = b.statistic# 11 | union all 12 | select 'LATCH.' || name, gets 13 | from v$latch 14 | union all 15 | select 'EVENT..' || e.event name, sum(time_waited)/100 value 16 | from v$system_event e 17 | group by event 18 | ; 19 | 20 | create public synonym allstats for sys.allstats; 21 | 22 | grant select on allstats to public; 23 | 24 | -- grant to user as well, if needed in a stored proc 25 | grant select on allstats to &&username; 26 | 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/run_stats.sql: -------------------------------------------------------------------------------- 1 | 2 | -- run_stats.sql 3 | -- query the stats created by test harness 4 | -- from Tom Kyte - asktom.oracle.com/~tkyte/runstats.html 5 | -- see ~/oracle/dba/run_stats for all files 6 | 7 | set linesize 80 8 | set pagesize 60 9 | col name format a40 10 | 11 | select a.name, b.value-a.value run1, c.value-b.value run2, 12 | ( (c.value-b.value)-(b.value-a.value)) diff 13 | from run_stats a, run_stats b, run_stats c 14 | where a.name = b.name 15 | and b.name = c.name 16 | and a.runid = 'before' 17 | and b.runid = 'after 1' 18 | and c.runid = 'after 2' 19 | and (c.value-a.value) > 0 20 | and (c.value-b.value) <> (b.value-a.value) 21 | order by abs( (c.value-b.value)-(b.value-a.value)) 22 | / 23 | 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/se.sql: -------------------------------------------------------------------------------- 1 | select 'RUN_1', se.event, sum(se.time_waited_micro) 2 | from v$session_event se, v$session sess 3 | where sess.sid = se.sid 4 | and sess.module = 'RUN_1' 5 | group by se.event 6 | / 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/sr.sql: -------------------------------------------------------------------------------- 1 | 2 | @clears 3 | 4 | set line 200 5 | set pagesize 50000 6 | 7 | col name format a30 wrap 8 | col child_count format 999 head 'CHILD|COUNT' 9 | col run1 format 99,999,999,999 head 'MATERIALIZE' 10 | col run2 format 99,999,999,999 head 'INLINE' 11 | col diff format 999,999,999,999 12 | 13 | --select name, child_count, run1, run2, diff 14 | select name, run1, run2, diff 15 | from results 16 | where abs(decode(least(run1,run2),0,.0001, least(run1,run2)) / greatest(run1,run2) ) < .1 17 | and abs(diff) > 100 18 | order by abs(diff) 19 | / 20 | 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/th.conf: -------------------------------------------------------------------------------- 1 | 2 | USERNAME=scott 3 | PASSWORD=tiger 4 | DATABASE=orcl 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/thc1.sh: -------------------------------------------------------------------------------- 1 | 2 | # get number of processes to run from cmd line 3 | 4 | if [ -z $1 ]; then 5 | #default to 5 6 | MAX_CHILD_COUNT=5 7 | else 8 | MAX_CHILD_COUNT=$1 9 | fi 10 | 11 | echo Running $MAX_CHILD_COUNT children 12 | 13 | # force parent to wait 14 | touch ./child.lock 15 | 16 | . ./th.conf 17 | 18 | unset SQLPATH 19 | 20 | CHILD_COUNT=0 21 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 22 | do 23 | (( CHILD_COUNT = CHILD_COUNT + 1)) 24 | sqlplus -s $USERNAME/$PASSWORD@$DATABASE @thc1.sql $CHILD_COUNT 2>/dev/null & 25 | done 26 | 27 | CHILD_COUNT=0 28 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 29 | do 30 | (( CHILD_COUNT = CHILD_COUNT + 1)) 31 | LOCK_FILE=child_${CHILD_COUNT}.lock 32 | while [ ! -f $LOCK_FILE ] 33 | do 34 | sleep 1; 35 | done 36 | rm $LOCK_FILE 37 | done 38 | 39 | # only remove main lock file after all child lock 40 | # files removed 41 | rm ./child.lock 42 | 43 | 44 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats/thc2.sh: -------------------------------------------------------------------------------- 1 | 2 | # get number of processes to run from cmd line 3 | 4 | if [ -z $1 ]; then 5 | #default to 5 6 | MAX_CHILD_COUNT=5 7 | else 8 | MAX_CHILD_COUNT=$1 9 | fi 10 | 11 | echo Running $MAX_CHILD_COUNT children 12 | 13 | # force parent to wait 14 | touch ./child.lock 15 | 16 | . ./th.conf 17 | 18 | unset SQLPATH 19 | 20 | CHILD_COUNT=0 21 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 22 | do 23 | (( CHILD_COUNT = CHILD_COUNT + 1)) 24 | sqlplus -s $USERNAME/$PASSWORD@$DATABASE @thc2.sql $CHILD_COUNT 2>/dev/null & 25 | done 26 | 27 | CHILD_COUNT=0 28 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 29 | do 30 | (( CHILD_COUNT = CHILD_COUNT + 1)) 31 | LOCK_FILE=child_${CHILD_COUNT}.lock 32 | while [ ! -f $LOCK_FILE ] 33 | do 34 | sleep 1; 35 | done 36 | rm $LOCK_FILE 37 | done 38 | 39 | # only remove main lock file after all child lock 40 | # files removed 41 | rm ./child.lock 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/README: -------------------------------------------------------------------------------- 1 | 2 | run_stats_load 3 | 4 | Used to put a load on the database 5 | run under a different user account than the run_stats 6 | being used for testing 7 | 8 | --- 9 | 10 | similar to run_stats, but runs multiple processes to run the same code. 11 | collects and compares the results from 2 runs. 12 | 13 | edit defaults.sql 14 | change username to name of account you will be testing with. 15 | 16 | edit th.conf - sets the database, username and password 17 | 18 | edit thc1.sql and thc2.sql - the code to execute 19 | 20 | login as sys 21 | 22 | @../run_stats/grants.sql 23 | @mview.sql 24 | 25 | login as user 26 | 27 | @create.sql 28 | @../run_stats/get_epoch_microseconds.sql 29 | 30 | @test_harness_m.sql - run the code to compare 31 | 32 | it also runs: 33 | run_stats.sql - will compare the stats 34 | insert_results.sql - data for comparison 35 | 36 | sr.sql - compare results 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/child_wait.sh: -------------------------------------------------------------------------------- 1 | 2 | while : 3 | do 4 | if [ -f ./child.lock ]; then 5 | sleep 1; 6 | else 7 | exit 8 | fi 9 | done 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/create.sql: -------------------------------------------------------------------------------- 1 | 2 | @@drop 3 | 4 | -- create run_stats table 5 | @ ../run_stats/table.sql 6 | 7 | create table semaphore ( systime date ); 8 | create table exit_semaphore ( systime date ); 9 | 10 | create table results( 11 | child_count number(6,0) not null, 12 | name varchar2(60) not null, 13 | run1 number, 14 | run2 number, 15 | diff number 16 | ) 17 | / 18 | 19 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/d1.sql: -------------------------------------------------------------------------------- 1 | select 2 | dummy 3 | , ( select dummy from dual d where d.dummy = d1.dummy) 4 | from dual d1 5 | / 6 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/defaults.sql: -------------------------------------------------------------------------------- 1 | 2 | define username='js001292' 3 | 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/drop.sql: -------------------------------------------------------------------------------- 1 | 2 | drop table semaphore cascade constraints; 3 | drop table exit_semaphore cascade constraints; 4 | drop table results cascade constraints; 5 | drop table run_stats cascade constraints; 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/insert_results.sql: -------------------------------------------------------------------------------- 1 | 2 | truncate table results; 3 | 4 | insert into results( child_count, name, run1, run2, diff) 5 | select to_number('&&child_count') child_count, a.name name, b.value-a.value run1, c.value-b.value run2, 6 | ( (c.value-b.value)-(b.value-a.value)) diff 7 | from run_stats a, run_stats b, run_stats c 8 | where a.name = b.name 9 | and b.name = c.name 10 | and a.runid = 'before' 11 | and b.runid = 'after 1' 12 | and c.runid = 'after 2' 13 | and (c.value-a.value) > 0 14 | and (c.value-b.value) <> (b.value-a.value) 15 | order by abs( (c.value-b.value)-(b.value-a.value)) 16 | / 17 | 18 | commit; 19 | 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/mview.sql: -------------------------------------------------------------------------------- 1 | 2 | -- view.sql 3 | -- run as SYS 4 | 5 | @@defaults 6 | 7 | create or replace view allstats 8 | as select 'STAT...' || a.name name, value 9 | from v$statname a, v$sysstat b --, v$session c 10 | where a.statistic# = b.statistic# 11 | union all 12 | select 'LATCH.' || name, gets 13 | from v$latch 14 | union all 15 | select 'EVENT..' || e.event name, sum(time_waited)/100 value 16 | from v$system_event e 17 | group by event 18 | ; 19 | 20 | create public synonym allstats for sys.allstats; 21 | 22 | grant select on allstats to public; 23 | 24 | -- grant to user as well, if needed in a stored proc 25 | grant select on allstats to &&username; 26 | 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/run_stats.sql: -------------------------------------------------------------------------------- 1 | 2 | -- run_stats.sql 3 | -- query the stats created by test harness 4 | -- from Tom Kyte - asktom.oracle.com/~tkyte/runstats.html 5 | -- see ~/oracle/dba/run_stats for all files 6 | 7 | set linesize 80 8 | set pagesize 60 9 | col name format a40 10 | 11 | select a.name, b.value-a.value run1, c.value-b.value run2, 12 | ( (c.value-b.value)-(b.value-a.value)) diff 13 | from run_stats a, run_stats b, run_stats c 14 | where a.name = b.name 15 | and b.name = c.name 16 | and a.runid = 'before' 17 | and b.runid = 'after 1' 18 | and c.runid = 'after 2' 19 | and (c.value-a.value) > 0 20 | and (c.value-b.value) <> (b.value-a.value) 21 | order by abs( (c.value-b.value)-(b.value-a.value)) 22 | / 23 | 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/sr.sql: -------------------------------------------------------------------------------- 1 | 2 | @clears 3 | 4 | set line 200 5 | 6 | select name, child_count, run1, run2, diff 7 | from results 8 | where abs(decode(least(run1,run2),0,.0001, least(run1,run2)) / greatest(run1,run2) ) < .1 9 | and abs(diff) > 100 10 | order by abs(diff) 11 | / 12 | 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/th.conf: -------------------------------------------------------------------------------- 1 | 2 | USERNAME=load 3 | PASSWORD=load 4 | DATABASE=jks1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/thc1.sh: -------------------------------------------------------------------------------- 1 | 2 | # get number of processes to run from cmd line 3 | 4 | if [ -z $1 ]; then 5 | #default to 5 6 | MAX_CHILD_COUNT=5 7 | else 8 | MAX_CHILD_COUNT=$1 9 | fi 10 | 11 | echo Running $MAX_CHILD_COUNT children 12 | 13 | # force parent to wait 14 | touch ./child.lock 15 | 16 | . ./th.conf 17 | 18 | unset SQLPATH 19 | 20 | CHILD_COUNT=0 21 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 22 | do 23 | (( CHILD_COUNT = CHILD_COUNT + 1)) 24 | sqlplus -s $USERNAME/$PASSWORD@$DATABASE @thc1.sql $CHILD_COUNT 2>/dev/null & 25 | done 26 | 27 | CHILD_COUNT=0 28 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 29 | do 30 | (( CHILD_COUNT = CHILD_COUNT + 1)) 31 | LOCK_FILE=child_${CHILD_COUNT}.lock 32 | while [ ! -f $LOCK_FILE ] 33 | do 34 | sleep 1; 35 | done 36 | rm $LOCK_FILE 37 | done 38 | 39 | # only remove main lock file after all child lock 40 | # files removed 41 | rm ./child.lock 42 | 43 | 44 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/thc2.sh: -------------------------------------------------------------------------------- 1 | 2 | # get number of processes to run from cmd line 3 | 4 | if [ -z $1 ]; then 5 | #default to 5 6 | MAX_CHILD_COUNT=5 7 | else 8 | MAX_CHILD_COUNT=$1 9 | fi 10 | 11 | echo Running $MAX_CHILD_COUNT children 12 | 13 | # force parent to wait 14 | touch ./child.lock 15 | 16 | . ./th.conf 17 | 18 | unset SQLPATH 19 | 20 | CHILD_COUNT=0 21 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 22 | do 23 | (( CHILD_COUNT = CHILD_COUNT + 1)) 24 | sqlplus -s $USERNAME/$PASSWORD@$DATABASE @thc2.sql $CHILD_COUNT 2>/dev/null & 25 | done 26 | 27 | CHILD_COUNT=0 28 | while [ $CHILD_COUNT -lt $MAX_CHILD_COUNT ] 29 | do 30 | (( CHILD_COUNT = CHILD_COUNT + 1)) 31 | LOCK_FILE=child_${CHILD_COUNT}.lock 32 | while [ ! -f $LOCK_FILE ] 33 | do 34 | sleep 1; 35 | done 36 | rm $LOCK_FILE 37 | done 38 | 39 | # only remove main lock file after all child lock 40 | # files removed 41 | rm ./child.lock 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/x.sql: -------------------------------------------------------------------------------- 1 | select * from allstats 2 | where name = 'LATCH.cache buffers chains' 3 | / 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/x1.sql: -------------------------------------------------------------------------------- 1 | select /*+ gather_plan_statistics */ 2 | p.prod_name 3 | , sum(q1.unit_cost) 4 | , sum(q2.unit_cost) 5 | , sum(q3.unit_cost) 6 | from sh.products p 7 | , ( select prod_id, unit_cost from sh.costs where time_id between to_date('01/01/2000','mm/dd/yyyy') and to_date('03/31/2000')) q1 8 | , ( select prod_id, unit_cost from sh.costs where time_id between to_date('04/01/2000','mm/dd/yyyy') and to_date('06/30/2000')) q2 9 | , ( select prod_id, unit_cost from sh.costs where time_id between to_date('07/01/2000','mm/dd/yyyy') and to_date('09/30/2000')) q3 10 | where q1.prod_id = p.prod_id 11 | and q2.prod_id = p.prod_id 12 | and q3.prod_id = p.prod_id 13 | group by p.prod_name 14 | order by p.prod_name 15 | / 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/x2.sql: -------------------------------------------------------------------------------- 1 | 2 | col runid format a10 3 | col name format a30 4 | col value format 999,999,999,999 5 | 6 | select runid, name, value 7 | from run_stats 8 | where name = 'LATCH.cache buffers chains' 9 | / 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter10_scripts/sf_run_stats_load/x3.sql: -------------------------------------------------------------------------------- 1 | 2 | 3 | col value format 999,999,999,999 4 | 5 | select runid, name, value 6 | from run_stats a, run_stats b, run_stats c 7 | where a.name = 'LATCH.cache buffers chains' 8 | and b.name = 'LATCH.cache buffers chains' 9 | and c.name = 'LATCH.cache buffers chains' 10 | / 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex.sql: -------------------------------------------------------------------------------- 1 | select /* not in */ department_name 2 | from hr.departments 3 | where department_id not in (select department_id from hr.employees); 4 | 5 | select /* not exists */ department_name 6 | from hr.departments dept 7 | where not exists (select null from hr.employees emp 8 | where emp.department_id = dept.department_id); 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex1.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- anti_ex1.sql 3 | 4 | select /* IN */ department_name 5 | from hr.departments dept 6 | where department_id not in (select department_id from hr.employees emp); 7 | 8 | select /* EXISTS */ department_name 9 | from hr.departments dept 10 | where not exists (select null from hr.employees emp 11 | where emp.department_id = dept.department_id); 12 | set echo off 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex1a.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | /bin/bash: ./junk.sql: Permission denied 3 | -- anti_ex1a.sql 4 | 5 | select /* using in */ department_name 6 | from hr.departments dept 7 | where department_id not in (select department_id from hr.employees emp); 8 | 9 | set echo off 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex1b.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- anti_ex1b.sql 3 | 4 | select /* using exists */ department_name 5 | from hr.departments dept 6 | where not exists (select null from hr.employees emp 7 | where emp.department_id = dept.department_id); 8 | set echo off 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex2.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- anti_ex2.sql 3 | 4 | select /* IN */ department_name 5 | from hr.departments dept 6 | where department_id not in (select department_id from hr.employees emp); 7 | 8 | select /* EXISTS */ department_name 9 | from hr.departments dept 10 | where not exists (select null from hr.employees emp 11 | where emp.department_id = dept.department_id); 12 | 13 | select /* IN with NVL */ department_name 14 | from hr.departments dept 15 | where department_id not in (select nvl(department_id,-10) from hr.employees emp); 16 | 17 | select /* IN with NOT NULL */ department_name 18 | from hr.departments dept 19 | where department_id not in (select department_id from hr.employees emp where department_id is not null); 20 | 21 | set echo off 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex2a.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- anti_ex2a.sql 3 | 4 | select /* IN */ department_name 5 | from hr.departments dept 6 | where department_id not in (select department_id from hr.employees emp); 7 | set echo off 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex3.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- anti_ex3.sql 3 | 4 | select /* IN */ department_name 5 | from hr.departments dept 6 | where department_id not in (select /*+ nl_aj */ department_id from hr.employees emp); 7 | 8 | select /* EXISTS */ department_name 9 | from hr.departments dept 10 | where not exists (select /*+ nl_aj */ null from hr.employees emp 11 | where emp.department_id = dept.department_id); 12 | 13 | set echo off 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex4.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- anti_ex4.sql 3 | 4 | select /* EXISTS */ department_name 5 | from hr.departments dept 6 | where not exists (select null from hr.employees emp 7 | where emp.department_id = dept.department_id); 8 | 9 | select /* EXISTS with hint */ department_name 10 | from hr.departments dept 11 | where not exists (select /*+ hash_aj */ null from hr.employees emp 12 | where emp.department_id = dept.department_id); 13 | 14 | select /* IN */ department_name 15 | from hr.departments dept 16 | where department_id not in (select department_id from hr.employees emp); 17 | 18 | alter session set "_always_anti_join"=off; 19 | 20 | select /* IN with AAJ=OFF*/ department_name 21 | from hr.departments dept 22 | where department_id not in (select department_id from hr.employees emp); 23 | 24 | alter session set "_always_anti_join"=choose; 25 | 26 | set echo off 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex5.sql: -------------------------------------------------------------------------------- 1 | 2 | set echo on 3 | -- anti_ex5.sql 4 | 5 | set autotrace trace exp 6 | 7 | select /* NOT IN */ department_name 8 | from hr.departments dept 9 | where department_id not in (select department_id from hr.employees emp); 10 | 11 | select /* NOT NULL */ department_name 12 | from hr.departments dept 13 | where department_id not in (select department_id from hr.employees emp 14 | where department_id is not null); 15 | 16 | select /* NVL */ department_name 17 | from hr.departments dept 18 | where department_id not in (select nvl(department_id,'-10') 19 | from hr.employees emp); 20 | 21 | set echo off 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_ex7.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex7.sql - restrictions 3 | 4 | select /* not in with or */ department_name 5 | from hr.departments 6 | where 1=2 7 | or department_id not in (select department_id from hr.employees); 8 | 9 | select /* not exists with or */ department_name 10 | from hr.departments dept 11 | where 1=2 or not exists (select null from hr.employees emp 12 | where emp.department_id = dept.department_id); 13 | 14 | select /* in with and */ department_name 15 | from hr.departments 16 | where 1=1 17 | and department_id not in (select department_id from hr.employees); 18 | 19 | select /* exists with and */ department_name 20 | from hr.departments dept 21 | where 1=1 and not exists (select null from hr.employees emp 22 | where emp.department_id = dept.department_id); 23 | set echo off 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_hints2.sql: -------------------------------------------------------------------------------- 1 | alter session set "_always_anti_join"=hash; 2 | select /* NOT IN NL HINT */ department_name 3 | from hr.departments dept 4 | where department_id not in (select /*+ nl_aj */ department_id from hr.employees emp); 5 | alter session set "_always_anti_join"=nested_loops; 6 | select /* NOT IN NL HINT */ department_name 7 | from hr.departments dept 8 | where department_id not in (select /*+ nl_aj */ department_id from hr.employees emp); 9 | alter session set "_always_anti_join"=choose; 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/anti_odd1.sql: -------------------------------------------------------------------------------- 1 | SELECT /* full outer */ d.department_id, e.employee_id 2 | FROM hr.employees e 3 | FULL OUTER JOIN hr.departments d 4 | ON e.department_id = d.department_id 5 | ORDER BY d.department_id; 6 | 7 | SELECT /* full outer anti */ /*+ NO_NATIVE_FULL_OUTER_JOIN */ 8 | d.department_id, e.employee_id 9 | FROM hr.employees e 10 | FULL OUTER JOIN hr.departments d 11 | ON e.department_id = d.department_id 12 | ORDER BY d.department_id; 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi1.sql: -------------------------------------------------------------------------------- 1 | select ename from scott.emp 2 | where deptno in (select deptno from scott.dept where loc > 'E%') 3 | / 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi2.sql: -------------------------------------------------------------------------------- 1 | select ename from scott.emp 2 | where exists (select deptno from scott.dept where loc > 'E%' and emp.deptno = dept.deptno) 3 | / 4 | select ename from scott.emp 5 | where exists (select deptno from scott.dept where emp.deptno = dept.deptno) 6 | / 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi3.sql: -------------------------------------------------------------------------------- 1 | select distinct ename from scott.emp, scott.dept 2 | where emp.deptno = dept.deptno 3 | and loc > 'E%' 4 | / 5 | 6 | select ename from scott.emp, scott.dept 7 | where emp.deptno = dept.deptno 8 | and loc > 'E%' 9 | / 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex.sql 3 | 4 | select /* using in */ department_name 5 | from hr.departments 6 | where department_id in (select department_id from hr.employees); 7 | 8 | select /* using exists */ department_name 9 | from hr.departments dept 10 | where exists (select null from hr.employees emp 11 | where emp.department_id = dept.department_id); 12 | set echo off 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex1.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex1.sql 3 | 4 | select /* using in */ department_name 5 | from hr.departments dept 6 | where department_id in (select department_id from hr.employees emp); 7 | 8 | select /* using exists */ department_name 9 | from hr.departments dept 10 | where exists (select null from hr.employees emp 11 | where emp.department_id = dept.department_id); 12 | set echo off 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex1a.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex1a.sql 3 | 4 | select /* using in */ department_name 5 | from hr.departments dept 6 | where department_id in (select department_id from hr.employees emp); 7 | 8 | set echo off 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex1b.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex1b.sql 3 | 4 | select /* using exists */ department_name 5 | from hr.departments dept 6 | where exists (select null from hr.employees emp 7 | where emp.department_id = dept.department_id); 8 | set echo off 9 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex2.sql: -------------------------------------------------------------------------------- 1 | -- semi_ex2.sql - alternatives to EXISTS and IN 2 | 3 | set echo on 4 | select /* inner join */ department_name 5 | from hr.departments dept, hr.employees emp 6 | where dept.department_id = emp.department_id; 7 | 8 | -- obviously not the same due to the number of rows returned 9 | -- let’s try throwing in a DISTINCT 10 | 11 | select /* inner join with distinct */ distinct department_name 12 | from hr.departments dept, hr.employees emp 13 | where dept.department_id = emp.department_id; 14 | 15 | -- that one looks promising 16 | -- let’s try one more with an intersect 17 | 18 | select /* ugly intersect */ department_name 19 | from hr.departments dept, 20 | (select department_id from hr.departments 21 | intersect 22 | select department_id from hr.employees) b 23 | where b.department_id = dept.department_id; 24 | 25 | -- finally there is the somewhat obscure ANY keyword 26 | 27 | select /* inner join ANY */ department_name 28 | from hr.departments dept 29 | where department_id = ANY (select department_id from hr.employees emp); 30 | set echo off 31 | 32 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex3.sql: -------------------------------------------------------------------------------- 1 | -- semi_ex3.sql - SEMI and DISTINCT not the same 2 | 3 | select /* SEMI using IN */ department_id 4 | from hr.employees 5 | where department_id in (select department_id from hr.departments); 6 | 7 | select /* inner join with distinct */ distinct emp.department_id 8 | from hr.departments dept, hr.employees emp 9 | where dept.department_id = emp.department_id; 10 | 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex4.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex4.sql – EXISTS with non-correlated subquery mistake 3 | 4 | select /* correlated */ department_id 5 | from hr.departments dept 6 | where exists (select department_id from hr.employees emp 7 | where emp.department_id = dept.department_id); 8 | 9 | select /* not correlated */ department_id 10 | from hr.departments dept 11 | where exists (select department_id from hr.employees emp ); 12 | 13 | select /* not correlated no nulls */ department_id 14 | from hr.departments dept 15 | where exists (select department_id from hr.employees emp where department_id is not null); 16 | 17 | select /* not correlated totally unrelated */ department_id 18 | from hr.departments dept 19 | where exists (select 'anything' from dual); 20 | 21 | select /* not correlated empty subquery */ department_id 22 | from hr.departments dept 23 | where exists (select 'anything' from dual where 1=2); 24 | set echo off 25 | 26 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex5.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex5.sql - no_semijoin hint 3 | 4 | select /* in no_semijoin */ department_name 5 | from hr.departments dept 6 | where department_id in (select /*+ no_semijoin */ department_id from hr.employees emp); 7 | 8 | select /* exists no_semijoin */ department_name 9 | from hr.departments dept 10 | where exists (select /*+ no_semijoin */ null from hr.employees emp 11 | where emp.department_id = dept.department_id); 12 | set echo off 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex6.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex6.sql 3 | 4 | select /* exists no_sj */ department_name 5 | from hr.departments dept 6 | where exists (select /*+ no_semijoin */ null from hr.employees emp 7 | where emp.department_id = dept.department_id); 8 | 9 | select /* exists hash_sj */ department_name 10 | from hr.departments dept 11 | where exists (select /*+ hash_sj */ null from hr.employees emp 12 | where emp.department_id = dept.department_id); 13 | 14 | select /* exists merge_sj */ department_name 15 | from hr.departments dept 16 | where exists (select /*+ merge_sj */ null from hr.employees emp 17 | where emp.department_id = dept.department_id); 18 | 19 | select /* exists nl_sj */ department_name 20 | from hr.departments dept 21 | where exists (select /*+ nl_sj */ null from hr.employees emp 22 | where emp.department_id = dept.department_id); 23 | 24 | set echo off 25 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex7.sql: -------------------------------------------------------------------------------- 1 | set echo on 2 | -- semi_ex7.sql - restrictions 3 | 4 | select /* in with or */ department_name 5 | from hr.departments 6 | where 1=2 7 | or department_id in (select department_id from hr.employees); 8 | 9 | select /* exists with or */ department_name 10 | from hr.departments dept 11 | where 1=2 or exists (select null from hr.employees emp 12 | where emp.department_id = dept.department_id); 13 | 14 | select /* in with and */ department_name 15 | from hr.departments 16 | where 1=1 17 | and department_id in (select department_id from hr.employees); 18 | 19 | select /* exists with and */ department_name 20 | from hr.departments dept 21 | where 1=1 and exists (select null from hr.employees emp 22 | where emp.department_id = dept.department_id); 23 | set echo off 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_ex9.sql: -------------------------------------------------------------------------------- 1 | select last_name from hr.employees 2 | where department_id in (select department_id from hr.departments where department_name like 'I%') 3 | / 4 | select last_name from hr.employees emp 5 | where exists (select null from hr.departments dept where emp.department_id = dept.department_id and department_name like 'I%') 6 | / 7 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_hash.sql: -------------------------------------------------------------------------------- 1 | select /* semi */ /*+ hash_sj */ count(*) 2 | from kso.big 3 | where object_id in ( select /*+ hash_sj */ object_id from kso.small ); 4 | 5 | select /* semi */ /*+ hash_sj */ count(*) 6 | from kso.big 7 | where exists ( select /*+ hash_sj */ null from kso.small where small.object_id = big.object_id ); 8 | 9 | select /* semi */ /*+ hash_sj */ count(*) 10 | from kso.small 11 | where exists ( select /*+ hash_sj */ null from kso.big where small.object_id = big.object_id ); 12 | 13 | select /* semi */ /*+ hash_sj */ count(*) 14 | from kso.small 15 | where object_id in ( select /*+ hash_sj */ object_id from kso.big ); 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_hint.sql: -------------------------------------------------------------------------------- 1 | select ename from scott.emp 2 | where exists (select /*+ hash_sj */ deptno from scott.dept where loc > 'E%' and emp.deptno = dept.deptno) 3 | / 4 | select ename from scott.emp 5 | where exists (select /*+ merge_sj */ deptno from scott.dept where loc > 'E%' and emp.deptno = dept.deptno) 6 | / 7 | select ename from scott.emp 8 | where exists (select /*+ nl_sj */ deptno from scott.dept where loc > 'E%' and emp.deptno = dept.deptno) 9 | / 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_inter.sql: -------------------------------------------------------------------------------- 1 | select /* semi big */ object_name 2 | from kso.big 3 | where exists ( select /*+ semijoin */ null from kso.small where small.object_id = big.object_id ); 4 | 5 | select /* semi intersect */ object_name 6 | from kso.big 7 | intersect 8 | select object_name from kso.small 9 | / 10 | 11 | 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter11_scripts/semi_tom2.sql: -------------------------------------------------------------------------------- 1 | select count(created) 2 | from big 3 | where object_id in ( select object_id from small ); 4 | 5 | select count(created) 6 | from big 7 | where exists ( select null from small where small.object_id = big.object_id ); 8 | 9 | select count(created) 10 | from small 11 | where exists ( select null from big where small.object_id = big.object_id ); 12 | 13 | select count(created) 14 | from small 15 | where object_id in ( select object_id from big ); 16 | 17 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_1.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Index access path 3 | PROMPT 4 | col region format A15 5 | col product format A30 6 | col week format 9999 7 | col sale format 999999.99 8 | col receipts format 999999.99 9 | set lines 120 pages 100 10 | alter session set statistics_level=all; 11 | drop index sales_fact_c2; 12 | create index sales_fact_c2 on sales_fact ( country); 13 | set head off 14 | select count(distinct(region)) from sales_fact s where country='Spain' ; 15 | @x 16 | select /*+ index ( s sales_fact_c2) */ count(distinct(region)) from sales_fact s where country='Spain' ; 17 | @x 18 | 19 | 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_10.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Distribution 3 | PROMPT 4 | 5 | select dbms_rowid.rowid_object(rowid), count(*) from sales_fact_part 6 | group by dbms_rowid.rowid_object(rowid); 7 | 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_11.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Distribution with part id 3 | PROMPT 4 | 5 | select dbms_rowid.rowid_object(rowid) obj_id, ora_hash ( id, 31, 0) part_id ,count(*) from sales_fact_part 6 | group by dbms_rowid.rowid_object(rowid), ora_hash(id,31,0) 7 | order by 1; 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_12.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Compressed index 3 | PROMPT 4 | col region format A15 5 | col product format A30 6 | col week format 9999 7 | col sale format 999999.99 8 | col receipts format 999999.99 9 | set lines 120 pages 100 10 | select * from ( 11 | select product, year,week, sale 12 | from sales_fact 13 | order by product, year,week 14 | ) where rownum <21; 15 | drop index sales_fact_c1; 16 | create index sales_fact_c1 on sales_fact ( product, year, week); 17 | set head off 18 | select 'Compressed index size (MB) :' ||trunc(bytes/1024/1024, 2) from user_segments 19 | where segment_name='SALES_FACT_C1'; 20 | set head on 21 | drop index sales_fact_c1; 22 | create index sales_fact_c1 on sales_fact ( product, year, week) 23 | compress 2; 24 | set head off 25 | select 'Compressed index size (MB) :' ||trunc(bytes/1024/1024,2) from user_segments 26 | where segment_name='SALES_FACT_C1'; 27 | set head on 28 | 29 | 30 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_14.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Function based indexes 3 | PROMPT 4 | drop index sales_fact_part_fbi1; 5 | select * from sales_fact_part where to_char(id)='1000'; 6 | @x 7 | create index sales_fact_part_fbi1 on sales_fact_part( to_char(id)) 8 | ; 9 | @analyze_table_sfp 10 | select * from sales_fact_part where to_char(id)='1000'; 11 | @x 12 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_18.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Reverse Key indexes 3 | PROMPT 4 | 5 | drop index sales_fact_part_n1; 6 | 7 | create unique index sales_fact_part_n1 on sales_fact_part ( id ) global reverse ; 8 | select * from sales_fact_part where id=1000; 9 | @x 10 | set termout off 11 | select * from sales_fact_part where id between 1000 and 1001; 12 | set termout on 13 | @x 14 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_19.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Descending indexes 3 | PROMPT 4 | 5 | column index_name format A30 6 | 7 | drop index sales_fact_c1; 8 | 9 | create index sales_fact_c1 on sales_fact ( product desc, year desc, week desc ) ; 10 | 11 | @analyze_sf.sql 12 | 13 | set termout off 14 | select year, week from sales_fact s where year in ( 1998,1999,2000) and week<5 15 | and product='Xtend Memory' 16 | order by product desc,year desc, week desc ; 17 | set termout on 18 | @x 19 | 20 | select index_name, index_type from dba_indexes 21 | where index_name='SALES_FACT_C1'; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_2.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Index access path 2 3 | PROMPT 4 | col region format A15 5 | col product format A30 6 | col week format 9999 7 | col sale format 999999.99 8 | col receipts format 999999.99 9 | set lines 120 pages 100 10 | alter session set statistics_level=all; 11 | select product, year, week from sales_fact where product='Xtend Memory' 12 | and year=1998 and week=1; 13 | @x 14 | select /*+ full(sales_fact) */ product, year, week from sales_fact where product='Xtend Memory' 15 | and year=1998 and week=1; 16 | @x 17 | 18 | 19 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_20.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Invisible indexes 3 | PROMPT 4 | 5 | column index_name format A30 6 | set lines 120 pages 100 7 | 8 | drop index sales_fact_c1; 9 | create index sales_fact_c1 on sales_fact ( product, year , week) ; 10 | 11 | set termout off 12 | select * from ( 13 | select * from sales_fact where product = 'Xtend Memory' and year=1998 and week=1 14 | ) where rownum <21 15 | ; 16 | set termout on 17 | @x 18 | alter index sales_fact_c1 invisible; 19 | set termout off 20 | select * from ( 21 | select * from sales_fact where product = 'Xtend Memory' and year=1998 and week=1 22 | ) where rownum <21 23 | ; 24 | set termout on 25 | @x 26 | 27 | alter session set optimizer_use_invisible_indexes =true; 28 | set termout off 29 | select * from ( 30 | select * from sales_fact where product = 'Xtend Memory' and year=1998 and week=1 31 | ) where rownum <21 32 | ; 33 | set termout on 34 | @x 35 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_22.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Virtual indexes 3 | PROMPT 4 | create index sales_virt on sales ( cust_id, promo_id) nosegment; 5 | alter session set "_use_nosegment_indexes"=true; 6 | explain plan for select * from sales where cust_id=:b1 and promo_id=:b2 7 | / 8 | select * from table(dbms_xplan.display(null,'','all')) 9 | / 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_23.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Bitmap join indexes 3 | PROMPT 4 | drop index sales_bji1; 5 | select sum(s.quantity_sold), sum(s.amount_sold) 6 | from sales s, products p, customers c, channels ch 7 | where s.prod_id = p.prod_id and 8 | s.cust_id = c.cust_id and 9 | s.channel_id = ch.channel_id and 10 | p.prod_name='Y box' and 11 | c.cust_first_name='Abigail' and 12 | ch.channel_desc = 'Direct_sales' 13 | / 14 | @x -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_24.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Bitmap join indexes 3 | PROMPT 4 | 5 | alter table products modify primary key validate; 6 | alter table customers modify primary key validate; 7 | alter table channels modify primary key validate; 8 | 9 | create bitmap index sales_bji1 on sales ( p.prod_name, c.cust_first_name, ch.channel_desc) 10 | from sales s, products p, customers c, channels ch 11 | where s.prod_id = p.prod_id and 12 | s.cust_id = c.cust_id and 13 | s.channel_id = ch.channel_id 14 | LOCAL 15 | / 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_3.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Nulls and indexes 3 | PROMPT 4 | drop table t1; 5 | create table t1 (n1 number, n2 varchar2(100) ); 6 | insert into t1 select object_id, object_name from dba_objects where rownum<101; 7 | commit; 8 | @analyze_table 9 | create index t1_n1 on t1(n1); 10 | select * from t1 where n1 is null; 11 | @x 12 | create index t1_n10 on t1(n1,0); 13 | select * from t1 where n1 is null; 14 | @x 15 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_4.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Bitmap indexes 3 | PROMPT 4 | drop index sales_fact_part_bm1; 5 | drop index sales_fact_part_bm2; 6 | 7 | create bitmap index sales_fact_part_bm1 on sales_fact_part ( country ) Local; 8 | create bitmap index sales_fact_part_bm2 on sales_fact_part ( region ) Local ; 9 | 10 | set termout off 11 | select * from sales_fact_part where country='Spain' and 12 | region='Western Europe' ; 13 | set termout on 14 | @x -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_5.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Index Organized Tables 3 | PROMPT 4 | 5 | drop table sales_iot; 6 | create table sales_iot 7 | ( prod_id number not null, 8 | cust_id number not null, 9 | time_id date not null, 10 | channel_id number not null, 11 | promo_id number not null, 12 | quantity_sold number (10,2) not null, 13 | amount_sold number(10,2) not null, 14 | primary key ( prod_id, cust_id, time_id, channel_id, promo_id) 15 | ) 16 | organization index ; 17 | insert into sales_iot select * from sales; 18 | commit; 19 | @analyze_table 20 | select quantity_sold, amount_sold from sales_iot where 21 | prod_id=13 and cust_id=2 and channel_id=3 and promo_id=999; 22 | @x 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_6.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Index Organized Tables - Secondary indexes 3 | PROMPT 4 | 5 | drop index sales_iot_sec ; 6 | create index sales_iot_sec on 7 | sales_iot (channel_id, time_id, promo_id, cust_id) ; 8 | 9 | select quantity_sold, amount_sold from sales_iot where 10 | channel_id=3 and promo_id=999 and cust_id=12345 and time_id='30-JAN-00'; 11 | @x 12 | 13 | col segment_name format A30 14 | select segment_name, sum( bytes/1024/1024) sz from dba_segments 15 | where segment_name in ('SALES_IOT_PK','SALES_IOT_SEC') 16 | group by segment_name 17 | / 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_7.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Local index execution plan 3 | PROMPT 4 | 5 | drop table sales_fact_part; 6 | CREATE table sales_fact_part 7 | partition by range ( year ) 8 | ( partition p_1997 values less than ( 1998) , 9 | partition p_1998 values less than ( 1999), 10 | partition p_1999 values less than (2000), 11 | partition p_2000 values less than (2001), 12 | partition p_max values less than (maxvalue) 13 | ) 14 | AS 15 | SELECT * from sales_fact; 16 | 17 | create index sales_fact_part_n1 on sales_fact_part( product, year) 18 | local; 19 | 20 | col region format A15 21 | col product format A30 22 | col week format 9999 23 | col sale format 999999.99 24 | col receipts format 999999.99 25 | set lines 120 pages 100 26 | set serveroutput off 27 | select * from ( 28 | select * from sales_fact_part where product = 'Xtend Memory' 29 | ) where rownum <21 30 | ; 31 | @x 32 | select * from ( 33 | select * from sales_fact_part where product = 'Xtend Memory' and year=1998 34 | ) where rownum <21 35 | ; 36 | @x -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_8.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Global partitioned index 3 | PROMPT 4 | 5 | create index sales_fact_part_n1 on sales_fact_part (year) 6 | global partition by range ( year) 7 | (partition p_1998 values less than (1999), 8 | partition p_2000 values less than (2001), 9 | partition p_max values less than (maxvalue) 10 | ) 11 | / 12 | 13 | col region format A15 14 | col product format A30 15 | col week format 9999 16 | col sale format 999999.99 17 | col receipts format 999999.99 18 | set lines 120 pages 100 19 | set serveroutput off 20 | 21 | select * from ( 22 | select * from sales_fact_part where product = 'Xtend Memory' and year=1998 23 | ) where rownum <21 24 | ; 25 | @x -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_9.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT Hash partitioning 3 | PROMPT 4 | drop sequence sfseq; 5 | create sequence sfseq cache 200; 6 | 7 | drop table sales_fact_part; 8 | 9 | CREATE table sales_fact_part 10 | partition by hash ( id ) 11 | partitions 32 12 | AS 13 | SELECT sfseq.nextval id , f.* from sales_fact f; 14 | 15 | create unique index sales_fact_part_n1 on sales_fact_part( id ) 16 | local; 17 | 18 | col region format A15 19 | col product format A30 20 | col week format 9999 21 | col sale format 999999.99 22 | col receipts format 999999.99 23 | set lines 120 pages 100 24 | 25 | set serveroutput off 26 | 27 | select * from sales_fact_part where id =1000; 28 | 29 | @x 30 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/Listing_12_cr_table.sql: -------------------------------------------------------------------------------- 1 | PROMPT 2 | PROMPT FACT table creation. 3 | PROMPT 4 | 5 | drop table sales_fact; 6 | CREATE table sales_fact AS 7 | SELECT country_name country,country_subRegion region, prod_name product, calendar_year year, calendar_week_number week, 8 | SUM(amount_sold) sale, 9 | sum(amount_sold* 10 | ( case 11 | when mod(rownum, 10)=0 then 1.4 12 | when mod(rownum, 5)=0 then 0.6 13 | when mod(rownum, 2)=0 then 0.9 14 | when mod(rownum,2)=1 then 1.2 15 | else 1 16 | end )) receipts 17 | FROM sales, times, customers, countries, products 18 | WHERE sales.time_id = times.time_id AND 19 | sales.prod_id = products.prod_id AND 20 | sales.cust_id = customers.cust_id AND 21 | customers.country_id = countries.country_id 22 | GROUP BY 23 | country_name,country_subRegion, prod_name, calendar_year, calendar_week_number; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/analyze_sf.sql: -------------------------------------------------------------------------------- 1 | set lines 120 pages 100 2 | 3 | begin 4 | dbms_stats.gather_table_stats ( 5 | ownname =>user, 6 | tabname=>'SALES_FACT', 7 | estimate_percent=>30, 8 | cascade=>true); 9 | end; 10 | / 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/analyze_sfp.sql: -------------------------------------------------------------------------------- 1 | set lines 120 pages 100 2 | begin 3 | dbms_stats.gather_table_stats ( 4 | ownname =>user, 5 | tabname=>'SALES_FACT_PART', 6 | estimate_percent=>30, 7 | cascade=>true); 8 | end; 9 | / 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/analyze_table.sql: -------------------------------------------------------------------------------- 1 | set lines 120 pages 100 2 | begin 3 | dbms_stats.gather_table_stats ( 4 | ownname =>user, 5 | tabname=>'&tabname', 6 | estimate_percent=>100, 7 | cascade=>true); 8 | end; 9 | / 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter12_scripts/x.sql: -------------------------------------------------------------------------------- 1 | PROMPT Explain plan.. 2 | PROMPT 3 | set lines 120 pages 100 4 | select * from table (dbms_xplan.display_cursor('','','ALL')); 5 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter13_scripts/compare_insert_merge_ctas.sql: -------------------------------------------------------------------------------- 1 | set timing on 2 | drop table skew3; 3 | set echo on 4 | @flush_pool 5 | create /* compare_insert_merge_ctas.sql */ table skew3 6 | as select * from skew; 7 | select name, value from v$mystat s, v$statname n 8 | where n.statistic# = s.statistic# and name = 'physical writes direct'; 9 | 10 | truncate table skew3 drop storage; 11 | INSERT /*+ APPEND */ /* compare_insert_merge_ctas.sql */ 12 | INTO skew3 13 | select * from skew; 14 | select name, value from v$mystat s, v$statname n 15 | where n.statistic# = s.statistic# and name = 'physical writes direct'; 16 | 17 | truncate table skew3 drop storage; 18 | MERGE /*+ APPEND */ /* compare_insert_merge_ctas.sql */ 19 | INTO skew3 t 20 | USING (select * from skew) s 21 | ON (t.pk_col = s.pk_col) 22 | WHEN NOT MATCHED THEN INSERT 23 | (t.pk_col, t.col1, t.col2, t.col3, t.col4) 24 | VALUES (s.pk_col, s.col1, s.col2, s.col3, s.col4); 25 | select name, value from v$mystat s, v$statname n 26 | where n.statistic# = s.statistic# and name = 'physical writes direct'; 27 | set echo off 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter13_scripts/constraints.sql: -------------------------------------------------------------------------------- 1 | col search_condition for a40 2 | select table_name,constraint_name,constraint_type, search_condition, status 3 | from dba_constraints 4 | where owner like nvl('&owner',owner) 5 | and table_name like nvl('&table_name',table_name) 6 | and constraint_type like nvl('&constraint_type',constraint_type) 7 | / 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter13_scripts/create_people.sql: -------------------------------------------------------------------------------- 1 | create table people 2 | (person_id number, first_name varchar2(80), last_name varchar2(80), parent_id number); 3 | 4 | create table denormalized_people 5 | (person_id number, first_name varchar2(80), last_name varchar2(80), 6 | child1 varchar2(80), 7 | child2 varchar2(80), 8 | child3 varchar2(80), 9 | child4 varchar2(80), 10 | child5 varchar2(80), 11 | child6 varchar2(80), 12 | child7 varchar2(80), 13 | child8 varchar2(80), 14 | child9 varchar2(80), 15 | child10 varchar2(80), 16 | child11 varchar2(80), 17 | child12 varchar2(80)); 18 | 19 | create sequence people_seq start with 1000; 20 | 21 | create or replace trigger people_autonumber 22 | before insert on people for each row 23 | begin 24 | if :new.person_id is null then 25 | select people_seq.nextval into :new.person_id from dual; 26 | end if; 27 | end; 28 | / 29 | 30 | insert into denormalized_people (person_id, first_name, last_name) 31 | select employee_id, first_name, last_name from hr.employees; 32 | 33 | update denormalized_people set child1 = 'Jordan', child2='Jacob', child3='Noah', child4='Lindsey'; 34 | 35 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter13_scripts/create_skew.sql: -------------------------------------------------------------------------------- 1 | create table skew 2 | (pk_col number primary key, 3 | col1 number, 4 | col2 varchar2 (30), 5 | col3 date, 6 | col4 varchar2(1)) 7 | / 8 | create sequence skew_seq 9 | / 10 | begin 11 | for i in 1..100000 loop 12 | insert into skew (pk_col, col1, col2, col3, col4) 13 | select skew_seq.nextval, skew_seq.currval, 'asddsadasd', 14 | sysdate-(skew_seq.currval/3600), 'Y' from dual; 15 | end loop; 16 | end; 17 | / 18 | select count(*) from skew; 19 | update skew set col1 = 1 where rownum < 100000; 20 | update skew set col4 = 'N' where rownum < 100; 21 | commit; 22 | 23 | create or replace package skew_package as 24 | skew_record skew%rowtype; 25 | end; 26 | / 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter13_scripts/flush_pool.sql: -------------------------------------------------------------------------------- 1 | alter system flush shared_pool; 2 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter13_scripts/mystats.sql: -------------------------------------------------------------------------------- 1 | col name for a60 2 | col value for 99999999999999 3 | select name, value 4 | from v$mystat s, v$statname n 5 | where n.statistic# = s.statistic# 6 | and name like nvl('&event_name',name) 7 | / 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/14.11_AutonomousTransaction.sql: -------------------------------------------------------------------------------- 1 | create table order_log ( customer_id number not null, 2 | order_id number not null, 3 | order_date number not null, 4 | order_outcome varchar2(10) constraint order_log_pk primary key (customer_id, order_id, order_date) 5 | ); 6 | 7 | create or replace procedure record_new_order (p_customer_id IN NUMBER, 8 | p_order_id IN NUMBER) as 9 | pragma autonomous_transaction; 10 | begin insert into order_log 11 | (customer_id, order_id, order_date) values 12 | (p_customer_id, p_order_id, sysdate); 13 | 14 | commit; 15 | end; 16 | / -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/14.3_VerifyAvailableInventory.sql: -------------------------------------------------------------------------------- 1 | select product_id, quantity_on_hand 2 | from inventories where product_id in (2255, 2274, 2537) 3 | order by product_id ; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/14.5_ConfirmTransactionUpdates.sql: -------------------------------------------------------------------------------- 1 | select order_id, customer, mobile, status, order_total, order_date 2 | from order_detail_header where order_id = 2459 ; 3 | 4 | select line_item_id ITEM, product_name, unit_price, discount_price, quantity, line_item_total from order_detail_line_items 5 | where order_id = 2459 order by line_item_id ; 6 | 7 | select product_id, quantity_on_hand 8 | from inventories where product_id in (2255, 2274, 2537) 9 | order by product_id ; 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/14.6_OrderWithTransactionIDandSCN.sql: -------------------------------------------------------------------------------- 1 | insert into orders (order_id, order_date, order_mode, order_status, customer_id, sales_rep_id) values 2 | (2459, sysdate, 'direct', 1, 141, 145) ; 3 | 4 | select current_scn from v$database ; select xid, status from v$transaction ; 5 | --- Update order total 6 | 7 | update orders 8 | set order_total = 7208 9 | where order_id = 2459; 10 | select order_id, 11 | customer, 12 | mobile, 13 | status, 14 | order_total, 15 | order_date 16 | from order_detail_header where order_id = 2459; 17 | 18 | select line_item_id, 19 | product_name, 20 | unit_price, 21 | discount_price, 22 | quantity, 23 | line_item_total 24 | from order_detail_line_items 25 | where order_id = 2459 order by line_item_id ; 26 | 27 | select current_scn from v$database ; select xid, status from v$transaction ; 28 | rollback; 29 | 30 | select current_scn from v$database ; select xid, status from v$transaction ; 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/14.8_VerifyDataAfterRollback.sql: -------------------------------------------------------------------------------- 1 | select order_id, 2 | customer, 3 | mobile, 4 | status, 5 | order_total, 6 | order_date 7 | from order_detail_header where order_id = 2459; 8 | 9 | select line_item_id, 10 | product_name, 11 | unit_price, 12 | discount_price, 13 | quantity, 14 | line_item_total from order_detail_line_items 15 | where order_id = 2459 16 | order by line_item_id ; 17 | 18 | select product_id, 19 | quantity_on_hand 20 | from inventories where product_id in (2255, 2274, 2537) 21 | order by product_id ; -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/credit_request.prc: -------------------------------------------------------------------------------- 1 | create or replace procedure credit_request(p_customer_id IN NUMBER, 2 | p_amount IN NUMBER) 3 | 4 | IS 5 | 6 | v_authorization NUMBER; 7 | 8 | BEGIN 9 | 10 | v_authorization := round(dbms_random.value(p_customer_id, p_amount), 0); 11 | 12 | -- begin dbms_lock.sleep(60); end; 13 | 14 | begin dbms_output.put_line('Customer ID = ' || p_customer_id); 15 | end; 16 | 17 | begin dbms_output.put_line('Amount = ' || p_amount); 18 | end; 19 | 20 | begin dbms_output.put_line('Authorization = ' || v_authorization); 21 | end; 22 | 23 | END credit_request; 24 | / 25 | 26 | grant execute on credit_request to oe ; 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/formats.sql: -------------------------------------------------------------------------------- 1 | set termout off 2 | alter session set nls_date_format = 'dd Mon yyyy'; 3 | 4 | column customer format a25 5 | column sales_rep format a25 6 | column mobile format a15 7 | column item format 9999 8 | column product_name format a35 9 | column unit_price format 99,999.99 10 | column discount_price format 99,999.99 11 | column line_item_total format 99,999.99 12 | column order_total format 99,999,999.99 13 | 14 | set termout on 15 | 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/get_listprice.fnc: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION Get_ListPrice(p_product_id IN product_information.product_id%TYPE) 2 | RETURN NUMBER 3 | 4 | IS 5 | 6 | v_list_price NUMBER; 7 | 8 | BEGIN 9 | 10 | IF ( (p_product_id) IS NULL ) THEN 11 | RETURN NULL; 12 | END IF; 13 | 14 | SELECT 15 | list_price 16 | INTO v_list_price 17 | FROM (SELECT list_price 18 | from product_information 19 | where product_id = p_product_id); 20 | 21 | RETURN v_list_price; 22 | 23 | EXCEPTION 24 | WHEN NO_DATA_FOUND THEN 25 | RETURN NULL; 26 | 27 | END Get_ListPrice; 28 | / 29 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/get_orderitemcount.fnc: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION Get_OrderItemCount(p_order_id IN orders.order_id%TYPE) 2 | RETURN NUMBER 3 | IS 4 | 5 | v_order_item_count NUMBER; 6 | 7 | BEGIN 8 | 9 | IF ( (p_order_id) IS NULL ) THEN 10 | RETURN NULL; 11 | END IF; 12 | 13 | SELECT ITEM_COUNT 14 | INTO v_order_item_count 15 | FROM (SELECT count(line_item_id) ITEM_COUNT 16 | from order_detail_line_items 17 | where order_id = p_order_id); 18 | 19 | RETURN v_order_item_count; 20 | 21 | EXCEPTION 22 | WHEN NO_DATA_FOUND THEN 23 | RETURN NULL; 24 | 25 | WHEN OTHERS THEN 26 | RAISE; 27 | 28 | END Get_OrderItemCount; 29 | / 30 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/get_ordertotal.fnc: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION Get_OrderTotal(p_order_id IN orders.order_id%TYPE) 2 | RETURN NUMBER 3 | IS 4 | 5 | v_order_total NUMBER; 6 | 7 | BEGIN 8 | 9 | IF ( (p_order_id) IS NULL ) THEN 10 | RETURN NULL; 11 | END IF; 12 | 13 | SELECT TOTAL 14 | INTO v_order_total 15 | FROM (SELECT sum(line_item_total) TOTAL from order_detail_line_items where order_id = p_order_id); 16 | 17 | RETURN v_order_total; 18 | 19 | EXCEPTION 20 | WHEN NO_DATA_FOUND THEN 21 | RETURN NULL; 22 | 23 | WHEN OTHERS THEN 24 | RAISE; 25 | 26 | END Get_OrderTotal; 27 | / 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter14_scripts/order_detail_views.sql: -------------------------------------------------------------------------------- 1 | create or replace view order_detail_header 2 | as 3 | select c.cust_first_name||' '||c.cust_last_name CUSTOMER, 4 | c.phone_number MOBILE, 5 | o.order_id, 6 | o.order_date, 7 | o.order_mode, 8 | os.order_status_name STATUS, 9 | o.order_total, 10 | e.last_name SALES_REP 11 | from customers c, orders o, order_status os, hr.employees e 12 | where c.customer_id = o.customer_id 13 | and o.order_status = os.order_status 14 | and o.sales_rep_id = e.employee_id 15 | order by c.cust_last_name, c.cust_first_name, o.order_id ; 16 | 17 | 18 | create or replace view order_detail_line_items 19 | as 20 | select oi.order_id, 21 | oi.line_item_id, 22 | pi.product_name, 23 | oi.quantity, 24 | oi.unit_price, 25 | oi.discount_price, 26 | (oi.quantity*oi.discount_price) LINE_ITEM_TOTAL 27 | from product_information pi, order_items oi 28 | where oi.product_id = pi.product_id 29 | order by oi.order_id, oi.line_item_id ; 30 | 31 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.10_ILOExecInAProcedure.sql: -------------------------------------------------------------------------------- 1 | --- ILO Execution in a Single Session 2 | 3 | exec ilo_timer.set_mark_all_tasks_interesting(TRUE,TRUE); 4 | 5 | exec ilo_task.begin_task('Month-end','Purchasing'); 6 | 7 | @purchasing_report 8 | 9 | exec ilo_task.end_task; 10 | 11 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.11_ExecutionScript.sql: -------------------------------------------------------------------------------- 1 | --- Execution Script 2 | set serveroutput on 3 | 4 | DECLARE 5 | P_CUSTOMER_ID NUMBER; 6 | P_AMOUNT NUMBER; 7 | P_AUTHORIZATION NUMBER; 8 | P_STATUS_CODE NUMBER; 9 | P_STATUS_MESSAGE VARCHAR2(200); 10 | 11 | BEGIN 12 | P_CUSTOMER_ID := '&customer'; 13 | P_AMOUNT := '&amount'; 14 | 15 | billing.credit_request( 16 | P_CUSTOMER_ID => P_CUSTOMER_ID, 17 | P_AMOUNT => P_AMOUNT, 18 | P_AUTHORIZATION => P_AUTHORIZATION, 19 | P_STATUS_CODE => P_STATUS_CODE, 20 | P_STATUS_MESSAGE => P_STATUS_MESSAGE ); 21 | commit; 22 | DBMS_OUTPUT.PUT_LINE('P_CUSTOMER_ID = ' || P_CUSTOMER_ID); DBMS_OUTPUT.PUT_LINE('P_AMOUNT = ' || P_AMOUNT); 23 | DBMS_OUTPUT.PUT_LINE('P_AUTHORIZATION = ' || P_AUTHORIZATION); DBMS_OUTPUT.PUT_LINE('P_STATUS_CODE = ' || P_STATUS_CODE); DBMS_OUTPUT.PUT_LINE('P_STATUS_MESSAGE = ' || P_STATUS_MESSAGE); 24 | 25 | END; 26 | / 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.1_CheckInvalidObjects.sql: -------------------------------------------------------------------------------- 1 | select object_name, object_type, last_ddl_time, status from user_objects 2 | where status != 'VALID'; 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.5_InvalidObjectsAndRecompile.sql: -------------------------------------------------------------------------------- 1 | --- Invalid Objects Unit Test and Object Recompile 2 | 3 | select object_name, 4 | object_type, 5 | last_ddl_time, 6 | status 7 | from user_objects 8 | where status != 'VALID'; 9 | 10 | alter function GET_LISTPRICE compile ; 11 | 12 | alter procedure GET_ORDER_TOTAL compile ; 13 | 14 | select object_name, 15 | object_type, 16 | last_ddl_time, 17 | status 18 | from user_objects 19 | where status != 'VALID'; 20 | 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.7_OrderReportExecPlanBefore.sql: -------------------------------------------------------------------------------- 1 | --- Order Report Execution Plan (Before) 2 | @formats.sql 3 | 4 | alter session set statistics_level = 'ALL'; 5 | 6 | set linesize 105 7 | 8 | column order_id new_value v_order noprint 9 | column order_date new_value v_o_date noprint 10 | column ID format 99 11 | column order_total format 999,999,999.99 12 | 13 | BREAK ON order_id SKIP 2 PAGE 14 | BTITLE OFF 15 | 16 | compute sum of line_item_total on order_id 17 | 18 | ttitle left 'Order ID: ' v_order - 19 | right 'Order Date: ' v_o_date - 20 | skip 2 21 | spool logs/order_report_all_pre.txt 22 | 23 | select /* OrdersPreChange */ h.order_id ORDER_ID, 24 | order_date, 25 | li.line_item_id ID, 26 | li.product_name, 27 | li.product_id ITEM_NO, 28 | li.unit_price, 29 | li.discount_price, 30 | li.quantity, 31 | li.line_item_total from order_detail_header h, order_detail_line_items li 32 | and h.order_id = li.order_id 33 | order by h.order_id, li.line_item_id ; 34 | 35 | spool off 36 | 37 | set lines 150 38 | 39 | spool logs/OrdersPreChange.txt 40 | 41 | @pln.sql OrdersPreChange -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.8_OrderReportExecPlanAfter.sql: -------------------------------------------------------------------------------- 1 | --- Order Report Execution Plan (After) 2 | @formats.sql 3 | alter session set statistics_level = 'ALL'; 4 | 5 | set linesize 115 6 | 7 | column order_id new_value v_order noprint 8 | column order_date new_value v_o_date noprint 9 | column ID format 99 10 | column order_total format 999,999,999.99 11 | 12 | BREAK ON order_id SKIP 2 PAGE 13 | BTITLE OFF 14 | 15 | compute sum of line_item_total on order_id 16 | 17 | ttitle left 'Order ID: ' v_order - 18 | right 'Order Date: ' v_o_date - 19 | skip 2 20 | 21 | spool logs/order_report_all_fail.txt 22 | 23 | select /* OrdersChangeFail */ h.order_id ORDER_ID, 24 | order_date, 25 | li.line_item_id ID, 26 | li.product_name, 27 | p.supplier_product_id ITEM_NO, 28 | li.unit_price, 29 | li.discount_price, 30 | li.quantity, 31 | li.line_item_total from order_detail_header h, order_detail_line_items li, product_information p 32 | where h.order_id = li.order_id 33 | and li.product_id = p.product_id 34 | order by h.order_id, li.line_item_id ; 35 | 36 | spool off 37 | set lines 150 38 | 39 | spool logs/OrdersChangeFail.log 40 | @pln.sql OrdersChangeFail -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/15.9_OrderReportExecPlanCorrected.sql: -------------------------------------------------------------------------------- 1 | --- Order Report Execution Plan (Corrected) 2 | @formats.sql 3 | 4 | alter session set statistics_level = 'ALL'; 5 | 6 | set linesize 115 7 | 8 | column order_id new_value v_order noprint 9 | column order_date new_value v_o_date noprint 10 | column ID format 99 11 | column order_total format 999,999,999.99 12 | 13 | BREAK ON order_id SKIP 2 PAGE 14 | BTITLE OFF 15 | 16 | compute sum of line_item_total on order_id 17 | 18 | ttitle left 'Order ID: ' v_order - 19 | right 'Order Date: ' v_o_date - 20 | skip 2 21 | spool logs/order_report_all_corrected.txt 22 | 23 | select /* OrdersCorrected */ h.order_id ORDER_ID, 24 | order_date, 25 | li.line_item_id ID, 26 | li.product_name, 27 | li.supplier_product_id ITEM_NO, 28 | li.unit_price, 29 | li.discount_price, 30 | li.quantity, 31 | li.line_item_total from order_detail_header h, order_detail_line_items li 32 | where h.order_id = li.order_id 33 | order by h.order_id, li.line_item_id ; 34 | 35 | spool off set lines 150 36 | 37 | spool logs/OrdersCorrected_plan.txt 38 | 39 | @pln.sql OrdersCorrected -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/order_detail_views.sql: -------------------------------------------------------------------------------- 1 | create or replace view order_detail_header 2 | as 3 | select c.cust_first_name||' '||c.cust_last_name CUSTOMER, 4 | c.phone_number MOBILE, 5 | o.order_id, 6 | o.order_date, 7 | o.order_mode, 8 | os.order_status_name STATUS, 9 | o.order_total, 10 | e.last_name SALES_REP 11 | from customers c, orders o, order_status os, hr.employees e 12 | where c.customer_id = o.customer_id 13 | and o.order_status = os.order_status 14 | and o.sales_rep_id = e.employee_id 15 | order by c.cust_last_name, c.cust_first_name, o.order_id ; 16 | 17 | 18 | create or replace view order_detail_line_items 19 | as 20 | select oi.order_id, 21 | oi.line_item_id, 22 | pi.product_id, 23 | pi.product_name, 24 | oi.quantity, 25 | oi.unit_price, 26 | oi.discount_price, 27 | (oi.quantity*oi.discount_price) LINE_ITEM_TOTAL 28 | from product_information pi, order_items oi 29 | where oi.product_id = pi.product_id 30 | order by oi.order_id, oi.line_item_id ; 31 | 32 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/order_detail_views2.sql: -------------------------------------------------------------------------------- 1 | create or replace view order_detail_header 2 | as 3 | select c.cust_first_name||' '||c.cust_last_name CUSTOMER, 4 | c.phone_number MOBILE, 5 | o.order_id, 6 | o.order_date, 7 | o.order_mode, 8 | os.order_status_name STATUS, 9 | o.order_total, 10 | e.last_name SALES_REP 11 | from customers c, orders o, order_status os, hr.employees e 12 | where c.customer_id = o.customer_id 13 | and o.order_status = os.order_status 14 | and o.sales_rep_id = e.employee_id 15 | order by c.cust_last_name, c.cust_first_name, o.order_id ; 16 | 17 | 18 | create or replace view order_detail_line_items 19 | as 20 | select oi.order_id, 21 | oi.line_item_id, 22 | pi.product_id, 23 | pi.supplier_product_id, 24 | pi.product_name, 25 | oi.quantity, 26 | oi.unit_price, 27 | oi.discount_price, 28 | (oi.quantity*oi.discount_price) LINE_ITEM_TOTAL 29 | from product_information pi, order_items oi 30 | where oi.product_id = pi.product_id 31 | order by oi.order_id, oi.line_item_id ; 32 | 33 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/order_detail_w_supplier_id.sql: -------------------------------------------------------------------------------- 1 | create or replace view order_items_w_supplier 2 | as 3 | select oi.order_id, 4 | oi.line_item_id, 5 | pi.product_id, 6 | pi.supplier_product_id, 7 | pi.product_name, 8 | oi.quantity, 9 | oi.unit_price, 10 | oi.discount_price, 11 | (oi.quantity*oi.discount_price) LINE_ITEM_TOTAL 12 | from product_information pi, order_items oi 13 | where oi.product_id = pi.product_id 14 | order by oi.order_id, oi.line_item_id ; 15 | 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/order_report.sql: -------------------------------------------------------------------------------- 1 | --- order_report.sql 2 | 3 | set linesize 115 4 | 5 | column order_id new_value v_order noprint 6 | column order_date new_value v_o_date noprint 7 | column line_no format 99 8 | column order_total format 999,999,999.99 9 | 10 | BREAK ON order_id 11 | SKIP 2 PAGE 12 | BTITLE OFF 13 | 14 | compute sum of line_item_total on order_id 15 | 16 | ttitle left 'Order ID: ' v_order - 17 | right 'Order Date: ' v_o_date - 18 | skip 2 19 | 20 | --- Be sure to create the logs directory before executing! 21 | 22 | spool logs/order_report.txt 23 | 24 | select h.order_id ORDER_ID, 25 | h.order_date, 26 | li.line_item_id LINE_NO, 27 | li.supplier_product_id SUPP_PROD_ID, 28 | li.product_name, 29 | li.unit_price, 30 | li.discount_price, 31 | li.quantity, 32 | li.line_item_total from order_detail_header h, order_detail_line_items li 33 | where h.order_id = li.order_id 34 | and h.order_id = '&Order_Number' 35 | order by h.order_id, line_item_id ; 36 | spool off 37 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/order_reports_all.sql: -------------------------------------------------------------------------------- 1 | --- order_reports_all.sql 2 | set linesize 115 3 | 4 | column order_id new_value v_order noprint 5 | column order_date new_value v_o_date noprint 6 | column line_no format 99 7 | column order_total format 999,999,999.99 8 | 9 | BREAK ON order_id SKIP 2 PAGE 10 | BTITLE OFF 11 | 12 | compute sum of line_item_total on order_id 13 | 14 | ttitle left 'Order ID: ' v_order - 15 | right 'Order Date: ' v_o_date - 16 | skip 2 17 | 18 | spool logs/order_reports_all.txt 19 | 20 | select h.order_id ORDER_ID, 21 | h.order_date, 22 | li.line_item_id line_no, 23 | li.product_name, 24 | li.supplier_product_id ITEM_NO, 25 | li.unit_price, 26 | li.discount_price, 27 | li.quantity, 28 | li.line_item_total from order_detail_header h, order_detail_line_item li 29 | where h.order_id = li.order_id order by h.order_id, li.line_item_id ; 30 | 31 | spool off -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/pln.sql: -------------------------------------------------------------------------------- 1 | SELECT xplan.* 2 | FROM 3 | ( 4 | select max(sql_id) keep 5 | (dense_rank last order by last_active_time) sql_id 6 | , max(child_number) keep 7 | (dense_rank last order by last_active_time) child_number 8 | from v$sql 9 | where upper(sql_text) like '%&1%' 10 | and upper(sql_text) not like '%FROM V$SQL WHERE UPPER(SQL_TEXT) LIKE %' 11 | ) sqlinfo, 12 | table(DBMS_XPLAN.DISPLAY_CURSOR(sqlinfo.sql_id, sqlinfo.child_number, 'ALLSTATS LAST')) xplan 13 | / 14 | 15 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter15_scripts/purchasing_report.sql: -------------------------------------------------------------------------------- 1 | --- purchasing_report.sql 2 | 3 | break on supplier skip 1 4 | 5 | column target_price format 999,999.99 6 | 7 | set termout off 8 | 9 | spool logs/purchasing_report.txt 10 | 11 | select p.supplier_id SUPPLIER, 12 | p.supplier_product_id SUPP_PROD_ID, 13 | p.product_name PRODUCT_NAME, 14 | i.quantity_on_hand QTY_ON_HAND, 15 | (p.min_price * .5) TARGET_PRICE from product_information p, inventories i 16 | where p.product_id = i.product_id and p.product_status = 'orderable' and i.quantity_on_hand < 1000 17 | order by p.supplier_id, p.supplier_product_id ; 18 | 19 | spool off set termout on -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/as.sql: -------------------------------------------------------------------------------- 1 | set pagesize 999 2 | set lines 150 3 | col username format a13 4 | col prog format a10 trunc 5 | col sql_text format a41 trunc 6 | col sid format 999 7 | col child for 99999 8 | col avg_etime for 999,999.99 9 | select sid, substr(program,1,19) prog, address, hash_value, b.sql_id, child_number child, plan_hash_value, executions execs, 10 | (elapsed_time/decode(nvl(executions,0),0,1,executions))/1000000 avg_etime, 11 | sql_text 12 | from v$session a, v$sql b 13 | where status = 'ACTIVE' 14 | and username is not null 15 | and a.sql_id = b.sql_id 16 | and a.sql_child_number = b.child_number 17 | and sql_text not like 'select sid, substr(program,1,19) prog, address, hash_value, b.sql_id, child_number child,%' -- don't show this query 18 | order by sql_id, sql_child_number 19 | / 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/awr_plan_change.sql: -------------------------------------------------------------------------------- 1 | set lines 155 2 | col execs for 999,999,999 3 | col avg_etime for 999,999.999 4 | col avg_lio for 999,999,999.9 5 | col begin_interval_time for a30 6 | col node for 99999 7 | break on plan_hash_value on startup_time skip 1 8 | select ss.snap_id, ss.instance_number node, begin_interval_time, sql_id, plan_hash_value, 9 | nvl(executions_delta,0) execs, 10 | (elapsed_time_delta/decode(nvl(executions_delta,0),0,1,executions_delta))/1000000 avg_etime, 11 | (buffer_gets_delta/decode(nvl(buffer_gets_delta,0),0,1,executions_delta)) avg_lio, 12 | (disk_reads_delta/decode(nvl(buffer_gets_delta,0),0,1,executions_delta)) avg_pio 13 | from DBA_HIST_SQLSTAT S, DBA_HIST_SNAPSHOT SS 14 | where sql_id = nvl('&sql_id','4dqs2k5tynk61') 15 | and ss.snap_id = S.snap_id 16 | and ss.instance_number = S.instance_number 17 | and s.instance_number like nvl('&instance_number',s.instance_number) 18 | and executions_delta > 0 19 | order by 1, 2, 3 20 | / 21 | 22 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/baseline_hints.sql: -------------------------------------------------------------------------------- 1 | select 2 | extractvalue(value(d), '/hint') as outline_hints 3 | from 4 | xmltable('/outline_data/hint' 5 | passing ( 6 | select 7 | xmltype(comp_data) as xmlval 8 | from 9 | sqlobj$data sod, sqlobj$ so 10 | where so.signature = sod.signature 11 | and so.plan_id = sod.plan_id 12 | and comp_data is not null 13 | and name like '&baseline_plan_name' 14 | ) 15 | ) d; 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/baselines.sql: -------------------------------------------------------------------------------- 1 | set lines 155 2 | col sql_text for a35 trunc 3 | col last_executed for a28 4 | col enabled for a7 5 | col plan_hash_value for a16 6 | col last_executed for a16 7 | select spb.sql_handle, spb.plan_name, to_char(so.plan_id) plan_hash_value, 8 | dbms_lob.substr(sql_text,3999,1) sql_text, 9 | spb.enabled, spb.accepted, spb.fixed, 10 | to_char(spb.last_executed,'dd-mon-yy HH24:MI') last_executed 11 | from 12 | dba_sql_plan_baselines spb, sqlobj$ so 13 | where spb.signature = so.signature 14 | and spb.plan_name = so.name 15 | and dbms_lob.substr(sql_text,3999,1) like nvl('&sql_text', dbms_lob.substr(sql_text,3999,1)) 16 | -- spb.sql_text like nvl('%'||'&sql_text'||'%',spb.sql_text) 17 | and spb.sql_handle like nvl('&name',spb.sql_handle) 18 | and spb.plan_name like nvl('&plan_name',spb.plan_name) 19 | / 20 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/col_stats.sql: -------------------------------------------------------------------------------- 1 | set verify off 2 | set pagesize 999 3 | set lines 165 4 | col table_name format a25 trunc 5 | col column_name format a25 6 | col avg_len format 9999999 7 | col NDV format 999,999,999 8 | col buckets format 999999 9 | col low_value format a15 10 | col high_value format a15 11 | col density for .999999999 12 | col data_type for a10 13 | select column_name, data_type, avg_col_len, density, num_distinct NDV, histogram, num_buckets buckets, sample_size, last_analyzed, 14 | display_raw(low_value,data_type) low_value, display_raw(high_value,data_type) high_value 15 | from dba_tab_cols 16 | where owner like nvl('&owner',owner) 17 | and table_name like nvl('&table_name',table_name) 18 | and column_name like nvl('%&column_name%',column_name) 19 | order by internal_column_id 20 | / 21 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/create_baseline.sql: -------------------------------------------------------------------------------- 1 | /* 2 | exec :ret := dbms_spm.load_plans_from_cursor_cache(- 3 | sql_id=>'&v_sql_id', - 4 | plan_hash_value=>&v_plan_hash_value, - 5 | */ 6 | var ret number 7 | exec :ret := dbms_spm.load_plans_from_cursor_cache(- 8 | sql_id=>'&sql_id', - 9 | plan_hash_value=>&plan_hash_value,- 10 | fixed=>'&fixed'); 11 | 12 | 13 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/create_skew.sql: -------------------------------------------------------------------------------- 1 | create table skew 2 | (pk_col number primary key, 3 | col1 number, 4 | col2 varchar2 (30), 5 | col3 date, 6 | col4 varchar2(1)) 7 | / 8 | create sequence skew_seq 9 | / 10 | begin 11 | for i in 1..100000 loop 12 | insert into skew (pk_col, col1, col2, col3, col4) 13 | select skew_seq.nextval, skew_seq.currval, 'asddsadasd', 14 | sysdate-(skew_seq.currval/3600), 'Y' from dual; 15 | end loop; 16 | end; 17 | / 18 | select count(*) from skew; 19 | update skew set col1 = 1 where rownum < 100000; 20 | update skew set col4 = 'N' where rownum < 100; 21 | commit; 22 | 23 | create or replace package skew_package as 24 | skew_record skew%rowtype; 25 | end; 26 | / 27 | 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/dba_tables.sql: -------------------------------------------------------------------------------- 1 | select owner, table_name, status, last_analyzed, num_rows, blocks 2 | from dba_tables 3 | where owner like nvl('&owner',owner) 4 | and table_name like nvl('&table_name',table_name) 5 | / 6 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/display_raw.sql: -------------------------------------------------------------------------------- 1 | create or replace function display_raw (rawval raw, type varchar2) 2 | return varchar2 3 | is 4 | cn number; 5 | cv varchar2(32); 6 | cd date; 7 | cnv nvarchar2(32); 8 | cr rowid; 9 | cc char(32); 10 | begin 11 | if (type = 'NUMBER') then 12 | dbms_stats.convert_raw_value(rawval, cn); 13 | return to_char(cn); 14 | elsif (type = 'VARCHAR2') then 15 | dbms_stats.convert_raw_value(rawval, cv); 16 | return to_char(cv); 17 | elsif (type = 'DATE') then 18 | dbms_stats.convert_raw_value(rawval, cd); 19 | return to_char(cd); 20 | elsif (type = 'NVARCHAR2') then 21 | dbms_stats.convert_raw_value(rawval, cnv); 22 | return to_char(cnv); 23 | elsif (type = 'ROWID') then 24 | dbms_stats.convert_raw_value(rawval, cr); 25 | return to_char(cnv); 26 | elsif (type = 'CHAR') then 27 | dbms_stats.convert_raw_value(rawval, cc); 28 | return to_char(cc); 29 | else 30 | return 'UNKNOWN DATATYPE'; 31 | end if; 32 | end; 33 | / 34 | 35 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/dplan.sql: -------------------------------------------------------------------------------- 1 | set lines 150 2 | select * from table(dbms_xplan.display_cursor('&sql_id','&child_no','typical')) 3 | / 4 | 5 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/dplan_alias.sql: -------------------------------------------------------------------------------- 1 | set lines 150 2 | select * from table(dbms_xplan.display_cursor('&sql_id','&child_no','alias')) 3 | / 4 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/dplan_awr.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM table(dbms_xplan.display_awr(nvl('&sql_id','a96b61z6vp3un'),nvl('&plan_hash_value',null),null,'typical +peeked_binds')) 2 | / 3 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/find_sql.sql: -------------------------------------------------------------------------------- 1 | col sql_text for a60 wrap 2 | set verify off 3 | set pagesize 999 4 | set lines 155 5 | col username format a13 6 | col prog format a22 7 | col sid format 999 8 | col child_number format 99999 heading CHILD 9 | col ocategory format a10 10 | col avg_etime format 9,999,999.99 11 | col avg_pio format 9,999,999.99 12 | col avg_lio format 999,999,999 13 | col etime format 9,999,999.99 14 | 15 | select sql_id, child_number, plan_hash_value plan_hash, executions execs, 16 | (elapsed_time/1000000)/decode(nvl(executions,0),0,1,executions) avg_etime, 17 | buffer_gets/decode(nvl(executions,0),0,1,executions) avg_lio, 18 | sql_text 19 | from v$sql s 20 | where upper(sql_text) like upper(nvl('&sql_text',sql_text)) 21 | and sql_text not like '%from v$sql where sql_text like nvl(%' 22 | and sql_id like nvl('&sql_id',sql_id) 23 | order by 1, 2, 3 24 | / 25 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/find_sql_using_baseline.sql: -------------------------------------------------------------------------------- 1 | col sql_text for a40 trunc 2 | set verify off 3 | set pagesize 999 4 | set lines 155 5 | col username format a13 6 | col prog format a22 7 | col sid format 999 8 | col child_number format 99999 heading CHILD 9 | col ocategory format a10 10 | col avg_etime format 9,999,999.99 11 | col avg_pio format 9,999,999.99 12 | col avg_lio format 999,999,999 13 | col etime format 9,999,999.99 14 | 15 | select sql_id, child_number, plan_hash_value plan_hash, sql_plan_baseline, executions execs, 16 | (elapsed_time/1000000)/decode(nvl(executions,0),0,1,executions) avg_etime, 17 | buffer_gets/decode(nvl(executions,0),0,1,executions) avg_lio, 18 | sql_text 19 | from v$sql s 20 | where upper(sql_text) like upper(nvl('&sql_text',sql_text)) 21 | and sql_text not like '%from v$sql where sql_text like nvl(%' 22 | and sql_id like nvl('&sql_id',sql_id) 23 | and plan_hash_value like nvl('&plan_hash_value',plan_hash_value) 24 | and sql_plan_baseline is not null 25 | order by 1, 2, 3 26 | / 27 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/find_sql_using_outline.sql: -------------------------------------------------------------------------------- 1 | col sql_text for a60 wrap 2 | set verify off 3 | set pagesize 999 4 | set lines 155 5 | col username format a13 6 | col prog format a22 7 | col sid format 999 8 | col child_number format 99999 heading CHILD 9 | col ocategory format a10 10 | col avg_etime format 9,999,999.99 11 | col avg_pio format 9,999,999.99 12 | col avg_lio format 999,999,999 13 | col etime format 9,999,999.99 14 | 15 | select sql_id, child_number, plan_hash_value plan_hash, executions execs, 16 | (elapsed_time/1000000)/decode(nvl(executions,0),0,1,executions) avg_etime, 17 | buffer_gets/decode(nvl(executions,0),0,1,executions) avg_lio, 18 | sql_text 19 | from v$sql s 20 | where upper(sql_text) like upper(nvl('&sql_text',sql_text)) 21 | and sql_text not like '%from v$sql where sql_text like nvl(%' 22 | and sql_id like nvl('&sql_id',sql_id) 23 | and outline_category is not null 24 | order by 1, 2, 3 25 | / 26 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/find_sql_using_profile.sql: -------------------------------------------------------------------------------- 1 | break on sql_id on sql_profile on sql_text skip 1 2 | set lines 165 3 | col sql_profile for a30 4 | col sql_text for a60 wrap 5 | set verify off 6 | set pagesize 999 7 | col username format a13 8 | col prog format a22 9 | col sid format 999 10 | col child_number format 99999 heading CHILD 11 | col ocategory format a10 12 | col avg_etime format 9,999,999.99 13 | col avg_pio format 9,999,999.99 14 | col avg_lio format 999,999,999 15 | col etime format 9,999,999.99 16 | 17 | select sql_id, child_number, plan_hash_value plan_hash, sql_profile, executions execs, 18 | (elapsed_time/1000000)/decode(nvl(executions,0),0,1,executions) avg_etime, 19 | buffer_gets/decode(nvl(executions,0),0,1,executions) avg_lio, 20 | sql_text 21 | from v$sql s 22 | where upper(sql_text) like upper(nvl('&sql_text',sql_text)) 23 | and sql_text not like '%from v$sql where sql_text like nvl(%' 24 | and sql_id like nvl('&sql_id',sql_id) 25 | and sql_profile is not null 26 | order by 1, 2, 3 27 | / 28 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/optim_parms.sql: -------------------------------------------------------------------------------- 1 | col child format 99999 2 | col name for a40 3 | col value for a40 4 | select * from ( 5 | select 6 | -- INST_ID, 7 | -- KQLFSQCE_PHAD, 8 | -- KQLFSQCE_HASH, 9 | KQLFSQCE_SQLID sql_id, 10 | -- KQLFSQCE_HADD, 11 | KQLFSQCE_CHNO child, 12 | -- KQLFSQCE_PNUM, 13 | KQLFSQCE_PNAME name, 14 | KQLFSQCE_PVALUE value, 15 | decode(bitand(KQLFSQCE_FLAGS, 2), 0, 'NO', 'YES') isdefault 16 | from X$KQLFSQCE 17 | -- where bitand(KQLFSQCE_FLAGS, 8) = 0 18 | -- and (bitand(KQLFSQCE_FLAGS, 4) = 0 or bitand(KQLFSQCE_FLAGS, 2) = 0) 19 | where KQLFSQCE_SQLID like nvl('&sql_id',KQLFSQCE_SQLID) 20 | and decode(bitand(KQLFSQCE_FLAGS, 2), 0, 'NO', 'YES') like nvl('&isdefault','%') 21 | ) 22 | order by 1,2 asc,decode(substr(name,1,1),'_',2,1), replace(name,'_','') 23 | / 24 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/outline_hints.sql: -------------------------------------------------------------------------------- 1 | select name, hint from dba_outline_hints 2 | where name like nvl('&name',name) 3 | and upper(hint) like upper(nvl('&hint',hint)) 4 | / 5 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/outlines.sql: -------------------------------------------------------------------------------- 1 | set pagesize 999 2 | set lines 155 3 | column owner format a12 4 | column category format a15 5 | column name format a30 6 | col used for a12 7 | col enabled for a12 8 | column sql_text format a70 trunc 9 | column hints for 99999 10 | break on owner 11 | select category, ol_name name, 12 | decode(bitand(flags, 1), 0, 'UNUSED', 1, 'USED') used, 13 | decode(bitand(flags, 4), 0, 'ENABLED', 4, 'DISABLED') enabled, 14 | hintcount hints, 15 | sql_text 16 | from outln.ol$ 17 | where 18 | category like nvl('&category',category) 19 | and ol_name like nvl('&name',ol_name) 20 | order by 1, 2, 4, 3 21 | / 22 | 23 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/restore_table_stats.sql: -------------------------------------------------------------------------------- 1 | begin 2 | dbms_stats.restore_table_stats ( 3 | '&Owner', 4 | '&table_name', 5 | '&as_of_date'||' 12.00.00.000000000 AM -04:00'); /* Noon */ 6 | end; 7 | / 8 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/sql_hints.sql: -------------------------------------------------------------------------------- 1 | select 2 | extractvalue(value(d), '/hint') as outline_hints 3 | from 4 | xmltable('/*/outline_data/hint' 5 | passing ( 6 | select 7 | xmltype(other_xml) as xmlval 8 | from 9 | v$sql_plan 10 | where 11 | sql_id like nvl('&sql_id',sql_id) 12 | and child_number = &child_no 13 | and other_xml is not null 14 | ) 15 | ) d; 16 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/sql_profiles.sql: -------------------------------------------------------------------------------- 1 | col category for a15 2 | col sql_text for a70 trunc 3 | select name, category, status, sql_text, force_matching 4 | from dba_sql_profiles 5 | where sql_text like nvl('&sql_text','%') 6 | and name like nvl('&name',name) 7 | order by last_modified 8 | / 9 | 10 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/tab_stats_history.sql: -------------------------------------------------------------------------------- 1 | select owner, table_name, stats_update_time 2 | from DBA_TAB_STATS_HISTORY 3 | where owner like nvl('&owner',owner) 4 | and table_name like nvl('&table_name',table_name) 5 | / 6 | -------------------------------------------------------------------------------- /Pro Oracle SQL/chapter16_scripts/valid_hints.sql: -------------------------------------------------------------------------------- 1 | select name,version from v$sql_hint 2 | where upper(name) like '%'||upper(nvl('&hint',name))||'%' 3 | order by name; 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Pro Oracle SQL*](http://www.apress.com/9781430232285) by Karen Morton, Robyn Sands, Jared Still, Riyaj Shamsudeen, and Kerry Osborne (Apress, 2010). 4 | 5 | ![Cover image](9781430232285.jpg) 6 | 7 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 8 | 9 | ## Releases 10 | 11 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 12 | 13 | ## Contributions 14 | 15 | See the file Contributing.md for more information on how you can contribute to this repository. 16 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! --------------------------------------------------------------------------------