Skip to content

Commit

Permalink
N+1 havereadを一度に取得
Browse files Browse the repository at this point in the history
  • Loading branch information
kkenya committed Jul 15, 2019
1 parent 0b52627 commit 3bdcd22
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions webapp/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ const pool = mysql.createPool({
})
pool.query = promisify(pool.query, pool)

// Promise stack tarace
process.on('unhandledRejection', console.dir);

app.get('/initialize', getInitialize)
function getInitialize(req, res) {
return pool.query('DELETE FROM user WHERE id > 1000')
.then(() => pool.query('DELETE FROM channel WHERE id > 10'))
.then(() => pool.query('DELETE FROM message WHERE id > 10000'))
.then(() => pool.query('DELETE FROM haveread'))
// .then(() => pool.query('DELETE FROM haveread_summary'))
.then(() => res.status(204).send(''))
}

Expand Down Expand Up @@ -256,6 +260,12 @@ function getMessage(req, res) {
VALUES (?, ?, ?, NOW(), NOW())
ON DUPLICATE KEY UPDATE message_id = ?, updated_at = NOW()`,
[userId, channel_id, maxMessageId, maxMessageId])
// .then(() => {
// return pool.query(`INSERT INTO haveread_summary (user_id, channel_id, count)
// values(?, ?, ?)
// ON DUPLICATE KEY UPDATE count = ?`,
// [userId, channel_id, maxMessageId, maxMessageId])
// })
.then(() => res.json(response))
})
})
Expand All @@ -278,27 +288,42 @@ function fetchUnread(req, res) {
}

return sleep(1.0)
.then(() => pool.query('SELECT id FROM channel'))
.then(rows => {
.then(() => {
return Promise.all([
pool.query('SELECT id FROM channel'),
pool.query('SELECT message_id, channel_id FROM haveread WHERE user_id = ?', [userId])
.then((havereads) => {
const channelIdMessageIdMapping = new Map()

havereads.forEach(haveread => {
channelIdMessageIdMapping.set(haveread.channel_id, haveread.message_id)
})

return channelIdMessageIdMapping
}),
])
})
.then(([rows, channelIdMessageIdMapping]) => {
const channelIds = rows.map(row => row.id)
const results = []
let p = Promise.resolve()

channelIds.forEach(channelId => {
p = p.then(() => pool.query('SELECT message_id FROM haveread WHERE user_id = ? AND channel_id = ?', [userId, channelId]))
.then(([row]) => {
if (row) {
return pool.query('SELECT COUNT(*) as cnt FROM message WHERE channel_id = ? AND ? < id', [channelId, row.message_id])
} else {
return pool.query('SELECT COUNT(*) as cnt FROM message WHERE channel_id = ?', [channelId])
}
})
.then(([row3]) => {
const r = {}
r.channel_id = channelId
r.unread = row3.cnt
results.push(r)
})
p = p.then(() => {
const maxMessageId = channelIdMessageIdMapping.get(channelId)

if (maxMessageId) {
return pool.query('SELECT COUNT(*) as cnt FROM message WHERE channel_id = ? AND ? < id', [channelId, maxMessageId])
} else {
return pool.query('SELECT COUNT(*) as cnt FROM message WHERE channel_id = ?', [channelId])
}
})
.then(([row3]) => {
const r = {}
r.channel_id = channelId
r.unread = row3.cnt
results.push(r)
})
})

return p.then(() => results)
Expand Down

0 comments on commit 3bdcd22

Please sign in to comment.