I'm new to golang and also to database optimizations.
I have simple app written on go and mysql database where send queries initialised through the web.
For receiving requests it take around 5s or little bit more? Is it possible somehow to optimize it?
Also if refreshing several times, then response could be already 50s and even more, exceptions with "invalid memory address or nil pointer dereference" or "Error 1040: Too many connections could appear".
How to avoid this and have all requests to be managiable in a efficient time frame?
This is table structure
CREATE TABLE sportsmen (
sp_no INT NOT NULL,
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender ENUM('M','F') NOT NULL,
point_date DATE NOT NULL,
PRIMARY KEY (sp_no)
);
CREATE TABLE points (
sp_no INT NOT NULL,
point INT NOT NULL,
date DATE NOT NULL
);
Number of records is around 300000 for sportsmen and 1 000 000 for their points.
this is function which is called on every request
func sportsmanPoints(w http.ResponseWriter, r *http.Request) {
start := time.Now()
db, err := sql.Open("mysql", "<conn_string>?charset=utf8")
checkErr(err)
sportsmen, err := db.Query("SELECT sp_no, first_name FROM sportsmen LIMIT ?,20", rand.Intn(100000))
checkErr(err)
for sportsmen.Next() {
var spNo string
var firstName string
err = sportsmen.Scan(&spNo, &firstName)
checkErr(err)
spPoints, err := db.Query("SELECT max(point) FROM points WHERE sp_no =" + spNo)
for spPoints.Next() {
var spPoint int
err = spPoints.Scan(&spPoint)
checkErr(err)
points.Data = append(points.Data, Point{Name: firstName, Point: spPoint})
}
}
data, err := json.Marshal(points.Data)
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(w, string(data))
elapsed := time.Since(start)
fmt.Println("Date:", time.Now(), "Response time:", elapsed)
points.Data = nil
data = nil
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
http.HandleFunc("/", sportsmanPoints)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Thank you.
Aucun commentaire:
Enregistrer un commentaire