0% found this document useful (0 votes)
18 views15 pages

DB2 - Session04 1

The document discusses static and dynamic SQL in DB2 and provides examples of their syntax. It also discusses indexes in DB2 including what they are, why they are used, guidelines for creation and usage, and syntax for index creation. Other DB2 objects like views, aliases and synonyms are also briefly covered.

Uploaded by

Nagamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

DB2 - Session04 1

The document discusses static and dynamic SQL in DB2 and provides examples of their syntax. It also discusses indexes in DB2 including what they are, why they are used, guidelines for creation and usage, and syntax for index creation. Other DB2 objects like views, aliases and synonyms are also briefly covered.

Uploaded by

Nagamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DB2 1

DB2 – Session 4
DB2 2

Static SQL

• Hard-coded into an application program


• cannot be modified during the program’s execution
except for changes to the values assigned to the host
variables
• Cursors are used to access set-level data (i.e when a
SQL SELECT returns more than 1 row)
• The general form is EXEC SQL
[SQL statements]
END-EXEC.
DB2 3

Dynamic SQL

• Statements can change throughout the program’s


execution
• When the SQL is bound, the application plan or
package that is created does not contain the same
information as that for a static SQL program
• The access paths cannot be determined before
execution
DB2 4

Indexes
What is an Index ?
‘An index is an ordered set of pointers to rows of a
base table’.
Or
‘An Index is a balanced B-tree structure that orders
the values of columns in a table’

Why an Index ?
‘One can access data directly and more efficiently’
DB2 5
Indexes (contd...)
• Each index is based on the values of data in one or
more columns. An index is an object that is separate
from the data in the table.
• When you define an index using the CREATE INDEX
statement, DB2 builds this structure and maintains it
automatically.
• Indexes can be used by DB2 to improve performance
and ensure uniqueness.
• In most cases, access to data is faster with an index.
• A table with a unique index cannot have rows with
identical keys.
DB2 6

Indexes (contd...)
Syntax : For creation of an Index

CREATE INDEX <indexname> ON <tabname>


(colname
asc/desc)
DB2 7
Index Parameters for Creation
• CLUSTER
• USING STOGROUP/VCAT (the corresponding name)
• FREEPAGE
• PCTFREE
• PRIQTY / SECQTY
• BUFFERPOOL
• CLOSE - Yes/No
• ERASE Yes/No
DB2 8

Index Guidelines - What to do ?


1. Consider indexing on columns used in UNION,
DISTINCT, GROUP BY, ORDER BY & WHERE
clauses.
2. Limit the indexing of frequently updated columns
3. Create explicitly, a clustering index
4. Create a unique index on the primary key and
indexes on foreign keys
DB2 9

Index Guidelines (contd...)


5. Overloading of index when row length of a table to
be accessed is short
6. Atleast one index must be defined for a table with
more than 100 pages
7. Use Multicolumn index rather than a multi-index
(appln dependent); however the latter requires more
DASD .
DB2 10

Index Guidelines (contd...)


8. Create indexes before loading the table.
9. Clustering reduces I/O; DB2 optimizer usually tries
to use an index on clustered column before using the
other indexes.
10. Specify Indexspace freespace the same as
tablespace freespace
DB2 11

Index Guidelines (contd...)


11. Use the DEFER option while creating the index.
RECOVER INDEX utility can then be used to populate
the index. Recover utility populates index entries faster.
12. Use different STOGROUP’s for Tablespaces &
indexspaces
13. Create Critical indexes in a different bufferpool than
the tablespaces.
DB2 12

Index Guidelines - What Not to do ?

1. Avoid indexing on Variable columns


2. Limit the number of indexes on partitioned TS
3. Avoid indexes if
• the table is very small (< 10 pages)
• it has heavy inserts and deletes and is relatively
small (< 20 pages)
• it is accessed with a scan.
4. Avoid defining redundant indexes
DB2 13

Other DB2 Objects

VIEWS
• It is a logical derivation of a table from other
table/tables. A View does not exist in its own right.
• They provide a certain amount if logical independence
• They allow the same data to be seen by different users
in different ways
• In DB2 a view that is to accept a update must be
derived from a single base table
DB2 14

DB2 Objects (contd...)


Aliases
• Mean ‘another name’ for the table.
• Aliases are used basically for accessing remote tables
(in distributed data processing), which add a location
prefix to their names.
• Using aliases creates a shorter name.

Synonym
• Also means another name for the table, but is private to
the user who created it.
DB2 15

DB2 Objects (contd...)


Syntax:
CREATE VIEW <Viewname> (<columns>)
AS Subquery (Subquery - SELECT FROM
other Table(s))

CREATE ALIAS <Aliasname> FOR <Tablename>

CREATE SYNONYM <Synonymname> FOR


<Tablename>

You might also like