JaxNode June 2021
fek.io/blog
youtube.com/c/polyglotengineer
github.com/davidfekke
@jaxnode @polyglotengine1
Single Point of Failure
Slow
Difficult to Scale
Data broken up in Tables
Bad support JSON docs
Use Standard SQL
Lots of experienced DBAs
Efficient Data space
ACID
One Database to Rule them All!
SELECT *
FROM Users
WHERE clue > 0;
-- results: 0
SQL Statements
SELECT
id, firstname, lastname, age
FROM
person
WHERE
age >= 32;
SELECT p.firstname, p.lastname, p.age, t.username
FROM person p
JOIN user_table t
ON p.id = t.person_id
WHERE p.age > 32;
SELECT p.firstname, p.lastname, p.age, t.username
FROM person p
LEFT JOIN user_table t
ON p.id = t.person_id
WHERE p.age > 32;
id | firstname | lastname | username |
---|---|---|---|
1 | John | Smith | NULL |
2 | Bob | MacKenzie | bmac |
3 | Jane | Doe | NULL |
CREATE TABLE stock (
id SERIAL PRIMARY KEY,
symbol TEXT NOT NULL,
name TEXT NOT NULL,
exchange TEXT NOT NULL,
is_etf BOOLEAN NOT NULL
);
CREATE INDEX ON mention (stock_id, dt DESC);
CREATE VIEW comedies AS
SELECT *
FROM films
WHERE kind = 'Comedy';
create or replace procedure movemoney(
sender int,
receiver int,
amount dec
)
language plpgsql
as $$
begin
-- subtracting the amount from the sender's account
update bank_accounts
set balance = balance - amount
where id = sender;
-- adding the amount to the receiver's account
update bank_accounts
set balance = balance + amount
where id = receiver;
commit;
end;$$
create function get_disc_count(len_from int, len_to int)
returns int
language plpgsql
as
$$
declare
disc_count integer;
begin
select count(*)
into disc_count
from disc
where length between len_from and len_to;
return disc_count;
end;
$$;
CREATE TABLE mention (
stock_id INTEGER,
dt TIMESTAMP WITHOUT TIME ZONE NOT NULL,
message TEXT NOT NULL,
source TEXT NOT NULL,
url TEXT NOT NULL,
PRIMARY KEY (stock_id, dt),
CONSTRAINT fk_mention_stock FOREIGN KEY (stock_id) REFERENCES stock (id)
);
CREATE INDEX ON mention (stock_id, dt DESC);
-- Hypertable Created Here
SELECT create_hypertable('mention', 'dt');
import path from 'path';
import { promises as fsPromises } from 'fs';
import PG from 'pg';
const Pool = PG.Pool;
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'etfdb',
password: 'password',
port: 5432
});
try {
const nysedata = await fsPromises.readFile('NYSE_screener.csv', 'UTF-8');
const nyseArray = nysedata.split('\r\n');
nyseArray.shift();
for (let item of nyseArray) {
const row = item.split(',');
console.log(`NASDAQ Symbol: ${row[0]}, name: ${row[1]}`);
await pool.query(insert_query,[row[0], row[1],'NYSE', 'false']);
}
} catch (err) {
console.error(err);
}
pool.end();