Improve MySQL performance (#1477)

* Improve MySQL performance
This commit is contained in:
AlteredCoder 2022-04-28 12:53:14 +02:00 committed by GitHub
parent 2e37d5ce97
commit f22e4eb24e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 107 additions and 11 deletions

View file

@ -22,7 +22,7 @@ type Decision struct {
// UpdatedAt holds the value of the "updated_at" field.
UpdatedAt *time.Time `json:"updated_at,omitempty"`
// Until holds the value of the "until" field.
Until time.Time `json:"until,omitempty"`
Until *time.Time `json:"until,omitempty"`
// Scenario holds the value of the "scenario" field.
Scenario string `json:"scenario,omitempty"`
// Type holds the value of the "type" field.
@ -128,7 +128,8 @@ func (d *Decision) assignValues(columns []string, values []interface{}) error {
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field until", values[i])
} else if value.Valid {
d.Until = value.Time
d.Until = new(time.Time)
*d.Until = value.Time
}
case decision.FieldScenario:
if value, ok := values[i].(*sql.NullString); !ok {
@ -244,8 +245,10 @@ func (d *Decision) String() string {
builder.WriteString(", updated_at=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", until=")
builder.WriteString(d.Until.Format(time.ANSIC))
if v := d.Until; v != nil {
builder.WriteString(", until=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", scenario=")
builder.WriteString(d.Scenario)
builder.WriteString(", type=")

View file

@ -447,6 +447,20 @@ func UntilLTE(v time.Time) predicate.Decision {
})
}
// UntilIsNil applies the IsNil predicate on the "until" field.
func UntilIsNil() predicate.Decision {
return predicate.Decision(func(s *sql.Selector) {
s.Where(sql.IsNull(s.C(FieldUntil)))
})
}
// UntilNotNil applies the NotNil predicate on the "until" field.
func UntilNotNil() predicate.Decision {
return predicate.Decision(func(s *sql.Selector) {
s.Where(sql.NotNull(s.C(FieldUntil)))
})
}
// ScenarioEQ applies the EQ predicate on the "scenario" field.
func ScenarioEQ(v string) predicate.Decision {
return predicate.Decision(func(s *sql.Selector) {

View file

@ -55,6 +55,14 @@ func (dc *DecisionCreate) SetUntil(t time.Time) *DecisionCreate {
return dc
}
// SetNillableUntil sets the "until" field if the given value is not nil.
func (dc *DecisionCreate) SetNillableUntil(t *time.Time) *DecisionCreate {
if t != nil {
dc.SetUntil(*t)
}
return dc
}
// SetScenario sets the "scenario" field.
func (dc *DecisionCreate) SetScenario(s string) *DecisionCreate {
dc.mutation.SetScenario(s)
@ -275,9 +283,6 @@ func (dc *DecisionCreate) defaults() {
// check runs all checks and user-defined validators on the builder.
func (dc *DecisionCreate) check() error {
if _, ok := dc.mutation.Until(); !ok {
return &ValidationError{Name: "until", err: errors.New(`ent: missing required field "Decision.until"`)}
}
if _, ok := dc.mutation.Scenario(); !ok {
return &ValidationError{Name: "scenario", err: errors.New(`ent: missing required field "Decision.scenario"`)}
}
@ -345,7 +350,7 @@ func (dc *DecisionCreate) createSpec() (*Decision, *sqlgraph.CreateSpec) {
Value: value,
Column: decision.FieldUntil,
})
_node.Until = value
_node.Until = &value
}
if value, ok := dc.mutation.Scenario(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{

View file

@ -59,6 +59,20 @@ func (du *DecisionUpdate) SetUntil(t time.Time) *DecisionUpdate {
return du
}
// SetNillableUntil sets the "until" field if the given value is not nil.
func (du *DecisionUpdate) SetNillableUntil(t *time.Time) *DecisionUpdate {
if t != nil {
du.SetUntil(*t)
}
return du
}
// ClearUntil clears the value of the "until" field.
func (du *DecisionUpdate) ClearUntil() *DecisionUpdate {
du.mutation.ClearUntil()
return du
}
// SetScenario sets the "scenario" field.
func (du *DecisionUpdate) SetScenario(s string) *DecisionUpdate {
du.mutation.SetScenario(s)
@ -386,6 +400,12 @@ func (du *DecisionUpdate) sqlSave(ctx context.Context) (n int, err error) {
Column: decision.FieldUntil,
})
}
if du.mutation.UntilCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Column: decision.FieldUntil,
})
}
if value, ok := du.mutation.Scenario(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
@ -612,6 +632,20 @@ func (duo *DecisionUpdateOne) SetUntil(t time.Time) *DecisionUpdateOne {
return duo
}
// SetNillableUntil sets the "until" field if the given value is not nil.
func (duo *DecisionUpdateOne) SetNillableUntil(t *time.Time) *DecisionUpdateOne {
if t != nil {
duo.SetUntil(*t)
}
return duo
}
// ClearUntil clears the value of the "until" field.
func (duo *DecisionUpdateOne) ClearUntil() *DecisionUpdateOne {
duo.mutation.ClearUntil()
return duo
}
// SetScenario sets the "scenario" field.
func (duo *DecisionUpdateOne) SetScenario(s string) *DecisionUpdateOne {
duo.mutation.SetScenario(s)
@ -963,6 +997,12 @@ func (duo *DecisionUpdateOne) sqlSave(ctx context.Context) (_node *Decision, err
Column: decision.FieldUntil,
})
}
if duo.mutation.UntilCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Column: decision.FieldUntil,
})
}
if value, ok := duo.mutation.Scenario(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,

View file

@ -81,7 +81,7 @@ var (
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "created_at", Type: field.TypeTime, Nullable: true},
{Name: "updated_at", Type: field.TypeTime, Nullable: true},
{Name: "until", Type: field.TypeTime},
{Name: "until", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"mysql": "datetime"}},
{Name: "scenario", Type: field.TypeString},
{Name: "type", Type: field.TypeString},
{Name: "start_ip", Type: field.TypeInt64, Nullable: true},
@ -114,6 +114,16 @@ var (
Unique: false,
Columns: []*schema.Column{DecisionsColumns[6], DecisionsColumns[7]},
},
{
Name: "decision_value",
Unique: false,
Columns: []*schema.Column{DecisionsColumns[12]},
},
{
Name: "decision_until",
Unique: false,
Columns: []*schema.Column{DecisionsColumns[3]},
},
},
}
// EventsColumns holds the columns for the "events" table.

View file

@ -3482,7 +3482,7 @@ func (m *DecisionMutation) Until() (r time.Time, exists bool) {
// OldUntil returns the old "until" field's value of the Decision entity.
// If the Decision object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *DecisionMutation) OldUntil(ctx context.Context) (v time.Time, err error) {
func (m *DecisionMutation) OldUntil(ctx context.Context) (v *time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldUntil is only allowed on UpdateOne operations")
}
@ -3496,9 +3496,22 @@ func (m *DecisionMutation) OldUntil(ctx context.Context) (v time.Time, err error
return oldValue.Until, nil
}
// ClearUntil clears the value of the "until" field.
func (m *DecisionMutation) ClearUntil() {
m.until = nil
m.clearedFields[decision.FieldUntil] = struct{}{}
}
// UntilCleared returns if the "until" field was cleared in this mutation.
func (m *DecisionMutation) UntilCleared() bool {
_, ok := m.clearedFields[decision.FieldUntil]
return ok
}
// ResetUntil resets all changes to the "until" field.
func (m *DecisionMutation) ResetUntil() {
m.until = nil
delete(m.clearedFields, decision.FieldUntil)
}
// SetScenario sets the "scenario" field.
@ -4447,6 +4460,9 @@ func (m *DecisionMutation) ClearedFields() []string {
if m.FieldCleared(decision.FieldUpdatedAt) {
fields = append(fields, decision.FieldUpdatedAt)
}
if m.FieldCleared(decision.FieldUntil) {
fields = append(fields, decision.FieldUntil)
}
if m.FieldCleared(decision.FieldStartIP) {
fields = append(fields, decision.FieldStartIP)
}
@ -4482,6 +4498,9 @@ func (m *DecisionMutation) ClearField(name string) error {
case decision.FieldUpdatedAt:
m.ClearUpdatedAt()
return nil
case decision.FieldUntil:
m.ClearUntil()
return nil
case decision.FieldStartIP:
m.ClearStartIP()
return nil

View file

@ -2,6 +2,7 @@ package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
@ -22,7 +23,9 @@ func (Decision) Fields() []ent.Field {
field.Time("updated_at").
Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(),
field.Time("until"),
field.Time("until").Nillable().Optional().SchemaType(map[string]string{
dialect.MySQL: "datetime",
}),
field.String("scenario"),
field.String("type"),
field.Int64("start_ip").Optional(),
@ -49,5 +52,7 @@ func (Decision) Edges() []ent.Edge {
func (Decision) Indexes() []ent.Index {
return []ent.Index{
index.Fields("start_ip", "end_ip"),
index.Fields("value"),
index.Fields("until"),
}
}