diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f97102ab..c01d484c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Parameter `--db-user` to set a database user [#1327](https://github.com/greenbone/gvmd/pull/1327) - Add `allow_simult_ips_same_host` field for targets [#1346](https://github.com/greenbone/gvmd/pull/1346) -- Speed up GET_VULNS [#1354](https://github.com/greenbone/gvmd/pull/1354) +- Speed up GET_VULNS [#1354](https://github.com/greenbone/gvmd/pull/1354) [#1355](https://github.com/greenbone/gvmd/pull/1354) ### Changed - Move EXE credential generation to a Python script [#1260](https://github.com/greenbone/gvmd/pull/1260) [#1262](https://github.com/greenbone/gvmd/pull/1262) diff --git a/src/manage_pg.c b/src/manage_pg.c index 92efa50c7..19d7418e4 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1646,14 +1646,6 @@ manage_create_result_indexes () sql ("SELECT create_index ('results_by_date', 'results', 'date');"); } -/** - * @brief Results WHERE SQL for creating views in create_tabes. - */ -#define VULNS_RESULTS_WHERE \ - " WHERE uuid IN" \ - " (SELECT nvt FROM results" \ - " WHERE (results.severity != " G_STRINGIFY (SEVERITY_ERROR) "))" - /** * @brief Create or replace the vulns view. */ @@ -1667,30 +1659,36 @@ create_view_vulns () " ::integer;", sql_database ())) sql ("CREATE OR REPLACE VIEW vulns AS" + " WITH used_nvts" + " AS (SELECT DISTINCT nvt FROM results" + " WHERE (results.severity != " G_STRINGIFY (SEVERITY_ERROR) "))" " SELECT id, uuid, name, creation_time, modification_time," " cast (cvss_base AS double precision) AS severity, qod," " 'nvt' AS type" " FROM nvts" - VULNS_RESULTS_WHERE + " WHERE uuid in (SELECT * FROM used_nvts)" " UNION SELECT id, uuid, name, creation_time, modification_time," " score / 10.0 AS severity, " G_STRINGIFY (QOD_DEFAULT) " AS qod," " 'cve' AS type" " FROM cves" - VULNS_RESULTS_WHERE + " WHERE uuid in (SELECT * FROM used_nvts)" " UNION SELECT id, uuid, name, creation_time, modification_time," " score / 10.0 AS severity, " G_STRINGIFY (QOD_DEFAULT) " AS qod," " 'ovaldef' AS type" " FROM ovaldefs" - VULNS_RESULTS_WHERE); + " WHERE uuid in (SELECT * FROM used_nvts)"); else sql ("CREATE OR REPLACE VIEW vulns AS" + " WITH used_nvts" + " AS (SELECT DISTINCT nvt FROM results" + " WHERE (results.severity != " G_STRINGIFY (SEVERITY_ERROR) "))" " SELECT id, uuid, name, creation_time, modification_time," " cast (cvss_base AS double precision) AS severity, qod," " 'nvt' AS type" " FROM nvts" - VULNS_RESULTS_WHERE); + " WHERE uuid in (SELECT * FROM used_nvts)"); } #undef VULNS_RESULTS_WHERE