go-gorm/sharding là dự án Go với 334 sao trong nhóm Data. High performance table sharding plugin for Gorm.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Gorm Sharding plugin using SQL parser and replace for splits large tables into smaller ones, redirects Query into sharding tables. Give you a high performance database access.
Config the sharding middleware, register the tables which you want to shard.
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/sharding"
)
db, err := gorm.Open(postgres.New(postgres.Config{DSN: "postgres://localhost:5432/sharding-db?sslmode=disable"))
db.Use(sharding.Register(sharding.Config{
ShardingKey: "user_id",
NumberOfShards: 64,
PrimaryKeyGenerator: sharding.PKSnowflake,
}, "orders", Notification{}, AuditLog{}))
// This case for show up give notifications, audit_logs table use same sharding rule.
Use the db session as usual. Just note that the query should have the Sharding Key when operate sharding tables.
// Gorm create example, this will insert to orders_02
db.Create(&Order{UserID: 2})
// sql: INSERT INTO orders_2 ...
// Show have use Raw SQL to insert, this will insert into orders_03
db.Exec("INSERT INTO orders(user_id) VALUES(?)", int64(3))
// This will throw ErrMissingShardingKey error, because there not have sharding key presented.
db.Create(&Order{Amount: 10, ProductID: 100})
fmt.Println(err)
// Find, this will redirect query to orders_02
var orders []Order
db.Model(&Order{}).Where("user_id", int64(2)).Find(&orders)
fmt.Printf("%#v\n", orders)
// Raw SQL also supported
db.Raw("SELECT * FROM orders WHERE user_id = ?", int64(3)).Scan(&orders)
fmt.Printf("%#v\n", orders)
// This will throw ErrMissingShardingKey error, because WHERE conditions not included sharding key
err = db.Model(&Order{}).Where("product_id", "1").Find(&orders).Error
fmt.Println(err)
// Update and Delete are similar to create and query
db.Exec("UPDATE orders SET product_id = ? WHERE user_id = ?", 2, int64(3))
err = db.Exec("DELETE FROM orders WHERE product_id = 3").Error
fmt.Println(err) // ErrMissingShardingKey
The full example is here.
🚨 NOTE: Gorm config PrepareStmt: true is not supported for now.
🚨 NOTE: Default snowflake generator in multiple nodes may result conflicted primary key, use your custom primary key generator, or regenerate a primary key when conflict occurs.
Primary Key
When you sharding tables, you need consider how the primary key generate.
There has built-in PostgreSQL sequence primary key implementation in Gorm Sharding, you just configure PrimaryKeyGenerator: sharding.PKPGSequence to use.
You don't need create sequence manually, Gorm Sharding check and create when the PostgreSQL sequence does not exists.
This sequence name followed gorm_sharding_${table_name}_id_seq, for example orders table, the sequence name is gorm_sharding_orders_id_seq.
There has built-in MySQL sequence primary key implementation in Gorm Sharding, you just configure PrimaryKeyGenerator: sharding.PKMySQLSequence to use.
You don't need create sequence manually, Gorm Sharding check and create when the MySQL sequence does not exists.
This sequence name followed gorm_sharding_${table_name}_id_seq, for example orders table, the sequence name is gorm_sharding_orders_id_seq.
If your table doesn't have a primary key, or has a primary key that isn't called id, anyway, you don't want to auto-fill the id field, then you can set PrimaryKeyGenerator to PKCustom and have PrimaryKeyGeneratorFn return 0.
graph TD
first("SELECT * FROM orders WHERE user_id = ? AND status = ?
args = [100, 1]")
first--->gorm(["Gorm Query"])
subgraph "Gorm"
gorm--->gorm_query
gorm--->gorm_exec
gorm--->gorm_queryrow
gorm_query["connPool.QueryContext(sql, args)"]
gorm_exec[/"connPool.ExecContext"/]
gorm_queryrow[/"connPool.QueryRowContext"/]
end
subgraph "database/sql"
gorm_query-->conn(["Conn"])
gorm_exec-->conn(["Conn"])
gorm_queryrow-->conn(["Conn"])
ExecContext[/"ExecContext"/]
QueryContext[/"QueryContext"/]
QueryRowContext[/"QueryRowContext"/]
conn-->ExecContext
conn-->QueryRowContext
conn-->QueryContext
end
subgraph sharding ["Sharding"]
QueryContext-->router-->| Format to get full SQL string |format_sql-->| Parser to AST |parse-->check_table
router[["router(sql, args)<br>"]]
format_sql>"sql = SELECT * FROM orders WHERE user_id = 100 AND status = 1"]
check_table{"Check sharding rules<br>by table name"}
check_table-->| Exist |process_ast
check_table_1{{"Return Raw SQL"}}
not_match_error[/"Return Error<br>SQL query must has sharding key"\]
parse[["ast = sqlparser.Parse(sql)"]]
check_table-.->| Not exist |check_table_1
process_ast(("Sharding rules"))
get_new_table_name[["Use value in WhereValue (100) for get sharding table index<br>orders + (100 % 16)<br>Sharding Table = orders_4"]]
new_sql{{"SELECT * FROM orders_4 WHERE user_id = 100 AND status = 1"}}
process_ast-.->| Not match ShardingKey |not_match_error
process_ast-->| Match ShardingKey |match_sharding_key-->| Get table name |get_new_table_name-->| Replace TableName to get new SQL |new_sql
end
subgraph database [Database]
orders_other[("orders_0, orders_1 ... orders_3")]
orders_4[(orders_4)]
orders_last[("orders_5 ... orders_15")]
other_tables[(Other non-sharding tables<br>users, stocks, topics ...)]
new_sql-->| Sharding Query | orders_4
check_table_1-.->| None sharding Query |other_tables
end
orders_4-->result
other_tables-.->result
result[/Query results\]
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/go-gorm/sharding là nguồn chính thức.
go-gorm/sharding có những chủ đề gì?
GitHub topics của go-gorm/sharding: "gorm", "mysql", "postgresql", "sharding". TopGit xếp repo vào nhóm Data.
go-gorm/sharding có phải mã nguồn mở không?
Có — go-gorm/sharding phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/go-gorm/sharding.
go-gorm/sharding có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho go-gorm/sharding. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
go-gorm/sharding còn đang phát triển không?
Commit gần nhất trên go-gorm/sharding là 8 tháng trước (theo timestamp GitHub). Repo có 74 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
go-gorm/sharding dùng license gì?
go-gorm/sharding phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về sharding?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về sharding.