Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 1.

Similar presentations


Presentation on theme: "Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 1."— Presentation transcript:

1 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 1

2 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Day 1 Agenda SQL data selection Introduction –Normalization –Tables –Unique Key / Primary Key –Foreign key SQL –Introduction to SQL –SELECT, WHERE, ORDER BY –Selection from multiple tables –Subqueries Practice

3 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Normalization In the field of relational database design, normalization is a systematic way of ensuring that a database structure is suitable for general-purpose querying and free of certain undesirable characteristics—insertion, update, and deletion anomalies—that could lead to a loss of data integrity Informally, a relational database table (the computerized representation of a relation) is often described as "normalized" if it is in the Third Normal Form (3NF). A standard piece of database design guidance is that the designer should create a fully normalized design; selective denormalization can subsequently be performed for performance reasons. However, some modeling disciplines, such as the dimensional modeling approach to data warehouse design, explicitly recommend non-normalized designs, i.e. designs that in large part do not adhere to 3NF

4 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Normalization – an example NameSurnameLevelCharge StefanAnonimProgrammer20 NataliaKowalskaProgrammer20 JanuszInnySenior Programmer25 ZofiaMiłaProgrammer20 YurijZdolnyConsultant30 PawełSzybkiConsultant30 ZbigniewMolendaAnalyst20 KazimierzStarszyManager50 Table before normalisation Normalized table EMPLOYEE NameSurnameLevel StefanAnonimProgrammer NataliaKowalskaProgrammer JanuszInnySenior Programmer ZofiaMiłaProgrammer YurijZdolnyConsultant PawełSzybkiConsultant ZbigniewMolendaAnalyst KazimierzStarszyManager Normalized table CHARGE LevelCharge Programmer20 Senior Programmer25 Consultant30 Analyst20 Manager50

5 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Tables

6 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Unique Key In relational database design, a unique key (UK) can uniquely identify each row in a table. An unique key comprises a single column or a set of columns. A unique key must uniquely identify all possible rows that exist in a table and not only the currently existing rows. Examples of unique keys are telephone numbers. Names, addresses are not a good candidates for an unique key. No two distinct rows in a table can have the same value (or combination of values) in those columns if NULL values are not used. Depending on its design, a table may have arbitrarily many unique keys.

7 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Primary Key A table can have at most one primary key (PK), but more than one unique key. A primary key is a combination of columns which uniquely specify a row. It is a special case of unique keys. One difference is that for unique keys the implicit NOT NULL constraint is not automatically enforced, while for primary keys it is enforced. Thus, the values in unique key columns may or may not be NULL. Another difference is that primary keys must be defined using another syntax. Thus Primary Key column allows no row having NULL while Unique Key column allows null value.

8 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ?Question What are the differences between UK and PK ?

9 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Unique key – an example NameSurnameLevel StefanAnonimProgrammer NataliaKowalskaProgrammer JanuszInnySenior Programmer ZofiaMiłaProgrammer YurijZdolnyConsultant PawełSzybkiConsultant ZbigniewMolendaAnalyst KazimierzStarszyManager Bad example

10 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Unique key – an example 2 LvlIDLevelCharge PRGProgrammer20 SPRSenior Programmer25 CNSConsultant30 ANSAnalyst20 MGRManager50 EmpIDNameSurnameLevel 325417StefanAnonimPRG 437835NataliaKowalskaPRG 338765JanuszInnySPR 245365ZofiaMiłaPRG 264386YurijZdolnyCNS 342317PawełSzybkiANS 235378ZbigniewMolendaANS 97684KazimierzStarszyMGR

11 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Foreign Key 1/2 A foreign key (FK) is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a set of columns in another (referenced) table. The columns in the referencing table must be the primary key or other candidate key in the referenced table. The values in one row of the referencing columns must occur in a single row in the referenced table. Thus, a row in the referencing table cannot contain values that don't exist in the referenced table (except potentially NULL). This way references can be made to link information together and it is an essential part of database normalization. Multiple rows in the referencing table may refer to the same row in the referenced table. Most of the time, it reflects the one (master table, or referenced table) to many (child table, or referencing table) relationship.

12 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Foreign Key 2/2 A table may have multiple foreign keys, and each foreign key can have a different referenced table. Each foreign key is enforced independently by the database system. Cascading relationships between tables can be established using foreign keys. The referencing and referenced table may be the same table, i.e. the foreign key refers back

13 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Foreign key – an example EmpIDNameSurnameLevel 325417StefanAnonimPRG 437835NataliaKowalskaPRG 338765JanuszInnySPR 245365ZofiaMiłaPRG 264386YurijZdolnyCNS 342317PawełSzybkiANS 235378ZbigniewMolendaANS 97684KazimierzStarszyMGR LvlIDLevelChargeDptID PRGProgrammer20ATS SPRSenior Programmer25ATS CNSConsultant30CONS ANSAnalyst20CONS MGRManager50CONS IDDepartment ATSAccenture Technology Solutions CONSAccenture Consulting

14 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Introduction to SQL SQL (Structured Query Language) allows the users to access data in relational database management systems, such as Oracle, Sybase, Informix, Microsoft SQL Server, Access, and others using English- like statements giving the users the ability to describe the data they wish to see. SQL also allows users to define, view and manipulate the data in a database.

15 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Statements Data retrieval SELECT Data manipulation language (DML) INSERT, UPDATE, DELETE, MERGE Data definition language (DDL) CREATE, ALTER, DROP, RENAME, TRUNCATE Transaction control COMMIT, ROLLBACK, SAVEPOINT Data control language GRANT, REVOKE

16 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL statements characteristics Are not case sensitive Can be one or more lines Keywords can not be abbreviated or split across lines Clauses are usually places in separated lines Indents are used to enhance readability Arithmetical expressions can be used (+ - * / )

17 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL standards Use a single case for all SQL verbs Begin all SQL verbs on a new line Right or left align verbs within the initial SQL verb Separate all words with a single space

18 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SELECT statement SELECT identifies what columns FROM identified which tables SELECT * | {DISTINCT column, expression [alias],… } FROM table; SELECT * FROM employees ; SELECT job_id, first_name FROM employees ;

19 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SELECT Data projection

20 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SELECT statement - duplicated rows By default SELECT returns all rows Duplicates can be eliminated by using the DISTINCT keyword SELECT job_id FROM employees; SELECT DISTINCT job_id FROM employees;

21 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. WHERE clause Restrict rows selected by using the WHERE clause The WHERE clause follows FROM clause SELECT * | {distinct column, expression [alias],… } FROM table [WHERE condition(s)]; SELECT * FROM emp; SELECT * FROM emp WHERE deptno = 30;

22 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. WHERE data selection

23 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Comparison conditions =, >, =, BETWEEN … AND … IN (set) LIKE IS NULL

24 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Examples SELECT e1.first_name FROM employees e1 WHERE e1.salary > (SELECT MAX(e2.salary) FROM employees e2 WHERE e2.job = ’SALESMAN’); SELECT first_name FROM employees WHERE substr(first_name, 1, 1) IN (’A’,’W’,’J’);

25 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ?Question How many rows will return following query ? SELECT * FROM employees WHERE commission_pct = NULL;

26 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Logical conditions AND OR NOT WHERE salary >= 1000 AND last_name like ‘%MAN%’ WHERE salary > 1000 OR last_name like ‘%MAN%’ WHERE last_name NOT in (‘DOE’,SMITH’)

27 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ORDER BY clause Sort rows with ORDER BY clause –ASC ascending (default) Numeric columns with the lowest values first Character columns in alphabetical order Date columns with earlier dates first –DESC descending SELECT * | {distinct column, expression [alias],… } FROM table [ORDER BY {column, expr} [ASC|DESC]]; SELECT * FROM employees ORDER BY department_id, salary DESC;

28 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ?Question What will be returned by following query ? SELECT commission_pct FROM employees ORDER BY commission_pct DESC;

29 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ORDER BY clause Sorting null values NULLS FIRST / NULLS LAST (default) Column numbers aliases Expressions in ORDER BY clause ORDER BY commission_pct NULLS FIRST; ORDER BY 2, 1 DESC; ORDER BY DECODE(last_name, ‘DOE', 'A', ‘SMITH', 'B', ‘KING', 'C', 'Z');

30 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Selecting data from multiple tables

31 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ?Question How many rows will return following query ? SELECT * FROM employees, departments;

32 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Selecting data from multiple tables Cartesian Product emp (count = 13), dept (count = 4) Result (13 x 4 = 52 rows ) Cartesian product is generated when WHERE clause is omitted SELECT first_name, department_name FROM employees, departments;

33 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Joining tables Oracle syntax Write JOIN condition in WHERE clause Prefix column name with table name when the same column appears in both tables. SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2 SELECT first_name, department_name FROM employees, departments WHERE employees. department_id = departments. department_id;

34 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Joining tables What is in WHERE clause? SELECT ename, dname FROM employees, departments WHERE employees.department_id= departments.department_id; FK PK

35 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Joining more than two tables SELECT table1.column, table2.column, table3.column FROM table1, table2, table3 WHERE table1.column1 = table2.column2 AND table2.column3 = table3.column4

36 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Outer join Oracle syntax Use outer join to see rows that do not meet the join condition Outer join is (+) operator SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 (+) = table2.column2; SELECT first_name, department_name FROM employees, departments WHERE employees.department_id(+)= departments.department_id; SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2 (+);

37 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ?Question You want to create a report displaying employee, department names and locations. Which query should you use to create an equi-join. 1.SELECT first_name, department_id, location_id FROM employees, departments; 2.SELECT emp.first_name, dept.department_id, dept.location_id FROM employees e, departments d WHERE e. department_id =d.department_id 3.SELECT e. first_name, d.department_id, d.location_id FROM employees e, departments d WHERE manager_id = manager_id 4.SELECT e.first_name, d.department_id, d. location_id FROM employees e, departments d WHERE e.department_id =d.department_id

38 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Subqueries Subqueries allow the results from one query to be passed directly into another query. A subquery is sometimes called an ‘inner query’, the parent query being called the ‘outer query’. Subqueries can return none, one or more than one row to the parent query in which it has been embedded. Subqueries can be correlated or non correlated.

39 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Subqueries syntax SELECT column1, … FROM table1 WHERE condition expr (SELECT column2 FROM table 2 WHERE condition); SELECT column1, (SELECT column2 FROM table 2 WHERE condition) FROM table1 WHERE condition;

40 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Subqueries examples SELECT first_name, department_id, (SELECT department_name FROM departments WHERE departments.department_id = employees.department_id) FROM employees SELECT first_name, department_id FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_name LIKE '%A%')

41 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SELECT from SELECT SELECT table1.column1, table2.column2, … FROM table1, (SELECT column1, column2 FROM table 2 WHERE condition) vtable2 WHERE table1.column = vtable2.column2

42 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. IN / EXISTS condition SELECT department_id FROM departments WHERE NOT EXISTS (SELECT null FROM employees WHERE employees.department_id = departments.department_id) SELECT department_id FROM departments WHERE department_id NOT IN (SELECT emp.department_id FROM employees emp)

43 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. ?Question Evalute the SQL statement: What is the result when the query is executed ? SELECT * FROM departments WHERE department_id = (SELECT t.department_id FROM employees t WHERE t.job_id = ‘IT_PROG’)

44 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL_1 Please write statement which lists the name, salary and commision for all the employees who have a manager and earn more than 1500.

45 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL_2 You would like to display the name of employee with his salary and name of his supervisor. If the supervisor isn’t specified for the employee, the value should be NULL. Please write this SQL statement.

46 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Performance tuning What shall be checked before we start optimalization Understand how our query is executed Understand if it can run faster Chose best option (execution plan)

47 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Try to optimize the query: SELECT * FROM employees_opt eo WHERE eo.last_name = 'Mavris'; 00 sql for optimalization.sql

48 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization As the fisrt step of optimalization you shall check if statistics calculated for tables used in the query are in place. TIP: use user_tables view Useful columns of user_tables view: table_name – the name of the table num_rows – number of rows in the table blocks – number of blocks in the table avg_row_len – average row length of the table last_analyzed – the last date of gather statistics for table

49 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Answer: SELECT table_name, num_rows, blocks, avg_row_len, to_char(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') FROM user_tables WHERE table_name = 'EMPLOYEES_OPT'; 01 user_tables.sql

50 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization As the second step of optimalization you shall check if statistics calculated for indexes of tables used in the query are in place TIP use user_indexes view. Useful columns of user_indexes view: table_name – the name of the table index_name – the name of the index num_rows – number of rows in the index distinct_keys – number of distinct values in the index last_analyzed – the last date of gather statistics for index

51 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Answer: SELECT index_name, to_char(last_analyzed, 'YYYY-MM-DD HH24:MI:SS'), num_rows, distinct_keys FROM user_indexes WHERE table_name = 'EMPLOYEES_OPT' ORDER BY index_name; 02 user_indexes.sql

52 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Check also date of histograms for tables used in the query. TIP: use user_tab_histograms view. Useful columns of user_tab_histograms view: table_name – the name of the table column_name – the name of the column on which histograms was calculated endpoint_number – histogram bucket number endpoint_value – normalized endpoint value for this bucket

53 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Answer: 03 user_tab_histograms.sql SELECT table_name, column_name, endpoint_number, endpoint_value FROM user_tab_histograms WHERE table_name = 'EMPLOYEES_OPT';

54 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization If it is necessary, gather the statistics use procedure listed below: EXEC dbms_stats.gather_table_stats( ownname=>'HR', tabname=>'EMPLOYEES_OPT', method_opt=>'for all columns size auto', estimate_percent => 10, cascade=> true); gather statistics for all table columns, size auto means that Oracle determines the columns to collect histograms based on data distribution and the workload of the columns gather statistics on the indexes for this table also % of rows to scan 04 gather_stats.sql

55 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Execute the query which you want to optimize: SELECT * FROM employees_opt eo WHERE eo.last_name = 'Mavris'; 00 sql for optimalization.sql

56 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Check total of memory blocks read and total of disk blocks read per execution for SQL statement. TIP: use v$sql view. Useful columns of v$sql view: hash_value – a hash value for the statement which is guaranteed to be unique disk_reads – cumulative total of disk blocks read for this statement buffer_gets – cumulative total of memory blocks read for this statement executions – how many times the statement has been executed sql_text – the first 1000 characters of the statement rows_processed – cumulative total of rows processed by this statement

57 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Answer: SELECT hash_value, disk_reads, buffer_gets, executions, round(disk_reads / executions, 2), round(buffer_gets / executions, 2), sql_text, rows_processed FROM v$sql WHERE executions != 0 AND lower(sql_text) LIKE '%select *%from employees_opt eo%where eo.last_name = ''mavris''%' ORDER BY 6 DESC; 05 v$sql.sql

58 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Receive explain plan for the query TIP: use v$sql_plan, v$sql view. Useful columns of v$sql_plan view: hash_value – hash value of the parent statement in the library cache id – a number assigned to each step in the execution plan parent_id – ID of the next execution step that operates on the output of the current step depth – depth (or level) of the operation in the tree cost – cost of the operation as estimated by the optimizer's cost-based approach operation – name of the internal operation performed in this step (for example, TABLE ACCESS) options – a variation on the operation described in the OPERATION column (for example, FULL) object_name - name of the table or index

59 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Answer: SELECT DISTINCT id, parent_id, depth, cost, lpad(' ', LEVEL - 1) || operation || ' ' || options operation, object_name FROM (SELECT id, parent_id, depth, cost, operation, options, object_name FROM v$sql_plan WHERE hash_value = (SELECT MAX(hash_value) FROM v$sql WHERE lower(sql_text) LIKE '%select *%from employees_opt eo%where eo.last_name = ''mavris''%')) START WITH id = 0 CONNECT BY PRIOR id = parent_id; 06 execution plan.sql

60 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization HASH_VA LUE DISK_R EADS BUFFER_ GETS EXECUTI ONS ROUND(DISK_RE ADS/EXECUTION S,2) ROUND(BUFFER_GETS/ EXECUTIONS,2 SQL_TEX T ROWS_PROC ESSED 14413436 950 25 32210 12 661 select * from employee s_opt eo where eo.last_na me = 'Mavris'1 ID PAREN T_IDDEPTHCOSTOPERATIONOBJECT_NAME 101 3 498 TABLE ACCESS FULLEMPLOYEES_OPT 0 0 3 498 SELECT STATEMENT

61 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization ? Share your ideas what shall be done

62 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Create index on last_name and first_name column of employees_opt table. Gather statistics for new index. create index EMP_OPT_NAME_IX on employees_opt (LAST_NAME, FIRST_NAME); EXEC dbms_stats.gather_index_stats( 'HR', 'EMP_OPT_NAME_IX');

63 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Check statistics for indexes for EMPLOYEES_OPT table. TIP:use user_indexes view. 02 user_indexes.sql

64 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Execute the query which you want to optimize: SELECT * FROM employees_opt eo WHERE eo.last_name = 'Mavris'; 00 sql for optimalization.sql

65 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Check total of memory blocks read and total of disk blocks read per execution and execution plan. TIP: use v$sql view. TIP: use v$sql_plan, v$sql view. 05 v$sql.sql 06 execution plan.sql

66 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization HASH_VA LUE DISK_R EAD S BUFFER_ GETS EXECUTI ONS ROUND(DISK_RE ADS/EXECUTI ONS,2) ROUND(BUFFER_GETS/ EXECUTIONS,2 SQL_TEX T ROWS_PROC ESSED 30807466 790 1420 select * from emplo yees_ opt eo1 where eo1.la st_na me = 'Mavri s'2 ID PAREN T_I DDEPTHCOSTOPERATIONOBJECT_NAME 101 4 TABLE ACCESS BY INDEX ROWIDEMPLOYEES_OPT 212 3 INDEX RANGE SCANEMP_OPT_NAME_IX 0 0 4 SELECT STATEMENT

67 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Conclusion: In this case index range access is better than full access.

68 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Execute the query which you want to optimize: 10 sql for optimalization.sql SELECT COUNT(DISTINCT eo.employee_id) FROM employees_opt eo WHERE eo.last_name LIKE 'King%';

69 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Check total of memory blocks read and total of disk blocks read per execution and execution plan. TIP: use v$sql view. TIP: use v$sql_plan, v$sql view. 11 v$sql.sql 12 execution plan.sql

70 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization HASH_VA LUE DISK_R EADS BUFFER_ GETS EXECUTI ONS ROUND(DISK_RE ADS/EXECUTION S,2) ROUND(BUFFER_GETS/ EXECUTIONS,2 SQL_TEX T ROWS_PROC ESSED 23425505 98461 205 1121461 205 112 select count(disti nct eo.employ ee_id) from employee s_opt eo where eo.last_na me like 'King%'1 ID PAREN T_IDDEPTHCOSTOPERATIONOBJECT_NAME 101 SORT GROUP BY 212 4 TABLE ACCESS BY INDEX ROWIDEMPLOYEES_OPT 0 0 4 SELECT STATEMENT 323 3 INDEX RANGE SCANEMP_OPT_NAME_IX

71 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization ? Share your ideas what shall be done

72 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Execute the query which you want to optimize: 13 sql for optimalization.sql SELECT /*+ FULL(eo) */ COUNT(DISTINCT eo.employee_id) FROM employees_opt eo WHERE eo.last_name LIKE 'King%';

73 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Check total of memory blocks read and total of disk blocks read per execution and execution plan. TIP: use v$sql view. TIP: use v$sql_plan, v$sql view. 11 v$sql.sql 12 execution plan.sql

74 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization HASH_VA LUE DISK_R EADS BUFFER_ GETS EXECUTI ONS ROUND(DISK_RE ADS/EXECUTION S,2) ROUND(BUFFER_GETS/ EXECUTIONS,2 SQL_TEX T ROWS_PROC ESSED 27655663 6377 12 6661377 12 666 select /*+ FULL(eo) */ count(disti nct eo.employ ee_id) from employee s_opt eo where eo.last_na me like 'King%'1 ID PAREN T_IDDEPTHCOSTOPERATIONOBJECT_NAME 101 SORT GROUP BY 212 3 509 TABLE ACCESS FULLEMPLOYEES_OPT 0 0 3 509 SELECT STATEMENT

75 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Practice SQL query optimalization Conclusion: In this case table full scan is better than index range access.


Download ppt "Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 1."

Similar presentations


Ads by Google