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.

https://github.com/go-gorm/sharding

Features

  • Non-intrusive design. Load the plugin, specify the config, and all done.
  • Lighting-fast. No network based middlewares, as fast as Go.
  • Multiple database (PostgreSQL, MySQL) support.
  • Integrated primary key generator (Snowflake, PostgreSQL Sequence, Custom, …).

Usage

Config the sharding middleware, register the tables which you want to shard. See Godoc for config details.

  1. import (
  2. "fmt"
  3. "gorm.io/driver/postgres"
  4. "gorm.io/gorm"
  5. "gorm.io/sharding"
  6. )
  7. db, err := gorm.Open(postgres.New(postgres.Config{DSN: "postgres://localhost:5432/sharding-db?sslmode=disable"))
  8. db.Use(sharding.Register(sharding.Config{
  9. ShardingKey: "user_id",
  10. NumberOfShards: 64,
  11. PrimaryKeyGenerator: sharding.PKSnowflake,
  12. }, "orders", Notification{}, AuditLog{}))
  13. // 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.

  1. // Gorm create example, this will insert to orders_02
  2. db.Create(&Order{UserID: 2})
  3. // sql: INSERT INTO orders_2 ...
  4. // Show have use Raw SQL to insert, this will insert into orders_03
  5. db.Exec("INSERT INTO orders(user_id) VALUES(?)", int64(3))
  6. // This will throw ErrMissingShardingKey error, because there not have sharding key presented.
  7. db.Create(&Order{Amount: 10, ProductID: 100})
  8. fmt.Println(err)
  9. // Find, this will redirect query to orders_02
  10. var orders []Order
  11. db.Model(&Order{}).Where("user_id", int64(2)).Find(&orders)
  12. fmt.Printf("%#v\n", orders)
  13. // Raw SQL also supported
  14. db.Raw("SELECT * FROM orders WHERE user_id = ?", int64(3)).Scan(&orders)
  15. fmt.Printf("%#v\n", orders)
  16. // This will throw ErrMissingShardingKey error, because WHERE conditions not included sharding key
  17. err = db.Model(&Order{}).Where("product_id", "1").Find(&orders).Error
  18. fmt.Println(err)
  19. // Update and Delete are similar to create and query
  20. db.Exec("UPDATE orders SET product_id = ? WHERE user_id = ?", 2, int64(3))
  21. err = db.Exec("DELETE FROM orders WHERE product_id = 3").Error
  22. fmt.Println(err) // ErrMissingShardingKey

The full example is here.