Index and stuff Flashcards

1
Q

Why use Views from JDBC?

A

Why use Views from JDBC
• Security
– Separate permissions on VIEW (e.g. read-only, potentially
enforced with trigger)
• Hide data
– Tables may contain sensitive data that not everyone needs
access to (e.g. credit card numbers)
• Hide complexity
– Queries may be complicated with joins, unions, subqueries, etc.
This complexity can be hidden with a view
• Support legacy code
– Refactoring a table may break a lot of code. A view can preserve
the original look of a table while the latter changes in the
background.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How costly is this operation (naive solution)?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain Index.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the structure of Disk and Main Memory.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Some (over-simplified) typical costs of disk
accessing for database operations on a
relation stored over n blocks are:

A

Some (over-simplified) typical costs of disk
accessing for database operations on a
relation stored over n blocks:
– Query the full relation: n (disk operations)
– Query with the help of index: k, where k is the
number of blocks pointed to (1 for key).
– Access index: 1
– Insert new value: 2 (one read, one write)
– Update index: 2 (one read, one write)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

SELECT *
FROM Lectures
WHERE course = ’TDA357’
AND period = 3;

How to solve this faster?

A

Assume Lectures is stored in n disk blocks. With no
index to help the lookup, we must look at all rows,
which means looking in all n disk blocks for a total
cost of n.
With an index, we find that there are 2 rows with the
correct values for the course and period attributes.
These are stored in two different blocks, so the total
cost is 3 (2 blocks + reading index).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

With no Index?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

With Index?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to Create Index in DMBS?

A

CREATE INDEX
• Most DBMS support the statement
CREATE INDEX index name
ON table (attributes);
– Example:

CREATE INDEX courseIndex
ON Courses (code);

– Statement not in the SQL standard, but most
DBMS support it anyway.
– Primary keys are given indexes implicitly (by
the SQL standard).
– In PostgreSQL, use \di to list indexes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Important properties of index.

A

Important properties
• Indexes are separate data stored by itself.
§ Can be created
üon newly created relations
üon existing relations
- will take a long time on large relations.
§ Can be dropped without deleting any table data.
• SQL statements do not have to be
changed
– a DBMS automatically uses any indexes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why don’t we have indexes on all (combinations of) attributes for faster lookups?

A

– Indexes require disk space.
– Modifications of tables are more expensive.
• Need to update both table and index.
– Not always useful
• The table is very small.
• We don’t perform lookups over it (Note: lookups ≠ queries).
– Using an index costs extra disk block accesses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Summary – indexes

A

• Indexes make certain lookups and joins more
efficient.
– Disk block access matters.
– Multi-attribute indexes
• CREATE INDEX
• Usage analysis
– What are the expected operations?
– How much do they cost?
Σ(cost of operation)x(proportion of operations of that kind)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are natural key?

A

Natural keys
• A natural key is a key consisting of
attributes in the domain model.
• In some cases, no suitable natural key
exists.
– No suitably unique natural candidate key.
– Natural candidate key ”too large”.
– Natural candidate key ”not stable”.
–…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain Artificial key.

A

Artificial key
• Extra attribute added to a table with the
purpose of being the key.
– Does not exist in ”reality”
– Can be verified for correctness
– Can be distinguished from artificial keys on
other tables in database.
• Examples:
– Personal numbers, car registration numbers,
course codes, etc.

17
Q

Explain Surrogate Key.

A

Surrogate key
• System-generated key to replace the actual key
behind the covers.
– AUTO_INCREMENT, SEQUENCE, IDENTITY, …
– Totally unrelated to domain.
– NOT exposed to user modification – database
consistency would be at great risk!
• Remember: From the database perspective, application
programmers count as users!
– Example: post/comment IDs managed by Wordpress

18
Q

Explain exposed locators.

A

Exposed locators
• Unholy mix of artificial and surrogate keys:
– System-generated, non-verifiable value with no
relation to data model (like a surrogate key).
– … but exposed to user (like an artificial key).

”[Exposed locators] are handy for lazy, non-RDBMS
programmers who do not want to research or think!
This is the worst way to program in SQL.”
Joe Celko, SQL programming guru
http://www.

19
Q

Advantages with non-natural keys

A

Advantages
• Non-natural keys can be more compact.
– Smaller references, smaller indexes.
– Faster comparisons, faster joins.
• Non-natural keys are immutable.
– Not tied to data in domain, so changes of the data will not cause
key to change.
• (Recall: Oracle does not support ON UPDATE CASCADE)
– Applications never lose their reference to a particular row in the
database.
• …

20
Q

Disadvantages with non-natural keys

A

Disadvantages
• Non-natural keys may degrade performance.
– An extra key on a table requires an extra index to handle
external lookups on the natural key
• extra disk space to store index
• modifications become more costly
– Reference to non-natural key means external lookups on the
natural key in referencing table requires one or more extra joins.
• Non-natural keys may make maintenance harder.
– Harder to spot errors, in keys and in references.
• …

21
Q
A
22
Q
A
23
Q

Explain Denormalization.

A

Denormalization
• ”Re-compose” decomposed tables or
attributes, to avoid joining.
– Can think of this as pre-computing joins
– Trade-off: query speed vs. redundancy
– Are updates frequent?
– ”NULLs approach” for sub-entities and manyto-
at-most-one is a special case – both
composed tables have the same key, so less
data will be stored.

24
Q
A