Skip to content

Commit 4ef9440

Browse files
crazycs520zz-jason
authored andcommitted
infoschema/slow_query: add host column in slow_query table. (#10693)
1 parent ea489c8 commit 4ef9440

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
@@ -38,6 +38,7 @@ var slowQueryCols = []columnInfo{
3838
{variable.SlowLogTimeStr, mysql.TypeTimestamp, 26, 0, nil, nil},
3939
{variable.SlowLogTxnStartTSStr, mysql.TypeLonglong, 20, mysql.UnsignedFlag, nil, nil},
4040
{variable.SlowLogUserStr, mysql.TypeVarchar, 64, 0, nil, nil},
41+
{variable.SlowLogHostStr, mysql.TypeVarchar, 64, 0, nil, nil},
4142
{variable.SlowLogConnIDStr, mysql.TypeLonglong, 20, mysql.UnsignedFlag, nil, nil},
4243
{variable.SlowLogQueryTimeStr, mysql.TypeDouble, 22, 0, nil, nil},
4344
{execdetails.ProcessTimeStr, mysql.TypeDouble, 22, 0, nil, nil},
@@ -163,6 +164,7 @@ type slowQueryTuple struct {
163164
time time.Time
164165
txnStartTs uint64
165166
user string
167+
host string
166168
connID uint64
167169
queryTime float64
168170
processTime float64
@@ -207,7 +209,13 @@ func (st *slowQueryTuple) setFieldValue(tz *time.Location, field, value string)
207209
}
208210
st.txnStartTs = num
209211
case variable.SlowLogUserStr:
210-
st.user = value
212+
fields := strings.SplitN(value, "@", 2)
213+
if len(field) > 0 {
214+
st.user = fields[0]
215+
}
216+
if len(field) > 1 {
217+
st.host = fields[1]
218+
}
211219
case variable.SlowLogConnIDStr:
212220
num, err := strconv.ParseUint(value, 10, 64)
213221
if err != nil {
@@ -327,6 +335,7 @@ func (st *slowQueryTuple) convertToDatumRow() []types.Datum {
327335
}))
328336
record = append(record, types.NewUintDatum(st.txnStartTs))
329337
record = append(record, types.NewStringDatum(st.user))
338+
record = append(record, types.NewStringDatum(st.host))
330339
record = append(record, types.NewUintDatum(st.connID))
331340
record = append(record, types.NewFloat64Datum(st.queryTime))
332341
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)