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

Commit

Permalink
feat(schema): add index to frequent queried columns (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 authored May 1, 2024
1 parent 04c7687 commit bbce259
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS "members" (
"id" text,
"id" text PRIMARY KEY NOT NULL,
"guild_id" text,
"nick" text,
"avatar" text,
Expand All @@ -9,8 +9,7 @@ CREATE TABLE IF NOT EXISTS "members" (
"deaf" boolean,
"mute" boolean,
"pending" boolean,
"communication_disabled_until" text,
CONSTRAINT "members_id_guild_id" PRIMARY KEY("id","guild_id")
"communication_disabled_until" text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
Expand All @@ -34,7 +33,7 @@ CREATE TABLE IF NOT EXISTS "member_roles" (
"member_id" text NOT NULL,
"role_id" text NOT NULL,
"guild_id" text NOT NULL,
CONSTRAINT "voice_states_member_id_role_id" PRIMARY KEY("member_id","role_id")
CONSTRAINT "member_roles_member_id_role_id_pk" PRIMARY KEY("member_id","role_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "roles" (
Expand Down Expand Up @@ -116,12 +115,12 @@ CREATE TABLE IF NOT EXISTS "channels_overwrite" (
"type" integer,
"allow" text,
"deny" text,
CONSTRAINT "channels_overwrite_user_or_role_channel_id" PRIMARY KEY("user_or_role","channel_id")
CONSTRAINT "channels_overwrite_channel_id_user_or_role_pk" PRIMARY KEY("channel_id","user_or_role")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "voice_states" (
"member_id" text NOT NULL,
"guild_id" text NOT NULL,
"member_id" text NOT NULL,
"channel_id" text NOT NULL,
"session_id" text,
"deaf" boolean,
Expand All @@ -132,7 +131,7 @@ CREATE TABLE IF NOT EXISTS "voice_states" (
"self_video" boolean,
"suppress" boolean,
"request_to_speak_timestamp" text,
CONSTRAINT "voice_states_member_id_guild_id" PRIMARY KEY("member_id","guild_id")
CONSTRAINT "voice_states_guild_id_member_id_pk" PRIMARY KEY("guild_id","member_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "messages" (
Expand All @@ -153,3 +152,14 @@ CREATE TABLE IF NOT EXISTS "messages" (
"flags" integer,
"position" integer
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "members_guildId_idx" ON "members" ("guild_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "member_roles_guildId_idx" ON "member_roles" ("guild_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "roles_guildId_idx" ON "roles" ("guild_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "channels_guildId_idx" ON "channels" ("guild_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "channels_overwrite_actorId_idx" ON "channels_overwrite" ("user_or_role");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "voice_states_channelId_idx" ON "voice_states" ("channel_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "voice_states_sessionId_idx" ON "voice_states" ("session_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "messages_guildId_idx" ON "messages" ("guild_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "messages_channelId_idx" ON "messages" ("channel_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "messages_authorId_idx" ON "messages" ("author_id");
129 changes: 99 additions & 30 deletions packages/kanao-schema/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "b8909ecb-c2db-4001-96d5-033f1840e500",
"id": "ef39418c-c6bf-47b7-8468-58ccbc774352",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
Expand All @@ -11,8 +11,8 @@
"id": {
"name": "id",
"type": "text",
"primaryKey": false,
"notNull": false
"primaryKey": true,
"notNull": true
},
"guild_id": {
"name": "guild_id",
Expand Down Expand Up @@ -75,17 +75,17 @@
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"members_id_guild_id": {
"name": "members_id_guild_id",
"indexes": {
"members_guildId_idx": {
"name": "members_guildId_idx",
"columns": [
"id",
"guild_id"
]
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
Expand Down Expand Up @@ -205,11 +205,19 @@
"notNull": true
}
},
"indexes": {},
"indexes": {
"member_roles_guildId_idx": {
"name": "member_roles_guildId_idx",
"columns": [
"guild_id"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {
"voice_states_member_id_role_id": {
"name": "voice_states_member_id_role_id",
"member_roles_member_id_role_id_pk": {
"name": "member_roles_member_id_role_id_pk",
"columns": [
"member_id",
"role_id"
Expand Down Expand Up @@ -265,7 +273,15 @@
"notNull": false
}
},
"indexes": {},
"indexes": {
"roles_guildId_idx": {
"name": "roles_guildId_idx",
"columns": [
"guild_id"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
Expand Down Expand Up @@ -627,7 +643,15 @@
"notNull": false
}
},
"indexes": {},
"indexes": {
"channels_guildId_idx": {
"name": "channels_guildId_idx",
"columns": [
"guild_id"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
Expand Down Expand Up @@ -667,14 +691,22 @@
"notNull": false
}
},
"indexes": {},
"indexes": {
"channels_overwrite_actorId_idx": {
"name": "channels_overwrite_actorId_idx",
"columns": [
"user_or_role"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {
"channels_overwrite_user_or_role_channel_id": {
"name": "channels_overwrite_user_or_role_channel_id",
"channels_overwrite_channel_id_user_or_role_pk": {
"name": "channels_overwrite_channel_id_user_or_role_pk",
"columns": [
"user_or_role",
"channel_id"
"channel_id",
"user_or_role"
]
}
},
Expand All @@ -684,14 +716,14 @@
"name": "voice_states",
"schema": "",
"columns": {
"member_id": {
"name": "member_id",
"guild_id": {
"name": "guild_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"guild_id": {
"name": "guild_id",
"member_id": {
"name": "member_id",
"type": "text",
"primaryKey": false,
"notNull": true
Expand Down Expand Up @@ -757,14 +789,29 @@
"notNull": false
}
},
"indexes": {},
"indexes": {
"voice_states_channelId_idx": {
"name": "voice_states_channelId_idx",
"columns": [
"channel_id"
],
"isUnique": false
},
"voice_states_sessionId_idx": {
"name": "voice_states_sessionId_idx",
"columns": [
"session_id"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {
"voice_states_member_id_guild_id": {
"name": "voice_states_member_id_guild_id",
"voice_states_guild_id_member_id_pk": {
"name": "voice_states_guild_id_member_id_pk",
"columns": [
"member_id",
"guild_id"
"guild_id",
"member_id"
]
}
},
Expand Down Expand Up @@ -871,7 +918,29 @@
"notNull": false
}
},
"indexes": {},
"indexes": {
"messages_guildId_idx": {
"name": "messages_guildId_idx",
"columns": [
"guild_id"
],
"isUnique": false
},
"messages_channelId_idx": {
"name": "messages_channelId_idx",
"columns": [
"channel_id"
],
"isUnique": false
},
"messages_authorId_idx": {
"name": "messages_authorId_idx",
"columns": [
"author_id"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
Expand Down
4 changes: 2 additions & 2 deletions packages/kanao-schema/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "5",
"when": 1711192755584,
"tag": "0000_skinny_shiva",
"when": 1714564879918,
"tag": "0000_thankful_marvel_boy",
"breakpoints": true
}
]
Expand Down
9 changes: 6 additions & 3 deletions packages/kanao-schema/src/Schema/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pgTable, text, integer, boolean } from "drizzle-orm/pg-core";
import { pgTable, text, integer, boolean, primaryKey, index } from "drizzle-orm/pg-core";

export const channels = pgTable("channels", {
id: text("id").primaryKey(),
Expand All @@ -22,7 +22,7 @@ export const channels = pgTable("channels", {
flags: integer("flags"),

guildId: text("guild_id")
});
}, table => ({ guildIdIdx: index("channels_guildId_idx").on(table.guildId) }));

export const channelsOverwrite = pgTable("channels_overwrite", {
userOrRole: text("user_or_role"),
Expand All @@ -31,4 +31,7 @@ export const channelsOverwrite = pgTable("channels_overwrite", {
type: integer("type"),
allow: text("allow"),
deny: text("deny")
});
}, table => ({
primaryKey: primaryKey({ columns: [table.channelId, table.userOrRole] }),
actorIdx: index("channels_overwrite_actorId_idx").on(table.userOrRole)
}));
4 changes: 2 additions & 2 deletions packages/kanao-schema/src/Schema/member.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pgTable, integer, text, boolean } from "drizzle-orm/pg-core";
import { pgTable, integer, text, boolean, index } from "drizzle-orm/pg-core";

export const members = pgTable("members", {
id: text("id").primaryKey(),
Expand All @@ -13,4 +13,4 @@ export const members = pgTable("members", {
mute: boolean("mute"),
pending: boolean("pending"),
communicationDisabledUntil: text("communication_disabled_until")
});
}, table => ({ guildIdIdx: index("members_guildId_idx").on(table.guildId) }));
8 changes: 6 additions & 2 deletions packages/kanao-schema/src/Schema/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pgTable, text, boolean, integer } from "drizzle-orm/pg-core";
import { pgTable, text, boolean, integer, index } from "drizzle-orm/pg-core";

export const messages = pgTable("messages", {
id: text("id").primaryKey(),
Expand All @@ -19,4 +19,8 @@ export const messages = pgTable("messages", {
applicationId: text("application_id"),
flags: integer("flags"),
position: integer("position")
});
}, table => ({
guildIdIdx: index("messages_guildId_idx").on(table.guildId),
channelIdIdx: index("messages_channelId_idx").on(table.channelId),
authorIdIdx: index("messages_authorId_idx").on(table.authorId)
}));
10 changes: 7 additions & 3 deletions packages/kanao-schema/src/Schema/roles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { boolean, integer, pgTable, text } from "drizzle-orm/pg-core";
import { boolean, index, integer, pgTable, primaryKey, text } from "drizzle-orm/pg-core";

export const roles = pgTable("roles", {
id: text("id").primaryKey(),
Expand All @@ -7,12 +7,16 @@ export const roles = pgTable("roles", {
hoist: boolean("hoist"),
position: integer("position"),
permissions: text("permissions"),

guildId: text("guild_id")
});
}, table => ({ guildIdIdx: index("roles_guildId_idx").on(table.guildId) }));

export const memberRoles = pgTable("member_roles", {
memberId: text("member_id").notNull(),
roleId: text("role_id").notNull(),
guildId: text("guild_id").notNull()
});
}, table => ({
primaryKey: primaryKey({ columns: [table.memberId, table.roleId] }),
guildIdIdx: index("member_roles_guildId_idx").on(table.guildId)
}));

12 changes: 9 additions & 3 deletions packages/kanao-schema/src/Schema/state.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { pgTable, text, boolean } from "drizzle-orm/pg-core";
import { pgTable, text, boolean, primaryKey, index } from "drizzle-orm/pg-core";

export const voiceStates = pgTable("voice_states", {
memberId: text("member_id").notNull(),
guildId: text("guild_id").notNull(),
memberId: text("member_id").notNull(),

channelId: text("channel_id").notNull(),
sessionId: text("session_id"),

Expand All @@ -14,4 +15,9 @@ export const voiceStates = pgTable("voice_states", {
selfVideo: boolean("self_video"),
suppress: boolean("suppress"),
requestToSpeakTimestamp: text("request_to_speak_timestamp")
});
}, table => ({
// Member only have one voice state per guild, which only one channel
primaryKey: primaryKey({ columns: [table.guildId, table.memberId] }),
channelIdIdx: index("voice_states_channelId_idx").on(table.channelId),
sessionIdIdx: index("voice_states_sessionId_idx").on(table.sessionId)
}));

0 comments on commit bbce259

Please sign in to comment.