Skip to content

Commit

Permalink
Explicitly made timestamp non null. Switched behavior on email search…
Browse files Browse the repository at this point in the history
… to be the 'slow' case insensitively search, since it turned out LIKE operators with suffix wildcards don't use the btree index in postgres 9.5
  • Loading branch information
dhakim87 committed Jun 29, 2020
1 parent a72133d commit c57c36f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion microsetta_private_api/db/patches/0065.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CREATE TABLE ag.event_log(
id uuid PRIMARY KEY NOT NULL,
event_type varchar(100) NOT NULL,
event_subtype varchar(100) NOT NULL,
event_time timestamptz default current_timestamp,
event_time timestamptz default current_timestamp NOT NULL,
event_state jsonb);

-- Full event log sorted by time
Expand Down
13 changes: 6 additions & 7 deletions microsetta_private_api/repo/event_log_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,20 @@ def get_events_by_subtype(self,
# See https://www.postgresql.org/docs/9.5/functions-json.html#FUNCTIONS-JSON-OP-TABLE # noqa
# to understand referencing email field from jsonb representation

# TODO: I believe the LIKE operator can make use of the btree index i've
# set on this table so long as the pattern specified is a case sensitive
# prefix. To support ILIKE or searching middle of email field, may have
# to pull it out of the jsonb field, or dive into gin_trgm_ops
# Based on results of EXPLAIN of queries in psql 9.5, looks like postgres
# can use our index for exact matches, but can't use it for anything with a
# wildcard
# TODO: Should test against newest postgresql, May need to look up gin
# indexes to improve this if performance becomes an issue.
# See https://stackoverflow.com/questions/33025890/indexing-jsonb-data-for-pattern-matching-searches # noqa
def get_events_by_email(self, email: str):
with self._transaction.dict_cursor() as cur:
cur.execute("SELECT " + _read_cols + " FROM "
"event_log "
"WHERE "
"event_state->>'email' LIKE %s "
"event_state->>'email' ILIKE %s "
"ORDER BY "
"event_state->>'email', event_time DESC",
# Do not change this pattern without analyzing
# the query in postgres to ensure it uses indexes
(email+"%",))
return [_row_to_event(row) for row in cur.fetchall()]

Expand Down

0 comments on commit c57c36f

Please sign in to comment.