To complete a recent query from the database results in a lot of features
Start direct all select out
Dahon select this operation is to say no too long
Then check the batch inserted into the batch program
It comes to how to write a statement
Because the frame with the gorm carrying the id of a primary key
So the natural step is to use the id and to select the corresponding results
But explain the execution of the statement found great rows
And behind the limit should be the same figures under ideal conditions
Because of limited time only step is indeed the case
Then I began to suspect that the execution speed of this statement
After a while the investigation
I decided to compare two different statements
One is to use offset restrictions
SELECT * FROM `task_file_result` WHERE (step = 5) LIMIT 100 OFFSET 100000;
One is used where restrictions
SELECT * FROM `task_file_result` WHERE (id > 100000 and step = 5) LIMIT 100;
Types of statements can achieve the same effect
I first built a table of 1000w
RDS.CreateTable(TaskFileResult{}) insertList := make([]interface{}, 0) for i := 1; i <= 1000000; i++ { res := "" for j := 1; j <= 10; j++ { res += "x" insertList = append(insertList, TaskFileResult{ TaskID: 1, TaskFileID: uint(i), Step: uint(j), }) } } if err = run.Insert(RDS, insertList, 5000); err != nil { println(err) }
Then supplement statement
offset := 100000 limit := 100 chooseStep := 5 RDS.Table("task_file_result").Where("step = ?", chooseStep).Limit(limit).Offset(offset).Find(&resultList) RDS.Table("task_file_result").Where("id > ? and step = ?", offset, chooseStep).Limit(limit).Find(&resultList) offset = 500000 RDS.Table("task_file_result").Where("step = ?", chooseStep).Limit(limit).Offset(offset).Find(&resultList) RDS.Table("task_file_result").Where("id > ? and step = ?", offset, chooseStep).Limit(limit).Find(&resultList) offset = 1000000 RDS.Table("task_file_result").Where("step = ?", chooseStep).Limit(limit).Offset(offset).Find(&resultList) RDS.Table("task_file_result").Where("id > ? and step = ?", offset, chooseStep).Limit(limit).Find(&resultList)
Finally, where the results expressed better
It seems a lot of row sweep speed may soon be possible because this row with the two fields was increasing