文章

顯示從 6月, 2025 起發佈的文章

to_tsquery , ts_rank

  SELECT to_tsquery( 'batatas | nutshell' ); 'batatas' | 'nutshell' SELECT ts_rank(to_tsvector( 'The quick brown fox jumps over the lazy dog' ), to_tsquery( 'fox | dog ' ));

to_tsvector -> plainto_tsquery -> xx & yy & zz

圖片
  Query  select plainto_tsquery(( 'I am going to create a great thing' )); Result  'i' & 'am' & 'going' & 'to' & 'create' & 'a' & 'great' & 'thing'

(drafting) install pg_jieba (中文分詞)

  CREATE EXTENSION pg_jieba;

to_tsvector (Basic)

Query    SELECT to_tsvector( COALESCE ( NULL , 'hey' ) || ' ' || COALESCE ( 'hello' , '2' ) || ' ' || COALESCE ( NULL , '我是香港人' ) ); Result  'hello':2 'hey':1 '我是香港人':3

change password

 postgres=# ALTER USER postgres WITH PASSWORD '12345678'; ALTER USER postgres WITH PASSWORD '12345678'; postgres is username, 12345678 is password

COALESCE (argument_1, argument_2, …);

 有效地處理 NULL 值在資料庫管理中至關重要,PostgreSQL提供了一個強大的函數COALESCE來解決這個問題。  COALESCE函數傳回其參數中第一個非空的參數,這使其在SELECT 語句中特別有用。 Query: SELECT COALESCE(1, 2); Output coalesce ---------- 1 (1 row) Query: SELECT COALESCE(NULL, 2, 1); Output coalesce ---------- 2 (1 row) Query:   SELECT COALESCE ( NULL , 'hey' ) || ' ' || COALESCE ( 'hello' , '2' ); Output: hey hello

PostgreSQL 教學大全 (目錄)

  PostgreSQL 教學大全 第一部分:PostgreSQL 基礎入門與核心概念 PostgreSQL 簡介與安裝 :了解 PostgreSQL 的歷史、特性、優勢以及如何在不同操作系統上進行安裝。 資料庫與表格的建立 :學習如何創建資料庫,以及如何定義和創建表格,包括常見的資料型別。 基本的 SQL 查詢 (SELECT) :掌握 SELECT 語句的基礎用法,包括選擇欄位、過濾數據 ( WHERE ) 和排序結果 ( ORDER BY )。 數據插入、更新與刪除 (INSERT, UPDATE, DELETE) :學習如何向表格中添加新數據、修改現有數據以及移除不再需要的數據。 約束 (Constraints) 的應用 :理解並應用主鍵 ( PRIMARY KEY )、外鍵 ( FOREIGN KEY )、唯一約束 ( UNIQUE ) 和非空約束 ( NOT NULL ) 以確保數據完整性。 索引 (Indexes) 的重要性與類型 :深入了解索引的作用、不同類型的索引(B-tree、Hash、GIN、GiST 等)以及如何選擇合適的索引。 視圖 (Views) 與物化視圖 (Materialized Views) :學習如何創建和使用視圖來簡化複雜查詢,以及物化視圖在性能優化中的應用。 事務 (Transactions) 與 ACID 特性 :理解事務的概念,以及 ACID 特性如何保證數據的一致性和可靠性。 用戶與權限管理 :學習如何創建和管理用戶角色,以及分配不同級別的數據庫操作權限。 數據備份與還原策略 :掌握 PostgreSQL 數據的備份方法( pg_dump )和數據恢復流程。 第二部分:進階功能與性能優化 資料類型進階:JSONB、陣列、幾何類型 :探索 PostgreSQL 豐富的進階資料類型,特別是 JSONB 在半結構化數據儲存中的應用。 常用內建函數與操作符 :學習日期/時間函數、字串函數、聚合函數等,提升 SQL 查詢的靈活性。 子查詢與 CTE (Common Table Expressions) :掌握如何使用子查詢和 CTE 來解決更複雜的查詢問題,提高 SQL 的可讀性和效率。 窗口函數 (Window Functions) :深入學習窗口函數,它如何執行分組計算而不會減少行數,...

A powerful full-text search in PostgreSQL in less than 20 lines

圖片
 https://faun.pub/a-powerful-full-text-search-in-postgresql-in-less-than-20-lines-bbac8b236b6d Need a full-text search? PostgreSQL is good enough. Give it a try. POSTGRESQL FULL TEXT SEARCH Seeding data In order to explain further the fundamentals of textual search, relevance and results ranking, we have to seed our database with real data and compare different search strategies. Let’s create a table called  courses  containing only a  title  and  description  columns. Those columns will be our "searchable" columns in which we will perform a text search against: CREATE TABLE courses (id SERIAL PRIMARY KEY, title VARCHAR(80) NOT NULL, description VARCHAR(200) NOT NULL); Next, we will populate the table with some dummy data: INSERT INTO courses (title, description) VALUES ('Improve your sales skills', 'A complete course that will help you to improve your sales skills'), ('Intro to Computer Science', 'Understant how computers work'), ('L...