Skip to content

Commit 6b2f690

Browse files
crazycs520zz-jason
authored andcommitted
infoschema/slow_query: add host column in slow_query table. (#10693) (#10735)
1 parent 7e6f146 commit 6b2f690

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

infoschema/slow_log.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var slowQueryCols = []columnInfo{
3737
{variable.SlowLogTimeStr, mysql.TypeTimestamp, 26, 0, nil, nil},
3838
{variable.SlowLogTxnStartTSStr, mysql.TypeLonglong, 20, mysql.UnsignedFlag, nil, nil},
3939
{variable.SlowLogUserStr, mysql.TypeVarchar, 64, 0, nil, nil},
40+
{variable.SlowLogHostStr, mysql.TypeVarchar, 64, 0, nil, nil},
4041
{variable.SlowLogConnIDStr, mysql.TypeLonglong, 20, mysql.UnsignedFlag, nil, nil},
4142
{variable.SlowLogQueryTimeStr, mysql.TypeDouble, 22, 0, nil, nil},
4243
{execdetails.ProcessTimeStr, mysql.TypeDouble, 22, 0, nil, nil},
@@ -162,6 +163,7 @@ type slowQueryTuple struct {
162163
time time.Time
163164
txnStartTs uint64
164165
user string
166+
host string
165167
connID uint64
166168
queryTime float64
167169
processTime float64
@@ -205,7 +207,13 @@ func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string)
205207
}
206208
st.txnStartTs = num
207209
case variable.SlowLogUserStr:
208-
st.user = value
210+
fields := strings.SplitN(value, "@", 2)
211+
if len(field) > 0 {
212+
st.user = fields[0]
213+
}
214+
if len(field) > 1 {
215+
st.host = fields[1]
216+
}
209217
case variable.SlowLogConnIDStr:
210218
num, err := strconv.ParseUint(value, 10, 64)
211219
if err != nil {
@@ -325,6 +333,7 @@ func (st *slowQueryTuple) convertToDatumRow() []types.Datum {
325333
}))
326334
record = append(record, types.NewUintDatum(st.txnStartTs))
327335
record = append(record, types.NewStringDatum(st.user))
336+
record = append(record, types.NewStringDatum(st.host))
328337
record = append(record, types.NewUintDatum(st.connID))
329338
record = append(record, types.NewFloat64Datum(st.queryTime))
330339
record = append(record, types.NewFloat64Datum(st.processTime))

infoschema/slow_log_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ select * from t;`)
5353
}
5454
recordString += str
5555
}
56-
expectRecordString := "2019-04-28 15:24:04.309074,405888132465033227,,0,0.216905,0.021,0,0,1,637,0,,,1,42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772,t1:1,t2:2,0.1,0.2,0.03,127.0.0.1:20160,0.05,0.6,0.8,0.0.0.0:20160,70724,select * from t;"
56+
expectRecordString := "2019-04-28 15:24:04.309074,405888132465033227,,,0,0.216905,0.021,0,0,1,637,0,,,1,42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772,t1:1,t2:2,0.1,0.2,0.03,127.0.0.1:20160,0.05,0.6,0.8,0.0.0.0:20160,70724,select * from t;"
5757
c.Assert(expectRecordString, Equals, recordString)
5858

5959
// fix sql contain '# ' bug

infoschema/tables_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,10 @@ select * from t_slim;`))
352352
tk.MustExec("set time_zone = '+08:00';")
353353
re := tk.MustQuery("select * from information_schema.slow_query")
354354
re.Check(testutil.RowsWithSep("|",
355-
"2019-02-12 19:33:56.571953|406315658548871171|root@127.0.0.1|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|t1:1,t2:2|0.1|0.2|0.03|127.0.0.1:20160|0.05|0.6|0.8|0.0.0.0:20160|70724|select * from t_slim;"))
355+
"2019-02-12 19:33:56.571953|406315658548871171|root|127.0.0.1|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|t1:1,t2:2|0.1|0.2|0.03|127.0.0.1:20160|0.05|0.6|0.8|0.0.0.0:20160|70724|select * from t_slim;"))
356356
tk.MustExec("set time_zone = '+00:00';")
357357
re = tk.MustQuery("select * from information_schema.slow_query")
358-
re.Check(testutil.RowsWithSep("|", "2019-02-12 11:33:56.571953|406315658548871171|root@127.0.0.1|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|t1:1,t2:2|0.1|0.2|0.03|127.0.0.1:20160|0.05|0.6|0.8|0.0.0.0:20160|70724|select * from t_slim;"))
358+
re.Check(testutil.RowsWithSep("|", "2019-02-12 11:33:56.571953|406315658548871171|root|127.0.0.1|6|4.895492|0.161|0.101|0.092|1|100001|100000|test||0|42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772|t1:1,t2:2|0.1|0.2|0.03|127.0.0.1:20160|0.05|0.6|0.8|0.0.0.0:20160|70724|select * from t_slim;"))
359359
}
360360

361361
func (s *testTableSuite) TestForAnalyzeStatus(c *C) {

sessionctx/variable/session.go

+2
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,8 @@ const (
949949
SlowLogTxnStartTSStr = "Txn_start_ts"
950950
// SlowLogUserStr is slow log field name.
951951
SlowLogUserStr = "User"
952+
// SlowLogHostStr only for slow_query table usage.
953+
SlowLogHostStr = "Host"
952954
// SlowLogConnIDStr is slow log field name.
953955
SlowLogConnIDStr = "Conn_ID"
954956
// SlowLogQueryTimeStr is slow log field name.

0 commit comments

Comments
 (0)