Skip to content

Commit 8824f5d

Browse files
committed
db.d: optimizations related to rooms
1 parent 8305567 commit 8824f5d

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/db.d

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ final class Database
116116
query("PRAGMA foreign_keys = ON;");
117117
query("PRAGMA secure_delete = ON;");
118118
query("PRAGMA busy_timeout = 5000;"); // 5 seconds
119+
query("PRAGMA temp_store = MEMORY;");
119120

120121
if (log_db) check_integrity();
121122

@@ -942,11 +943,12 @@ final class Database
942943
"DELETE FROM ", rooms_table,
943944
" WHERE room = ? AND (",
944945
" type != ? OR NOT EXISTS (",
945-
" SELECT 1 FROM ", tickers_table, " WHERE room = ?",
946+
" SELECT 1 FROM ", tickers_table,
947+
" WHERE room = ", rooms_table, ".room",
946948
" )",
947949
" );"
948950
);
949-
query(sql, [room_name, text(cast(int) RoomType._public), room_name]);
951+
query(sql, [room_name, text(cast(int) RoomType._public)]);
950952

951953
if (changes() == 0)
952954
return false;
@@ -1121,23 +1123,34 @@ final class Database
11211123
return true;
11221124
}
11231125

1124-
void del_user_tickers(RoomType type)(string username)
1126+
bool del_user_tickers(RoomType type)(string username)
11251127
{
11261128
auto sql = text(
1127-
"DELETE FROM ", tickers_table,
1128-
" WHERE username = ? AND room IN (",
1129-
" SELECT r.room FROM ", rooms_table, " r",
1130-
" WHERE r.room = ", tickers_table, ".room"
1129+
"WITH tickers_to_delete AS (",
1130+
" SELECT t.rowid FROM ", tickers_table, " t",
1131+
" JOIN ", rooms_table, " r ON t.room = r.room",
1132+
" WHERE t.username = ?"
11311133
);
11321134
auto parameters = [username];
11331135

11341136
if (type != RoomType.any) {
11351137
sql ~= " AND r.type = ?";
11361138
parameters ~= [text(cast(int) type)];
11371139
}
1138-
sql ~= ");";
1140+
sql ~= text(
1141+
") DELETE FROM ", tickers_table,
1142+
" WHERE rowid IN (SELECT rowid FROM tickers_to_delete);"
1143+
);
11391144

11401145
query(sql, parameters);
1146+
1147+
if (changes() == 0)
1148+
return false;
1149+
1150+
if (log_db) writeln(
1151+
"[DB] Removed user ", blue, username, norm, "'s tickers"
1152+
);
1153+
return true;
11411154
}
11421155

11431156
string del_oldest_ticker(string room_name)

0 commit comments

Comments
 (0)