Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Add an index and revise a select related to deleting all shots (#3629)
Browse files Browse the repository at this point in the history
* Remove two seq scans and a sort from a select query. (#3568)
* Add index to column that's used in a WHERE.
* Update db schema.
* Up the DB level.
* Use acct id to get device ids; del img data for all device ids.
* Use device id when there's no account id to delete shots.
  • Loading branch information
chenba authored and ianb committed Oct 18, 2017
1 parent e5a3f9f commit f183b3c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
1 change: 1 addition & 0 deletions server/db-patches/patch-20-21.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX images_shotid_idx ON images (shotid);
1 change: 1 addition & 0 deletions server/db-patches/patch-21-20.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX images_shotid_idx;
1 change: 1 addition & 0 deletions server/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ALTER TABLE ONLY states
ADD CONSTRAINT states_pkey PRIMARY KEY (state);
CREATE INDEX data_deviceid_idx ON data USING btree (deviceid);
CREATE INDEX devices_accountid_idx ON devices USING btree (accountid);
CREATE INDEX images_shotid_idx ON images USING btree (shotid);
CREATE INDEX searchable_text_idx ON data USING gin (searchable_text);
CREATE INDEX states_deviceid_idx ON states USING btree (deviceid);
ALTER TABLE ONLY data
Expand Down
2 changes: 1 addition & 1 deletion server/src/dbschema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const mozlog = require("./logging").mozlog("dbschema");

// When updating the database, please also run ./bin/dumpschema --record
// This updates schema.sql with the latest full database schema
const MAX_DB_LEVEL = exports.MAX_DB_LEVEL = 20;
const MAX_DB_LEVEL = exports.MAX_DB_LEVEL = 21;

exports.forceDbVersion = function(version) {
mozlog.info("forcing-db-version", {db: db.constr, version});
Expand Down
2 changes: 1 addition & 1 deletion server/src/pages/leave-screenshots/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ app.post("/leave", function(req, res) {
if (!req.deviceId) {
res.status(403).send(req.getText("leavePageErrorAddonRequired"));
}
Shot.deleteEverythingForDevice(req.backend, req.deviceId).then(() => {
Shot.deleteEverythingForDevice(req.backend, req.deviceId, req.accountId).then(() => {
res.redirect("/leave-screenshots/?complete");
}).catch((e) => {
mozlog.error("delete-account-error", {msg: "An error occurred trying to delete account", error: e});
Expand Down
66 changes: 37 additions & 29 deletions server/src/servershot.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,42 +620,50 @@ Shot.deleteShot = function(backend, shotId, deviceId, accountId) {
})
};

Shot.deleteEverythingForDevice = function(backend, deviceId) {
return db.select(
`SELECT images.id
FROM images JOIN data
ON images.shotid = data.id
WHERE data.deviceid = $1`,
[deviceId]
).then((rows) => {
rows.forEach((row) => del(row.id))
Shot.deleteEverythingForDevice = function(backend, deviceId, accountId) {
let deviceIds;

const getDeviceIds = () => {
if (accountId) {
return db.select(
`SELECT devices.id
FROM devices
WHERE devices.accountid = $1`,
[accountId]);
}
return Promise.resolve([{id: deviceId}]);
};

const imageIdsSelect = (deviceIdRows) => {
deviceIds = deviceIdRows.map(row => row.id);
if (!deviceIds.length) {
deviceIds = [deviceId];
}
).then(() => {
return db.select(
`SELECT DISTINCT devices.id
FROM devices, devices AS devices2
WHERE devices.id = $1
OR (devices.accountid = devices2.accountid
AND devices2.id = $1)
`,
[deviceId]);
}
).then((rows) => {
let ids = [];
for (let i = 0; i < rows.length; i++) {
ids.push(rows[i].id);
}
if (!ids.length) {
ids = [deviceId];
}
`SELECT images.id
FROM images JOIN data
ON images.shotid = data.id
WHERE data.deviceid IN (${db.markersForArgs(1, deviceIds.length)})`,
deviceIds);
};

const deleteImageData = (imageIdRows) => {
imageIdRows.forEach(row => del(row.id));
};

const deleteShotRecords = () => {
let deleteSql = `DELETE FROM data WHERE
deviceid IN (${db.markersForArgs(1, ids.length)})`;
deviceid IN (${db.markersForArgs(1, deviceIds.length)})`;
return db.update(
deleteSql,
ids
deviceIds
);
}

});
return getDeviceIds()
.then(imageIdsSelect)
.then(deleteImageData)
.then(deleteShotRecords);
};

ClipRewrites = class ClipRewrites {
Expand Down

0 comments on commit f183b3c

Please sign in to comment.