Download presentation
Presentation is loading. Please wait.
Published byKatrina Barrett Modified over 9 years ago
1
Indexes- What? Optional structures associated with tables Provides a quick access path to table data You can create indexes on one or more columns of a table Avoids the need for large-table, full-table scans and disk sorts
2
Indexes - Types B-tree – default index, works well on large tables and columns with distinct values, updates relatively inexpensive. Bitmap indexes – used on columns which contains many repeated values on a large table. create bitmap index city_ind on emp (city); Function-based indexes – Built on expressions create index salcomm_ind on emp (sal+comm); For Queries with SAL+COMM in where SALCOMM_IND index will be used
3
Indexes – When to use and on what columns Access less than 5% of the rows in the table. Don't index tables that are frequently updated. Tables that don't have duplicate values on the columns usually selected in WHERE clauses. Columns frequently specified in WHERE clauses Columns that have unique values Columns that are commonly used to join tables
4
Indexes – How to use it? Some indexes are created part of creating constraints eg: PrimaryKey and UniqueKey Can be created using the graphical utilities of Oracle Enterprise Manager Using CREATE INDEX command CREATE INDEX emp_hiredate_idx ON employees (hire_date); CREATE INDEX empdept_ind on emp (empno,deptno); - Composite ALTER INDEX emp_hiredate_id RENAME TO emp_hire_date_idx; DROP INDEX emp_hire_date_idx;
5
Indexes – How to view them? DBA_INDEXES - indexes on all tables in the database ALL_INDEXES - indexes on all tables accessible to the user USER_INDEXES - restricted to indexes owned by the user Columns of indexes on tables DBA_IND_COLUMNS,ALL_IND_COLUMNS, USER_IND_COLUMNS Function-based indexes on tables. DBA_IND_EXPRESSIONS,ALL_IND_EXPRESSIONS,USER_IND_EXPRESSIONS
6
Indexes- Rebuilding Rebuilding an index based on an existing data source removes intra-block fragmentation. Compared to dropping the index and using the CREATE INDEX statement, re-creating an existing index offers better performance. The following statement rebuilds the existing index emp_name: ALTER INDEX emp_name REBUILD;
7
Indexes - Clustering Co-locate related tables for faster access and for less storage. create cluster emp_dept (deptno number(2)); create index on cluster emp_dept; create table dept (deptno number(2),name varchar2(20),loc varchar2(20)) cluster emp_dept (deptno); create table emp (empno number(5),name varchar2(20), sal number(10,2), deptno number(2)) cluster emp_dept (deptno) Not good if frequent updates are expected
8
Indexes – refresher Assuming index on S# created, will the index be used? WHERE S# > 100 - YES WHERE S# <> 100 - NO WHERE S#= 100 – YES Assume index on S_NMAE WHERE S_NAME like 'S%‘ - YES WHERE S_NAME like '%S‘- NO WHERE S_NAME is NULL - NO:
9
Views – What? SQL statement that is stored in memory for re- use CREATE VIEW view_emp AS SELECT empid FROM emp; SELECT * FROM view_emp WHERE empid BETWEEN 500 AND 1000; Oracle will transform the query into this: SELECT * FROM (select empid from emp) WHERE empid BETWEEN 500 AND 1000;
10
Views - Benefits Common set of SQL, less likely to require parsing Security: hide tables that actually contain the data you are querying. Restrict Rows/Columns that a given user has access to. Logical data independence. - Same object name is interpreted differently by different users eg: Table S can be supplier table for user1 whereas it means restricted view s_view for user2 with user2.S as synonym for user1.s_view
11
Views - Commands Basic Views representing entire table: CREATE OR REPLACE VIEW AS SELECT FROM ; Restrictive Views: CREATE OR REPLACE VIEW AS SELECT FROM WHERE = ; Multi table views – CREATE OR REPLACE VIEW explan_four AS SELECT DISTINCT s.srvr_id FROM servers s, serv_inst i WHERE s.srvr_id = i.srvr_id;
12
Views – Commands contd. Drop view - DROP VIEW date_view; Updating views - will update underlying base table. UPDATE upd_view SET person_id = person_id * 10; Read only views - CREATE OR REPLACE VIEW person_ro_view AS SELECT first_name, last_name, ssn FROM personWITH READ ONLY; Views with Check option - CREATE OR REPLACE VIEW checkoption_view AS SELECT person_id, first_name, last_name, dob, ssn FROM person WHERE person_id < 10 WITH CHECK OPTION;
13
Views – Data Dictionary data dictionary views are divided into three groups: USER, ALL, and DBA. USER : information about objects owned by the current SQL user USER TABLES all tables with their name, no. of columns, storage and statistical information etc. (TABS) USER_CATALOG tables, views, and synonyms (CAT) USER_COL_COMMENTS comments on columns USER_CONSTRAINTS constraint definitions for tables USER_INDEXES_all information about indexes created for tables (IND) USER_OBJECTS_all database objects owned by the user (OBJ)
14
Views – Data Dictionary contd. USER_TAB_COLUMNS columns of the tables and views owned by the user (COLS) USER_TAB_COMMENTS comments on tables and views USER_TRIGGERS triggers defined by the user USER_USERS information about the current user USER_VIEWS views defined by the user
15
Views – Materialized Database object contains results of a query. Local copies of data located remotely Create summary tables based on aggregations of a table's data. Also know as snapshots. A materialized view can query tables, views, and other materialized views. Collectively these are called master tables
16
Views – Materialized contd. FAST refresh uses the materialized view logs to send the rows that have changed from master tables to the materialized view. COMPLETE refresh re-creates the entire materialized view. Refresh Method - FORCE Clause When you specify a FORCE clause, Oracle will perform a fast refresh if one is possible or a complete refresh otherwise. FORCE is the default.
17
Views – Materialized contd. Primary Key Materialized Views CREATE MATERIALIZED VIEW mv_emp_pk REFRESH FAST START WITH SYSDATE REFRESH FAST START WITH SYSDATE NEXT SYSDATE + 1/48 WITH PRIMARY KEY AS SELECT * FROM emp@remote_db; Rowid Materialized Views CREATE MATERIALIZED VIEW mv_emp_rowid REFRESH WITH ROWID AS SELECT * FROM emp@remote_db; AS SELECT * FROM emp@remote_db; Subquery Materialized Views CREATE MATERIALIZED VIEW mv_empdept AS SELECT * FROM emp@remote_db e WHERE EXISTS (SELECT * FROM dept@remote_db d WHERE e.dept_no = d.dept_no)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.