|
- SELECT cust_street_address
- FROM customers
- 3 WHERE cust_postal_code = 68054;
- 193 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 2008213504
- -------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- -------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 89 | 2581 | 405 (1)| 00:00:05 |
- |* 1 | TABLE ACCESS FULL| CUSTOMERS | 89 | 2581 | 405 (1)| 00:00:05 |
- -------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter(TO_NUMBER("CUST_POSTAL_CODE")=68054)
- Statistics
- ----------------------------------------------------------
- 2 recursive calls
- 0 db block gets
- 1474 consistent gets
- 0 physical reads
- 0 redo size
- 7393 bytes sent via SQL*Net to client
- 651 bytes received via SQL*Net from client
- 14 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 193 rows processed
- SQL> SELECT cust_street_address
- 2 FROM customers
- 3 WHERE cust_postal_code = to_char(68054) ;
- 193 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1219101926
- ----------------------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- ----------------------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 89 | 2581 | 85 (0)| 00:00:02 |
- | 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS | 89 | 2581 | 85 (0)| 00:00:02 |
- |* 2 | INDEX RANGE SCAN | CUST_POSTAL_CODE_IDX | 89 | | 1 (0)| 00:00:01 |
- ----------------------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 2 - access("CUST_POSTAL_CODE"='68054')
- Statistics
- ----------------------------------------------------------
- 1 recursive calls
- 0 db block gets
- 191 consistent gets
- 1 physical reads
- 0 redo size
- 7393 bytes sent via SQL*Net to client
- 651 bytes received via SQL*Net from client
- 14 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 193 rows processed
- SQL>
复制代码- select *
- from sales s1
- where time_id = (select max(time_id)
- from sales s2
- where s1.prod_id = s2.prod_id
- and s1.cust_id = s2.cust_id
- and prod_id = 13
- and cust_id = 1762);
- select s.prod_id, a. maxt, s.amount_sold
- from sales s , (select prod_id , cust_id , max(time_id) maxt
- from sales
- group by prod_id , cust_id ) a
- where s.prod_id=a.prod_id and
- s.cust_id =a.cust_id
- and s.prod_id=13
- and s.cust_id=1762;
复制代码- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 3826430447
- ----------------------------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- ----------------------------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1220 | 43920 | 1151 (0)| 00:00:14 |
- | 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS | 1220 | 43920 | 1151 (0)| 00:00:14 |
- |* 2 | INDEX SKIP SCAN | CUST_COUNTRY_STATE_CITY_IX | 1220 | | 16 (0)| 00:00:01 |
- ----------------------------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
复制代码- SELECT /*+ index(s sales_pk) */ sum(amount_sold)
- FROM sales s
- WHERE prod_id BETWEEN 130 AND 150
- AND cust_id BETWEEN 10000 AND 10100;
- SELECT sum(amount_sold)
- FROM sales s
- WHERE prod_id BETWEEN 130 AND 150
- AND cust_id BETWEEN 10000 AND 10100;
- 最好的(带星型):
- SELECT sum(s.amount_sold)
- FROM sales s , products p
- WHERE s.prod_id=p.prod_id and
- p.prod_id BETWEEN 130 AND 150
- AND s.cust_id BETWEEN 10000 AND 10100;
复制代码- select * from sales;
- SELECT count(*)
- FROM sales s, customers c
- WHERE s.cust_id = c.cust_id
- AND prod_id = 13
- AND c.cust_credit_limit > 10000
- select cust_id
- from sales s
- where exists ( select 'X' from customers c
- where s.cust_id=c.cust_id and s.prod_id=13 and c.cust_credit_limit > 10000 );
复制代码- SELECT d.department_id, d.department_name, d.location_id,
- e.employee_id, e.last_name, e.first_name, e.salary,
- e.job_id FROM departments d ,employees e
- WHERE e.department_id (+) = d.department_id ;
- CREATE OR REPLACE VIEW emp_dept
- AS
- SELECT d.department_id, d.department_name, d.location_id,
- e.employee_id, e.last_name, e.first_name, e.salary,
- e.job_id FROM departments d ,employees e
- WHERE e.department_id (+) = d.department_id;
- SELECT v.last_name, v.first_name, l.state_province
- FROM locations l, emp_dept v
- WHERE l.state_province = 'California'
- AND
- v.location_id = l.location_id (+);
- SELECT v.last_name, v.first_name, l.state_province
- FROM locations l, emp_dept v
- WHERE l.state_province = 'California'
- AND
- v.location_id = l.location_id (+);
复制代码- select * from user_constraints c
- where c.TABLE_NAME='PRODUCTS';
-
- alter table products modify constraint PRODUCTS_PK
- validate ;
-
-
- create bitmap index isales on sales (p.prod_subcategory
- )
- from products p , sales s
- where p.prod_id=s.prod_id local ;
-
复制代码
内存表是物化视图还要快的数据结构:
- SET SERVEROUTPUT ON
- DECLARE
- TYPE emp_table_type is table of
- employees%ROWTYPE INDEX BY BINARY_INTEGER;
- my_emp_table emp_table_type;
- v_count
- NUMBER(3):= 104;
- BEGIN
- FOR i IN 100..v_count
- LOOP
- SELECT * INTO my_emp_table(i) FROM employees
- WHERE employee_id = i;
- END LOOP;
- FOR i IN my_emp_table.FIRST..my_emp_table.LAST
- LOOP
- DBMS_OUTPUT.PUT_LINE(my_emp_table(i).last_name);
- END LOOP;
- END;
复制代码
从原始结构了解cursor:
- declare
- type type_departments is table of departments%rowtype index by binary_integer;
- v_departments type_departments ;
- type type_1 is ref cursor;
- v_1 type_1;
- v_2 varchar2(200) :='select department_id, department_name, manager_id,location_id
- from departments';
- i integer :=1;
- begin
-
- dbms_output.put_line(lpad('DEPARTMENT_ID',13,' ')||' '
- ||rpad('DEPARTMENT_NAME',30,' ')||' '
- ||lpad('MANAGER_ID',10,' ')||' '||
- lpad('LOCATION_ID',11,' '));
- dbms_output.put_line(lpad('-',13,'-')||' '
- ||rpad('-',30,'-')||' '
- ||lpad('-',10,'-')||' '||
- lpad('-',11,'-'));
- open v_1 for v_2;
- loop
- fetch v_1 into v_departments(i);
- exit when v_1%notfound;
- dbms_output.put_line(lpad(to_char(v_departments(i).DEPARTMENT_ID),13,' ')||' '
- ||rpad(v_departments(i).DEPARTMENT_NAME,30,' ')||' '
- ||lpad(nvl(to_char(v_departments(i).MANAGER_ID),' '),10,' ')||' '||
- lpad(to_char(v_departments(i).LOCATION_ID),11,' '));
- i := i +1;
- end loop;
- close v_1;
- end;
复制代码
|
|