Lecture 5 Index - View-SP - Trigger
Lecture 5 Index - View-SP - Trigger
DATABASES
Non-table Objects
NGUYEN Hoang Ha
Email: [email protected]
INDEX
Why indexing?
Indexing are one of the most important and useful tools for
achieving high performance in a relational database
Many database administrators consider indexes to be the
single most critical tool for improving database performance
An index is a data structure that contains a copy of some of
the data from one or more existing database tables
A database index provides an organizational framework that
the DBMS can use to quickly locate the information that it
needs
This can vastly improve the speed with which SQL queries
can be answered
3
Without index
𝑀𝑎𝑥𝑖𝑚𝑢𝑚 = 𝑛
4
With index
5 5
Index concepts
Indexes are created on one or more columns in a table
For example:
An index is created on a PK column
The index will contain the PK value for each row in the table, along with
each row’s ordinal position (row number) within the table
When a query involving the PK is run, the DBMS will find the PK value
within the index. The DBMS will then know the position of the row within
the table
The DBMS can then quickly locate the row in the table that is associated
with the PK value
6
Index concepts
An index can be created on most, but not all, columns.
Whether an index can be created on a column depends on
the column's datatype
Columns with large object data types cannot be indexed
without employing additional mechanisms These data types
include:
Text
ntext
Image
varchar (max)
Nvarchar(max)
varbinary(max)
7
Index concepts
Creating an index increases the amount of storage space
required by the database
This occurs because an index contains a copy of some of the data in a
table
To estimate the storage space requirements of an index, we can use
the following formula:
Number of rows in table x Average number of bytes
required per row for the indexed columns
8
B-Tree Index
Balance-Tree: the most common type of database indexing
B-trees use pointers and several layers of nodes in order to
quickly locate desired data
Root node
Intermediate nodes
Leaf nodes
When the DBMS processes a query which includes an
indexed column, it starts at the root node of the B-tree and
navigates downward until it finds the desired leaf
9
B-tree example
10
Clustered Indexes
In a clustered index, the actual data rows that comprise the
table are stored at the leaf level of the index
The indexed values are stored in a sorted order
This means that there can be only one clustered index per table
PK columns are good candidates for clustered indexes
11
Clustered B-Tree Example
12
Non-clustered index
The Non-Clustered index is an index structure separate
from the data stored in a table
→ A table can have more than one non-clustered index
Non-clustered indexes are slower than clustered indexes
because the DMBS must follow a pointer to retrieve the
actual data row.
The leaf nodes of a non-clustered index can optionally
contain values from non-indexed columns
13
Clustered vs. Nonclustered indexes
14
Indexing Guidelines
If a table is heavily updated, index as few columns as possible
If a table is updated rarely, use as many indexed columns as
necessary to achieve maximum query performance
Clustered indexes are best used on columns that do not
allow null values and whose value are unique
The performance benefits on an index are related to the
uniqueness of the values in the indexed column
Index performance is poor when an indexed column contains a large
proportion of duplicate values
Index performance is best when an indexed column contains unique
values
15
VIEW
View concept
A view is a “virtual” or logical table that is derived from
other tables
17
View
Uses:
Restrict data access
Hide sensitive data
Names of tables and columns
Simplify data
Reuse complex queries
18
Syntax
CREATE [ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}]
VIEW view_name [(column_list)]
AS
select-statement [WITH CHECK OPTION];
ALGORITHM
MERGE: MySQL combines input query with the select-statement.
MERGE is not allowed if the SELECT statement contains aggregate
functions or DISTINCT, GROUP
BY, HAVING, LIMIT, UNION, UNION ALL, subquery, SELECT
statement refers to no table.
TEMPTABLE: MySQL creates a temporary table based on the
SELECT statement that defines the view, then performs query against
this temporary table.
UNDEFINED: MySQL makes choice of MERGE or TEMTABLE.
19
Pros vs. Cons
Pros:
Simplify complex queries
Enable computed columns
Provide a security layer: hide sensitive data
Enable backward capability
Cons
Performance
Table dependency: table changes → need to change views
20
View examples
Computed columns
CREATE VIEW sale_per_order AS
SELECT order_id, SUM(quantity * unit_price * (1-discount)) total
FROM order_details
GROUP BY order_id
ORDER BY total DESC;
Based on a sub query
CREATE VIEW above_avg_products AS
SELECT product_code, product_name, list_price
FROM products
WHERE list_price > (SELECT AVG(list_price)
FROM products)
ORDER BY list_price DESC;
Based on another view
CREATE VIEW big_sale_orders AS
SELECT order_id, ROUND(total,2) AS total
FROM sale_per_order
WHERE total > 1000;
21
Updatable views
SELECT statement defining the view must not contain following
elements:
Aggregate functions such as MIN, MAX, SUM, AVG, and COUNT.
DISTINCT
GROUP BY clause.
HAVING clause.
UNION or UNION ALL clause.
Left join or outer join.
Subquery in the SELECT clause or in the WHERE clause that refers to
the table appeared in the FROM clause.
Reference to non-updatable view in the FROM clause.
Reference only to literal values.
Multiple references to any column of the base table
22
WITH CHECK OPTION Clause
Role: to prevent updating or inserting rows that are not
visible through the view
Example
CREATE OR REPLACE VIEW northwind_products
AS
SELECT id, product_code, product_name
FROM products
WHERE product_name LIKE 'Northwind%'
WITH CHECK OPTION;
UPDATE northwind_products
SET product_name = 'Nwd beer'
WHERE product_code = 'NWnew'; --- WITH CHECK OPTION will prevent this statement from running
23
View management
Show view definition
SHOW CREATE VIEW [database_name].[view_ name];
Delete view:
DROP VIEW [IF EXISTS] view_name
Change view
ALTER[ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}]
VIEW view_name [(column_list)]
AS
select-statement [WITH CHECK OPTION];
26
Pros vs. Cons
Pros:
Better performance
Reduce traffic
Be reusable and transparent
Provide a secure way to access data
Cons
CPU usage can increase if logical operators are overused
Hard to debug, maintain
27
Example
28
Input parameter
DELIMITER //
CREATE PROCEDURE get_customers_by_city(IN search_city nvarchar(255))
AS
BEGIN
SELECT * FROM customers
WHERE city = search_city;
END //
DELIMITER ;
CALL get_customers_by_city('Seattle');
29
Stored Procedure (SP)
SP is a collection of T-SQL statements that SQL Server compiles
into a single execution plan.
SP is stored in cache area of memory when it is first executed
so that it can be used repeatedly, not need recompiled
Parameters:
Input
Output
30
SP Syntax
[ENCRYPTION]
[RECOMPILE]
[EXECUTE AS username]
32
Types of SP
System stored procedure:
Name begins with sp_
Created in master database
For application in any database
Often used by sysadmins
33
Executing a SP
EXEC pr_GetTopProducts
With parameters
By Name:
EXEC pr_GetTopProducts
@StartID = 1, @EndID = 10
By Position:
EXEC pr_GetTopProducts 1, 10
Leveraging Default values
EXEC pr_GetTopProducts @EndID=10
Place parameters with default values at the end of the list for
flexibility of use
34
Output parameters
Used to send non-recordset information back to client
Example: returning identity field
GO
35
Encrypting stored procedures
When the stored procedures created, the text for
them is saved in the SysComments table.
If the stored procedures are created with the
“WITH ENCRYPTION” then the text in
SysComments is not directly readable
“WITH ENCRYPTION” is a common practice for
software vendors
36
Advantages of SP
Security
Code reuse, modular programming
Performance
Reduce traffic
37
Example: Reduced traffic
39
Variables
Declare a variable:
DECLARE @limit money
DECLARE @min_range int, @hi_range int
Assign a value into a variable:
SET @min_range = 0, @hi_range = 100
40
Control of Flow
BEGIN…END
IF…ELSE
CASE … WHEN
RETURN [n]
WHILE
PRINT
41
PRINT
Display message in SQL Query Analyze (Console)
USE AdventureWorks2008R2;
GO
IF (SELECT SUM(i.Quantity)
FROM Production.ProductInventory i
JOIN Production.Product p
ON i.ProductID = p.ProductID
WHERE Name = 'Hex Nut 17'
) < 1100
PRINT N'There are less than 1100 units of Hex Nut 17 in stock.'
GO
42
TRY CATCH structure
43
TRIGGERS
Trigger overview
Definition: A trigger is a special SP executed automatically as
part of a data modification (INSERT, UPDATE, or DELETE)
Associated with a table
Invoked automatically
Cannot be called explicitly
45
Syntax
CREATE TRIGGER trigger_name
ON <tablename>
<{FOR | AFTER}>
{[DELETE] [,] [INSERT] [,] [UPDATE]}
AS
SQL_Statement [...n]
46
Simplied Syntax
CREATE TRIGGER trg_one
ON tablename
FOR INSERT, UPDATE, DELETE Temporary table holding new
records
AS
BEGIN
SELECT * FROM Inserted
Temporary table holding old,
SELECT * FROM Deleted deleted, updated records
END
47
Uses of Triggers
Maintenance of duplicate and derived data
Ensure integrity
Complex column constraints
Cascading referential integrity
Inter-database referential integrity
Complex defaults
Logging/Auditing
Maintaining de-normalized data
48
Trigger example
Use Northwind
GO
CREATE TRIGGER Cust_Delete_Only1 ON Customers
FOR DELETE
AS
IF (SELECT COUNT(*) FROM Deleted) > 1
BEGIN
RAISERROR('You are not allowed to delete more than one customer at a
time.', 16, 1)
ROLLBACK TRANSACTION
END
Order Details
OrderID ProductID UnitPrice Quantity Discount ProductID UnitsInStock … …
10522 10 31.00 7 0.2 1 15
10523 41 9.65 9 0.15 2 10
5
10524 7 30.00 24 0.0 3 65
10523 2 19.00 5 0.2 4 20
Products
GO
UPDATE Products
SET UnitPrice = UnitPrice + 2
51
Enforcing integrity with Trigger
CREATE TRIGGER Products_Delete
ON Products FOR DELETE AS
IF (SELECT COUNT(*)
FROM [Order Details] OD
WHERE OD.ProductID = (SELECT ProductID FROM deleted)
) > 0
BEGIN
PRINT 'Violate Foreign key reference. Rollback!!!'
ROLLBACK TRAN
END
DELETE Products
WHERE ProductID = 11
52
Performance Considerations
Triggers work quickly because the Inserted and
Deleted tables are in cache
Execution time is determined by:
Number of tables that are referenced
Number of rows that are affected
53