Skip to content

Commit

Permalink
*: Fix slow query and start_ts not working in some cases (#793)
Browse files Browse the repository at this point in the history
Signed-off-by: Breezewish <me@breeswish.org>
  • Loading branch information
breezewish authored Nov 12, 2020
1 parent ab97091 commit 0f22ecb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
53 changes: 28 additions & 25 deletions pkg/apiserver/slowquery/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ import (

const (
SlowQueryTable = "INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY"
SelectStmt = "*, (unix_timestamp(Time) + 0E0) as timestamp"
SelectStmt = "*, (UNIX_TIMESTAMP(Time) + 0E0) AS timestamp"
)

type SlowQuery struct {
Digest string `gorm:"column:Digest" json:"digest"`
Query string `gorm:"column:Query" json:"query"`

Instance string `gorm:"column:INSTANCE" json:"instance"`
DB string `gorm:"column:DB" json:"db"`
ConnectionID uint `gorm:"column:Conn_ID" json:"connection_id"`
Instance string `gorm:"column:INSTANCE" json:"instance"`
DB string `gorm:"column:DB" json:"db"`
// TODO: Switch back to uint64 when modern browser as well as Swagger handles BigInt well.
ConnectionID string `gorm:"column:Conn_ID" json:"connection_id"`
Success int `gorm:"column:Succ" json:"success"`

Timestamp float64 `gorm:"column:timestamp" proj:"(unix_timestamp(Time) + 0E0)" json:"timestamp"` // finish time
Timestamp float64 `gorm:"column:timestamp" proj:"(UNIX_TIMESTAMP(Time) + 0E0)" json:"timestamp"` // finish time
QueryTime float64 `gorm:"column:Query_time" json:"query_time"` // latency
ParseTime float64 `gorm:"column:Parse_time" json:"parse_time"`
CompileTime float64 `gorm:"column:Compile_time" json:"compile_time"`
ProcessTime float64 `gorm:"column:Process_time" json:"process_time"`

MemoryMax int `gorm:"column:Mem_max" json:"memory_max"`
DiskMax int `gorm:"column:Disk_max" json:"disk_max"`
TxnStartTS uint `gorm:"column:Txn_start_ts" json:"txn_start_ts"`
MemoryMax int `gorm:"column:Mem_max" json:"memory_max"`
DiskMax int `gorm:"column:Disk_max" json:"disk_max"`
// TODO: Switch back to uint64 when modern browser as well as Swagger handles BigInt well.
TxnStartTS string `gorm:"column:Txn_start_ts" json:"txn_start_ts"`

// Detail
PrevStmt string `gorm:"column:Prev_stmt" json:"prev_stmt"`
Expand Down Expand Up @@ -91,13 +93,13 @@ type SlowQuery struct {
}

type GetListRequest struct {
LogStartTS int64 `json:"logStartTS" form:"logStartTS"`
LogEndTS int64 `json:"logEndTS" form:"logEndTS"`
DB []string `json:"db" form:"db"`
Limit int `json:"limit" form:"limit"`
Text string `json:"text" form:"text"`
OrderBy string `json:"orderBy" form:"orderBy"`
DESC bool `json:"desc" form:"desc"`
RangeBeginTs uint `json:"rangeBeginTs" form:"rangeBeginTs"`
RangeEndTs uint `json:"rangeEndTs" form:"rangeEndTs"`
DB []string `json:"db" form:"db"`
Limit uint `json:"limit" form:"limit"`
Text string `json:"text" form:"text"`
OrderBy string `json:"orderBy" form:"orderBy"`
IsDesc bool `json:"desc" form:"desc"`

// for showing slow queries in the statement detail page
Plans []string `json:"plans" form:"plans"`
Expand Down Expand Up @@ -136,7 +138,8 @@ func getProjectionsByFields(jsonFields ...string) ([]string, error) {
type GetDetailRequest struct {
Digest string `json:"digest" form:"digest"`
Timestamp float64 `json:"timestamp" form:"timestamp"`
ConnectID int64 `json:"connect_id" form:"connect_id"`
// TODO: Switch back to uint64 when modern browser as well as Swagger handles BigInt well.
ConnectID string `json:"connect_id" form:"connect_id"`
}

func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) {
Expand All @@ -153,18 +156,18 @@ func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) {
tx := db.
Table(SlowQueryTable).
Select(strings.Join(projections, ", ")).
Where("Time between from_unixtime(?) and from_unixtime(?)", req.LogStartTS, req.LogEndTS).
Where("Time BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?)", req.RangeBeginTs, req.RangeEndTs).
Limit(req.Limit)

if req.Text != "" {
lowerStr := strings.ToLower(req.Text)
arr := strings.Fields(lowerStr)
for _, v := range arr {
tx = tx.Where(
`txn_start_ts REGEXP ?
OR LOWER(digest) REGEXP ?
OR LOWER(CONVERT(prev_stmt USING utf8)) REGEXP ?
OR LOWER(CONVERT(query USING utf8)) REGEXP ?`,
`Txn_start_ts REGEXP ?
OR LOWER(Digest) REGEXP ?
OR LOWER(CONVERT(Prev_stmt USING utf8)) REGEXP ?
OR LOWER(CONVERT(Query USING utf8)) REGEXP ?`,
v, v, v, v,
)
}
Expand All @@ -183,10 +186,10 @@ func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) {
if strings.Contains(order[0], " AS ") {
order[0] = req.OrderBy
}
if req.DESC {
tx = tx.Order(fmt.Sprintf("%s desc", order[0]))
if req.IsDesc {
tx = tx.Order(fmt.Sprintf("%s DESC", order[0]))
} else {
tx = tx.Order(fmt.Sprintf("%s asc", order[0]))
tx = tx.Order(fmt.Sprintf("%s ASC", order[0]))
}

if len(req.Plans) > 0 {
Expand All @@ -211,7 +214,7 @@ func QuerySlowLogDetail(db *gorm.DB, req *GetDetailRequest) (*SlowQuery, error)
Table(SlowQueryTable).
Select(SelectStmt).
Where("Digest = ?", req.Digest).
Where("Time = from_unixtime(?)", req.Timestamp).
Where("Time = FROM_UNIXTIME(?)", req.Timestamp).
Where("Conn_id = ?", req.ConnectID).
First(&result).Error
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/apiserver/slowquery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func Register(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
endpoint := r.Group("/slow_query")
endpoint.Use(auth.MWAuthRequired())
endpoint.Use(utils.MWConnectTiDB(s.params.TiDBClient))
endpoint.GET("/list", s.listHandler)
endpoint.GET("/detail", s.detailhandler)
endpoint.GET("/list", s.getList)
endpoint.GET("/detail", s.getDetails)
}

// @Summary List all slow queries
Expand All @@ -51,7 +51,7 @@ func Register(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
// @Router /slow_query/list [get]
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) listHandler(c *gin.Context) {
func (s *Service) getList(c *gin.Context) {
var req GetListRequest
if err := c.ShouldBindQuery(&req); err != nil {
utils.MakeInvalidRequestErrorFromError(c, err)
Expand All @@ -73,7 +73,7 @@ func (s *Service) listHandler(c *gin.Context) {
// @Router /slow_query/detail [get]
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) detailhandler(c *gin.Context) {
func (s *Service) getDetails(c *gin.Context) {
var req GetDetailRequest
if err := c.ShouldBindQuery(&req); err != nil {
utils.MakeInvalidRequestErrorFromError(c, err)
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/apps/SlowQuery/pages/Detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import TabCopr from './DetailTabCopr'
import TabTxn from './DetailTabTxn'

export interface IPageQuery {
connectId?: number
connectId?: string
digest?: string
timestamp?: number
}
Expand Down
4 changes: 2 additions & 2 deletions ui/lib/apps/SlowQuery/utils/useSlowQueryTableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ export default function useSlowQueryTableController(
queryOptions.digest,
selectedFields,
queryOptions.limit,
queryTimeRange.endTime,
queryTimeRange.beginTime,
orderOptions.orderBy,
queryOptions.plans,
queryTimeRange.beginTime,
queryTimeRange.endTime,
queryOptions.searchText,
{
errorStrategy: ErrorStrategy.Custom,
Expand Down

0 comments on commit 0f22ecb

Please sign in to comment.