Optimisation
Optimisation
• HAVING clause
Chinatown Polanski Nicholson
Chinatown Polanski Polanski
– This is used to specify conditions for filtering data – e.g., which group result
should appear in the final results of the query. Table: Showing
• HAVING requires that a GROUP BY clause is present in a Title Cinema
Chinatown Odeon
query. Shirley Valentine Odeon
• Also note that HAVING is used with summarized group records The Pianist Phoenix
A Single Man Vue
• WHERE is used with individual records A Single Man Odeon
Mama Mia Vue
Chinatown Vue
3 4
• Create tables Films and Showing. The following query will insert data into the Films table
5 6
Example – add data into tables
Query – Find duplicate names of directors using aggregates
9 10
Query – explained SQL Queries - JOIN
Source: http://www.w3schools.com/sql/sql_join.asp
11 12
14
13
Example: INNER JOIN Example: INNER JOIN
• INNER JOIN operation on tables Student and Address • INNER JOIN operation on tables Student and Module
● A LEFT JOIN (or LEFT OUTER JOIN ) returns all rows ● A RIGHT JOIN (or RIGHT OUTER JOIN) returns all
from the left table (table1), with the matching rows in the rows from the right table (table2), with the matching rows
right table (table2). The result is NULL in the right side in the left table (table1). The result is NULL in the left
when there is no match. side when there is no match
Note:
DB Browser (SQLite) may not support this FULL JOIN
19 20
● For storing decimal values, there are various choices: ● Strings are the most complex data type to work with.
FLOAT, DOUBLE, and DECIMAL. (e.g., to store a number
like 42315.573, Datatype could be DOUBLE(8, 3) where
● There are essentially three choices for storing strings:
8 is the total number of digits, and 3 is the number of TEXT, VARCHAR and CHAR.
digits to follow the decimal). ● For both char types, you specify a size.
● The difference between these is in both range (how – VARCHAR takes only as much space as it needs
high/low a number you can store) and precision (how
many decimal places you can store). – CHAR always takes the size you specify, no matter
● DECIMAL has the largest range and precision, but FLOAT what.
and DOUBLE are much faster than DECIMAL. This is ● The drawback of VARCHAR is that, it needs extra
because DECIMAL is so big it cannot be processed
processing to re-organize the data (which isn’t an
directly on the computer CPU.
option if there are a million records).
23 24
● TEXT String types ( TINYTEXT, TEXT, MEDIUMTEXT, and ● Usually, every row in your database should have some
LONGTEXT) can be used if you want to store a very long kind of integer identifier. Often, this is the primary key
string, but are much more inefficient than the char types
● Integers are by far the fastest data type for computers
● TEXT types cannot have a default value to work with, and so using this integer internally to
● Use TEXT types only if you want to store large strings and identify the record will speed up your application.
do not need to index them. ● Using strings as identifiers – they are much slower for
the computer to work with than integers.
25 26
Speeding up queries
● Atomicity: a transaction should be atomic. Either all ● Create a table called accounts
operations of a transaction are successfully completed or ● Define some attributes, for example:
not.
● Consistency: a transaction must transform a database
from one consistent state to another. When a transaction
executes its statements to modify data, the database CREATE TABLE accounts (
becomes inconsistent. But when transaction is committed account_no INTEGER NOT NULL,
account_name char(30),
or rolled back, the database must be consistent.
balance DECIMAL NOT NULL DEFAULT 0,
● Isolation: concurrently running transactions must be
PRIMARY KEY(account_no)
executed in isolation so that database remains );
consistent.
● Durability: if a transaction is successfully committed, the
changes (updates) must be made permanent in the
database.
31 32
Insert some data into the table. ● Transaction for transferring money from one account to
another. For example, transfer 1000.00 from Ali’s account
INSERT INTO accounts (account_no,account_name, balance) to Sarah’s account
VALUES
(1,”Alan John”, 2000), ● This transfer will involve update operations
(2,”Ali Khan”, 9000), ● Deduct money from Ali’s account
(3, “Sarah Smith”, 5000);
● Deposit (add) money into Sarah’s account
● The update operations are run together as a transaction –
either both operations successfully complete or none of
account_no account_name balance them.
1 Alan John 2000 ● This is to maintain consistency of the data.
2 Ali Khan 9000
3 Sarah Smith 5000
33 34
Transactions - Example Database Transactions: Concurrency
● Transfer 1000.00 from Ali’s account to Sarah’s account ● A problem (also called a race condition) might occur if two
transfers occur at the same time (concurrent execution of
BEGIN TRANSACTION; database operations).
UPDATE accounts
● Consider another example: Ben has 500 pounds in his
SET balance = balance - 1000
WHERE account_no = 2; account. He transfers 200 to Betty and 200 to Beatrice.
● Betty’s bank’s transfer script sends a SELECT to read
UPDATE accounts Ben’s balance, and finds it is 500. Beatrice’s bank sends
SET balance = balance + 1000 the same query at the same time; the result is still 500.
WHERE account_no = 3;
● Betty’s bank computer subtracts 200 from Ben’s balance
of 500, and sends an UPDATE to set Ben’s balance to 300.
COMMIT;
account_no account_name balance
● Beatrice’s bank computer also subtracts 200 from Ben’s
1 Alan John 2000 balance, which it still thinks is 500! It has no idea that the
2 Ali Khan 8000 balance has changed since its SELECT query finished.
3 Sarah Smith 6000 35
● Beatrice’s bank wrongly UPDATEs Ben’s balance to 300. 36
● To deal with race condition, the common way is to use a ● If an instance tries to issue a LOCK TABLES command
lock, which tells SQL that the script wants uninterrupted while the tables in question are already locked, SQL
access to the table for a while. does not respond to the command - thus forcing the
● Issuing the command LOCK TABLE Balances instance to wait.
WRITE; will guarantee that the particular script instance ● Once the lock is released by the instance which held it,
SQL gets the lock for the requesting instance and
issuing the command will be the only one to access the
table Balances until it gives up the lock (via UNLOCK responds to the command, ending the wait.
● If several instances try to run LOCK TABLES while the
TABLES;).
tables are already locked, SQL effectively makes them
● Any other instance that wants to access the table will wait in a queue.
have to wait until the lock is released. ● Since locking forces scripts to wait, it slows down your
system. However, sometimes – as the example shows –
it is necessary to use locks.
37 38