-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (122 loc) · 3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"text/tabwriter"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/simonz05/util/log"
)
var (
help = flag.Bool("h", false, "show help text")
sqlDSN = flag.String("sqlDSN", "root:@tcp(localhost:3306)/?utf8&parseTime=True", "MySQL Data Source Name")
cpuprofile = flag.String("debug.cpuprofile", "", "write cpu profile to file")
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s <database> [OPTIONS]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
Description:
finds diff between real COUNT(*) and AUTO_INCREMENT
`)
}
func main() {
flag.Usage = usage
flag.Parse()
log.Println("Begin counting…")
if *help {
flag.Usage()
os.Exit(0)
}
runtime.GOMAXPROCS(runtime.NumCPU())
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
db, err := sql.Open("mysql", *sqlDSN)
if err != nil {
log.Errorln(err)
os.Exit(1)
}
sqlxDb := sqlx.NewDb(db, "mysql")
defer db.Close()
database := flag.Arg(0)
if database == "" {
flag.Usage()
os.Exit(1)
}
tables, err := autoTables(sqlxDb, database)
if err != nil {
log.Fatal(err)
}
err = printTables(tables)
if err != nil {
log.Fatal(err)
}
}
func autoTables(exec sqlx.Queryer, database string) ([]*table, error) {
q := "SELECT TABLE_NAME, AUTO_INCREMENT"
q += " FROM information_schema.TABLES"
q += " WHERE TABLE_SCHEMA = ?"
q += " AND AUTO_INCREMENT > 1 ORDER BY AUTO_INCREMENT DESC"
rows, err := exec.Query(q, database)
if err != nil {
log.Errorf("find tables err: %v", err)
return nil, err
}
defer rows.Close()
tables := make([]*table, 0)
for rows.Next() {
table := new(table)
err := rows.Scan(&table.name, &table.auto_increment)
table.auto_increment--
if err != nil {
return nil, err
}
tables = append(tables, table)
}
err = countTables(exec, database, tables)
return tables, err
}
func countTables(exec sqlx.Queryer, database string, tables []*table) error {
for _, table := range tables {
q := fmt.Sprintf("SELECT COUNT(*) FROM %s.%s", database, table.name)
err := exec.QueryRowx(q).Scan(&table.count)
if err != nil {
log.Errorf("err: %s %v", q, err)
return err
}
}
return nil
}
type table struct {
name string
auto_increment int
count int
}
func (t *table) String() string {
return fmt.Sprintf("%s, AUTO_INCREMENT: %d, COUNT(*): %d", t.name, t.auto_increment, t.count)
}
func (t *table) diff() float64 {
a, b := float64(t.auto_increment), float64(t.count)
return (a - b) / ((a + b) / 2) * 100
}
func printTables(tables []*table) error {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 10, 0, 1, ' ', 0)
fmt.Fprintln(w, "table\tAUTO\tcount\tΔ")
for _, t := range tables {
fmt.Fprintf(w, "%s\t%d\t%d\t%.2f%%\n", t.name, t.auto_increment, t.count, t.diff())
}
fmt.Fprintln(w)
return w.Flush()
}