1. Giới thiệu
Trong lớp học lập trình này, bạn sẽ tìm hiểu cách triển khai AlloyDB Omni và sử dụng Columnar Engine để cải thiện hiệu suất cho các truy vấn.

Điều kiện tiên quyết
- Hiểu biết cơ bản về Google Cloud Console
- Kỹ năng cơ bản về giao diện dòng lệnh và Google Shell
Kiến thức bạn sẽ học được
- Cách triển khai AlloyDB Omni trên máy ảo GCE trong Google Cloud
- Cách kết nối với AlloyDB Omni
- Cách tải dữ liệu lên AlloyDB Omni
- Cách bật Columnar Engine
- Cách kiểm tra Columnar Engine ở chế độ Tự động
- Cách điền dữ liệu vào Columnar Store theo cách thủ công
Bạn cần có
- Tài khoản Google Cloud và dự án Google Cloud
- Một trình duyệt web như Chrome
2. Thiết lập và yêu cầu
Thiết lập môi trường theo tốc độ của riêng bạn
- Đăng nhập vào Google Cloud Console rồi tạo một dự án mới hoặc sử dụng lại một dự án hiện có. Nếu chưa có tài khoản Gmail hoặc Google Workspace, bạn phải tạo một tài khoản.



- Tên dự án là tên hiển thị của những người tham gia dự án này. Đây là một chuỗi ký tự mà các API của Google không sử dụng. Bạn luôn có thể cập nhật thông tin này.
- Mã dự án là giá trị duy nhất trên tất cả các dự án trên Google Cloud và không thể thay đổi (bạn không thể thay đổi mã này sau khi đã đặt). Cloud Console sẽ tự động tạo một chuỗi duy nhất; thường thì bạn không cần quan tâm đến chuỗi này. Trong hầu hết các lớp học lập trình, bạn sẽ cần tham chiếu đến Mã dự án (thường được xác định là
PROJECT_ID). Nếu không thích mã nhận dạng được tạo, bạn có thể tạo một mã nhận dạng ngẫu nhiên khác. Hoặc bạn có thể thử tên người dùng của riêng mình để xem tên đó có được chấp nhận hay không. Bạn không thể thay đổi tên này sau bước này và tên này sẽ tồn tại trong suốt thời gian của dự án. - Để bạn nắm được thông tin, có một giá trị thứ ba là Số dự án mà một số API sử dụng. Tìm hiểu thêm về cả 3 giá trị này trong tài liệu.
- Tiếp theo, bạn cần bật tính năng thanh toán trong Cloud Console để sử dụng các tài nguyên/API trên Cloud. Việc thực hiện lớp học lập trình này sẽ không tốn nhiều chi phí, nếu có. Để tắt các tài nguyên nhằm tránh bị tính phí ngoài phạm vi hướng dẫn này, bạn có thể xoá các tài nguyên đã tạo hoặc xoá dự án. Người dùng mới của Google Cloud đủ điều kiện tham gia chương trình Dùng thử miễn phí trị giá 300 USD.
Khởi động Cloud Shell
Mặc dù có thể vận hành Google Cloud từ xa trên máy tính xách tay, nhưng trong lớp học lập trình này, bạn sẽ sử dụng Google Cloud Shell, một môi trường dòng lệnh chạy trên Cloud.
Trên Bảng điều khiển Google Cloud, hãy nhấp vào biểu tượng Cloud Shell trên thanh công cụ ở trên cùng bên phải:

Quá trình này chỉ mất vài phút để cung cấp và kết nối với môi trường. Khi quá trình này kết thúc, bạn sẽ thấy như sau:

Máy ảo này được trang bị tất cả các công cụ phát triển mà bạn cần. Nó cung cấp một thư mục chính có dung lượng 5 GB và chạy trên Google Cloud, giúp tăng cường đáng kể hiệu suất mạng và hoạt động xác thực. Bạn có thể thực hiện mọi thao tác trong lớp học lập trình này trong trình duyệt. Bạn không cần cài đặt bất cứ thứ gì.
3. Trước khi bắt đầu
Bật API
Kết quả:
Trong Cloud Shell, hãy đảm bảo bạn đã thiết lập mã dự án:
PROJECT_ID=$(gcloud config get-value project)
echo $PROJECT_ID
Nếu chưa được xác định trong cấu hình Cloud Shell, hãy thiết lập bằng các lệnh sau
export PROJECT_ID=<your project>
gcloud config set project $PROJECT_ID
Bật tất cả các dịch vụ cần thiết:
gcloud services enable compute.googleapis.com
Kết quả đầu ra dự kiến
student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-14650] student@cloudshell:~ (test-project-001-402417)$ gcloud config set project test-project-001-402417 Updated property [core/project]. student@cloudshell:~ (test-project-001-402417)$ gcloud services enable compute.googleapis.com Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.
4. Triển khai AlloyDB Omni trên GCE
Để triển khai AlloyDB Omni trên GCE, chúng ta cần chuẩn bị một máy ảo có cấu hình và phần mềm tương thích. Sau đây là ví dụ về cách triển khai AlloyDB Omni trên một VM dựa trên Debian.
Tạo một máy ảo GCE
Chúng ta cần triển khai một máy ảo có cấu hình phù hợp cho CPU, bộ nhớ và bộ nhớ. Chúng ta sẽ sử dụng hình ảnh Debian mặc định với kích thước ổ đĩa hệ thống tăng lên 20 GB để chứa các tệp cơ sở dữ liệu AlloyDB Omni.
Chúng ta có thể sử dụng cloud shell đã khởi động hoặc một thiết bị đầu cuối đã cài đặt Cloud SDK
Tất cả các bước cũng được mô tả trong phần bắt đầu nhanh cho AlloyDB Omni.
Thiết lập các biến môi trường cho quá trình triển khai.
export ZONE=us-central1-a
export MACHINE_TYPE=n2-highmem-2
export DISK_SIZE=20
export MACHINE_NAME=omni01
Sau đó, chúng ta sử dụng gcloud để tạo máy ảo GCE.
gcloud compute instances create $MACHINE_NAME \
--project=$(gcloud info --format='value(config.project)') \
--zone=$ZONE --machine-type=$MACHINE_TYPE \
--metadata=enable-os-login=true \
--create-disk=auto-delete=yes,boot=yes,size=$DISK_SIZE,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" \
--format="value(name)"),type=pd-ssd
Kết quả đầu ra dự kiến trên bảng điều khiển:
gleb@cloudshell:~ (gleb-test-short-001-415614)$ export ZONE=us-central1-a
export MACHINE_TYPE=n2-highmem-2
export DISK_SIZE=20
export MACHINE_NAME=omni01
gleb@cloudshell:~ (gleb-test-short-001-415614)$ gcloud compute instances create $MACHINE_NAME \
--project=$(gcloud info --format='value(config.project)') \
--zone=$ZONE --machine-type=$MACHINE_TYPE \
--metadata=enable-os-login=true \
--create-disk=auto-delete=yes,boot=yes,size=$DISK_SIZE,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" \
--format="value(name)"),type=pd-ssd
Created [https://www.googleapis.com/compute/v1/projects/gleb-test-short-001-415614/zones/us-central1-a/instances/omni01].
WARNING: Some requests generated warnings:
- Disk size: '20 GB' is larger than image size: '10 GB'. You might need to resize the root repartition manually if the operating system does not support automatic resizing. See https://cloud.google.com/compute/docs/disks/add-persistent-disk#resize_pd for details.
NAME: omni01
ZONE: us-central1-a
MACHINE_TYPE: n2-highmem-2
PREEMPTIBLE:
INTERNAL_IP: 10.128.0.3
EXTERNAL_IP: 35.232.157.123
STATUS: RUNNING
gleb@cloudshell:~ (gleb-test-short-001-415614)$
Cài đặt AlloyDB Omni
Kết nối với máy ảo đã tạo:
gcloud compute ssh omni01 --zone $ZONE
Kết quả đầu ra dự kiến trên bảng điều khiển:
gleb@cloudshell:~ (gleb-test-short-001-415614)$ gcloud compute ssh omni01 --zone $ZONE Warning: Permanently added 'compute.5615760774496706107' (ECDSA) to the list of known hosts. Linux omni01.us-central1-a.c.gleb-test-short-003-421517.internal 6.1.0-20-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.85-1 (2024-04-11) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. gleb@omni01:~$
Chạy lệnh sau trong thiết bị đầu cuối đã kết nối.
Cài đặt docker trên máy ảo:
sudo apt update
sudo apt-get -y install docker.io
Kết quả đầu ra dự kiến trên bảng điều khiển(đã chỉnh sửa):
gleb@omni01:~$ sudo apt update sudo apt-get -y install docker.io Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:5 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B] Get:7 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable InRelease [5146 B] Get:8 https://packages.cloud.google.com/apt cloud-sdk-bookworm InRelease [6406 B] Get:9 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable/main amd64 Packages [1916 B] Get:2 https://deb.debian.org/debian bookworm InRelease [151 kB] ... Setting up binutils (2.40-2) ... Setting up needrestart (3.6-4+deb12u1) ... Processing triggers for man-db (2.11.2-2) ... Processing triggers for libc-bin (2.36-9+deb12u4) ... gleb@omni01:~$
Xác định mật khẩu cho người dùng postgres:
export PGPASSWORD=<your password>
Tạo một thư mục cho dữ liệu AlloyDB Omni. Đây là một phương pháp không bắt buộc nhưng nên dùng. Theo mặc định, dữ liệu sẽ được tạo bằng lớp hệ thống tệp tạm thời của Docker và mọi thứ sẽ bị huỷ khi vùng chứa Docker bị xoá. Việc tách riêng cho phép bạn quản lý các vùng chứa độc lập với dữ liệu của mình và tuỳ ý đặt vùng chứa vào một bộ nhớ có đặc điểm IO tốt hơn
Sau đây là lệnh tạo một thư mục trong thư mục chính của người dùng, nơi tất cả dữ liệu sẽ được đặt:
mkdir -p $HOME/alloydb-data
Triển khai vùng chứa AlloyDB Omni:
sudo docker run --name my-omni \
-e POSTGRES_PASSWORD=$PGPASSWORD \
-p 5432:5432 \
-v $HOME/alloydb-data:/var/lib/postgresql/data \
-v /dev/shm:/dev/shm \
-d google/alloydbomni
Kết quả đầu ra dự kiến trên bảng điều khiển(đã chỉnh sửa):
gleb@omni01:~$ export PGPASSWORD=StrongPassword gleb@omni01:~$ sudo docker run --name my-omni \ -e POSTGRES_PASSWORD=$PGPASSWORD \ -p 5432:5432 \ -v $HOME/alloydb-data:/var/lib/postgresql/data \ -v /dev/shm:/dev/shm \ -d google/alloydbomni Unable to find image 'google/alloydbomni:latest' locally latest: Pulling from google/alloydbomni 71215d55680c: Pull complete ... 2e0ec3fe1804: Pull complete Digest: sha256:d6b155ea4c7363ef99bf45a9dc988ce5467df5ae8cd3c0f269ae9652dd1982a6 Status: Downloaded newer image for google/alloydbomni:latest 56de4ae0018314093c8b048f69a1e9efe67c6c8117f44c8e1dc829a2d4666cd2 gleb@omni01:~$
Cài đặt phần mềm máy khách PostgreSQL vào máy ảo (không bắt buộc – dự kiến phần mềm này đã được cài đặt):
sudo apt install -y postgresql-client
Kết quả đầu ra dự kiến trên bảng điều khiển:
gleb@omni01:~$ sudo apt install -y postgresql-client Reading package lists... Done Building dependency tree... Done Reading state information... Done postgresql-client is already the newest version (15+248). 0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
Kết nối với AlloyDB Omni:
psql -h localhost -U postgres
Kết quả đầu ra dự kiến trên bảng điều khiển:
gleb@omni01:~$ psql -h localhost -U postgres psql (15.6 (Debian 15.6-0+deb12u1), server 15.5) Type "help" for help. postgres=#
Ngắt kết nối khỏi AlloyDB Omni:
exit
Kết quả đầu ra dự kiến trên bảng điều khiển:
postgres=# exit gleb@omni01:~$
5. Chuẩn bị cơ sở dữ liệu kiểm thử
Để kiểm thử Columnar Engine, chúng ta cần tạo một cơ sở dữ liệu và điền vào đó một số dữ liệu kiểm thử.
Tạo cơ sở dữ liệu
Kết nối với máy ảo AlloyDB Omni và tạo cơ sở dữ liệu
Trong phiên Cloud Shell, hãy thực thi:
gcloud config set project $(gcloud config get-value project)
Kết nối với máy ảo AlloyDB Omni:
ZONE=us-central1-a
gcloud compute ssh omni01 --zone $ZONE
Kết quả đầu ra dự kiến trên bảng điều khiển:
student@cloudshell:~ (gleb-test-short-001-416213)$ gcloud config set project $(gcloud config get-value project) Updated property [core/project]. student@cloudshell:~ (gleb-test-short-001-416213)$ ZONE=us-central1-a gcloud compute ssh omni01 --zone $ZONE Linux omni01.us-central1-a.c.gleb-test-short-003-421517.internal 6.1.0-20-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.85-1 (2024-04-11) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Mar 4 18:17:55 2024 from 35.237.87.44 student@omni01:~$
Trong phiên SSH đã thiết lập, hãy thực thi:
export PGPASSWORD=<your password>
psql -h localhost -U postgres -c "CREATE DATABASE quickstart_db"
Kết quả đầu ra dự kiến trên bảng điều khiển:
student@omni01:~$ psql -h localhost -U postgres -c "CREATE DATABASE quickstart_db" CREATE DATABASE student@omni01:~$
Tạo bảng có dữ liệu mẫu
Đối với các thử nghiệm của mình, chúng ta sẽ sử dụng dữ liệu công khai về Nhà sản xuất bảo hiểm được cấp phép ở Iowa. Bạn có thể tìm thấy tập dữ liệu này trên trang web của chính quyền bang Iowa – https://data.iowa.gov/Regulation/Insurance-Producers-Licensed-in-Iowa/n4cc-vqyk/about_data .
Trước tiên, chúng ta cần tạo một bảng.
Trong máy ảo GCE, hãy thực thi:
psql -h localhost -U postgres -d quickstart_db -c "DROP TABLE if exists insurance_producers_licensed_in_iowa;
CREATE TABLE insurance_producers_licensed_in_iowa (
npn int8,
last_name text,
first_name text,
address_line_1 text,
address_line_2 text,
address_line_3 text,
city text,
state text,
zip int4,
firstactivedate timestamp,
expirydate timestamp,
business_phone text,
email text,
physical_location text,
iowaresident text,
loa_has_crop text,
loa_has_surety text,
loa_has_ah text,
loa_has_life text,
loa_has_variable text,
loa_has_personal_lines text,
loa_has_credit text,
loa_has_excess text,
loa_has_property text,
loa_has_casualty text,
loa_has_reciprocal text
);"
Kết quả đầu ra dự kiến trên bảng điều khiển:
otochkin@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "DROP TABLE if exists insurance_producers_licensed_in_iowa;
CREATE TABLE insurance_producers_licensed_in_iowa (
npn int8,
last_name text,
first_name text,
address_line_1 text,
address_line_2 text,
address_line_3 text,
city text,
state text,
zip int4,
firstactivedate timestamp,
expirydate timestamp,
business_phone text,
email text,
physical_location text,
iowaresident text,
loa_has_crop text,
loa_has_surety text,
loa_has_ah text,
loa_has_life text,
loa_has_variable text,
loa_has_personal_lines text,
loa_has_credit text,
loa_has_excess text,
loa_has_property text,
loa_has_casualty text,
loa_has_reciprocal text
);"
NOTICE: table "insurance_producers_licensed_in_iowa" does not exist, skipping
DROP TABLE
CREATE TABLE
otochkin@omni01:~$
Tải dữ liệu vào bảng.
Trong máy ảo GCE, hãy thực thi:
curl https://data.iowa.gov/api/views/n4cc-vqyk/rows.csv | psql -h localhost -U postgres -d quickstart_db -c "\copy insurance_producers_licensed_in_iowa from stdin csv header"
Kết quả đầu ra dự kiến trên bảng điều khiển:
otochkin@omni01:~$ curl https://data.iowa.gov/api/views/n4cc-vqyk/rows.csv | psql -h localhost -U postgres -d quickstart_db -c "\copy insurance_producers_licensed_in_iowa from stdin csv header"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 39.3M 0 39.3M 0 0 1004k 0 --:--:-- 0:00:40 --:--:-- 1028k
COPY 210898
otochkin@omni01:~$
Chúng tôi đã tải 210.898 bản ghi về nhà sản xuất bảo hiểm vào cơ sở dữ liệu của mình và có thể thực hiện một số kiểm thử.
Chạy truy vấn kiểm thử
Kết nối với quickstart_db bằng psql và bật tính năng đo thời gian để đo thời gian thực thi cho các truy vấn của chúng ta.
Trong máy ảo GCE, hãy thực thi:
psql -h localhost -U postgres -d quickstart_db
Kết quả đầu ra dự kiến trên bảng điều khiển:
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db
psql (13.14 (Debian 13.14-0+deb11u1), server 15.5
WARNING: psql major version 13, server major version 15.
Some psql features might not work.
Type "help" for help.
quickstart_db=#
Trong phiên PSQL, hãy thực thi:
\timing
Kết quả đầu ra dự kiến trên bảng điều khiển:
quickstart_db=# \timing Timing is on. quickstart_db=#
Hãy tìm 5 thành phố hàng đầu theo số lượng nhà cung cấp bảo hiểm bán bảo hiểm Tai nạn và Sức khoẻ, đồng thời có giấy phép còn hiệu lực ít nhất trong 6 tháng tới.
Trong phiên PSQL, hãy thực thi:
SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
Kết quả đầu ra dự kiến trên bảng điều khiển:
quickstart_db=# SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
city | count
-------------+-------
TAMPA | 1885
OMAHA | 1656
KANSAS CITY | 1279
AUSTIN | 1254
MIAMI | 1003
(5 rows)
Time: 94.965 ms
Tốt nhất là bạn nên chạy một truy vấn kiểm thử nhiều lần để có được thời gian thực thi đáng tin cậy. Chúng ta có thể thấy rằng thời gian trung bình để trả về kết quả là khoảng 94 mili giây. Trong các bước sau, chúng ta sẽ bật AlloyDB Columnar Engine và xem liệu nó có thể cải thiện hiệu suất hay không.
Thoát khỏi phiên psql:
exit
6. Bật Columnar Engine
Giờ đây, chúng ta cần bật Columnar Engine trên AlloyDB Omni.
Cập nhật các tham số của AlloyDB Omni
Chúng ta cần chuyển tham số phiên bản "google_columnar_engine.enabled" thành "on" cho AlloyDB Omni và cần phải khởi động lại.
Cập nhật postgresql.conf trong thư mục /var/alloydb/config rồi khởi động lại phiên bản.
Trong máy ảo GCE, hãy thực thi:
sudo docker exec my-omni /bin/bash -c "echo google_columnar_engine.enabled=true >>/var/lib/postgresql/data/postgresql.conf"
sudo docker exec my-omni /bin/bash -c "echo shared_preload_libraries=\'google_columnar_engine,google_job_scheduler,google_db_advisor,google_storage\' >>/var/lib/postgresql/data/postgresql.conf"
sudo docker stop my-omni
sudo docker start my-omni
Kết quả đầu ra dự kiến trên bảng điều khiển:
student@omni01:~$ sudo docker exec my-omni /bin/bash -c "echo google_columnar_engine.enabled=true >>/var/lib/postgresql/data/postgresql.conf" sudo docker exec my-omni /bin/bash -c "echo shared_preload_libraries=\'google_columnar_engine,google_job_scheduler,google_db_advisor,google_storage\' >>/var/lib/postgresql/data/postgresql.conf" sudo docker stop my-omni sudo docker start my-omni my-omni my-omni student@omni01:~$
Xác minh Columnar Engine
Kết nối với cơ sở dữ liệu bằng psql và xác minh công cụ theo cột.
Kết nối với cơ sở dữ liệu AlloyDB Omni
Trong phiên SSH của VM, hãy kết nối với cơ sở dữ liệu:
psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.enabled"
Lệnh này sẽ cho biết công cụ theo cột đã bật.
Kết quả đầu ra dự kiến trên bảng điều khiển:
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.enabled" google_columnar_engine.enabled -------------------------------- on (1 row)
7. So sánh hiệu suất
Giờ đây, chúng ta có thể điền sẵn cửa hàng công cụ theo cột và xác minh hiệu suất.
Automatic Columnar Store Population
Theo mặc định, công việc điền sẵn cửa hàng sẽ chạy mỗi giờ. Chúng tôi sẽ giảm thời gian này xuống còn 10 phút để bạn không phải chờ đợi.
Trong máy ảo GCE, hãy thực thi:
sudo docker exec my-omni /bin/bash -c "echo google_columnar_engine.auto_columnarization_schedule=\'EVERY 10 MINUTES\' >>/var/lib/postgresql/data/postgresql.conf"
sudo docker stop my-omni
sudo docker start my-omni
Sau đây là kết quả đầu ra dự kiến:
student@omni01:~$ sudo docker exec my-omni /bin/bash -c "echo google_columnar_engine.auto_columnarization_schedule=\'EVERY 5 MINUTES\' >>/var/lib/postgresql/data/postgresql.conf" sudo docker stop my-omni sudo docker start my-omni my-omni my-omni student@omni01:~$
Xác minh các chế độ cài đặt
Trong máy ảo GCE, hãy thực thi:
psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.auto_columnarization_schedule;"
Kết quả đầu ra dự kiến:
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.auto_columnarization_schedule;" google_columnar_engine.auto_columnarization_schedule ------------------------------------------------------ EVERY 10 MINUTES (1 row) student@omni01:~$
Kiểm tra các đối tượng trong Columnar Store. Bạn nên để trống.
Trong máy ảo GCE, hãy thực thi:
psql -h localhost -U postgres -d quickstart_db -c "SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns;"
Kết quả đầu ra dự kiến:
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns;" database_name | schema_name | relation_name | column_name ---------------+-------------+---------------+------------- (0 rows) student@omni01:~$
Kết nối với cơ sở dữ liệu và chạy cùng một truy vấn mà chúng ta đã thực thi trước đó nhiều lần.
Trong máy ảo GCE, hãy thực thi:
psql -h localhost -U postgres -d quickstart_db
Trong phiên PSQL.
Bật tính năng định thời gian
\timing
Chạy truy vấn một vài lần:
SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
Kết quả đầu ra dự kiến:
quickstart_db=# SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
city | count
-------------+-------
TAMPA | 1885
OMAHA | 1656
KANSAS CITY | 1279
AUSTIN | 1254
MIAMI | 1003
(5 rows)
Time: 94.289 ms
quickstart_db=# SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
city | count
-------------+-------
TAMPA | 1885
OMAHA | 1656
KANSAS CITY | 1279
AUSTIN | 1254
MIAMI | 1003
(5 rows)
Time: 94.608 ms
quickstart_db=#
Chờ 10 phút rồi kiểm tra xem các cột của bảng insurance_producers_licensed_in_iowa đã được điền vào kho lưu trữ theo cột hay chưa.
SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns;
Kết quả đầu ra dự kiến:
quickstart_db=# SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns; database_name | schema_name | relation_name | column_name ---------------+-------------+--------------------------------------+------------- quickstart_db | public | insurance_producers_licensed_in_iowa | city quickstart_db | public | insurance_producers_licensed_in_iowa | expirydate quickstart_db | public | insurance_producers_licensed_in_iowa | loa_has_ah (3 rows) Time: 0.643 ms
Giờ đây, chúng ta có thể chạy lại truy vấn cho bảng insurance_producers_licensed_in_iowa và xem hiệu suất có được cải thiện hay không.
SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
Kết quả đầu ra dự kiến:
quickstart_db=# SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
city | count
-------------+-------
TAMPA | 1885
OMAHA | 1656
KANSAS CITY | 1279
AUSTIN | 1254
MIAMI | 1003
(5 rows)
Time: 14.380 ms
quickstart_db=# SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
city | count
-------------+-------
TAMPA | 1885
OMAHA | 1656
KANSAS CITY | 1279
AUSTIN | 1254
MIAMI | 1003
(5 rows)
Time: 13.279 ms
Thời gian thực thi đã giảm từ 94 mili giây xuống 14 mili giây. Nếu không thấy bất kỳ điểm cải thiện nào, bạn có thể kiểm tra xem các cột đã được điền thành công vào kho lưu trữ dạng cột hay chưa bằng cách kiểm tra chế độ xem g_columnar_columns.
SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
Kết quả đầu ra dự kiến:
quickstart_db=# select relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
relation_name | column_name | column_type | status | size_in_bytes
--------------------------------------+-------------+-------------+--------+---------------
insurance_producers_licensed_in_iowa | city | text | Usable | 664231
insurance_producers_licensed_in_iowa | expirydate | timestamp | Usable | 212434
insurance_producers_licensed_in_iowa | loa_has_ah | text | Usable | 211734
(3 rows)
Bây giờ, chúng ta có thể xác minh xem kế hoạch thực thi truy vấn có sử dụng Columnar Engine hay không.
Trong phiên PSQL, hãy thực thi:
EXPLAIN (ANALYZE,SETTINGS,BUFFERS)
SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
Kết quả đầu ra dự kiến:
quickstart_db=# EXPLAIN (ANALYZE,SETTINGS,BUFFERS)
SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=2279.72..2279.73 rows=5 width=17) (actual time=12.248..12.252 rows=5 loops=1)
-> Sort (cost=2279.72..2292.91 rows=5277 width=17) (actual time=12.246..12.248 rows=5 loops=1)
Sort Key: (count(*)) DESC
Sort Method: top-N heapsort Memory: 25kB
-> HashAggregate (cost=2139.30..2192.07 rows=5277 width=17) (actual time=10.235..11.250 rows=7555 loops=1)
Group Key: city
Batches: 1 Memory Usage: 1169kB
-> Append (cost=20.00..1669.24 rows=94012 width=9) (actual time=10.231..10.233 rows=94286 loops=1)
-> Custom Scan (columnar scan) on insurance_producers_licensed_in_iowa (cost=20.00..1665.22 rows=94011 width=9) (actual time=10.229..10.231 rows=94286 loops=1)
Filter: ((loa_has_ah = 'Yes'::text) AND (expirydate > (now() + '6 mons'::interval)))
Rows Removed by Columnar Filter: 116612
Rows Aggregated by Columnar Scan: 94286
Columnar cache search mode: native
-> Seq Scan on insurance_producers_licensed_in_iowa (cost=0.00..4.02 rows=1 width=9) (never executed)
Filter: ((loa_has_ah = 'Yes'::text) AND (expirydate > (now() + '6 mons'::interval)))
Planning Time: 0.216 ms
Execution Time: 12.353 ms
Và chúng ta có thể thấy rằng thao tác "Seq Scan" trên phân đoạn bảng business_licenses chưa bao giờ được thực thi và thay vào đó, thao tác "Custom Scan (columnar scan)" đã được sử dụng. Điều đó đã giúp chúng tôi cải thiện thời gian phản hồi từ 94 mili giây xuống còn 12 mili giây.
Nếu muốn xoá nội dung được điền sẵn khỏi công cụ theo cột, chúng ta có thể làm việc này bằng hàm SQL google_columnar_engine_reset_recommendation.
Trong phiên PSQL, hãy thực thi:
SELECT google_columnar_engine_reset_recommendation(drop_columns => true);
Thao tác này sẽ xoá các cột được điền sẵn và bạn có thể xác minh trong các khung hiển thị g_columnar_columns và g_columnar_recommended_columns như đã trình bày trước đó.
SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns;
SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
Kết quả đầu ra dự kiến:
quickstart_db=# SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns; database_name | schema_name | relation_name | column_name ---------------+-------------+---------------+------------- (0 rows) Time: 0.447 ms quickstart_db=# select relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns; relation_name | column_name | column_type | status | size_in_bytes ---------------+-------------+-------------+--------+--------------- (0 rows) Time: 0.556 ms quickstart_db=#
Điền thông tin theo cách thủ công vào cửa hàng dạng cột
Chúng ta có thể thêm các cột vào Columnar Engine Store theo cách thủ công bằng cách sử dụng các hàm SQL hoặc chỉ định các thực thể bắt buộc trong cờ phiên bản để tự động tải các cột đó khi phiên bản khởi động.
Hãy thêm các cột tương tự như trước bằng cách sử dụng hàm SQL google_columnar_engine_add.
Trong phiên PSQL, hãy thực thi:
SELECT google_columnar_engine_add(relation => 'insurance_producers_licensed_in_iowa', columns => 'city,expirydate,loa_has_ah');
Chúng ta có thể xác minh kết quả bằng cùng một khung hiển thị g_columnar_columns:
SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
Kết quả đầu ra dự kiến:
quickstart_db=# SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
relation_name | column_name | column_type | status | size_in_bytes
--------------------------------------+-------------+-------------+--------+---------------
insurance_producers_licensed_in_iowa | city | text | Usable | 664231
insurance_producers_licensed_in_iowa | expirydate | timestamp | Usable | 212434
insurance_producers_licensed_in_iowa | loa_has_ah | text | Usable | 211734
(3 rows)
Time: 0.692 ms
quickstart_db=#
Bạn có thể xác minh rằng kho lưu trữ theo cột được dùng bằng cách chạy cùng một truy vấn như trước và kiểm tra kế hoạch thực thi:
EXPLAIN (ANALYZE,SETTINGS,BUFFERS)
SELECT city, count(*)
FROM insurance_producers_licensed_in_iowa
WHERE loa_has_ah ='Yes' and expirydate > now() + interval '6 MONTH'
GROUP BY city ORDER BY count(*) desc limit 5;
Thoát khỏi phiên psql:
exit
Nếu khởi động lại vùng chứa AlloyDB Omni, chúng ta có thể thấy rằng mọi thông tin theo cột đều bị mất.
Trong phiên shell, hãy thực thi:
sudo docker stop my-omni
sudo docker start my-omni
Chờ 5 đến 10 giây rồi chạy:
psql -h localhost -U postgres -d quickstart_db -c "SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns"
Kết quả đầu ra dự kiến:
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns" relation_name | column_name | column_type | status | size_in_bytes ---------------+-------------+-------------+--------+--------------- (0 rows)
Để tự động điền lại các cột trong quá trình khởi động lại, chúng ta có thể thêm các cột này dưới dạng cờ cơ sở dữ liệu vào các tham số AlloyDB Omni. Chúng tôi sẽ thêm cờ google_columnar_engine.relations=‘quickstart_db.public.insurance_producers_licensed_in_iowa(city,expirydate,loa_has_ah)' và khởi động lại vùng chứa.
Trong phiên shell, hãy thực thi:
sudo docker exec my-omni /bin/bash -c "echo google_columnar_engine.relations=\'quickstart_db.public.insurance_producers_licensed_in_iowa\(city,expirydate,loa_has_ah\)\' >>/var/lib/postgresql/data/postgresql.conf"
sudo docker stop my-omni
sudo docker start my-omni
Sau đó, chúng ta có thể thấy rằng các cột đã được tự động thêm vào Columnar Store sau khi khởi động.
Chờ 5 đến 10 giây rồi chạy:
psql -h localhost -U postgres -d quickstart_db -c "SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns"
Kết quả đầu ra dự kiến:
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns"
relation_name | column_name | column_type | status | size_in_bytes
--------------------------------------+-------------+-------------+--------+---------------
insurance_producers_licensed_in_iowa | city | text | Usable | 664231
insurance_producers_licensed_in_iowa | expirydate | timestamp | Usable | 212434
insurance_producers_licensed_in_iowa | loa_has_ah | text | Usable | 211734
(3 rows)
8. Dọn dẹp môi trường
Bây giờ, chúng ta có thể huỷ VM AlloyDB Omni
Xoá máy ảo GCE
Trong Cloud Shell, hãy thực thi:
export GCEVM=omni01
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
--zone=$ZONE \
--quiet
Kết quả đầu ra dự kiến trên bảng điều khiển:
student@cloudshell:~ (test-project-001-402417)$ export GCEVM=omni01
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
--zone=$ZONE \
--quiet
Deleted
9. Xin chúc mừng
Chúc mừng bạn đã hoàn thành lớp học lập trình này.
Nội dung đã đề cập
- Cách triển khai AlloyDB Omni trên máy ảo GCE trong Google Cloud
- Cách kết nối với AlloyDB Omni
- Cách tải dữ liệu lên AlloyDB Omni
- Cách bật Columnar Engine
- Cách kiểm tra Columnar Engine ở chế độ Tự động
- Cách điền dữ liệu vào Columnar Store theo cách thủ công
Bạn có thể đọc thêm về cách làm việc với Columnar Engine trong tài liệu.
10. Khảo sát
Kết quả: