Matura: Softwareentwicklung & Informationssysteme

9.2. Views und Indizes

Was sind Views?

Eine View ist eine SQL SELECT-Abfrage mit einem Namen.

Quelle: Views-MySQL (S. 3)

Eigenschaften und Vorteile

Quelle: Views-MySQL (S. 4-5, 9)

Wie erstelle ich eine View?

CREATE VIEW SoftwareVerkaeufe AS
    SELECT v.kaeufer, v.verkaeufer
    FROM   produkte p, verkaeufe v
    WHERE  p.produkt_id = v.produkt_id 
    AND    p.produkt = "Software"

Quelle: https://de.wikipedia.org/wiki/Sicht_(Datenbank)

Wie bestimme ich die Ausführungsart

Der Algorithmus bestimmt, wie MySQL eine Ansicht verarbeitet, und kann einen der drei Werte MERGE, TEMPTABLE und UNDEFINE annehmen.

CREATE ALGORITHM=MERGE VIEW SoftwareVerkaeufe AS
    SELECT v.kaeufer, v.verkaeufer
    FROM   produkte p, verkaeufe v
    WHERE  p.produkt_id = v.produkt_id 
    AND    p.produkt = "Software"

Quelle: Views-MySQL (S. 18)

Erkläre MERGE, TEMPTABLE, UNDEFINED

MERGE

CREATE ALGORTIHM=MERGE VIEW contactPersons(customerName, firstName, lastName, phone) AS SELECT customerName, contactFirstName, contactLastName, phone FROM customers;
/* you execute */
SELECT * FROM contactPersons WHERE customerName LIKE '%Co%';

/* mysql executes */
SELECT customerName, contactFirstName, contactLastName, phone FROM customers WHERE customerName LIKE '%Co%';

TEMPTABLE

UNDEFINED

Quelle: Views-MySQL (S. 19-22)

Erkläre ALTER View, Updateable Views und Beispiel

ALTER VIEW

ALTER ALGORITHM=MERGE VIEW salesOrders AS
    SELECT orderNumber, customerNumber, productCode, quantityOrdered, priceEach, status
    FROM orders
    INNER JOIN orderDetails USING (oderNumber); 

Updatable Views

/* erstellen der view */
CREATE VIEW officeInfo AS SELECT officeCode, phone, city FROM offices; 

/* beispiel: editieren einer tupel */
UPDATE officeInfo SET phone = '+33 14 723 5555' WHERE officeCode = 4; 

Quelle: Views-MySQL (S. 24-26)

Erkläre die WITH CHECK OPTION clause, Beispiel

CREATE OR REPLACE VIEW dummy AS SELECT ... WHERE name LIKE %ei%;

INSERT INTO dummy(name, ...) VALUES ('Wurst', ..);
/* nun würde die tupel hinzugefügt werden */

CREATE OR REPLACE VIEW dummy AS SELECT ... WHERE name LIKE %ei% WITH CHECK OPTION;

INSERT INTO dummy(name, ...) VALUES ('Wurst', ..);
/* nun würde die tupel nicht hinzugefügt werden -> error */

Quelle: Views-MySQL (S. 29-30)

Was sind Indizes?

Quelle: Index-MySQL (S. 2)

Wie erstelle ich einen Index?

/* Festlegen eines Index beim Erstellen der Tabelle */
CREATE TABLE t (
    c1 INT PRIMARY KEY,
    c2 INT NOT NULL,
    c3 INT NOT NULL,
    c4 VARCHAR(10),
    INDEX (c2, c3)
);

/* Nach der Erstellung der Tabelle einen Index definieren */
CREATE INDEX idx_c4 ON t(c4);

Quelle: Index-MySQL (S. 4)

Wie werden sie gespeichert?

Composite Index

CREATE TABLE table_name (
    c1 data_type PRIMARY KEY,
    c2 data_type,
    c3 data_type,
    c4 data_type,
    INDEX index_name (c2,c3,c4)
);

CREATE INDEX index_name ON table_name (c2, c3, c4);´

Funktioniert für Abfragen

Quelle: Index-MySQL (S. 19-21)

Invisible indices

CREATE INDEX index_name ON table_name(c1,c2,...) INVISIBLE;

ALTER TABLE table_name ALTER INDEX index_name [VISIBLE | INVISIBLE];

/* Anzeigen aller Indizes */
SELECT index_name, is_visible
FROM information_schema.statistics
WHERE table_schema = ...
  AND table_name = ...;

Quelle: Index-MySQL (S. 22-24)

Descending index

CREATE TABLE t (
    a INT NOT NULL,
    b INT NOT NULL,
    INDEX a_asc_b_asc (a ASC, b ASC),
    INDEX a_asc_b_desc (a ASC, b DESC),
    INDEX a_desc_b_asc (a DESC, b ASC)
);

EXPLAIN SELECT * FROM t ORDER BY a, b; -- use index a_asc_b_asc

Quelle: Index-MySQL (S. 25-29)

Use force Index

/* Use Index Hint */
SELECT select_list FROM table_name USE INDEX(index_list) WHERE condition;

/* Force Index */
SELECT * FROM table_name FORCE INDEX (index_list) WHERE condition;

Quelle: Index-MySQL (S. 30-33)