Recently, I am looking for open source library and table middleware that can be used in Go projects, and I found 3: ShardingSphere-Proxy, Kingshard, Gaea. The following is the test process and comparison results.
ShardingSphere-Proxy
/apache/shardingsphere
With the support of the apache foundation, the community is active
Kingshard
/flike/kingshard
Personal project, github is no longer updated
Gaea
/XiaoMi/Gaea
Xiaomi team released, the latest update is September 2024, which is quite new
Test code
package main
import (
"database/sql"
"encoding/json"
"fmt"
"math/rand"
"sharding/internal/models"
"strings"
_ "/go-sql-driver/mysql" // Import MySQL driver
"log"
)
const (
// ANSI escape sequence that defines colors
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
)
func main() {
(Red + "shardingsphere-proxy test" + Reset)
dsn_proxy := "sharding:sharding@tcp(127.0.0.1:13308)/sharding_user?charset=utf8mb4&parseTime=True&loc=Local"
sharding_query(dsn_proxy)
(Yellow + "kingshard test" + Reset)
dsn_kingshard := "kingshard:kingshard@tcp(127.0.0.1:9696)/sharding_user?charset=utf8mb4&parseTime=True&loc=Local"
sharding_query(dsn_kingshard)
(Blue + "gaea test" + Reset)
dsn_gaea := "sharding_gaea:sharding_gaea@tcp(127.0.0.1:13306)/sharding_user?charset=utf8mb4&parseTime=True&loc=Local"
sharding_query(dsn_gaea)
}
func sharding_query(dsn string) {
//sharding proxy
shardingProxyConn, err := ("mysql", dsn)
if err != nil {
(err)
Return
}
defer ()
id := (10)
userName := ("test%d", id)
notFound := false
// Complete SQL, no transmission parameters
var uid uint64
valScope := (userName, "'", "\\'")
err = (("select id from t_user where login_name = '%s'", valScope)).Scan(&uid)
if err != nil {
if err == {
("query 1 fail 1: ", ())
notFound = true
} else {
("query 1 fail 2:", err)
}
} else {
("query 1 success, useelect id from t_user where login_name = ?", userName).Scan(&uid)
if err != nil {
if err == {
("query 2 fail 1: ", ())
notFound = true
} else {
("query 2 fail 2:", err)
}
} else {
("query 2 success, uINsert into t_user (id, login_name,passwd,email) values (?, ?, ?, ?)"
result, err1 := (sqlIns, id, userName, "test1234", "test@")
if err1 != nil {
("insert fail:", err1)
} else {
rowsAffected, _ := ()
("insert RowsAffected", rowsAffected)
}
}
//Select
rows, err := ("select id,login_name,email,create_time,update_time from t_user where login_name like ? limit 10", "%test%")
if err != nil {
(err)
Return
}
defer ()
var userList []
for () {
var u
e := (&, &, &, &, &)
if e != nil {
("row scan err:", e)
Continue continue
}
userList = append(userList, u)
}
j, _ := (userList)
("select result:", string(j))
}
Test conclusion
shardingsphere-proxy 4.1
- Placeholder argument transmission does not support subtable query. Interlude: PHP has tested and supports argument transmission.
- Sub-tables only support full SQL
kingshard
- Placeholder argument transmission does not support subtable query. Interlude: PHP has tested and supports argument transmission.
- Subtable query supports full SQL
Gaea
- Placeholder parameter transfer is supported in table query
- Subtable query supports full SQL
Conclusion comparison
The engineering application of Go code wants to use library and table middleware, and the recommended order
First, Gaea is preferred because it supports placeholder argument transmission. The latest update of github is September 2024
The second is shardingsphere-proxy 4.1. Because it does not support placeholder parameters, if you want to use the go project, you have to convert it into complete SQL. However, the configuration is relatively simple and easy to deploy, with the support of the Apache Foundation, and the community is active
The third is kingshard. Because it does not support placeholder argument transmission, it must be converted to complete SQL, and github will no longer be updated.
Test result log
2025/04/19 17:04:24 shardingsphere-proxy test
2025/04/19 17:04:25 query 1 success, uid= 1119859588875681792
[mysql] 2025/04/19 17:04:25 :64 [warn] unexpected seq nr: expected 4, got 5
2025/04/19 17:04:25 query 2 fail 2: commands out of sync. You can't run this command now
[mysql] 2025/04/19 17:04:25 :64 [warn] unexpected seq nr: expected 8, got 17
2025/04/19 17:04:25 select result: null
2025/04/19 17:04:25 kingshard test
2025/04/19 17:04:25 query 1 success, uid= 4
2025/04/19 17:04:25 query 2 fail 2: Error 1105 (HY000): prepare error ERROR 1146 (42S02): Table 'sharding_user.t_user' doesn't exist
2025/04/19 17:04:25 Error 1105 (HY000): prepare error ERROR 1146 (42S02): Table 'sharding_user.t_user' doesn't exist
2025/04/19 17:04:25 gaea test
2025/04/19 17:04:25 query 1 fail 1: sql: no rows in result set
2025/04/19 17:04:25 query 2 fail 1: sql: no rows in result set
2025/04/19 17:04:25 insert RowsAffected 1
2025/04/19 17:04:25 select result: [{"created_time":"2025-04-19T00:51:23+08:00","update_time":"2025-04-19T00:51:23+08:00","id":1,"Email":"test@","LoginName":"test1","Passwd":"","LoginStatus":0,"LastLoginTime":null,"LoginCount":0},{"created_time":"2025-04-19T16:47:56+08:00","update_time":"2025-04-19T16:47:56+08:00","id":4,"Email":"test@","LoginName":"test4","Passwd":"","LoginStatus":0,"LastLoginTime":null,"LoginCount":0},{"created_time":"2025-04-19T16:48:05+08:00","update_time":"2025-04-19T16:48:05+08:00","id":7,"Email":"test@","LoginName":"test7","Passwd":"","LoginStatus":0,"LastLoginTime":null,"LoginCount":0},{"created_time":"2025-04-19T17:04:25+08:00","update_time":"2025-04-19T17:04:25+08:00","id":8,"Email":"test@","LoginName":"test8","Passwd":"","LoginStatus":0,"LastLoginTime":null,"LoginCount":0},{"created_time":"2025-04-19T16:45:56+08:00","update_time":"2025-04-19T16:45:56+08:00","id":9,"Email":"test@","LoginName":"test9","Passwd":"","LoginStatus":0,"LastLoginTime":null,"LoginCount":0}]