Last active
November 26, 2025 13:57
-
-
Save kavirajk/885fdad35f71ae2bd7b67f83a09811c4 to your computer and use it in GitHub Desktop.
select_perf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "crypto/tls" | |
| "database/sql" | |
| "fmt" | |
| "github.com/ClickHouse/clickhouse-go/v2" | |
| ) | |
| func main() { | |
| conn := clickhouse.OpenDB(&clickhouse.Options{ | |
| Addr: []string{"xxx.us-east1.gcp.clickhouse.cloud:9440"}, | |
| TLS: &tls.Config{}, | |
| Auth: clickhouse.Auth{ | |
| Username: "default", | |
| Password: "xxx", | |
| }, | |
| }) | |
| // conn := clickhouse.OpenDB(&clickhouse.Options{ | |
| // Addr: []string{"localhost:9000"}, | |
| // }) | |
| //if err := conn.Ping(); err != nil { | |
| // panic(err) | |
| //} | |
| defer conn.Close() | |
| if _, err := conn.Exec(`CREATE OR REPLACE TABLE test2 (name String, value Int64) ENGINE=MergeTree() ORDER BY name`); err != nil { | |
| panic(err) | |
| } | |
| if _, err := conn.Exec(`INSERT INTO test2 VALUES('kavi', 10)`); err != nil { | |
| panic(err) | |
| } | |
| val, err := do(conn) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println("val", val) | |
| } | |
| func do(conn *sql.DB) (int, error) { | |
| ctx := context.Background() | |
| countRunsBaseSQL := `SELECT value from test2` | |
| rows, err := conn.QueryContext(ctx, countRunsBaseSQL) | |
| if err != nil { | |
| return 0, err | |
| } | |
| // here we've spent 250ms | |
| defer func() { | |
| rows.Close() | |
| // here we spend 250 more | |
| }() | |
| var totalCount uint64 | |
| if rows.Next() { | |
| if err := rows.Scan(&totalCount); err != nil { | |
| return 0, err | |
| } | |
| } | |
| return int(totalCount), nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment