28 Mart 2021 Pazar

NOW() metodu

Giriş
MySQL için açıklaması şöyle
The time returned by NOW(), and other date time functions, is derived from the start time of the query. 
Açıklaması şöyle
Default Timestamps
It is always better to have the two timestamptz fields created_at and updated_at columns with default now() .... Storing what time a record was created or modified would be very useful in the future when going over some analytics or reporting. Audit logs might be required and timestamps are key.
Örnek
Görmek için şöyle yaparız. Burada sleep() olmasına rağmen now() tek select için aynı sonucu veriyor.
MariaDB [test]> select now(),sleep(10),now();
+---------------------+-----------+---------------------+
| now()               | sleep(10) | now()               |
+---------------------+-----------+---------------------+
| 2021-03-22 14:17:05 |         0 | 2021-03-22 14:17:05 |
+---------------------+-----------+---------------------+

23 Mart 2021 Salı

FULL OUTER JOIN - İki Tablonun Union'ı Gibidir

Giriş
Join tiplerini görsel olarak gösteren resimler burada.
Şeklen şöyle
Bir başka şekil şöyle


İki çeşit FULL OUTER JOIN var.
1.  FULL OUTER JOIN
2.  FULL OUTER JOIN (IF NULL)

Not : Bazı veri tabanları Full Outer Join'i desteklemez. Örneğin MySQL. Açıklaması şöyle
Unlike SQL Server, MySQL does not have a distinct JOIN type for FULL OUTER JOIN. You may, however, combine LEFT OUTER JOIN and RIGHT OUTER JOIN to get the same effects as FULL OUTER JOIN.
Bu durumda şöyle yaparız
SELECT *  FROM tableA 
LEFT JOIN tableB 
  ON tableA.id = tableB.id

UNION

SELECT * FROM tableA
RIGHT JOIN tableB 
  ON tableA.id = tableB.id

1.  FULL OUTER JOIN
Örnek
Şöyle yaparız
SELECT FROM tableA a FULL OUTER JOIN tableB b ON a.key = b.key
Örnek
Şöyle yaparız
-- Retrieve all employees and department names, including those -- without a department and departments without employees SELECT employees.employee_name, departments.department_name FROM employees FULL JOIN departments ON employees.department_id = departments.department_id;
2. RIGHT JOIN (IF NULL)
İki tablonun kesişimi olmayan satırları gösterir.
Örnek
Şöyle yaparız
SELECT FROM tableA a FULL OUTER JOIN tableB b on a.key = b.key
WHERE a.key IS NULL OR b.key IS NULL


RIGTH JOIN - İki Tablonun Kesişimi Yoksa Soldaki Satırı Null Döner

Giriş
Join tiplerini görsel olarak gösteren resimler burada.
Şeklen şöyle
İki çeşit RIGHT JOIN var.
1. RIGHT JOIN
2. RIGHT JOIN (IF NULL)

1. RIGHT JOIN
Sağdaki tablonun tamamını ve soldaki tablonun kesişimini verir. Dolayısıyla sağdaki tablo ile kesişimi olmayan satırlar için bazı sütunlar null gelir.

Örnek
Şöyle yaparız
SELECT from tableA A RIGHT JOIN tableB B on A.key = B.key
2. RIGHT JOIN (IF NULL)
Örnek
Şöyle yaparız
SELECT from tableA A RIGHT JOIN tableB B on A.key = B.key WhERE A.key is NULL


9 Mart 2021 Salı

Sütun Tipleri - Array

Giriş
Oracle'daki VARARRAY tipi gibidir.

Neden Lazım
Normalization'dan kurtabilir. 

Örnek
Elimizde foreign key table ile birleştirilen iki tablo olsun
create table users (
    user_id int not null primary key generated always as identity,
    name text not null unique
);

create table roles (
    role_id int not null primary key generated always as identity,
    name text not null unique
);

create table users_roles (
    user_id int not null references users,
    role_id int not null references roles,
    primary key (user_id, role_id)
);
Aslında rolleri array olarak ta saklayabiliriz. Şöyle yaparız
create table users (
    user_id int not null primary key generated always as identity,
    name text not null unique,
    roles text[] not null,
    constraint roles_check check (roles <@ array['admin', 'user', 'guest'])
);
Açıklaması şöyle
The constraint roles_check check (roles <@ array['admin', 'user', 'guest']) uses <@ array operator to ensure that every insert and update can only have array values that exist on the right side array ['admin', 'user', 'guest'].
Eğer constraint değiştirmek istersek şöyle yaparız
begin;

alter table users drop constraint roles_check;
alter table users add constraint roles_check check
  (roles <@ array['super', 'admin', 'user']); 

end;
Ancak mevcut kayıtların bazısı yeni kısıtı ihlal edebilir. Bu yüzden not valid kullanmak gerekiyor. Şöyle yaparız
begin;

alter table users drop constraint roles_check;
alter table users add constraint roles_check check
  (roles <@ array['super', 'admin', 'user']) not valid; 

end;
not_valid kullanılmasının sebebi şöyle
  • Records with invalid role guest will remain intact. That small deviation from the data integrity can be safely ignored (if business rules permit).
  • All new inserts and updates will enforce new data integrity rules — roles must exist in a new array: ['super', 'admin', 'user'].
  • The script that updates the new check constraint will run fast, even on big tables and it will not lock or block anyone or anything.

TEXT Array
Örnek 
Şöyle yaparız.
CREATE TABLE event (
  id INT8 NOT NULL,
  version INT4,
  sensor_names TEXT[],
  sensor_values INTEGER[],
  PRIMARY KEY (id)
)
Örnek
Şöyle yaparız
CREATE TABLE product (
  id        BIGINT,
  images    TEXT[]
)
INSERT INTO product(id, images) VALUES (1, '{"url1", "url2", "url3"}');

SELECT * FROM product WHERE images @> ARRAY['url2']

7 Mart 2021 Pazar

pg_sleep metodu

Örnek
Şöyle yaparız
select 1 from t_book where (select pg_sleep(1)) is not null limit 3;

23 Şubat 2021 Salı

LTREE Extension - Hierarchical Tree

Giriş
Açıklaması şöyle
What’s ltree?
ltree is a Postgres extension for representing and querying data stored in a hierarchical tree-like structure.
...
ltree enables powerful search functionality that can be used to model, query and validate hierarchical and arbitrarily nested data structures. 
Kurulum
Şöyle yaparız
CREATE EXTENSION IF NOT EXISTS LTREE;
LTREE Sütun Tipi
LTREE Sütun Tipi kullanılır

Index Yaratma
GIST veya BTREE tipinden index yaratılır
Örnek
Şöyle yaparız. Sütun ismi path, tablo ismi test
CREATE INDEX path_gist_idx ON test USING GIST (path);
CREATE INDEX path_idx ON test USING BTREE (path);
Örnek
Şöyle yaparız
CREATE INDEX tree_path_idx ON tree USING GIST(path);
Tablo Yaratma
Örnek
Şöyle yaparız
CREATE TABLE test (path LTREE);
INSERT INTO test VALUES ('Top'), ('Top.Science'), ('Top.Science.Astronomy'), ('Top.Science.Astronomy.Astrophysics'), ('Top.Science.Astronomy.Cosmology'), ('Top.Hobbies'), ('Top.Hobbies.Amateurs_Astronomy'), ('Top.Collections'), ('Top.Collections.Pictures'), ('Top.Collections.Pictures.Astronomy'), ('Top.Collections.Pictures.Astronomy.Stars'), ('Top.Collections.Pictures.Astronomy.Galaxies'), ('Top.Collections.Pictures.Astronomy.Astronauts'); -- Optionally, create indexes to speed up certain operations CREATE INDEX path_gist_idx ON test USING GIST (path); CREATE INDEX path_idx ON test USING BTREE (path);
Örnek
Şöyle yaparız
CREATE TABLE tree(
  id SERIAL PRIMARY KEY,
  letter CHARACTER,
  path LTREE
);
CREATE INDEX tree_path_idx ON tree USING GIST(path);
LTREE operators
Bazıları şöyle
_eq
_gt 
_is_null
_ancestor
_descendant 
__matches 
_matches_fulltext 
_any







21 Şubat 2021 Pazar

VACUUM - Tabloyu Bloke Etmez, Disk Alanını da Geri Vermez

Giriş
Açıklaması şöyleVACUUM komutu bir tablo üzerinde vakumlama işlemini elle başlatır. 
PostgreSQL provides two types of vacuums: manual and auto.
VACUUM Komutları Neden Lazım?
Açıklaması şöyle. PostgreSQL silinmesi istenen satırları hemen silmiyor. Hemen silinmemesiyle ilgili olarak MVCC yazısına da bakabilirsiniz. Bu yüzden bu satırları ara ara VACUMM ile silmek gerekiyor.
When your Java application executes a DELETE or UPDATE statement against a PostgreSQL database, a deleted record is not removed immediately nor is an existing record updated in its place. Instead, the deleted record is marked as a dead tuple and will remain in storage. The updated record is, in fact, a brand new record that PostgreSQL inserts by copying the previous version of the record and updating requested columns. The previous version of that updated record is considered deleted and, as with the DELETE operation, marked as a dead tuple.

There is a good reason why the database engine keeps old versions of the deleted and updated records in its storage. For starters, your application can run a bunch of transactions against PostgreSQL in parallel. Some of those transactions do start earlier than others. But if a transaction deletes a record that still might be of interest to a few transactions started earlier, then the record needs to be kept in the database (at least until the point in time when all earlier started transactions finish). This is how PostgreSQL implements MVCC (multi-version concurrency protocol).

It’s clear that PostgreSQL can’t and doesn’t want to keep the dead tuples forever. This is why the database has its own garbage collection process called vacuuming. There are two types of VACUUM — the plain one and the full one. The plain VACUUM works in parallel with your application workloads and doesn’t block your queries. This type of vacuuming marks the space occupied by dead tuples as free, making it available for new data that your app will add to the same table later. The plain VACUUM doesn’t return the space to the operating system so that it can be reused by other tables or 3rd party applications (except in some corner cases when a page includes only dead tuples and the page is in the end of a table).
VACUUM ve VACUUM FULL Farkı Nedir?
Açıklaması şöyle. Yani VACUUM tablonun kullandığı disk alanını işletim sistemine geri vermez ama tabloları da bloke etmez. VACUUM FULL ise disk alanını geri verir ama tabloyu bloke eder.
VACUUM
The VACUUM process removes DEAD tuples for future usage, but it does not return the space to the operating system.

Therefore, if you perform a bulk data deletion or updates, you might be using too much storage due to space occupied by these DEAD tuples. 

VACUUM FULL
The VACUUM FULL process returns the space to the operating system, ... It does the following tasks.

1. VACUUM FULL process obtains an exclusive lock on the table.
2. It creates a new empty storage table file.
3. Copy the live tuples to the new table storage.
4. Removes the old table file and frees the storage.
5. It rebuilds all associated table indexes, updates the system catalogs and statistics.
VACUUM şeklen şöyle

Örnek
İsmi SampleTable olan tabloyu vakumlamak için şöyle yaparız
VACUUM SampleTable;
Örnek
Şöyle yaparız
VACUUM ANALYZE table_name;