-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve notification query performance by reducing db calls #2470
Conversation
Just don't ask for stuff that you don't want to show anyways
- update incorrect variables
- we removed it, prevent null pointer error
Test summaryRun details
View run in Cypress Dashboard ➡️ This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard |
const readTxPromise = session.readTransaction(async tx => { | ||
const allReportsTransactionResponse = await tx.run( | ||
const readTxPromise = session.readTransaction(async transaction => { | ||
const reviewedReportsTransactionResponse = await transaction.run( | ||
` | ||
MATCH (resource)<-[:BELONGS_TO]-(report:Report {id: $id})<-[review:REVIEWED]-(moderator:User) | ||
RETURN moderator, review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by mattwr18
Dec 10, 2019
Outdated (history rewrite) - original diff
@@ -153,14 +155,14 @@ export default {
const { id } = parent
let reviewed
const readTxPromise = session.readTransaction(async tx => {
- const allReportsTransactionResponse = await tx.run(
- `
- MATCH (resource)<-[:BELONGS_TO]-(report:Report {id: $id})<-[review:REVIEWED]-(moderator:User)
- RETURN moderator, review
- ORDER BY report.updatedAt DESC, review.updatedAt DESC
- `,
- { id },
- )
+ const cypher = `
+ MATCH (resource)<-[:BELONGS_TO]-(report:Report {id: $id})<-[review:REVIEWED]-(moderator:User)
+ RETURN moderator, review
+ ORDER BY report.updatedAt DESC, review.updatedAt DESC
+ `
+ const params = { id }
+ const allReportsTransactionResponse = await tx.run(cypher, params)
+ log(allReportsTransactionResponse)
same question as your other PR
@roschaefer
why so much refactoring just to add log
??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by roschaefer
Dec 10, 2019
I want to see the database calls being made when I set DEBUG=human-connection:neo4j:*
. So I implemented this helper function which outputs the cypher statement and/or the database call statistics, the counters. We should always call the log
method from now on when we implement database calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by mattwr18
Dec 10, 2019
that part, I get... I don't understand how why that needed so much refactoring
I have refactored it and pushed it up.
Would this somehow not work with the code you added...
It would be nice to know how to use as well
If I run DEBUG=human-connection:neo4j:* yarn dev
it throws this error
No matches for wildcard “DEBUG=human-connection:neo4j:*”. See `help expand`.
env DEBUG=human-connection:neo4j:* yarn dev
so I'm assuming the wildcard needs to be replaced by something, but what? Ahh
it takes either human-connection:neo4j:cypher
or human-connection:neo4j:stats
??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by roschaefer
Dec 10, 2019
On fish shell:
env DEBUG="human-connection:neo4j:*" yarn run dev
if you don't escape the *
your shell believes it's a file glob pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by mattwr18
Dec 10, 2019
great thanks @roschaefer
then run a query and it will give me some crazy feedback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by mattwr18
Dec 10, 2019
ahhh, ok here are the result of running a reports
query
2019-12-10T16:04:20.346Z human-connection:neo4j:cypher
MATCH (report:Report)-[:BELONGS_TO]->(resource)
WHERE (resource:User OR resource:Post OR resource:Comment)
WITH report, resource,
[(submitter:User)-[filed:FILED]->(report) | filed {.*, submitter: properties(submitter)} ] as filed,
[(moderator:User)-[reviewed:REVIEWED]->(report) | reviewed {.*, moderator: properties(moderator)} ] as reviewed,
[(resource)<-[:WROTE]-(author:User) | author {.*} ] as optionalAuthors,
[(resource)-[:COMMENTS]->(post:Post) | post {.*} ] as optionalCommentedPosts,
resource {.*, __typename: labels(resource)[0] } as resourceWithType
WITH report, optionalAuthors, optionalCommentedPosts, reviewed, filed,
resourceWithType {.*, post: optionalCommentedPosts[0], author: optionalAuthors[0] } as finalResource
RETURN report {.*, resource: finalResource, filed: filed, reviewed: reviewed }
ORDER BY report.createdAt DESC
2019-12-10T16:04:20.348Z human-connection:neo4j:cypher {}
2019-12-10T16:04:20.349Z human-connection:neo4j:stats StatementStatistics { _stats: { nodesCreated: 0, nodesDeleted: 0, relationshipsCreated: 0, relationshipsDeleted: 0, propertiesSet: 0, labelsAdded: 0, labelsRemoved: 0, indexesAdded: 0, indexesRemoved: 0, constraintsAdded: 0, constraintsRemoved: 0 } }
2019-12-10T16:04:20.349Z human-connection:neo4j:stats { resultConsumedAfter: 2, resultAvailableAfter: 56 }
if (!txResult[0]) return null | ||
filed = txResult.map(reportedRecord => { | ||
const filedReports = await readTxPromise | ||
filed = filedReports.map(reportedRecord => { | ||
const { submitter, filed } = reportedRecord | ||
const relationshipWithNestedAttributes = { | ||
...filed, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authored by roschaefer
Dec 10, 2019
Outdated (history rewrite) - original diff
@@ -83,35 +82,35 @@ export default {
params.offset && typeof params.offset === 'number' ? `SKIP ${params.offset}` : ''
const limit = params.first && typeof params.first === 'number' ? `LIMIT ${params.first}` : ''
- const reportReadTxPromise = session.readTransaction(async tx => {
- const cypher = `
- MATCH (report:Report)-[:BELONGS_TO]->(resource)
- WHERE (resource:User OR resource:Post OR resource:Comment)
- ${filterClause}
- WITH report, resource,
- [(submitter:User)-[filed:FILED]->(report) | filed {.*, submitter: properties(submitter)} ] as filed,
- [(moderator:User)-[reviewed:REVIEWED]->(report) | reviewed {.*, moderator: properties(moderator)} ] as reviewed,
- [(resource)<-[:WROTE]-(author:User) | author {.*} ] as optionalAuthors,
- [(resource)-[:COMMENTS]->(post:Post) | post {.*} ] as optionalCommentedPosts,
- resource {.*, __typename: labels(resource)[0] } as resourceWithType
- WITH report, optionalAuthors, optionalCommentedPosts, reviewed, filed,
- resourceWithType {.*, post: optionalCommentedPosts[0], author: optionalAuthors[0] } as finalResource
- RETURN report {.*, resource: finalResource, filed: filed, reviewed: reviewed }
- ${orderByClause}
- ${offset} ${limit}
- `
- const allReportsTransactionResponse = await tx.run(cypher)
+ const reportReadTxPromise = session.readTransaction(async transaction => {
+ const allReportsTransactionResponse = await transaction.run(
+ `
+ MATCH (report:Report)-[:BELONGS_TO]->(resource)
+ WHERE (resource:User OR resource:Post OR resource:Comment)
+ ${filterClause}
+ WITH report, resource,
+ [(submitter:User)-[filed:FILED]->(report) | filed {.*, submitter: properties(submitter)} ] as filed,
+ [(moderator:User)-[reviewed:REVIEWED]->(report) | reviewed {.*, moderator: properties(moderator)} ] as reviewed,
+ [(resource)<-[:WROTE]-(author:User) | author {.*} ] as optionalAuthors,
+ [(resource)-[:COMMENTS]->(post:Post) | post {.*} ] as optionalCommentedPosts,
+ resource {.*, __typename: labels(resource)[0] } as resourceWithType
+ WITH report, optionalAuthors, optionalCommentedPosts, reviewed, filed,
+ resourceWithType {.*, post: optionalCommentedPosts[0], author: optionalAuthors[0] } as finalResource
+ RETURN report {.*, resource: finalResource, filed: filed, reviewed: reviewed }
+ ${orderByClause}
+ ${offset} ${limit}
+ `,
+ )
log(allReportsTransactionResponse)
return allReportsTransactionResponse.records.map(record => record.get('report'))
})
try {
- const txResult = await reportReadTxPromise
- if (!txResult[0]) return null
- reports = txResult
+ const reports = await reportReadTxPromise
+ if (!reports) return []
How could reports
ever be not an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great job @roschaefer !! this is really cool
now we need to make sure that we are adding logging
and know how to read the output!
🍰 Pullrequest
We had plenty of database calls for our notification query. That could very well be the source of our recent performance issues in the backend.