Skip to content

Commit

Permalink
pgsql: Reduce cost of GetNotification by 2.5
Browse files Browse the repository at this point in the history
By delaying the Layer join to the very end, we can cut the query costs from 540,836 to 219,477.

See Pull Request for details.
  • Loading branch information
Quentin-M committed Dec 4, 2016
1 parent 026f64a commit dc8f710
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions database/pgsql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,21 @@ const (
WHERE name = $1`

searchNotificationLayerIntroducingVulnerability = `
WITH subquery AS (
SELECT l.ID, l.name
FROM Vulnerability_Affects_FeatureVersion vafv, FeatureVersion fv, Layer_diff_FeatureVersion ldfv, Layer l
WHERE l.id >= $2
AND vafv.vulnerability_id = $1
AND vafv.featureversion_id = fv.id
AND ldfv.featureversion_id = fv.id
AND ldfv.modification = 'add'
AND ldfv.layer_id = l.id
ORDER BY l.ID
)
SELECT *
FROM subquery
LIMIT $3;
`

WITH LDFV AS (
SELECT ldfv.layer_id
FROM Vulnerability_Affects_FeatureVersion vafv, FeatureVersion fv, Layer_diff_FeatureVersion ldfv
WHERE ldfv.layer_id >= $2
AND vafv.vulnerability_id = $1
AND vafv.featureversion_id = fv.id
AND ldfv.featureversion_id = fv.id
AND ldfv.modification = 'add'
ORDER BY ldfv.ID
)
SELECT l.id, l.name
FROM LDFV, Layer l
WHERE LDFV.layer_id = l.id
LIMIT $3`

// complex_test.go
searchComplexTestFeatureVersionAffects = `
SELECT v.name
Expand Down

0 comments on commit dc8f710

Please sign in to comment.