4. docker run --name db -p 5432:5432 -e
POSTGRES_PASSWORD=abc -d postgres:9.6.1-alpine
Tốt nhất là chạy docker bộ cài chuẩn Postgresql Alpine Linux
1. Cài docker
2. Trên terminal, gõ lệnh dưới đây
28. • Kế thừa 1 bảng – single table inheritance
• Kế thừa nhiều bảng – multiple table inheritance
------------------------------------------------
• Liên kết đa hình - polymorphism association
• Product item variant
• EAV
34. • Data chi phối Meta Data
– Một đơn hàng có nhiều phương pháp thanh toán
• Dữ liệu khó chuẩn hóa
– Chủng loại mặt hàng lớn. Mỗi mặt hàng có nhiều
thuộc tính khác nhau
• Temporal data
– Thuộc tính của bản ghi, tập bản ghi thay đổi theo thời
gian
38. Giải pháp
”Data decides metadata” ~ dữ liệu chi phối
cấu trúc dữ liệu
• Khó có thể chỉ viết 1 câu lệnh SQL
• Viết logic chọn bảng ở PHP, Node.js, Python…
• Viết function trực tiếp trong Postgresql
42. Tách biệt mã hàng
• Tiện cho lập trình, quản lý
• Mất công gõ lại mô tả sản
phẩm
• Khó cho khách hàng so
sánh, tùy chỉnh
Một mã hàng nhiều lựa chọn
• Phức tạp khi lập trình, quản lý
• Tái sử dụng thông tin chung
• Dễ cho khách hàng so sánh,
tùy chỉnh
54. Event Sourcing
Lưu tất cả các sự kiện lên đối tượng và play back
lại
https://ookami86.github.io/event-sourcing-in-practice/
https://msdn.microsoft.com/en-us/library/dn589792.aspx
Đang thử nghiệm chưa dám phán, đối với 1 đơn
hàng, thay đổi < 20 sự kiện, thời gian playback lại
sẽ nhanh
55. Các câu hỏi đặt ra với Event Sourcing
• Event Source khác hẳn với CRUD, các constrain ràng buộc PK-FK còn áp dụng
được hay không
• Khi một đối tượng ràng buộc quan hệ với nhiều đối tượng hay tập khác (1:1,
1:nhiều, nhiều nhiều), việc lưu event tác động lên đối tượng đó sẽ dẫn theo
một loạt event lên nhiều đối tượng khác => xử lý transaction thế nào, thứ tự
trước sau….
• Pattern Event Sourcing có vẻ hay nhưng không dễ áp dụng !
60. SELECT a.first_name || ' ' || a.last_name AS fullname,
f.title FROM actor AS a
JOIN film_actor AS fa ON fa.actor_id = a.actor_id
JOIN film AS f ON f.film_id = fa.film_id
ORDER BY fullname;
61.
62. CREATE INDEX idx_title ON public.film
USING btree
(title COLLATE pg_catalog."default”
varchar_pattern_ops)
Tốc độ 8 lần
SELECT title FROM film WHERE title LIKE 'Bea%';
66. SELECT * FROM pg_available_extensions;
+--------------------+----------------------------------------------------------------------+
| name | comment |
|--------------------+----------------------------------------------------------------------|
| file_fdw | foreign-data wrapper for flat file access |
| dict_xsyn | text search dictionary template for extended synonym processing |
| refint | functions for implementing referential integrity (obsolete) |
| postgres_fdw | foreign-data wrapper for remote PostgreSQL servers |
| pgrowlocks | show row-level locking information |
| btree_gin | support for indexing common datatypes in GIN |
| tsm_system_time | TABLESAMPLE method which accepts time in milliseconds as a limit |
| intagg | integer aggregator and enumerator (obsolete) |
| pg_prewarm | prewarm relation data |
| pg_trgm | text similarity measurement and index searching based on trigrams |
| pg_stat_statements | track execution statistics of all SQL statements executed |
| moddatetime | functions for tracking last modification time |
| fuzzystrmatch | determine similarities and distance between strings |
| seg | data type for representing line segments or floating-point intervals |
| pg_buffercache | examine the shared buffer cache |
| dict_int | text search dictionary template for integers |
| pageinspect | inspect the contents of database pages at a low level |
| timetravel | functions for implementing time travel |
| tablefunc | functions that manipulate whole tables, including crosstab |
| ltree | data type for hierarchical tree-like structures |
67. +--------------------+----------------------------------------------------------------------+
| name | comment |
|--------------------+----------------------------------------------------------------------|
| pgstattuple | show tuple-level statistics |
| hstore | data type for storing sets of (key, value) pairs |
| cube | data type for multidimensional cubes |
| btree_gist | support for indexing common datatypes in GiST |
| pgcrypto | cryptographic functions |
| earthdistance | calculate great-circle distances on the surface of the Earth |
| dblink | connect to other PostgreSQL databases from within a database |
| plpgsql | PL/pgSQL procedural language |
| isn | data types for international product numbering standards |
| insert_username | functions for tracking who changed a table |
| tcn | Triggered change notifications |
| intarray | functions, operators, and index support for 1-D arrays of integers |
| sslinfo | information about SSL certificates |
| tsm_system_rows | TABLESAMPLE method which accepts number of rows as a limit |
| pg_freespacemap | examine the free space map (FSM) |
| lo | Large Object maintenance |
| chkpass | data type for auto-encrypted passwords |
| autoinc | functions for autoincrementing fields |
| citext | data type for case-insensitive character strings |
| adminpack | administrative functions for PostgreSQL |
| unaccent | text search dictionary that removes accents |
| tsearch2 | compatibility package for pre-8.3 text search functions |
+--------------------+----------------------------------------------------------------------+
SELECT * FROM pg_available_extensions;
70. CREATE EXTENSION unaccent;
SELECT unaccent('Hà nội')
+------------+
| unaccent |
|------------|
| Ha noi |
+------------+
À A
Á A
 A
à A
Ä A
Å A
à a
ộ o
Địa chỉ bằng tiếng Việt có quá nhiều biến thể, unaccent giúp bỏ dấu