0% found this document useful (0 votes)
15 views

Optimisation

Optimistic ML

Uploaded by

Umar Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Optimisation

Optimistic ML

Uploaded by

Umar Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL – more aggregats Example – SQL aggregates

Table: Films (example from week 8)


• Example of aggregate functions (see previous weeks slides)
Title Director Actor
MIN, MAX, SUM, AVG, COUNT, etc. Shirley Valentine Gilbert Collins
• GROUP BY clause Shirley Valentine Gilbert Lipman
The Pianist Polanski Lipman
– This is used with aggregate functions, such as COUNT, SUM, etc. A Single Man Ford Firth
– To group results of a query by one or more columns. For example, results Mama Mia Lloyd Firth
of a query can be grouped by number of students achieving ‘A’ grades Mama Mia Lloyd Walters

• 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

Example – SQL aggregates Example – add data into tables

• Create tables Films and Showing. The following query will insert data into the Films table

INSERT INTO Films(Title, Director, Actor)


VALUES
CREATE TABLE Films(
("Shirley Valentine","Gilbert", "Collins"),
Title text,
("Shirley Valentine", "Gilbert","Lipman"),
Director char(30),
("The Pianist", "Polanski", "Lipman"),
Actor char(30)
("A Single Man", "Ford", "Firth"),
);
("Mama Mia", "Lloyd", "Firth"),
("Mama Mia", "Lloyd", "Walters"),
("Chinatown", "Polanski","Nicholson"),
("Chinatown", "Polanski","Polanski");
CREATE TABLE Showing(
title char(20),
cinema char (20)
);

5 6
Example – add data into tables
Query – Find duplicate names of directors using aggregates

SELECT Title, Director


The following query will insert data into the Films table
FROM Films
INSERT INTO Showing(title, cinema) WHERE (((Director) IN
VALUES (SELECT Director FROM Films AS Tmp_Films GROUP
("Chinatown", "Odeon"), BY Director
("Shirley Valentine", "Odeon"),
("The Pianist", "Phoenix"),
HAVING Count(*) > 1 )))
("A Single Man", "Vue"), ORDER BY Director;
("A Single Man", "Odeon"),
("Mama Mia", "Vue"),
("Chinatown", "Vue"); Title Director
Shirley Valentine Gilbert
Shirley Valentine Gilbert
Mama Mia Lloyd
Mama Mia Lloyd
The Pianist Polanski
Chinatown Polanski
7
Chinatown Polanski 8

Query – explained Query – explained


• The GROUP BY statement is used with an aggregate functions
• SELECT and FROM are used to find / select required such as Count, Sum, Avg in order to group the results by one or
attributes from a table. more columns. For example, in this query the results are to be
• WHERE is used to specify the condition that needs to grouped by the number of names of director.
be met to select required attributes from a table. • The HAVING clause is used to specify conditions that do filtering
• In addition, WHERE clause contains other clauses: -- which group result should appear in the final results of the
query.
– SELECT
• In this query the condition is Count(*)>1. That is, count names of
– FROM directors which appear more than once in a table. Count is used
– GROUP BY to count names of director in a table
– HAVING • As explained before, HAVING requires that a GROUP BY clause
is present in a query.
– Count
• Also HAVING is used with summarized group records. WHERE
is used with individual records

9 10
Query – explained SQL Queries - JOIN

• A JOIN query is used to combine rows from two or more


• The ORDER BY is used to sort results in tables, based on a common field (attribute) between the
ascending or descending order. tables
• By default ORDER BY sorts the records in • The most common type of join is INNER JOIN or simply
JOIN.
ascending order.
• JOIN operation returns all rows from multiple tables
• For example, the results of this query will where the condition is met, which is based on a common
be sorted in ascending order according to field between tables.
Director’s name.

Source: http://www.w3schools.com/sql/sql_join.asp

11 12

SQL Queries – Types of JOIN Example – JOIN Queries


• (INNER) JOIN: Returns records that have matching values in both tables Student Module
• LEFT (OUTER) JOIN: Returns all records from the left table (table1), and the ID Name ID Module
matched records from the right table (table2)
120 Alice 120 Database
• RIGHT (OUTER) JOIN: Returns all records from the right table (table2), and
the matched records from the left table (table1) 130 James 130 Maths
• FULL (OUTER) JOIN: Returns all records when there is a match in either left
(table1) or right table (table2) 140 Khan 140 Web design
RIGHT OUTER
120 Web design

INNER Table 1 Table 2 Address


Table 1 Table 2

ID HNo Street City Postcode

FULL OUTER 120 22 London Rd Oxford OX3 1HX


LEFT OUTER
130 11 High St Oxford OX1 3RE
Table 1 Table 1 Table 2
Table 2
140 30 Old Road Oxford OX3 3WS

14
13
Example: INNER JOIN Example: INNER JOIN

• INNER JOIN operation on tables Student and Address • INNER JOIN operation on tables Student and Module

SELECT Student.Name, Address.H_NO, Address.Street, Address.Postcode SELECT Student.Name, Module.module


FROM Student
INNER JOIN Address
FROM Student
ON Student.ID = Address.ID; INNER JOIN Module
ON Student.ID = Module.ID;

Name H_No Street Postcode


Result
Alice 22 London Rd OX3 1HX
Alice 30 Old Road OX3 3WS Name Module
James 11 High St OX1 3RE Alice Database
Alice Web Design
James Maths
Khan Web Design
15 16

Example: LEFT JOIN Example – RIGHT JOIN

● 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

SELECT Student.ID, Student.Name, Module.module FROM SELECT * FROM


Student Student
LEFT JOIN Module RIGHT JOIN Module
ON Student.ID = Module.ID ON Student.ID = Module.ID

ID Name Module Note:


120 Alice Database DB Browser (SQLite) may not support this RIGHT JOIN
120 Alice Web Design
130 James Maths
140 Khan Web Design
17 18
Example – FULL JOIN

● A FULL JOIN (or FULL OUTER JOIN) returns all records


when there is a match in either left (table1) or right table
(table2)

SELECT Student.ID, Student.Name, Address.City


FROM Student
FULL OUTER JOIN Address Database Optimisation
ON Student.ID=Address.ID
ORDER BY Student.Name

Note:
DB Browser (SQLite) may not support this FULL JOIN

19 20

Database Optimisation Designing your database: Datatypes


● Optimising database is a major factor in database design
and development. It becomes more important when dealing Integers:
with large amount of data
● When choosing datatypes, try to use the smallest type
● Designing a database may have several alternative ways
that will hold all the data you want.
● But database should be designed in a way that is efficient ● SQL has several different types for storing integer
and less costly in terms of answering queries. numbers, ranging from TINYINT to BIGINT. The
difference is in the range of numbers that can be stored.
● For instance:
For example, a TINYINT can be from -128 to 127.
– Queries should be designed such that their response time ● If you don’t need to store negative numbers, you can
is less -- i.e., they take less time to return results declare the data type as UNSIGNED, to apply the whole
– They should have less cost in terms of I/O (Input/Output) range in the positive direction.
operations. i.e., reading/writing data from/to storage ● But think ahead – changing the type once your
devices such as disks (as disks are slower than computer application is running is a complex job.
memory). 21 22
Datatypes – Float point numbers Datatypes – Strings

● 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

Datatypes – Strings Defining/Using IDs

● 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

● Don’t SELECT more columns than you need. For


example, if you only need a customer’s ID number,
SELECT just that field. Avoid using SELECT * statement.
● Don’t SELECT more rows if not needed. SQL may not be
slow, but it still internally finds all rows. Database Transactions
● Likewise, even if you know that only one or two rows
meet a particular criteria, SQL might not and will search
the whole database just in case.
● You can tell SQL to stop after finding a certain number of
rows by adding LIMIT 2 (for example) to the end of the
SELECT statement. For example the following query:
SELECT * FROM Films LIMIT 5;
will return the first 5 records from table, Films. 27 28

Database Transactions Database Transactions


● Database Transactions are used to group different operations
● Transactions try to solve this problem by following ACID
(SQL queries) together and execute them as one unit of work
(atomicity property). (Atomicity, Consistency, Isolation, Durability) properties or
criteria
● They are used for concurrency and recovery of database ● In SQL, you start a transaction with the command BEGIN
operations that are related to (or depend on) each other. TRANSACTION command.
● Suppose that your database represents a bank data, and you ● Once a transaction is started, any commands you issue
want to transfer some money between two accounts. that make changes to the database will have no
● You will need an UPDATE to take the money from the balance of immediate effect. No actual changes will be made until
the first account, and another UPDATE to add it to the balance of you indicate the end of the transaction, whereupon they
the second. will all be made at once.
● You can end a transaction with the command COMMIT.
● However, if the system was to (due to a fault, or a crash) perform
one of these UPDATEs but not the other, then the consistency of ● Also, if – partway through processing – you realise that
your database would be damaged (and you or the bank would the entire transaction was a mistake, you can cancel the
loose money – money is deducted from one account but not whole thing with the command ROLLBACK.
added to another account)
29 30
Database Transactions - ACID Properties Transactions - Example

● 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

Transactions - Example Transactions - Example

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

Database Transactions: Concurrency Database Transactions: Concurrency

● 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

You might also like