Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
fix: real (non-jsonb) based indexing on timestamp (#10)
Browse files Browse the repository at this point in the history
the timestamp_utc field was used in almost every query and it was very
slow since it needed quite some casting to get useable answers. this
makes inserting a bit costlier while making querying dirt cheap
  • Loading branch information
hairmare authored May 2, 2021
1 parent 247dda5 commit 5517766
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
38 changes: 28 additions & 10 deletions acr/configure_acr_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import (
type requestIDKeyType string

const (
resultsRecordTsUTCQuery = "to_timestamp((result -> 'data' -> 'metadata' ->> 'timestamp_utc') || ' 0000', 'YYYY-MM-DD HH24:MI:SS TZH')"
requestIDKey requestIDKeyType = ""
requestIDKey requestIDKeyType = ""
)

var dbConn *gorm.DB
Expand Down Expand Up @@ -105,7 +104,7 @@ func configureAPI(api *operations.ACRWebhooksAPI) http.Handler {
return apiop.NewGetResultsInternalServerError()
}
query = query.Where(
fmt.Sprintf("%s >= ?", resultsRecordTsUTCQuery),
"timestamp >= ?",
from,
)
}
Expand All @@ -115,7 +114,7 @@ func configureAPI(api *operations.ACRWebhooksAPI) http.Handler {
return apiop.NewGetResultsInternalServerError()
}
query = query.Where(
fmt.Sprintf("%s <= ?", resultsRecordTsUTCQuery),
"timestamp <= ?",
to,
)
}
Expand All @@ -125,7 +124,7 @@ func configureAPI(api *operations.ACRWebhooksAPI) http.Handler {
).Offset(
int(*params.Offset),
).Order(
fmt.Sprintf("%s desc", resultsRecordTsUTCQuery),
"timestamp desc",
).Scan(
&result,
)
Expand Down Expand Up @@ -162,13 +161,13 @@ func configureAPI(api *operations.ACRWebhooksAPI) http.Handler {
"result -> 'result_type' AS result_type",
"result -> 'data' -> 'status' AS status",
}).Where(
fmt.Sprintf("%s >= ?", resultsRecordTsUTCQuery),
"timestamp >= ?",
date.Format("2006-01-02T15:04:05Z"),
).Where(
fmt.Sprintf("%s < ?", resultsRecordTsUTCQuery),
"timestamp < ?",
date.Add(24*time.Hour).Format("2006-01-02T15:04:05Z"),
).Order(
fmt.Sprintf("%s asc", resultsRecordTsUTCQuery),
"timestamp asc",
).Scan(
&result,
)
Expand Down Expand Up @@ -199,11 +198,30 @@ func configureServer(s *http.Server, scheme, addr string) {
if err := getDatabase().AutoMigrate(&models.Result{}); err != nil {
log.WithError(err).Fatal(err)
}
idx := `
sql := `
-- index to allow all kinds of searched
CREATE INDEX IF NOT EXISTS idx_result
ON results USING gin (result);
-- populates the timestamp field which is also indexable for faster to/from searches
CREATE OR REPLACE FUNCTION fn_results_insert()
RETURNS trigger
AS $$
BEGIN
NEW.timestamp = TO_TIMESTAMP((NEW.result -> 'data' -> 'metadata' ->> 'timestamp_utc') || ' 0000', 'YYYY-MM-DD HH24:MI:SS TZH');
RETURN NEW;
END;
$$
LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS trg_results_insert ON results;
CREATE TRIGGER trg_results_insert
AFTER INSERT
ON results
FOR EACH ROW
EXECUTE PROCEDURE fn_results_insert();
`
if tx := getDatabase().Exec(idx); tx.Error != nil {
if tx := getDatabase().Exec(sql); tx.Error != nil {
log.WithError(tx.Error).Fatal(tx.Error)
}
}
Expand Down
10 changes: 10 additions & 0 deletions acr/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions models/result.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ definitions:
result:
$ref: '#/definitions/Webhook'
x-go-custom-tag: gorm:"type:jsonb;"
timestamp:
type: "string"
format: "date-time"
x-go-custom-tag: gorm:"type:time;index;"
Webhook:
type: "object"
properties:
Expand Down

0 comments on commit 5517766

Please sign in to comment.