1. บทนำ
ในโค้ดแล็บนี้ คุณจะได้เรียนรู้วิธีทำให้ AlloyDB Omni ใช้งานได้และใช้ Columnar Engine เพื่อปรับปรุงประสิทธิภาพการค้นหา
ข้อกำหนดเบื้องต้น
- ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud Console
- ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Google Shell
สิ่งที่จะได้เรียนรู้
- วิธีทำให้ AlloyDB Omni ใช้งานได้บน GCE VM ใน Google Cloud
- วิธีเชื่อมต่อกับ AlloyDB Omni
- วิธีโหลดข้อมูลไปยัง AlloyDB Omni
- วิธีเปิดใช้ Columnar Engine
- วิธีตรวจสอบเครื่องยนต์คอลัมน์ในโหมดอัตโนมัติ
- วิธีป้อนข้อมูลสตอเรจคอลัมน์ด้วยตนเอง
สิ่งที่ต้องมี
- บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
- เว็บเบราว์เซอร์ เช่น Chrome
2. การตั้งค่าและข้อกําหนด
การตั้งค่าสภาพแวดล้อมด้วยตนเอง
- ลงชื่อเข้าใช้ Google Cloud Console และสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
- ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ คุณจะอัปเดตได้ทุกเมื่อ
- รหัสโปรเจ็กต์จะต้องไม่ซ้ำกันสำหรับโปรเจ็กต์ Google Cloud ทั้งหมดและจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้หลังจากตั้งค่าแล้ว) คอนโซล Cloud จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นจะเป็นอะไร ในโค้ดแล็บส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (ปกติจะระบุเป็น
PROJECT_ID
) หากไม่ชอบรหัสที่สร้างขึ้น คุณอาจสร้างรหัสอื่นแบบสุ่มได้ หรือจะลองใช้อุปกรณ์ของคุณเองเพื่อดูว่าอุปกรณ์พร้อมใช้งานหรือไม่ก็ได้ คุณจะเปลี่ยนแปลงชื่อหลังจากขั้นตอนนี้ไม่ได้ และชื่อนี้จะคงอยู่ตลอดระยะเวลาของโปรเจ็กต์ - โปรดทราบว่ามีค่าที่ 3 ซึ่งเป็นหมายเลขโปรเจ็กต์ที่ API บางรายการใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 รายการนี้ได้ในเอกสารประกอบ
- ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำตามโค้ดแล็บนี้จะไม่เสียค่าใช้จ่ายมากนัก หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินหลังจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่สร้างไว้หรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD
เริ่ม Cloud Shell
แม้ว่า Google Cloud จะทำงานจากระยะไกลจากแล็ปท็อปได้ แต่ในโค้ดแล็บนี้ คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์
จากคอนโซล Google Cloud ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน
การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมจะใช้เวลาเพียงไม่กี่นาที เมื่อดำเนินการเสร็จแล้ว คุณควรเห็นข้อมูลดังต่อไปนี้
เครื่องเสมือนนี้โหลดเครื่องมือการพัฒนาทั้งหมดที่คุณต้องการ ซึ่งจะมีไดเรกทอรีหลักขนาด 5 GB ถาวรและทำงานบน Google Cloud ซึ่งจะช่วยเพิ่มประสิทธิภาพเครือข่ายและการรับรองได้อย่างมีประสิทธิภาพ คุณทํางานทั้งหมดในโค้ดแล็บนี้ได้ภายในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไรเลย
3. ก่อนเริ่มต้น
เปิดใช้ API
เอาต์พุต:
ใน Cloud Shell ให้ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์แล้ว โดยทำดังนี้
PROJECT_ID=$(gcloud config get-value project)
echo $PROJECT_ID
หากไม่ได้กำหนดไว้ในการกำหนดค่า Cloud Shell ให้ตั้งค่าโดยใช้คำสั่งต่อไปนี้
export PROJECT_ID=<your project>
gcloud config set project $PROJECT_ID
เปิดใช้บริการที่จำเป็นทั้งหมด
gcloud services enable compute.googleapis.com
ผลลัพธ์ที่คาดหวัง
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. ติดตั้งใช้งาน AlloyDB Omni ใน GCE
หากต้องการติดตั้งใช้งาน AlloyDB Omni ใน GCE เราต้องเตรียมเครื่องเสมือนที่มีการกำหนดค่าและซอฟต์แวร์ที่เข้ากันได้ ต่อไปนี้คือตัวอย่างวิธีทำให้ AlloyDB Omni ใช้งานได้ใน VM ที่ใช้ Debian
สร้าง VM ใน GCE
เราต้องติดตั้งใช้งาน VM ที่มีการกำหนดค่าที่ยอมรับได้สำหรับ CPU, หน่วยความจำ และพื้นที่เก็บข้อมูล เราจะใช้อิมเมจ Debian เริ่มต้นโดยเพิ่มขนาดดิสก์ระบบเป็น 20 GB เพื่อรองรับไฟล์ฐานข้อมูล AlloyDB Omni
เราสามารถใช้ Cloud Shell ที่เริ่มแล้วหรือเทอร์มินัลที่ติดตั้ง Cloud SDK ไว้
ขั้นตอนทั้งหมดนี้ยังอธิบายอยู่ในการเริ่มต้นใช้งานฉบับย่อ สําหรับ AlloyDB Omni ด้วย
ตั้งค่าตัวแปรสภาพแวดล้อมสําหรับการทําให้ใช้งานได้
export ZONE=us-central1-a
export MACHINE_TYPE=n2-highmem-2
export DISK_SIZE=20
export MACHINE_NAME=omni01
จากนั้นใช้ gcloud เพื่อสร้าง GCE VM
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
ผลลัพธ์ที่คาดหวังในคอนโซล
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)$
ติดตั้ง AlloyDB Omni
เชื่อมต่อกับ VM ที่สร้างขึ้น
gcloud compute ssh omni01 --zone $ZONE
ผลลัพธ์ที่คาดหวังในคอนโซล
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:~$
เรียกใช้คำสั่งต่อไปนี้ในเทอร์มินัลที่เชื่อมต่อ
ติดตั้ง Docker ใน VM โดยทำดังนี้
sudo apt update
sudo apt-get -y install docker.io
ผลลัพธ์ที่คาดหวังจากคอนโซล(ปกปิดบางส่วน)
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:~$
กำหนดรหัสผ่านสำหรับผู้ใช้ postgres
export PGPASSWORD=<your password>
สร้างไดเรกทอรีสําหรับข้อมูล AlloyDB Omni ซึ่งไม่บังคับแต่แนะนำ โดยค่าเริ่มต้น ระบบจะสร้างข้อมูลโดยใช้เลเยอร์ไฟล์ระบบชั่วคราวของ Docker และทุกอย่างจะถูกทำลายเมื่อลบคอนเทนเนอร์ Docker การเก็บแยกต่างหากจะช่วยให้คุณจัดการคอนเทนเนอร์แยกจากข้อมูลได้ และเลือกที่จะจัดเก็บคอนเทนเนอร์ไว้ในพื้นที่เก็บข้อมูลที่มีลักษณะ IO ดีกว่าได้
ต่อไปนี้เป็นคำสั่งที่สร้างไดเรกทอรีในไดเรกทอรีหลักของผู้ใช้ซึ่งจะเป็นที่เก็บข้อมูลทั้งหมด
mkdir -p $HOME/alloydb-data
ทำให้คอนเทนเนอร์ 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
ผลลัพธ์ที่คาดหวังจากคอนโซล(ปกปิดบางส่วน)
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:~$
ติดตั้งซอฟต์แวร์ไคลเอ็นต์ PostgreSQL ใน VM (ไม่บังคับ - คาดว่าจะติดตั้งไว้แล้ว)
sudo apt install -y postgresql-client
ผลลัพธ์ที่คาดหวังในคอนโซล
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.
เชื่อมต่อกับ AlloyDB Omni
psql -h localhost -U postgres
ผลลัพธ์ที่คาดหวังในคอนโซล
gleb@omni01:~$ psql -h localhost -U postgres psql (15.6 (Debian 15.6-0+deb12u1), server 15.5) Type "help" for help. postgres=#
ยกเลิกการเชื่อมต่อจาก AlloyDB Omni
exit
ผลลัพธ์ที่คาดหวังในคอนโซล
postgres=# exit gleb@omni01:~$
5. เตรียมฐานข้อมูลทดสอบ
หากต้องการทดสอบเครื่องมือ Columnar Engine เราต้องสร้างฐานข้อมูลและป้อนข้อมูลทดสอบบางส่วน
สร้างฐานข้อมูล
เชื่อมต่อกับ AlloyDB Omni VM และสร้างฐานข้อมูล
ในเซสชัน Cloud Shell ให้ดำเนินการต่อไปนี้
gcloud config set project $(gcloud config get-value project)
เชื่อมต่อกับ VM AlloyDB Omni โดยทำดังนี้
ZONE=us-central1-a
gcloud compute ssh omni01 --zone $ZONE
ผลลัพธ์ที่คาดหวังในคอนโซล
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:~$
ในเซสชัน SSH ที่สร้างไว้ ให้ดำเนินการต่อไปนี้
export PGPASSWORD=<your password>
psql -h localhost -U postgres -c "CREATE DATABASE quickstart_db"
ผลลัพธ์ที่คาดหวังในคอนโซล
student@omni01:~$ psql -h localhost -U postgres -c "CREATE DATABASE quickstart_db" CREATE DATABASE student@omni01:~$
สร้างตารางที่มีข้อมูลตัวอย่าง
เราจะใช้ข้อมูลสาธารณะเกี่ยวกับผู้รับอนุญาตการประกันภัยในไอโอวาในการทดสอบ คุณสามารถดูชุดข้อมูลนี้ได้ในเว็บไซต์ของรัฐบาลไอโอวาที่ https://data.iowa.gov/Regulation/Insurance-Producers-Licensed-in-Iowa/n4cc-vqyk/about_data
ก่อนอื่นเราต้องสร้างตาราง
ใน GCE VM ให้ทำดังนี้
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
);"
ผลลัพธ์ที่คาดหวังในคอนโซล
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:~$
โหลดข้อมูลไปยังตาราง
ใน GCE VM ให้ทำดังนี้
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"
ผลลัพธ์ที่คาดหวังในคอนโซล
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:~$
เราได้โหลดระเบียนเกี่ยวกับผู้รับประกันภัย 210898 รายการลงในฐานข้อมูลแล้วและสามารถทำการทดสอบได้
เรียกใช้ข้อความค้นหาทดสอบ
เชื่อมต่อกับ quickstart_db โดยใช้ psql และเปิดใช้การจับเวลาเพื่อวัดเวลาในการดำเนินการของคําค้นหา
ใน GCE VM ให้ทำดังนี้
psql -h localhost -U postgres -d quickstart_db
ผลลัพธ์ที่คาดหวังในคอนโซล
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=#
ในเซสชัน PSQL ให้ดำเนินการต่อไปนี้
\timing
ผลลัพธ์ที่คาดหวังในคอนโซล
quickstart_db=# \timing Timing is on. quickstart_db=#
มาค้นหาเมือง 5 อันดับแรกตามจำนวนผู้รับอนุญาตการประกอบธุรกิจประกันที่ขายประกันอุบัติเหตุและประกันสุขภาพและมีใบอนุญาตที่ใช้งานได้อย่างน้อยอีก 6 เดือนข้างหน้า
ในเซสชัน PSQL ให้ดำเนินการต่อไปนี้
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;
ผลลัพธ์ที่คาดหวังในคอนโซล
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
เรียกใช้การค้นหาทดสอบหลายครั้งเพื่อให้ได้เวลาดำเนินการที่เชื่อถือได้ เราพบว่าเวลาเฉลี่ยในการแสดงผลลัพธ์คือประมาณ 94 มิลลิวินาที ในขั้นตอนต่อไปนี้ เราจะเปิดใช้ AlloyDB Columnar Engine และดูว่าจะช่วยปรับปรุงประสิทธิภาพได้หรือไม่
ออกจากเซสชัน psql
exit
6. เปิดใช้ Columnar Engine
ตอนนี้เราต้องเปิดใช้ Columnar Engine ใน AlloyDB Omni
อัปเดตพารามิเตอร์ AlloyDB Omni
เราต้องเปลี่ยนพารามิเตอร์อินสแตนซ์ "google_columnar_engine.enabled" เป็น "on" สําหรับ AlloyDB Omni และต้องมีการรีสตาร์ท
อัปเดต postgresql.conf ในไดเรกทอรี /var/alloydb/config แล้วรีสตาร์ทอินสแตนซ์
ใน GCE VM ให้ทำดังนี้
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
ผลลัพธ์ที่คาดหวังในคอนโซล
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:~$
ยืนยัน Columnar Engine
เชื่อมต่อกับฐานข้อมูลโดยใช้ psql และยืนยันเครื่องมือแบบคอลัมน์
เชื่อมต่อกับฐานข้อมูล AlloyDB Omni
ในเซสชัน SSH ของ VM ให้เชื่อมต่อกับฐานข้อมูลโดยทำดังนี้
psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.enabled"
คำสั่งควรแสดงเครื่องมือจัดการข้อมูลแบบคอลัมน์ที่เปิดใช้
ผลลัพธ์ที่คาดหวังในคอนโซล
student@omni01:~$ psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.enabled" google_columnar_engine.enabled -------------------------------- on (1 row)
7. การเปรียบเทียบประสิทธิภาพ
ตอนนี้เราสร้างที่เก็บข้อมูลเครื่องยนต์แบบคอลัมน์และยืนยันประสิทธิภาพได้แล้ว
การสร้างร้านค้าแบบคอลัมน์อัตโนมัติ
โดยค่าเริ่มต้น งานการสร้างข้อมูลใน Store จะทำงานทุกชั่วโมง เราจะลดเวลานี้เหลือ 10 นาทีเพื่อหลีกเลี่ยงการรอ
ใน GCE VM ให้ทำดังนี้
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
ผลลัพธ์ที่คาดหวังมีดังนี้
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:~$
ยืนยันการตั้งค่า
ใน GCE VM ให้ทำดังนี้
psql -h localhost -U postgres -d quickstart_db -c "show google_columnar_engine.auto_columnarization_schedule;"
ผลลัพธ์ที่คาดหวัง
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:~$
ตรวจสอบออบเจ็กต์ใน Columnar Store โดยควรเป็นค่าว่าง
ใน GCE VM ให้ทำดังนี้
psql -h localhost -U postgres -d quickstart_db -c "SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns;"
ผลลัพธ์ที่คาดหวัง
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:~$
เชื่อมต่อกับฐานข้อมูลและเรียกใช้การค้นหาเดียวกันกับที่เราดำเนินการก่อนหน้านี้หลายครั้ง
ใน GCE VM ให้ทำดังนี้
psql -h localhost -U postgres -d quickstart_db
ในเซสชัน PSQL
เปิดใช้การกำหนดเวลา
\timing
เรียกใช้การค้นหา 2-3 ครั้ง
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;
ผลลัพธ์ที่คาดหวัง
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=#
รอ 10 นาทีแล้วตรวจสอบว่าระบบป้อนข้อมูลคอลัมน์ของตาราง insurance_producers_licensed_in_iowa ลงในที่เก็บข้อมูลคอลัมน์หรือไม่
SELECT database_name, schema_name, relation_name, column_name FROM g_columnar_recommended_columns;
ผลลัพธ์ที่คาดหวัง
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
ตอนนี้เราเรียกใช้การค้นหาตาราง insurance_producers_licensed_in_iowa อีกครั้งเพื่อดูว่าประสิทธิภาพดีขึ้นหรือไม่
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;
ผลลัพธ์ที่คาดหวัง
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
รันไทม์ลดลงจาก 94 มิลลิวินาทีเป็น 14 มิลลิวินาที หากไม่เห็นการปรับปรุงใดๆ คุณสามารถตรวจสอบว่าระบบสร้างคอลัมน์ในสตอเรจคอลัมน์อาร์เรย์เรียบร้อยแล้วหรือไม่โดยดูที่มุมมอง g_columnar_columns
SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
ผลลัพธ์ที่คาดหวัง
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)
ตอนนี้เราสามารถตรวจสอบได้ว่าแผนการดำเนินการค้นหาใช้เครื่องมือจัดการข้อมูลแบบคอลัมน์หรือไม่
ในเซสชัน PSQL ให้ดำเนินการต่อไปนี้
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;
ผลลัพธ์ที่คาดหวัง
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
และเราเห็นว่าการดำเนินการ "Seq Scan" ในส่วนตาราง business_licenses ไม่เคยทำงาน และระบบใช้ "Custom Scan (columnar scan)" แทน ซึ่งช่วยให้เราปรับปรุงเวลาในการตอบสนองจาก 94 เป็น 12 มิลลิวินาที
หากต้องการล้างเนื้อหาที่ระบบป้อนข้อมูลอัตโนมัติออกจากเครื่องมือแบบคอลัมน์ สามารถทำได้โดยใช้ฟังก์ชัน SQL google_columnar_engine_reset_recommendation
ในเซสชัน PSQL ให้ดำเนินการต่อไปนี้
SELECT google_columnar_engine_reset_recommendation(drop_columns => true);
ระบบจะล้างคอลัมน์ที่สร้างขึ้น และคุณสามารถยืนยันได้ในมุมมอง g_columnar_columns และ g_columnar_recommended_columns ตามที่แสดงไว้ก่อนหน้านี้
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;
ผลลัพธ์ที่คาดหวัง
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=#
จำนวนประชากรของร้านค้าแบบคอลัมน์ที่กำหนดเอง
เราสามารถเพิ่มคอลัมน์ลงในที่เก็บข้อมูลเครื่องยนต์แบบคอลัมน์ด้วยตนเองโดยใช้ฟังก์ชัน SQL หรือระบุเอนทิตีที่จำเป็นในแฟล็กอินสแตนซ์เพื่อโหลดโดยอัตโนมัติเมื่ออินสแตนซ์เริ่มต้น
มาเพิ่มคอลัมน์เดียวกันกับก่อนหน้านี้โดยใช้ฟังก์ชัน SQL google_columnar_engine_add
ในเซสชัน PSQL ให้ดำเนินการต่อไปนี้
SELECT google_columnar_engine_add(relation => 'insurance_producers_licensed_in_iowa', columns => 'city,expirydate,loa_has_ah');
และเรายืนยันผลลัพธ์ได้โดยใช้มุมมอง g_columnar_columns เดียวกัน ดังนี้
SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns;
ผลลัพธ์ที่คาดหวัง
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=#
คุณสามารถยืนยันได้ว่ามีการใช้ที่เก็บข้อมูลคอลัมน์โดยเรียกใช้การค้นหาแบบเดียวกับก่อนหน้านี้และตรวจสอบแผนการดำเนินการ ดังนี้
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;
ออกจากเซสชัน psql
exit
หากเรารีสตาร์ทคอนเทนเนอร์ AlloyDB Omni เราจะเห็นว่าข้อมูลคอลัมน์ทั้งหมดหายไป
ในเซสชันเชลล์ ให้ดำเนินการต่อไปนี้
sudo docker stop my-omni
sudo docker start my-omni
รอ 5-10 วินาทีแล้วเรียกใช้
psql -h localhost -U postgres -d quickstart_db -c "SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns"
ผลลัพธ์ที่คาดหวัง
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)
หากต้องการป้อนข้อมูลคอลัมน์อีกครั้งโดยอัตโนมัติระหว่างการรีสตาร์ท เราสามารถเพิ่มคอลัมน์เหล่านั้นเป็น Flag ฐานข้อมูลลงในพารามิเตอร์ AlloyDB Omni ได้ เราจะเพิ่ม Flag google_columnar_engine.relations=‘quickstart_db.public.insurance_producers_licensed_in_iowa(city,expirydate,loa_has_ah)' และรีสตาร์ทคอนเทนเนอร์
ในเซสชันเชลล์ ให้ดำเนินการต่อไปนี้
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
และหลังจากนั้น เราจะเห็นว่าระบบได้เพิ่มคอลัมน์ลงใน Columnar Store โดยอัตโนมัติหลังจากการเริ่มต้น
รอ 5-10 วินาทีแล้วเรียกใช้คำสั่งต่อไปนี้
psql -h localhost -U postgres -d quickstart_db -c "SELECT relation_name,column_name,column_type,status,size_in_bytes from g_columnar_columns"
ผลลัพธ์ที่คาดหวัง
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. ล้างสภาพแวดล้อม
ตอนนี้เราทำลาย AlloyDB Omni VM ได้แล้ว
ลบ VM ใน GCE
ใน Cloud Shell ให้ดำเนินการต่อไปนี้
export GCEVM=omni01
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
--zone=$ZONE \
--quiet
ผลลัพธ์ที่คาดหวังในคอนโซล
student@cloudshell:~ (test-project-001-402417)$ export GCEVM=omni01 export ZONE=us-central1-a gcloud compute instances delete $GCEVM \ --zone=$ZONE \ --quiet Deleted
9. ขอแสดงความยินดี
ขอแสดงความยินดีที่ทํา Codelab จนเสร็จสมบูรณ์
สิ่งที่เราได้พูดถึง
- วิธีทำให้ AlloyDB Omni ใช้งานได้บน GCE VM ใน Google Cloud
- วิธีเชื่อมต่อกับ AlloyDB Omni
- วิธีโหลดข้อมูลไปยัง AlloyDB Omni
- วิธีเปิดใช้ Columnar Engine
- วิธีตรวจสอบเครื่องยนต์คอลัมน์ในโหมดอัตโนมัติ
- วิธีป้อนข้อมูลสตอเรจคอลัมน์ด้วยตนเอง
อ่านข้อมูลเพิ่มเติมเกี่ยวกับการทำงานกับเครื่องมือจัดการข้อมูลแบบคอลัมน์ได้ในเอกสารประกอบ
10. แบบสำรวจ
เอาต์พุต: