database systems – sql sql optimization the key to an optimized database is good design. there are...

56
Database Systems – SQL Database Systems – SQL SQL OPTIMIZATION SQL OPTIMIZATION The key to an optimized database is good design. The key to an optimized database is good design. There are some basic physical constraints that databases must work There are some basic physical constraints that databases must work around. around. Disk Seeks – Average 10 ms, aka 100 seeks a second. Only real Disk Seeks – Average 10 ms, aka 100 seeks a second. Only real optimization is to distribute data across multiple disks. Seek time per optimization is to distribute data across multiple disks. Seek time per table is hard to improve. table is hard to improve. Disk Reading & Writing – 10 to 20 MB per second, best way to optimize is Disk Reading & Writing – 10 to 20 MB per second, best way to optimize is to distribute data across multiple disks to distribute data across multiple disks CPU Cycles – Data must be processed. Smaller data fits in memory and thus CPU Cycles – Data must be processed. Smaller data fits in memory and thus faster to process. faster to process.

Upload: spencer-holmes

Post on 13-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

The key to an optimized database is good design.The key to an optimized database is good design.

There are some basic physical constraints that databases must work around.There are some basic physical constraints that databases must work around.

Disk Seeks – Average 10 ms, aka 100 seeks a second. Only real optimization is to Disk Seeks – Average 10 ms, aka 100 seeks a second. Only real optimization is to distribute data across multiple disks. Seek time per table is hard to improve.distribute data across multiple disks. Seek time per table is hard to improve.

Disk Reading & Writing – 10 to 20 MB per second, best way to optimize is to distribute Disk Reading & Writing – 10 to 20 MB per second, best way to optimize is to distribute data across multiple disksdata across multiple disks

CPU Cycles – Data must be processed. Smaller data fits in memory and thus faster to CPU Cycles – Data must be processed. Smaller data fits in memory and thus faster to process.process.

Page 2: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

The more extensive your permissions are, the less optimized your database will be.The more extensive your permissions are, the less optimized your database will be.

Table level, column level permissions, resource counting, etc can be problematic if you Table level, column level permissions, resource counting, etc can be problematic if you have a large number of statements being executed. However, if you have a few very have a large number of statements being executed. However, if you have a few very queries that execute over a large amount of data, this factor may not be as significant.queries that execute over a large amount of data, this factor may not be as significant.

USING EXPLAINUSING EXPLAIN

Using EXPLAIN helps you understand how your query executes. It informs you what Using EXPLAIN helps you understand how your query executes. It informs you what order tables are joined and what indexes are used to join them. If you notice joins on order tables are joined and what indexes are used to join them. If you notice joins on unindexed fields, you can index them to improve performance. If tables are being unindexed fields, you can index them to improve performance. If tables are being joined in an order that you do not think is optimum, you can force MySQL to implement joined in an order that you do not think is optimum, you can force MySQL to implement the joins in a specific order.the joins in a specific order.

To execute an EXPLAIN simply type:To execute an EXPLAIN simply type:

EXPLAIN EXPLAIN SqlQuerySqlQuery;;

Page 3: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

Suppose that you have the SELECT statement shown here and that you plan to Suppose that you have the SELECT statement shown here and that you plan to examine it using EXPLAIN: examine it using EXPLAIN:

EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,

tt.ProjectReference, tt.EstimatedShipDate,tt.ProjectReference, tt.EstimatedShipDate,

tt.ActualShipDate, tt.ClientID,tt.ActualShipDate, tt.ClientID,

tt.ServiceCodes, tt.RepetitiveID,tt.ServiceCodes, tt.RepetitiveID,

tt.CurrentProcess, tt.CurrentDPPerson,tt.CurrentProcess, tt.CurrentDPPerson,

tt.RecordVolume, tt.DPPrinted, et.COUNTRY,tt.RecordVolume, tt.DPPrinted, et.COUNTRY,

et_1.COUNTRY, do.CUSTNAMEet_1.COUNTRY, do.CUSTNAME

FROM tt, et, et AS et_1, doFROM tt, et, et AS et_1, do

WHERE tt.SubmitTime IS NULLWHERE tt.SubmitTime IS NULL

AND tt.ActualPC = et.EMPLOYIDAND tt.ActualPC = et.EMPLOYID

AND tt.AssignedPC = et_1.EMPLOYIDAND tt.AssignedPC = et_1.EMPLOYID

AND tt.ClientID = do.CUSTNMBR;AND tt.ClientID = do.CUSTNMBR;

Page 4: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

For this example, make the following assumptions: For this example, make the following assumptions:

The columns being compared have been declared as follows: The columns being compared have been declared as follows:

TableTable ColumnColumn Data TypeData Type

tttt ActualPCActualPC CHAR(10)CHAR(10)

tttt AssignedPCAssignedPC CHAR(10)CHAR(10)

tttt ClientIDClientID CHAR(10)CHAR(10)

etet EMPLOYIDEMPLOYID CHAR(15)CHAR(15)

dodo CUSTNMBRCUSTNMBR CHAR(15)CHAR(15)

The tables have the following indexes:

TableTable IndexIndex

tttt ActualPCActualPC

tttt AssignedPCAssignedPC

tttt ClientIDClientID

etet EMPLOYID (primary key)EMPLOYID (primary key)

dodo CUSTNMBR (primary key)CUSTNMBR (primary key)

Page 5: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLOPTIMIZATIONOPTIMIZATION

The The tt.ActualPCtt.ActualPC values are not evenly distributed. values are not evenly distributed.

Initially, before any optimizations have been performed, the Initially, before any optimizations have been performed, the EXPLAINEXPLAIN statement produces the following information:statement produces the following information:

table type possible_keys key key_len ref rows Extratable type possible_keys key key_len ref rows Extraet ALL PRIMARY NULL NULL NULL 74et ALL PRIMARY NULL NULL NULL 74do ALL PRIMARY NULL NULL NULL 2135do ALL PRIMARY NULL NULL NULL 2135et_1 ALL PRIMARY NULL NULL NULL 74et_1 ALL PRIMARY NULL NULL NULL 74tt ALL AssignedPC, NULL NULL NULL 3872tt ALL AssignedPC, NULL NULL NULL 3872 ClientID,ClientID, ActualPCActualPC range checked for each record (key map: 35)range checked for each record (key map: 35)

Because Because typetype is is ALLALL for each table, this output indicates that MySQL is for each table, this output indicates that MySQL is generating a Cartesian product of all the tables.generating a Cartesian product of all the tables.

For the case at hand, this product is 74 × 2135 × 74 × 3872 = For the case at hand, this product is 74 × 2135 × 74 × 3872 = 45,268,558,720 rows. 45,268,558,720 rows.

Page 6: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLOPTIMIZATIONOPTIMIZATION

One problem here is that MySQL can use indexes on columns more efficiently if they One problem here is that MySQL can use indexes on columns more efficiently if they are declared as the same type and size. are declared as the same type and size.

In this context, In this context, VARCHARVARCHAR and and CHARCHAR are considered the same if they are declared as are considered the same if they are declared as the same size. the same size.

tt.ActualPCtt.ActualPC is declared as is declared as CHAR(10)CHAR(10) and and et.EMPLOYIDet.EMPLOYID is is CHAR(15)CHAR(15), so there is a , so there is a length mismatch. length mismatch.

To fix this disparity between column lengths, use To fix this disparity between column lengths, use ALTER TABLEALTER TABLE to lengthen to lengthen ActualPCActualPC from 10 characters to 15 characters.from 10 characters to 15 characters.

ALTER TABLE tt MODIFY ActualPC VARCHAR(15);ALTER TABLE tt MODIFY ActualPC VARCHAR(15);

Page 7: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

Now Now tt.ActualPCtt.ActualPC and and et.EMPLOYIDet.EMPLOYID are both are both VARCHAR(15)VARCHAR(15). .

Executing the Executing the EXPLAINEXPLAIN statement again produces this result: statement again produces this result:

table type possible_keys key key_len ref rows Extra table type possible_keys key key_len ref rows Extra

tt ALL AssignedPC, NULL NULL NULL 3872 Using ClientID, tt ALL AssignedPC, NULL NULL NULL 3872 Using ClientID, where where

ActualPC ActualPC

do ALL PRIMARY NULL NULL NULL 2135 do ALL PRIMARY NULL NULL NULL 2135

range checked for each record (key map: 1) range checked for each record (key map: 1)

et_1 ALL PRIMARY NULL NULL NULL 74 et_1 ALL PRIMARY NULL NULL NULL 74

range checked for each record (key map: 1) range checked for each record (key map: 1)

et eq_ref PRIMARY PRIMARY 15 tt.ActualPC 1et eq_ref PRIMARY PRIMARY 15 tt.ActualPC 1

This is not perfect, but is much better: The product of the This is not perfect, but is much better: The product of the rowsrows values is less by a values is less by a

factor of 74. This version executes in a couple of seconds.factor of 74. This version executes in a couple of seconds.

Page 8: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

A second alteration can be made to eliminate the column length mismatches for the A second alteration can be made to eliminate the column length mismatches for the tt.AssignedPC = et_1.EMPLOYIDtt.AssignedPC = et_1.EMPLOYID and and tt.ClientID = do.CUSTNNBRtt.ClientID = do.CUSTNNBR comparisons: comparisons:

ALTER TABLE tt MODIFY AssignedPC VARCHAR(15), -> MODIFY ClientID VARCHAR(15);ALTER TABLE tt MODIFY AssignedPC VARCHAR(15), -> MODIFY ClientID VARCHAR(15);

After that modification, After that modification, EXPLAINEXPLAIN produces the output shown here: produces the output shown here:

table type possible_keys key key_len ref rows Extra table type possible_keys key key_len ref rows Extra

et ALL PRIMARY NULL NULL NULL 74 et ALL PRIMARY NULL NULL NULL 74

tt ref AssignedPC, ActualPC 15 et.EMPLOYID 52 Using ClientID, where tt ref AssignedPC, ActualPC 15 et.EMPLOYID 52 Using ClientID, where ActualPC ActualPC

et_1 eq_ref PRIMARY PRIMARY 15 tt.AssignedPC 1 et_1 eq_ref PRIMARY PRIMARY 15 tt.AssignedPC 1

do eq_ref PRIMARY PRIMARY 15 tt.ClientID 1do eq_ref PRIMARY PRIMARY 15 tt.ClientID 1

Page 9: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

At this point, the query is optimized almost as well as possible. At this point, the query is optimized almost as well as possible.

The remaining problem is that, by default, MySQL assumes that values in the The remaining problem is that, by default, MySQL assumes that values in the tt.ActualPCtt.ActualPC column are evenly distributed, and that is not the case for the column are evenly distributed, and that is not the case for the tttt table. table.

Fortunately, it is easy to tell MySQL to analyze the key distribution: Fortunately, it is easy to tell MySQL to analyze the key distribution:

ANALYZE TABLE tt; ANALYZE TABLE tt;

With the additional index information, the join is perfect and With the additional index information, the join is perfect and EXPLAIN EXPLAIN produces this produces this result: result:

Page 10: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

OPTIMIZATIONOPTIMIZATION

table type possible_keys key key_len ref rows Extra table type possible_keys key key_len ref rows Extra

tt ALL AssignedPC NULL NULL NULL 3872 Using ClientID, where ActualPC tt ALL AssignedPC NULL NULL NULL 3872 Using ClientID, where ActualPC

et eq_ref PRIMARY PRIMARY 15 tt.ActualPC 1 et eq_ref PRIMARY PRIMARY 15 tt.ActualPC 1

et_1 eq_ref PRIMARY PRIMARY 15 tt.AssignedPC 1 et_1 eq_ref PRIMARY PRIMARY 15 tt.AssignedPC 1

do eq_ref PRIMARY PRIMARY 15 tt.ClientID 1 do eq_ref PRIMARY PRIMARY 15 tt.ClientID 1

Note that the Note that the rowsrows column in the output from column in the output from EXPLAINEXPLAIN is an educated guess from the is an educated guess from the MySQL join optimizer. MySQL join optimizer.

You should check whether the numbers are even close to the truth by comparing the You should check whether the numbers are even close to the truth by comparing the rowsrows product with the actual number of rows that the query returns. product with the actual number of rows that the query returns.

If the numbers are quite different, you might get better performance by using If the numbers are quite different, you might get better performance by using STRAIGHT_JOINSTRAIGHT_JOIN in your in your SELECTSELECT statement and trying to list the tables in a different statement and trying to list the tables in a different order in the order in the FROMFROM clause. clause.

Page 11: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

SPEEDING UP SELECTSSPEEDING UP SELECTS

When the data stored in a database changes, the statistics used to optimize queries are When the data stored in a database changes, the statistics used to optimize queries are not updated automatically. Therefore, use the ANALYZE TABLE command on each table not updated automatically. Therefore, use the ANALYZE TABLE command on each table to speed up results. to speed up results.

WHERE CLAUSE OPTIMIZATIONWHERE CLAUSE OPTIMIZATION

First note that any optimizations for the WHERE clause of a SELECT query also work for First note that any optimizations for the WHERE clause of a SELECT query also work for the WHERE clauses of DELETE and UPDATE queries.the WHERE clauses of DELETE and UPDATE queries.

Not all optimizations performed by MySQL are documentedNot all optimizations performed by MySQL are documented

Page 12: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

Some of the optimizations performed by MySQL follow: Some of the optimizations performed by MySQL follow:

Removal of unnecessary parentheses: Removal of unnecessary parentheses:

((a AND b) AND c OR (((a AND b) AND (c AND d)))) -> ((a AND b) AND c OR (((a AND b) AND (c AND d)))) ->

(a AND b AND c) OR (a AND b AND c AND d) (a AND b AND c) OR (a AND b AND c AND d)

Constant folding: Constant folding:

(a<b AND b=c) AND a=5 -> b>5 AND b=c AND a=5 (a<b AND b=c) AND a=5 -> b>5 AND b=c AND a=5

Constant condition removal (needed because of constant folding): Constant condition removal (needed because of constant folding):

(B>=5 AND B=5) OR (B=6 AND 5=5) OR (B=7 AND 5=6) -> B=5 OR B=6 (B>=5 AND B=5) OR (B=6 AND 5=5) OR (B=7 AND 5=6) -> B=5 OR B=6

These optimizations occur automatically and therefore you do not have to worry about These optimizations occur automatically and therefore you do not have to worry about performing them yourself.performing them yourself.

Page 13: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

Examples of very fast queriesExamples of very fast queries

Some examples of queries that are very fast: Some examples of queries that are very fast:

SELECT COUNT(*) FROM SELECT COUNT(*) FROM tbl_nametbl_name; ;

SELECT MIN(SELECT MIN(key_part1key_part1),MAX(),MAX(key_part1key_part1) FROM ) FROM tbl_nametbl_name; ;

SELECT MAX(SELECT MAX(key_part2key_part2) FROM ) FROM tbl_nametbl_name WHERE WHERE key_part1key_part1==constantconstant; ;

SELECT ... FROM SELECT ... FROM tbl_nametbl_name ORDER BY ORDER BY key_part1key_part1,,key_part2key_part2,... LIMIT 10; ,... LIMIT 10;

SELECT ... FROM SELECT ... FROM tbl_nametbl_name ORDER BY ORDER BY key_part1key_part1 DESC, DESC, key_part2key_part2 DESC, ... LIMIT 10; DESC, ... LIMIT 10;

Page 14: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

Indexes are ignored for the <> operator:Indexes are ignored for the <> operator:

SELECT * FROM tab WHERE score <> 0;SELECT * FROM tab WHERE score <> 0;

This can be a problem if the table is very slanted (eg >99% of the rows have the value This can be a problem if the table is very slanted (eg >99% of the rows have the value that is filtered). The obvious workaround is to use a UNION:that is filtered). The obvious workaround is to use a UNION:

(SELECT * FROM tab WHERE score > 0) UNION(SELECT * FROM tab WHERE score > 0) UNION(SELECT * FROM tab WHERE score < 0); (SELECT * FROM tab WHERE score < 0);

Page 15: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

The query optimizer also does badly on examples likeThe query optimizer also does badly on examples like

SELECT id FROM foo WHERE bar IN (SELECT bar FROM baz WHERE qux='foo');SELECT id FROM foo WHERE bar IN (SELECT bar FROM baz WHERE qux='foo');

where foo is a large table and baz a small one. Doing the subselect first would allow the where foo is a large table and baz a small one. Doing the subselect first would allow the use on an index to eliminate most of foo, which is what happens if you sayuse on an index to eliminate most of foo, which is what happens if you say

SELECT foo.id FROM foo, baz WHERE foo.bar=baz.bar and baz.qux='foo‘;SELECT foo.id FROM foo, baz WHERE foo.bar=baz.bar and baz.qux='foo‘;

Page 16: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

Indices lose their speed advantage when using them in OR-situations (4.1.10):Indices lose their speed advantage when using them in OR-situations (4.1.10):

SELECT * FROM a WHERE index1 = 'foo'SELECT * FROM a WHERE index1 = 'foo'UNIONUNIONSELECT * FROM a WHERE index2 = 'bar';SELECT * FROM a WHERE index2 = 'bar';

is much faster thanis much faster than

SELECT * FROM a WHERE index1 = 'foo' OR index2 = 'bar'; SELECT * FROM a WHERE index1 = 'foo' OR index2 = 'bar';

Page 17: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

ORDER BYORDER BY

Optimization of the ORDER BY clause may not be as intuitive as one might think.Optimization of the ORDER BY clause may not be as intuitive as one might think.

Observe the following queries that optimize properly:Observe the following queries that optimize properly:

SELECT * FROM t1 ORDER BY SELECT * FROM t1 ORDER BY key_part1key_part1,,key_part2key_part2,... ; ,... ;

SELECT * FROM t1 WHERE SELECT * FROM t1 WHERE key_part1key_part1==constantconstant ORDER BY ORDER BY key_part2key_part2; ;

SELECT * FROM t1 ORDER BY SELECT * FROM t1 ORDER BY key_part1key_part1 DESC, DESC, key_part2key_part2 DESC; DESC;

SELECT * FROM t1 WHERE SELECT * FROM t1 WHERE key_part1key_part1=1 ORDER BY =1 ORDER BY key_part1key_part1 DESC, DESC, key_part2key_part2 DESC; DESC;

The reason these work is because there exists a compound key and the order of ORDER The reason these work is because there exists a compound key and the order of ORDER BY clause follows the sequence of fields listed in the compound key.BY clause follows the sequence of fields listed in the compound key.

Page 18: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

ORDER BYORDER BY

In some cases, MySQL In some cases, MySQL cannotcannot use indexes to resolve the use indexes to resolve the ORDER BYORDER BY, although it still , although it still uses indexes to find the rows that match the uses indexes to find the rows that match the WHEREWHERE clause. These cases include the clause. These cases include the following: following:

You use You use ORDER BYORDER BY on different keys: on different keys:

SELECT * FROM t1 ORDER BY SELECT * FROM t1 ORDER BY key1key1, , key2key2; ;

You use You use ORDER BYORDER BY on non-consecutive parts of a key: on non-consecutive parts of a key:

SELECT * FROM t1 WHERE SELECT * FROM t1 WHERE key2key2==constantconstant ORDER BY ORDER BY key_part2key_part2; ;

You mix You mix ASCASC and and DESCDESC: :

SELECT * FROM t1 ORDER BY SELECT * FROM t1 ORDER BY key_part1key_part1 DESC, DESC, key_part2key_part2 ASC; ASC;

The key used to fetch the rows is not the same as the one used in the The key used to fetch the rows is not the same as the one used in the ORDER BYORDER BY: :

SELECT * FROM t1 WHERE SELECT * FROM t1 WHERE key2key2==constantconstant ORDER BY ORDER BY key1key1; ;

Note the difference between key and key_part!Note the difference between key and key_part!

Page 19: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

GROUP BYGROUP BY

While there are other considerations, a general rule of thumb is that only GROUP BY’s While there are other considerations, a general rule of thumb is that only GROUP BY’s over a single table, those grouped by the first consecutive indexes, and those using over a single table, those grouped by the first consecutive indexes, and those using only MIN or MAX as aggregation functions are optimized.only MIN or MAX as aggregation functions are optimized.

For example:For example:

Assuming there is an index Assuming there is an index idx(c1,c2,c3)idx(c1,c2,c3) on table on table t1(c1,c2,c3,c4)t1(c1,c2,c3,c4): :

SELECT c1, c2 FROM t1 GROUP BY c1, c2; SELECT c1, c2 FROM t1 GROUP BY c1, c2;

SELECT DISTINCT c1, c2 FROM t1; SELECT DISTINCT c1, c2 FROM t1;

SELECT c1, MIN(c2) FROM t1 GROUP BY c1; SELECT c1, MIN(c2) FROM t1 GROUP BY c1;

SELECT c1, c2 FROM t1 WHERE c1 < SELECT c1, c2 FROM t1 WHERE c1 < constconst GROUP BY c1, c2; GROUP BY c1, c2;

SELECT MAX(c3), MIN(c3), c1, c2 FROM t1 WHERE c2 > SELECT MAX(c3), MIN(c3), c1, c2 FROM t1 WHERE c2 > constconst GROUP BY c1, c2; SELECT GROUP BY c1, c2; SELECT c2 FROM t1 WHERE c1 < c2 FROM t1 WHERE c1 < constconst GROUP BY c1, c2; GROUP BY c1, c2;

SELECT c1, c2 FROM t1 WHERE c3 = SELECT c1, c2 FROM t1 WHERE c3 = constconst GROUP BY c1, c2; GROUP BY c1, c2;

Page 20: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

INSERTINSERT

The time required for inserting a row is determined by the following factors, where the The time required for inserting a row is determined by the following factors, where the numbers indicate approximate proportions: numbers indicate approximate proportions:

Connecting: (3) Connecting: (3)

Sending query to server: (2) Sending query to server: (2)

Parsing query: (2) Parsing query: (2)

Inserting row: (1 × size of row) Inserting row: (1 × size of row)

Inserting indexes: (1 × number of indexes) Inserting indexes: (1 × number of indexes)

Closing: (1) Closing: (1)

Page 21: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

INSERTINSERT

If you are inserting many rows from the same client at the same time, use If you are inserting many rows from the same client at the same time, use INSERTINSERT statements with multiple statements with multiple VALUESVALUES lists to insert several rows at a time. This is lists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row considerably faster (many times faster in some cases) than using separate single-row INSERTINSERT statements. statements.

When loading a table from a text file, use When loading a table from a text file, use LOAD DATA INFILELOAD DATA INFILE. This is usually 20 times . This is usually 20 times faster than using faster than using INSERTINSERT statements. See statements. See Section 13.2.5, “LOAD DATA INFILE Syntax”. .

While outside of the scope of what I wish to cover, if you are loading a large amount of While outside of the scope of what I wish to cover, if you are loading a large amount of data in proportion to the current size of the table, it may be more efficient to drop the data in proportion to the current size of the table, it may be more efficient to drop the indexes, load the table via a LOAD DATA INFILE, and then reinstate the indexes.indexes, load the table via a LOAD DATA INFILE, and then reinstate the indexes.

Page 22: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

INSERTINSERT

To speed up To speed up INSERTINSERT operations that are performed with multiple statements for non- operations that are performed with multiple statements for non-transactional tables, lock your tables: transactional tables, lock your tables:

LOCK TABLES a WRITE; LOCK TABLES a WRITE;

INSERT INTO a VALUES (1,23),(2,34),(4,33); INSERT INTO a VALUES (1,23),(2,34),(4,33);

INSERT INTO a VALUES (8,26),(6,29); INSERT INTO a VALUES (8,26),(6,29);

... UNLOCK TABLES; ... UNLOCK TABLES;

INSERTINSERT, , UPDATEUPDATE, and , and DELETEDELETE operations are very fast in MySQL, but you can obtain operations are very fast in MySQL, but you can obtain better overall performance by adding locks around everything that does more than better overall performance by adding locks around everything that does more than about five successive inserts or updates. If you do very many successive inserts, you about five successive inserts or updates. If you do very many successive inserts, you could do a could do a LOCK TABLESLOCK TABLES followed by an followed by an UNLOCK TABLESUNLOCK TABLES once in a while (each 1,000 once in a while (each 1,000 rows or so) to allow other threads access to the table. This would still result in a nice rows or so) to allow other threads access to the table. This would still result in a nice performance gain. performance gain.

INSERTINSERT is still much slower for loading data than is still much slower for loading data than LOAD DATA INFILELOAD DATA INFILE, even when , even when using the strategies just outlined. using the strategies just outlined.

Page 23: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL OPTIMIZATIONSQL OPTIMIZATION

GENERAL TIPSGENERAL TIPS

Use persistent connections to the database to avoid connection overhead. Use persistent connections to the database to avoid connection overhead.

Run Run OPTIMIZE TABLEOPTIMIZE TABLE to defragment the table after you have deleted a lot of rows to defragment the table after you have deleted a lot of rows from it. from it.

Try to keep column names simple. For example, in a table named Try to keep column names simple. For example, in a table named customercustomer, use a , use a column name of column name of namename instead of instead of customer_namecustomer_name. I DISAGREE!. I DISAGREE!

To make your names portable to other SQL servers, you should keep them shorter than To make your names portable to other SQL servers, you should keep them shorter than 18 characters. 18 characters.

Page 24: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL TRANSACTIONSSQL TRANSACTIONS

It is often important to ensure a series of statements occur as an atomic unit or do not It is often important to ensure a series of statements occur as an atomic unit or do not occur at all. occur at all.

For example, if you wanted to transfer money from one account to another, you would For example, if you wanted to transfer money from one account to another, you would not want the removal of the funds from one account to occur without the depositing of not want the removal of the funds from one account to occur without the depositing of those funds in the second account. If something happened to prevent the depositing of those funds in the second account. If something happened to prevent the depositing of the funds, then you would want the withdrawal cancelled.the funds, then you would want the withdrawal cancelled.

This is accomplished through the use of transactions.This is accomplished through the use of transactions.

The syntax is simple:The syntax is simple:

START TRANSACTION;START TRANSACTION;

Any SQL commands you wish to execute atomicallyAny SQL commands you wish to execute atomically

COMMIT;COMMIT;

Page 25: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

SQL TRANSACTIONSSQL TRANSACTIONS

Note that some statements can not be rolled back. These are typically ones that alter Note that some statements can not be rolled back. These are typically ones that alter the definition of the database/table structure.the definition of the database/table structure.

If statements like these are included within a transaction, then if another statement If statements like these are included within a transaction, then if another statement fails within the transaction, then a full rollback to the beginning of the transaction can fails within the transaction, then a full rollback to the beginning of the transaction can not occur.not occur.

Transactions can be broken up so that you can rollback to a specific point within the Transactions can be broken up so that you can rollback to a specific point within the transaction using the SAVEPOINT command.transaction using the SAVEPOINT command.

SAVEPOINT SAVEPOINT identifieridentifier

ROLLBACK [WORK] TO SAVEPOINT ROLLBACK [WORK] TO SAVEPOINT identifieridentifier

RELEASE SAVEPOINT RELEASE SAVEPOINT identifieridentifier

Page 26: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

VIEWSVIEWS

A view in SQL is a great way to present information to a user in another way than the A view in SQL is a great way to present information to a user in another way than the logical table structure.logical table structure.

You might do this to limit the access of certain fields, i.e. Social Security Number or You might do this to limit the access of certain fields, i.e. Social Security Number or Date of birth.Date of birth.

You might wish to derive a field. i.e age from date of birth.You might wish to derive a field. i.e age from date of birth.

You might wish to denormalize a series of tables and remove the ID fields so they do You might wish to denormalize a series of tables and remove the ID fields so they do not confuse someone generating reports from the data.not confuse someone generating reports from the data.

Page 27: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

VIEWSVIEWS

The actual syntax has more options than this, but a simple form for creating a view is The actual syntax has more options than this, but a simple form for creating a view is as follows:as follows:

CREATE VIEW CREATE VIEW ViewNameViewName AS AS SelectStatementSelectStatement;;

So if you had the following table:So if you had the following table:

LastNamLastNamee

FirstNameFirstName DOBDOB

JeterJeter DerekDerek 5/6/19745/6/1974

KurtKurt SchillingSchilling 4/4/19614/4/1961

Babe Babe RuthRuth 1/1/19001/1/1900BaseballPlayers table

You could great a view that would show the following:

LastNameLastName FirstNameFirstName AgeAge

JeterJeter DerekDerek 3333

KurtKurt SchillingSchilling 4646

Babe Babe RuthRuth Really OldReally Old

BaseballPlayers view

The BaseballPlayers2 view can be created with the following statement:

CREATE VIEW BaseballPlayers2 AS SELECT LastName, FirstName, Year(DOB) AS Age FROM BaseballPlayers;

Page 28: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

VIEWSVIEWS

A View name can not be the same name as a table that already exists.A View name can not be the same name as a table that already exists.

Views must have unique column names.Views must have unique column names.

Columns selected can be column names or expressions. If you create an expression, Columns selected can be column names or expressions. If you create an expression, name it!name it!

A view can be created from many kinds of SELECT statements. It can refer to base A view can be created from many kinds of SELECT statements. It can refer to base tables or other views. It can use joins, UNION, and subqueries. tables or other views. It can use joins, UNION, and subqueries.

Page 29: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

VIEWSVIEWS

A view definition is subject to the following restrictions: A view definition is subject to the following restrictions:

The The SELECTSELECT statement cannot contain a subquery in the statement cannot contain a subquery in the FROMFROM clause. clause.

The The SELECTSELECT statement cannot refer to system or user variables. statement cannot refer to system or user variables.

Any table or view referred to in the definition must exist. Any table or view referred to in the definition must exist.

The definition cannot refer to a The definition cannot refer to a TEMPORARYTEMPORARY table, and you cannot create a table, and you cannot create a TEMPORARYTEMPORARY view. view.

The tables named in the view definition must already exist. The tables named in the view definition must already exist.

You cannot associate a trigger with a view. You cannot associate a trigger with a view.

Page 30: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

VIEWSVIEWS

Some views are updatable. Some views are updatable.

You can use them in statements such as UPDATE, DELETE, or INSERT to update the You can use them in statements such as UPDATE, DELETE, or INSERT to update the contents of the underlying table. contents of the underlying table.

For a view to be updatable, there must be a one-to-one relationship between the rows For a view to be updatable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table. in the view and the rows in the underlying table.

There are also certain other constructs that make a view non-updatable. There are also certain other constructs that make a view non-updatable.

To be more specific, a view is not updatable if it contains any of the following: To be more specific, a view is not updatable if it contains any of the following: Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth) Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth) DISTINCT DISTINCT GROUP BY GROUP BY HAVING HAVING UNION or UNION ALL UNION or UNION ALL Subquery in the select list Subquery in the select list

Certain joins (see additional join discussion later in this section) Certain joins (see additional join discussion later in this section)

Non-updatable view in the FROM clause Non-updatable view in the FROM clause

A subquery in the WHERE clause that refers to a table in the FROM clause A subquery in the WHERE clause that refers to a table in the FROM clause

Refers only to literal values (in this case, there is no underlying table to update) Refers only to literal values (in this case, there is no underlying table to update)

ALGORITHM = TEMPTABLE (use of a temporary table always makes a view ALGORITHM = TEMPTABLE (use of a temporary table always makes a view

Page 31: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

VIEWSVIEWS

A view is not updatable if it contains any of the following: A view is not updatable if it contains any of the following:

Certain joins (see additional join discussion later in this section) Certain joins (see additional join discussion later in this section) Non-updatable view in the FROM clause Non-updatable view in the FROM clause A subquery in the WHERE clause that refers to a table in the FROM clause A subquery in the WHERE clause that refers to a table in the FROM clause Refers only to literal values (in this case, there is no underlying table to update) Refers only to literal values (in this case, there is no underlying table to update) ALGORITHM = TEMPTABLE (use of a temporary table always makes a view ALGORITHM = TEMPTABLE (use of a temporary table always makes a view

With respect to insertability (being updatable with INSERT statements), an updatable With respect to insertability (being updatable with INSERT statements), an updatable view is insertable if it also satisfies these additional requirements for the view columns: view is insertable if it also satisfies these additional requirements for the view columns: There must be no duplicate view column names. There must be no duplicate view column names. The view must contain all columns in the base table that do not have a default value. The view must contain all columns in the base table that do not have a default value. The view columns must be simple column references and not derived columns. The view columns must be simple column references and not derived columns.

Page 32: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

DATE/TIME FUNCTIONSDATE/TIME FUNCTIONS

There are many date and time functions included in mySQL to help manipulate There are many date and time functions included in mySQL to help manipulate temporal values.temporal values.

A word of caution, manipulating date/time values in the result of a SELECT statement A word of caution, manipulating date/time values in the result of a SELECT statement should not impact performance, however manipulating date/time values within a should not impact performance, however manipulating date/time values within a WHERE clause may have a performance impact.WHERE clause may have a performance impact.

Specifically, a performance impact will usually occur if a date/time function is applied to Specifically, a performance impact will usually occur if a date/time function is applied to a date/time field from a table as part of a WHERE clause.a date/time field from a table as part of a WHERE clause.

SELECT * FROM websites WHERE DATEADD(create_date, 30)= CURRENT_DATE();SELECT * FROM websites WHERE DATEADD(create_date, 30)= CURRENT_DATE();

However, a date/time function applied to a scalar should not have a performance However, a date/time function applied to a scalar should not have a performance impact.impact.

SELECT * FROM websites WHERE create_date = CURRENT_DATE();SELECT * FROM websites WHERE create_date = CURRENT_DATE();

When a function is applied to an indexed field, the effectiveness of the index is limited.When a function is applied to an indexed field, the effectiveness of the index is limited.

Page 33: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

DATE/TIME FUNCTIONSDATE/TIME FUNCTIONS

NameName DescriptionDescription

ADDDATE()(v4.1.1) ADDDATE()(v4.1.1) Add dates.Add dates.

ADDTIME()(v4.1.1) ADDTIME()(v4.1.1) Add times.Add times.

CURRENT_DATE()CURRENT_DATE() Returns the current date.Returns the current date.

CURRENT_TIME()CURRENT_TIME() Returns the current time.Returns the current time.

CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), NOW()NOW()

Returns the current data and time.Returns the current data and time.

DATE_ADDDATE_ADD Add two dates.Add two dates.

DATE_SUBDATE_SUB Subtract two dates.Subtract two dates.

DATE()DATE() Extract the date part of a date or datetime expression.Extract the date part of a date or datetime expression.

DAY()DAY() Return the day of the month 1-31Return the day of the month 1-31

DAYNAME()DAYNAME() Return the name of the weekday.Return the name of the weekday.

DAYOFMONTH()DAYOFMONTH() Return the day of the month 1-31.Return the day of the month 1-31.

DAYOFWEEK()DAYOFWEEK() Return the weekday index of the argument.Return the weekday index of the argument.

DAYOFYEARS()DAYOFYEARS() Return the day of the year (1-366).Return the day of the year (1-366).

HOUR()HOUR() Extract the hour.Extract the hour.

Page 34: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

DATE/TIME FUNCTIONSDATE/TIME FUNCTIONS

NameName DescriptionDescription

MINUTE()MINUTE() Return the minute from the argument.Return the minute from the argument.

MONTH()MONTH() Return the month from the date passed.Return the month from the date passed.

MONTHNAME()MONTHNAME() Return the month from the date passed.Return the month from the date passed.

SECOND()SECOND() Return the second 0-59.Return the second 0-59.

TIME()TIME() Extract the time portion of the expression passed.Extract the time portion of the expression passed.

TIMEDIFF()TIMEDIFF() Subtract time.Subtract time.

WEEK()WEEK() Return the week number.Return the week number.

WEEKDAY()WEEKDAY() Return the week indexReturn the week index

WEEKOFYEAR()WEEKOFYEAR() Return the calendar week of the date (1-53)Return the calendar week of the date (1-53)

YEAR()YEAR() Return the year.Return the year.

Page 35: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

USER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

In addition to the built-in functions and procedures you can create your own using the In addition to the built-in functions and procedures you can create your own using the following syntax:following syntax:

CREATE PROCEDURE ProcedureName (parameter list)CREATE PROCEDURE ProcedureName (parameter list)

routine_bodyroutine_body;;

oror

CREATE FUNCTION FunctionName (parameter list)CREATE FUNCTION FunctionName (parameter list)

routine_bodyroutine_body

RETURNS typeRETURNS type

The The routine_bodyroutine_body consists of a valid SQL procedure statement. consists of a valid SQL procedure statement.

A valid SQL procedure statement may be a SELECT or INSERT or it can be a compound A valid SQL procedure statement may be a SELECT or INSERT or it can be a compound statement using a BEGIN and END.statement using a BEGIN and END.

Compound statements can contain declarations, loops, and other compound structures.Compound statements can contain declarations, loops, and other compound structures.

Page 36: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

USER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

Stored routines have some limitations:Stored routines have some limitations:

Stored functions can not do explicit or implicit commits or rollbacks, however stored Stored functions can not do explicit or implicit commits or rollbacks, however stored procedures can.procedures can.

Stored routines can not use load data infile.Stored routines can not use load data infile.

Check the online documentation for more restrictions.Check the online documentation for more restrictions.

To see which procedures are installed you can use:To see which procedures are installed you can use:

SELECT type,db,name,param_list FROM mysql.proc; SELECT type,db,name,param_list FROM mysql.proc;

Page 37: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

USER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

Simple Example:Simple Example:

CREATE FUNCTION Yankees (s CHAR(20)) RETURNS CHAR(50) CREATE FUNCTION Yankees (s CHAR(20)) RETURNS CHAR(50)

RETURN CONCAT('Yankees , ',s,'!');RETURN CONCAT('Yankees , ',s,'!');

SELECT Yankees(‘Rock');SELECT Yankees(‘Rock');

Returns:Returns:

Yankees, Rock! Yankees, Rock!

THIS IS SUPPOSED TO WORK, BUT ALAS IT DOES NOT.THIS IS SUPPOSED TO WORK, BUT ALAS IT DOES NOT.

Page 38: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

Simple Example:Simple Example:

delimiter //delimiter //

CREATE PROCEDURE simpleproc (OUT param1 INT)CREATE PROCEDURE simpleproc (OUT param1 INT) BEGINBEGIN SELECT COUNT(*) INTO param1 FROM t;SELECT COUNT(*) INTO param1 FROM t; END;END; ////

delimiter ;delimiter ;

Delimiter is required because the procedure contains a semicolon.Delimiter is required because the procedure contains a semicolon.

Page 39: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

More complex example to populate the table with values:More complex example to populate the table with values:

DROP PROCEDURE IF EXISTS build_table;DROP PROCEDURE IF EXISTS build_table;DELIMITER '/';DELIMITER '/';

CREATE PROCEDURE build_table()CREATE PROCEDURE build_table()BEGINBEGIN DECLARE i INTEGER; DECLARE i INTEGER; DECLARE v INTEGER; DECLARE v INTEGER; SET i = 1; SET i = 1; SET v = 100; SET v = 100; WHILE i <= 125 DO WHILE i <= 125 DO INSERT into mytable VALUES (i, v); INSERT into mytable VALUES (i, v); SET i = i + 1; SET i = i + 1; SET v = v + 2; SET v = v + 2; END WHILE; END WHILE;END/END/DELIMITER ';'/DELIMITER ';'/

Page 40: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

SELECTING INTO VARIABLESSELECTING INTO VARIABLES

SELECT SELECT col_namecol_name[,...] INTO [,...] INTO var_namevar_name[,...] [,...] table_exprtable_expr

This SELECT syntax stores selected columns directly into variables. Therefore, only a This SELECT syntax stores selected columns directly into variables. Therefore, only a single row may be retrieved. single row may be retrieved.

SELECT id,data INTO x,y FROM test.t1 LIMIT 1;SELECT id,data INTO x,y FROM test.t1 LIMIT 1;

Page 41: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

CURSORSCURSORS

CREATE PROCEDURE curdemo() CREATE PROCEDURE curdemo() BEGIN BEGIN DECLARE done INT DEFAULT 0; DECLARE done INT DEFAULT 0; DECLARE a CHAR(16); DECLARE a CHAR(16); DECLARE b,c INT; DECLARE b,c INT; DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1; DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1; DECLARE cur2 CURSOR FOR SELECT i FROM test.t2; DECLARE cur2 CURSOR FOR SELECT i FROM test.t2; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; OPEN cur1; OPEN cur1; OPEN cur2; OPEN cur2; REPEAT FETCH cur1 INTO a, b; REPEAT FETCH cur1 INTO a, b; FETCH cur2 INTO c; FETCH cur2 INTO c; IF NOT done THEN IF NOT done THEN IF b < c THEN IF b < c THEN INSERT INTO test.t3 VALUES (a,b); INSERT INTO test.t3 VALUES (a,b); ELSE ELSE INSERT INTO test.t3 VALUES (a,c); INSERT INTO test.t3 VALUES (a,c); END IF; END IF; END IF; END IF; UNTIL done END REPEAT; UNTIL done END REPEAT; CLOSE cur1; CLOSE cur1;

CLOSE cur2; ENDCLOSE cur2; END

Page 42: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

IF STATEMENTSIF STATEMENTS

IF IF search_conditionsearch_condition THEN THEN statement_liststatement_list [ELSEIF [ELSEIF search_conditionsearch_condition THEN THEN statement_liststatement_list] ... ] ... [ELSE [ELSE statement_liststatement_list] ] END IF END IF

Page 43: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

CASE STATEMENTSCASE STATEMENTS

CASE CASE case_valuecase_value WHEN WHEN when_valuewhen_value THEN THEN statement_liststatement_list [WHEN [WHEN when_valuewhen_value THEN THEN statement_liststatement_list] ... ] ... [ELSE [ELSE statement_liststatement_list] ] END CASE END CASE

Page 44: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

LOOP STATEMENTSLOOP STATEMENTS

[[begin_labelbegin_label:] LOOP:] LOOP statement_liststatement_list END LOOP [END LOOP [end_labelend_label] ]

LEAVE LEAVE labellabel

This statement is used to exit any labeled flow control construct. This statement is used to exit any labeled flow control construct. It can be used within It can be used within BEGIN ... ENDBEGIN ... END or loop constructs ( or loop constructs (LOOPLOOP, , REPEATREPEAT, , WHILEWHILE). ).

Page 45: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

REPEAT STATEMENTSREPEAT STATEMENTS

[[begin_labelbegin_label:] REPEAT :] REPEAT statement_liststatement_list UNTIL UNTIL search_conditionsearch_condition END REPEAT [END REPEAT [end_labelend_label] ]

The statement list within a The statement list within a REPEATREPEAT statement is repeated until the statement is repeated until the search_conditionsearch_condition is is true. Thus, a true. Thus, a REPEATREPEAT always enters the loop at least once. always enters the loop at least once. statement_liststatement_list consists of consists of one or more statements. one or more statements.

A A REPEATREPEAT statement can be labeled. statement can be labeled. end_labelend_label cannot be given unless cannot be given unless begin_labelbegin_label also also

is present. If both are present, they must be the same.is present. If both are present, they must be the same.

Page 46: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

More complex example to populate the table with values:More complex example to populate the table with values:

------ Then use the commands:-- Then use the commands:----DROP TABLE IF EXISTS mytable;DROP TABLE IF EXISTS mytable;------ create the table-- create the tableCREATE TABLE mytable (id INTEGER, value INTEGER); CREATE TABLE mytable (id INTEGER, value INTEGER); ------ call stored procedure to fill table with data-- call stored procedure to fill table with dataCALL build_table(); CALL build_table(); ------ display table and its new data-- display table and its new dataSELECT * from mytable; SELECT * from mytable;

Page 47: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLUSER DEFINED FUNCTIONS and PROCEDURESUSER DEFINED FUNCTIONS and PROCEDURES

+------+------++------+------+| i | v || i | v |+------+------++------+------+| 1 | 100 | | 1 | 100 | | 2 | 102 | | 2 | 102 | | 3 | 104 | | 3 | 104 | | 4 | 106 | | 4 | 106 | | 5 | 108 || 5 | 108 |......| 120 | 338 | | 120 | 338 | | 121 | 340 | | 121 | 340 | | 122 | 342 | | 122 | 342 | | 123 | 344 | | 123 | 344 | | 124 | 346 | | 124 | 346 | | 125 | 348 | | 125 | 348 | +------+------++------+------+mytable tablemytable table

Page 48: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

Triggers are database objects associated with permanent tables and are activated when Triggers are database objects associated with permanent tables and are activated when a particular event occurs.a particular event occurs.

Triggers are very useful to insure an action occurs at the database level, regardless of Triggers are very useful to insure an action occurs at the database level, regardless of what the user-program may do.what the user-program may do.

The syntax to create a trigger is as follows:The syntax to create a trigger is as follows:

CREATE TRIGGER CREATE TRIGGER trigger_name trigger_time trigger_eventtrigger_name trigger_time trigger_event

ON ON tbl_nametbl_name

FOR EACH ROW FOR EACH ROW trigger_stmttrigger_stmt;;

Page 49: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

CREATE TRIGGER CREATE TRIGGER trigger_name trigger_time trigger_eventtrigger_name trigger_time trigger_event

ON ON tbl_nametbl_name

FOR EACH ROW FOR EACH ROW trigger_stmttrigger_stmt;;

trigger_timetrigger_time is the trigger action time. It can be BEFORE or AFTER to indicate that the is the trigger action time. It can be BEFORE or AFTER to indicate that the trigger activates before or after the statement that activated it.trigger activates before or after the statement that activated it.

trigger_event trigger_event indicates the kind of statement that activates the trigger. The indicates the kind of statement that activates the trigger. The trigger_event can be one of the following: trigger_event can be one of the following:

INSERT: The trigger is activated whenever a new row is inserted into the table; for INSERT: The trigger is activated whenever a new row is inserted into the table; for example, through INSERT, LOAD DATA, and REPLACE statements. example, through INSERT, LOAD DATA, and REPLACE statements.

UPDATE: The trigger is activated whenever a row is modified; for example, through UPDATE: The trigger is activated whenever a row is modified; for example, through UPDATE statements. UPDATE statements.

DELETE: The trigger is activated whenever a row is deleted from the table; for example, DELETE: The trigger is activated whenever a row is deleted from the table; for example, through DELETE and REPLACE statements. However, DROP TABLE and TRUNCATE through DELETE and REPLACE statements. However, DROP TABLE and TRUNCATE statements on the table do not activate statements on the table do not activate

Page 50: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

Note that you can not have two triggers defined for a table with the same trigger action Note that you can not have two triggers defined for a table with the same trigger action time. time.

If you want to execute more than one statement in a trigger, use a BEGIN... END If you want to execute more than one statement in a trigger, use a BEGIN... END Construct. Construct.

Observe the following trigger that when a row is deleted from the table, copies an Observe the following trigger that when a row is deleted from the table, copies an archival version of the row to another table:archival version of the row to another table:

CREATE TRIGGER test1test2 BEFORE DELETE ON test1CREATE TRIGGER test1test2 BEFORE DELETE ON test1

FOR EACH ROW FOR EACH ROW

INSERT INTO test2 set test2.C1 = old.C1; INSERT INTO test2 set test2.C1 = old.C1;

Note, old refers to the row that is about to be deleted.Note, old refers to the row that is about to be deleted.

Page 51: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

I have experienced inconsistent behavior. I have experienced inconsistent behavior.

Triggers may be created even if you get an error message when defining it. Triggers may be created even if you get an error message when defining it.

Errors when executing a trigger does not necessarily impact the operation triggering Errors when executing a trigger does not necessarily impact the operation triggering the trigger. the trigger.

Sometimes the action occurs even if the error message about the trigger fires and Sometimes the action occurs even if the error message about the trigger fires and makes it seem as if it doesn’t.makes it seem as if it doesn’t.

Be careful with BEFORE triggers. Constraints may occur, where an insert will fail, but Be careful with BEFORE triggers. Constraints may occur, where an insert will fail, but actions from your BEFORE trigger will succeed. actions from your BEFORE trigger will succeed.

Page 52: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

Given the following tables:Given the following tables:

After the following trigger is installed:After the following trigger is installed:

CREATE TRIGGER test1test2 BEFORE DELETE ON test1CREATE TRIGGER test1test2 BEFORE DELETE ON test1

FOR EACH ROW FOR EACH ROW INSERT INTO test2 set test2.C1 = old.C1, INSERT INTO test2 set test2.C1 = old.C1, test2.C2=old.C2, test2.C3=old.C3; test2.C2=old.C2, test2.C3=old.C3;

and the following SQL query executed:and the following SQL query executed:

DELETE FROM test1;DELETE FROM test1;

C1C1 C2C2 C3C3

R1V1R1V1 R1V2R1V2 R1V3R1V3

R2V1R2V1 R2V2R2V2 R2V3R2V3

R3V1R3V1 R3V2R3V2 R3V3R3V3

test1 table

C1C1 C2C2 C3C3

test2 table

Page 53: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

All the data from table test1 is moved into table test2 and removed from table test1.All the data from table test1 is moved into table test2 and removed from table test1.

CREATE TRIGGER test1test2 BEFORE DELETE ON test1CREATE TRIGGER test1test2 BEFORE DELETE ON test1

FOR EACH ROW FOR EACH ROW INSERT INTO test2 set test2.C1 = old.C1, INSERT INTO test2 set test2.C1 = old.C1, test2.C2=old.C2, test2.C3=old.C3; test2.C2=old.C2, test2.C3=old.C3;

DELETE FROM test1;DELETE FROM test1;

C1C1 C2C2 C3C3

R1V1R1V1 R1V2R1V2 R1V3R1V3

R2V1R2V1 R2V2R2V2 R2V3R2V3

R3V1R3V1 R3V2R3V2 R3V3R3V3

test1 table

C1C1 C2C2 C3C3

test2 table

Page 54: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

Imagine you wished to track who made changes and inserts into your database. You Imagine you wished to track who made changes and inserts into your database. You can do this with triggers and a few extra fields added to every table.can do this with triggers and a few extra fields added to every table.

While I haven’t mentioned this before, it is standard practice to include the following 4 While I haven’t mentioned this before, it is standard practice to include the following 4 fields in every table in your database:fields in every table in your database:

ID_CREATE ID_CREATE

DATE_CREATEDDATE_CREATED

ID_MODIFIEDID_MODIFIED

DATE_MODIFIEDDATE_MODIFIED

When a record is first inserted into a table, the current user ID and date/time is When a record is first inserted into a table, the current user ID and date/time is recorded in the ID_CREATE and DATE_CREATED fields.recorded in the ID_CREATE and DATE_CREATED fields.

Similarly, when a record is modified, the current user ID and date/time is recorded in Similarly, when a record is modified, the current user ID and date/time is recorded in the ID_MODIFIED and DATE_MODIFIED fields.the ID_MODIFIED and DATE_MODIFIED fields.

Page 55: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQL

TRIGGERSTRIGGERS

When a record is first inserted into a table, the current user ID and date/time When a record is first inserted into a table, the current user ID and date/time is recorded in the ID_CREATE and DATE_CREATED fields.is recorded in the ID_CREATE and DATE_CREATED fields.

We accomplish this with the following code:We accomplish this with the following code:

CREATE TRIGGER test4INS BEFORE INSERT ON test4CREATE TRIGGER test4INS BEFORE INSERT ON test4

FOR EACH ROW FOR EACH ROW

set new.ID_CREATE = USER(), new.DATE_CREATED = DATE(NOW());set new.ID_CREATE = USER(), new.DATE_CREATED = DATE(NOW());

Notice, the use of BEFORE, we need to set values before we insert them into Notice, the use of BEFORE, we need to set values before we insert them into the database.the database.

Otherwise you will get an error similar to:Otherwise you will get an error similar to:

ERROR 1362 (HY000): Updating of NEW row is not allowed in after triggerERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger

Page 56: Database Systems – SQL SQL OPTIMIZATION The key to an optimized database is good design. There are some basic physical constraints that databases must

Database Systems – SQLDatabase Systems – SQLTRIGGERSTRIGGERS

When a record is modified, the current user ID and date/time is recorded in the When a record is modified, the current user ID and date/time is recorded in the ID_MODIFIED and DATE_MODIFIED fields.ID_MODIFIED and DATE_MODIFIED fields.

We accomplish this with the following code:We accomplish this with the following code:

CREATE TRIGGER test4UPD BEFORE UPDATE ON test4CREATE TRIGGER test4UPD BEFORE UPDATE ON test4 FOR EACH ROW FOR EACH ROW set new.ID_MODIFIED = USER(), new.DATE_MODIFIED = DATE(NOW());set new.ID_MODIFIED = USER(), new.DATE_MODIFIED = DATE(NOW());

Notice, the use of BEFORE, we need to set values before we insert them into the Notice, the use of BEFORE, we need to set values before we insert them into the database.database.