03 Introduction To PostgreSQL
03 Introduction To PostgreSQL
• VirtualBox based
• Has installed:
• PostgreSQL
• Dbeaver
• Sublime text editor
• Install Oracle VirtualBox, and add the virtual machine to your installation.
• The user is user1 and password user1
What is PostgreSQL
• descripció I breu història
• capacitats (replicas, transaccions, bloqueig sofisticat, vistes, integritat
referencial, tipus definits per l’usuari, herencia, regles)
https://www.postgresql.org/docs/9.1/sql.html
What can PostgreSQL offer us?
It is said to be “the most advanced open-source relational database in the world”.
Key points:
• Highly standard compliant
• Supports high concurrency with clever locks
• Transactions: ensures atomicity, isolation, and durability.
• Support for a high set of datatypes (numeric, shapes, addresses, bit strings, texts, json, etc..)
• Replication capabilities
Dedicated to
replication
Stored data
https://en.wikibooks.org/wiki/PostgreSQL/Architecture#:~:text=PostgreSQL%20is%20a%20relational%20database,request%20read%20and%20write%20operations.
PostgreSQL connectivity
• Achitecture of PostgreSQL. C/C#/VB/P
ython etc..
Java
application
Application in
any platform
that has
application drivers
Files in the fs
Installing postgresql in Linux
The easiest way to install it:
$ sudo apt install postgresql
It is available in Linux,
Windows and OSX.
A tool to know.
Connect to PostgreSQL with dbeaver
You an create a new connection to a
PostgreSQL database. Just select new
database connection and the PostgreSQL
type.
Martians Bases
The INNER join is the intersection zone, is to say the rows that
have a base_id that is in the second table.
Unlocated people
“provide a list of population that is not assigned to any base”
We have a foreign key that makes sure that all base_id in the martian table exists in the bases table.
The base_id in martian table may be null
We want to know who has a base_id set to null.
In this case we want to know who is a martian and its base, even if he does not have a base. Martians Bases
Is important not to forget anyone!
This operation is a LEFT join (the group on the left in the schema)
Of course, RIGHT join will show all martians that have a base, but also include the bases that
do not have martians Martians Bases
There is a fifth case… the ones that are not in the bases, but without looking at the NULL value
Martians Bases
in the base_id.
We will select the bases whose name is bigger than “T” and use that
selection as a table.
Calculations
“Calculate the value stored of each product supply per resort”
PostgreSQL not only can print values, also can perform multiple
calculations..
• And many, many others.. refer to the online user manual for more information.
https://www.postgresql.org/docs/12/functions.html the list is huge!!!
Grouping data
“Calculate the total stock value that each resort has stored”
• Let’s calculate
What is the cost of each product that all bases have?
We could add a HAVING clause after the group by. Will include an additional contition to be applied in the grouped information.
Paging data
Mars has a table of Earthers for marketing purposes.
It contains the Earther_Id, the name, surname and
email of the person. It has nearly 14 million records!
UPDATE <table_name>
SET <field1> = <value1>,
<field2> = <value2>
WHERE <condition> Bases table
UPDATE martians
SET base_id = 4
WHERE martian_id = 9
or
UPDATE martians
SET base_id = 4
WHERE name = ‘Josh’ and surname = ‘Martins’
Updating information Martians table (original data)
UPDATE martians
SET name = ‘Theresa’, surname = ‘Loid’
WHERE martian_id = 5
Updating information Supplies table
UPDATE supplies
SET value = value * 1.07
WHERE supply_id IN (1, 9, 11)
Indexes and optimization
“Earthers” is a table, for marketing purposes, of earthers that may be
interested on the visit to Mars. There are more than 13.5 Million
registers!!.
Its structure is:
create table earthers
(
earther_id serial,
name text not null,
surname text not null,
email text,
primary key (earther_id)
);
Indexes and optimization
Search Time (in
seconds)
The search times in my computer (yours may be different) are: Who has the email James_Ruhl@microsoft.com
Select * from Earthers where email=‘James_Ruhl@microsoft.com’ 5.77
Get the register from Leo White
Select * from Earthers where name=‘Leo’ and surname=‘White’ 5.17
Can we do it better? Sure!! Count how many people is named Noah
Select count(*) from Earthers where name=‘Noah’ 4.84
Count how many people is named Noah
Select count(*) from Earthers where surname=‘White’ 5,13
What will the DBMS do to resolve the queries? Use the EXPLAIN
clause..
https://www.postgresql.org/docs/12/sql-createindex.html
Indexes and optimization
Search Time (in
seconds)
Let’s create an index for the email field: Who has the email James_Ruhl@microsoft.com
Select * from Earthers where email=‘James_Ruhl@microsoft.com’ 5.77
Get the register from Leo White
Select * from Earthers where name=‘Leo’ and surname=‘White’ 5.17
CREATE INDEX ndx_earthers_email
Count how many people is named Noah
ON Earthers (email) Select count(*) from Earthers where name=‘Noah’ 4.84
Count how many people is named Noah
Select count(*) from Earthers where surname=‘White’ 5,13
It will take a bit… 2m 42s
https://www.postgresql.org/docs/12/sql-createindex.html
Indexes and optimization
So… why not to place index for every field in all tables?
So you have to know which are the queries to perform, and create
indexes only for those queries.
https://www.postgresql.org/docs/12/sql-createindex.html
Materialized views
A Materialized View is a view whose resulting data is stored on the
DBMS.
When is the snapshot taken? When you create it or when you refresh it.
It may not be up to date always? Yes. correct.
You can request the DBMS to refresh its data whenever you want.
It will provide the information much faster.
https://www.postgresql.org/docs/12/rules-materializedviews.html
Backup and recovery
From the terminal command line you have the following tools: