Skip to content

Commit

Permalink
Optimize queries: add indexes and help older query planners.
Browse files Browse the repository at this point in the history
  • Loading branch information
liffiton committed Jul 16, 2024
1 parent fdd9052 commit f3c9e78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/codehelp/migrations/20240716--codehelp--add-indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- SPDX-FileCopyrightText: 2024 Mark Liffiton <[email protected]>
--
-- SPDX-License-Identifier: AGPL-3.0-only

BEGIN;

DROP INDEX IF EXISTS queries_by_user;
CREATE INDEX queries_by_user ON queries(user_id);
DROP INDEX IF EXISTS queries_by_role;
CREATE INDEX queries_by_role ON queries(role_id);

DROP INDEX IF EXISTS tutor_chats_by_user;
CREATE INDEX tutor_chats_by_user ON tutor_chats(user_id);
DROP INDEX IF EXISTS tutor_chats_by_role;
CREATE INDEX tutor_chats_by_role ON tutor_chats(role_id);

COMMIT;
8 changes: 8 additions & 0 deletions src/codehelp/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ CREATE TABLE queries (
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(role_id) REFERENCES roles(id)
);
DROP INDEX IF EXISTS queries_by_user;
CREATE INDEX queries_by_user ON queries(user_id);
DROP INDEX IF EXISTS queries_by_role;
CREATE INDEX queries_by_role ON queries(role_id);

DROP TABLE IF EXISTS tutor_chats;
CREATE TABLE tutor_chats (
Expand All @@ -34,3 +38,7 @@ CREATE TABLE tutor_chats (
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(role_id) REFERENCES roles(id)
);
DROP INDEX IF EXISTS tutor_chats_by_user;
CREATE INDEX tutor_chats_by_user ON tutor_chats(user_id);
DROP INDEX IF EXISTS tutor_chats_by_role;
CREATE INDEX tutor_chats_by_role ON tutor_chats(role_id);
2 changes: 1 addition & 1 deletion src/gened/instructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
def get_queries(class_id: int, user: int | None = None) -> list[Row]:
db = get_db()

where_clause = "WHERE roles.class_id=?"
where_clause = "WHERE UNLIKELY(roles.class_id=?)" # UNLIKELY() to help query planner in older sqlite versions
params = [class_id]

if user is not None:
Expand Down

0 comments on commit f3c9e78

Please sign in to comment.