1
+ ------------------ SQL (Oracle applied)
2
+ What are DML and DDL
3
+ DDL
4
+ Data definition language (DDL) statements define, structurally change, and
5
+ drop schema objects.
6
+ An implicit COMMIT occurs immediately before the database executes a DDL
7
+ statement and a COMMIT or ROLLBACK occurs immediately afterward.
8
+ DML
9
+ Data manipulation language (DML) statements query or manipulate data in
10
+ existing schema objects. Whereas DDL statements enable you to change the
11
+ structure of the database, DML statements enable you to query or change the
12
+ contents. For example, ALTER TABLE changes the structure of a table, whereas
13
+ INSERT adds one or more rows to the table.
14
+
15
+ Agregate functions with examples
16
+ todo
17
+
18
+ What is a nested subquery?
19
+ A subquery is a SELECT statement nested within another SQL statement. Subqueries
20
+ are useful when you must execute multiple queries to solve a single problem.
21
+ These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.
22
+ Example:
23
+ SELECT first_name, last_name
24
+ FROM employees
25
+ WHERE department_id
26
+ IN (SELECT department_id FROM departments WHERE location_id = 1800);
27
+
28
+ Types of indices in Oracle
29
+ Oracle Database provides several indexing schemes, which provide complementary
30
+ performance functionality. The indexes can be categorized as follows:
31
+ B-tree indexes
32
+ These indexes are the standard index type. They are excellent for primary
33
+ key and highly-selective indexes. Used as concatenated indexes, B-tree
34
+ indexes can retrieve data sorted by the indexed columns.
35
+ B-tree indexes have the following subtypes:
36
+ � Index-organized tables
37
+ An index-organized table differs from a heap-organized because the data is
38
+ itself the index. See "Overview of Index-Organized Tables" on page 3-20.
39
+ � Reverse key indexes
40
+ In this type of index, the bytes of the index key are reversed, for
41
+ example, 103 is stored as 301. The reversal of bytes spreads out inserts
42
+ into the index over many blocks.
43
+ � Descending indexes
44
+ This type of index stores data on a particular column or columns in
45
+ descending order.
46
+ � B-tree cluster indexes
47
+ This type of index is used to index a table cluster key. Instead of
48
+ pointing to a row, the key points to the block that contains rows related
49
+ to the cluster key.
50
+ Bitmap and bitmap join indexes
51
+ In a bitmap index, an index entry uses a bitmap to point to multiple rows.
52
+ In contrast, a B-tree index entry points to a single row. A bitmap join
53
+ index is a bitmap index for the join of two or more tables.
54
+ Function-based indexes
55
+ This type of index includes columns that are either transformed by a
56
+ function, such as the UPPER function, or included in an expression.
57
+ B-tree or bitmap indexes can be function-based.
58
+ Application domain indexes
59
+ This type of index is created by a user for data in an application-specific
60
+ domain. The physical index need not use a traditional index structure and
61
+ can be stored either in the Oracle database as tables or externally as a
62
+ file.
63
+
64
+ What is a hierarchical query? How to create it?
65
+ http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm
66
+ If a table contains hierarchical data, then you can select rows in a hierarchical order using the hierarchical query clause:
67
+ [ START WITH condition ] CONNECT BY [ NOCYCLE ] condition
68
+ Example:
69
+ 1)show EXPLAIN PLAN
70
+ -- EXPLAIN PLAN
71
+ delete PLAN_TABLE where statement_id = 'ago01';
72
+ EXPLAIN PLAN SET STATEMENT_ID = 'ago01' FOR
73
+ ---------------------- put query below
74
+ select /*+ FIRST_ROWS(25) */
75
+ decode(message_type, '1','personal', '2','broadcast') as mess_type,
76
+ eb_messages_data.*
77
+ from eb_messages_data
78
+ where message_id in (select message_id
79
+ from eb_disposer_mailboxes
80
+ where disposer_number like '%3140007')
81
+ and message_type = '2';
82
+ ----------------------- put query above
83
+
84
+ select * from PLAN_TABLE where STATEMENT_ID = 'ago01';
85
+
86
+ SELECT lpad('.',level-1, '.')||operation||' '|| options||' '||object_type||' '||object_name "Plan",
87
+ cost, bytes, cardinality, access_predicates || filter_predicates as predicates
88
+ FROM PLAN_TABLE
89
+ CONNECT BY prior id = parent_id
90
+ AND prior statement_id = statement_id
91
+ START WITH id = 0
92
+ AND statement_id = 'ago01'
93
+ ORDER BY id;
94
+ 2)CONNECT BY Example
95
+ SELECT employee_id, last_name, manager_id
96
+ FROM employees
97
+ CONNECT BY PRIOR employee_id = manager_id;
98
+ 3)level example
99
+ SELECT employee_id, last_name, manager_id, LEVEL
100
+ FROM employees
101
+ CONNECT BY PRIOR employee_id = manager_id;
102
+ 4)START WITH Examples
103
+ SELECT last_name, employee_id, manager_id, LEVEL
104
+ FROM employees
105
+ START WITH employee_id = 100
106
+ CONNECT BY PRIOR employee_id = manager_id
107
+ ORDER SIBLINGS BY last_name;
108
+
109
+ What is query tuning? How to tune a query?
110
+ http://www.dba-oracle.com/art_sql_tune.htm
111
+ http://www.akadia.com/services/ora_query_tuning.html
112
+ http://www.orafaq.com/wiki/Oracle_database_Performance_Tuning_FAQ
113
+
114
+ Partitioning and methods of partitioning
115
+ Partitioning enables you to decompose very large tables and indexes into
116
+ smaller and more manageable pieces called partitions. Each partition is an
117
+ independent object with its own name and optionally its own storage characteristics.
118
+ From the perspective of an application, only one schema object exists. DML statements require no modification
119
+ to access partitioned tables. Partitioning is useful for many different types of database applications, particularly
120
+ those that manage large volumes of data. Benefits include:
121
+ Increased availability
122
+ Easier administration of schema objects
123
+ Reduced contention for shared resources in OLTP systems
124
+ Enhanced query performance in data warehouses
125
+ Partitioning Strategies:
126
+ Range Partitioning
127
+ CREATE TABLE time_range_sales
128
+ ( prod_id NUMBER(6)
129
+ , cust_id NUMBER
130
+ , time_id DATE
131
+ , channel_id CHAR(1)
132
+ , promo_id NUMBER(6)
133
+ , quantity_sold NUMBER(3)
134
+ , amount_sold NUMBER(10,2)
135
+ )
136
+ PARTITION BY RANGE (time_id)
137
+ (PARTITION SALES_1998 VALUES LESS THAN (TO_DATE('01-JAN-1999','DD-MON-YYYY')),
138
+ PARTITION SALES_1999 VALUES LESS THAN (TO_DATE('01-JAN-2000','DD-MON-YYYY')),
139
+ PARTITION SALES_2000 VALUES LESS THAN (TO_DATE('01-JAN-2001','DD-MON-YYYY')),
140
+ PARTITION SALES_2001 VALUES LESS THAN (MAXVALUE)
141
+ );
142
+ List Partitioning
143
+ CREATE TABLE list_sales
144
+ ( prod_id NUMBER(6)
145
+ , cust_id NUMBER
146
+ , time_id DATE
147
+ , channel_id CHAR(1)
148
+ , promo_id NUMBER(6)
149
+ , quantity_sold NUMBER(3)
150
+ , amount_sold NUMBER(10,2)
151
+ )
152
+ PARTITION BY LIST (channel_id)
153
+ (PARTITION even_channels VALUES (2,4),
154
+ PARTITION odd_channels VALUES (3,9)
155
+ );
156
+ Hash Partitioning
157
+ CREATE TABLE hash_sales
158
+ ( prod_id NUMBER(6)
159
+ , cust_id NUMBER
160
+ , time_id DATE
161
+ , channel_id CHAR(1)
162
+ , promo_id NUMBER(6)
163
+ , quantity_sold NUMBER(3)
164
+ , amount_sold NUMBER(10,2)
165
+ )
166
+ PARTITION BY HASH (prod_id)
167
+ PARTITIONS 2;
168
+
169
+ ------------------ DB
170
+ Primary key vs unique key. Differences.
171
+
172
+ What types of constraints does one know?
173
+ NOT NULL
174
+ Allows or disallows inserts or updates of rows
175
+ containing a null in a specified column.
176
+ Unique key
177
+ Prohibits multiple rows from having the same value in
178
+ the same column or combination of columns but allows
179
+ some values to be null.
180
+ Primary key
181
+ Combines a NOT NULL constraint and a unique constraint. It prohibits
182
+ multiple rows from having the same value in the same column or combination
183
+ of columns and prohibits values from being null
184
+ Foreign key
185
+ Designates a column as the foreign key and establishes a relationship
186
+ between the foreign key and a primary or unique key, called the referenced
187
+ key.
188
+ Check
189
+ Requires a database value to obey a specified condition.
190
+ REF
191
+ Dictates types of data manipulation allowed on values in a REF column and
192
+ how these actions affect dependent values. In an object-relational database,
193
+ a built-in data type called a REF encapsulates a reference to a row object
194
+ of a specified object type. Referential integrity constraints on REF columns
195
+ ensure that there is a row object for the REF.
196
+
197
+ What is a view/materialized view.
198
+ View
199
+ A view is a logical representation of one or more tables. In essence,
200
+ a view is a stored query. A view derives its data from the tables on which
201
+ it is based, called base tables. Base tables can be tables or other views.
202
+ All operations performed on a view actually affect the base tables. You can
203
+ use views in most places where tables are used.
204
+ Materialized View
205
+ Materialized views are query results that have been stored or "materialized"
206
+ in advance as schema objects. The FROM clause of the query can name tables,
207
+ views, and materialized views. Collectively these objects are called master
208
+ tables (a replication term) or detail tables (a data warehousing term).
209
+
210
+ What kind of joins do you know?
211
+ Inner joins
212
+ An inner join is a join of two or more tables that returns only rows that
213
+ satisfy the join condition. For example, if the join condition is employees.
214
+ department_id=departments.department_id, then rows that do not satisfy this
215
+ condition are not returned.
216
+ Outer joins
217
+ An outer join returns all rows that satisfy the join condition and also
218
+ returns rows from one table for which no rows from the other table satisfy
219
+ the condition. For example, a left outer join of employees and departments
220
+ retrieves all rows in the employees table even if there is no match in
221
+ departments. A right outer join retrieves all rows in departments even if
222
+ there is no match in employees.
223
+ Cartesian products
224
+ If two tables in a join query have no join condition, then the database
225
+ returns their Cartesian product. Each row of one table combines with each
226
+ row of the other. For example, if employees has 107 rows and departments
227
+ has 27, then the Cartesian product contains 107*27 rows. A Cartesian
228
+ product is rarely useful.
229
+
230
+ Types of tables (regular, temporary, index-organized etc)
231
+ Oracle Database tables fall into the following basic categories:
232
+ Relational tables: Relational tables have simple columns and are the most common table type.
233
+ A heap-organized table
234
+ does not store rows in any particular order. The CREATE
235
+ TABLE statement creates a heap-organized table by default.
236
+ An index-organized table
237
+ orders rows according to the primary key values. For
238
+ some applications, index-organized tables enhance performance and use disk
239
+ space more efficiently.
240
+ An external table
241
+ is a read-only table whose metadata is stored in the database but
242
+ whose data in stored outside the database.
243
+ Object tables: The columns correspond to the top-level attributes of an object type.
244
+ --
245
+ A table is either permanent or temporary. A permanent table definition and data
246
+ persist across sessions. A temporary table definition persists in the same way as a
247
+ permanent table definition, but the data exists only for the duration of a transaction or
248
+ session. Temporary tables are useful in applications where a result set must be held
249
+ temporarily, perhaps because the result is constructed by running multiple operations.
250
+
251
+ Are database Indexes useful? What is the role of them?
252
+ An index is an optional structure, associated with a table or table cluster,
253
+ that can sometimes speed data access. By creating an index on one or more
254
+ columns of a table, you gain the ability in some cases to retrieve a small
255
+ set of randomly distributed rows from the table. Indexes are one of many means
256
+ of reducing disk I/O.
257
+ If a heap-organized table has no indexes, then the database must perform a
258
+ full table scan to find a value. For example, without an index, a query of
259
+ location 2700 in the hr.departments table requires the database to search
260
+ every row in every table block for this value. This approach does not scale
261
+ well as data volumes increase.
262
+
263
+ Why many indexes are not good for performance
264
+ todo
265
+
266
+ What does it mean database de-normalization?
267
+ todo
268
+
269
+ Which of SELECT, UPDATE, DELETE, ADD�s performance is mostly affected by performance of indexes?
270
+ todo
271
+
272
+ What are ways to increase performance of database
273
+ todo
0 commit comments