Resolve "Linear Project#ancestors"
What does this MR do?
Project#ancestors
(aliased to #ancestors_upto
) uses ObjectHierarchy
to make recursive calls. We can replace this method with the linear version from the Namespace
model. In practice we've found the linear versions to be faster and less complicated for the database optimizer to work with.
SQL Changes
Click through to the postgres.ai links for actual IDs used.
Project#ancestors
New
SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."id" IN (22, 23, 24)
Time: 4.003 ms
- planning: 2.956 ms
- execution: 1.047 ms
- I/O read: 0.927 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 6 (~48.00 KiB) from the buffer pool
- reads: 13 (~104.00 KiB) from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/5825/commands/19745
Old
WITH RECURSIVE "base_and_ancestors" AS (
(
SELECT
"namespaces".*
FROM
"namespaces"
WHERE
"namespaces"."type" = 'Group'
AND "namespaces"."id" = 24
)
UNION
(
SELECT
"namespaces".*
FROM
"namespaces",
"base_and_ancestors"
WHERE
"namespaces"."type" = 'Group'
AND "namespaces"."id" = "base_and_ancestors"."parent_id"
)
)
SELECT
"namespaces".*
FROM
"base_and_ancestors" AS "namespaces"
Namespace#ancestors with hierarchy_order
The "SELECT namespaces.*" wrapper was added to normalize the SELECT
. Otherwise you can get mismatched column count errors with unions. Union queries are common when working with namespace hierarchies.
SELECT
"namespaces".*
FROM
(
SELECT
"namespaces".*,
ABS(
3 - array_length(traversal_ids, 1)
) as depth
FROM
"namespaces"
WHERE
"namespaces"."id" IN (27, 28)
ORDER BY
"depth" DESC
) namespaces
Time: 4.275 ms
- planning: 3.130 ms
- execution: 1.145 ms
- I/O read: 0.964 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 9 (~72.00 KiB) from the buffer pool
- reads: 13 (~104.00 KiB) from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/5836/commands/19748
How to setup and validate locally (strongly suggested)
Does this MR meet the acceptance criteria?
Conformity
-
I have included changelog trailers, or none are needed. (Does this MR need a changelog?) -
I have added/updated documentation, or it's not needed. (Is documentation required?) -
I have properly separated EE content from FOSS, or this MR is FOSS only. (Where should EE code go?) -
I have added information for database reviewers in the MR description, or it's not needed. (Does this MR have database related changes?) -
I have self-reviewed this MR per code review guidelines. -
This MR does not harm performance, or I have asked a reviewer to help assess the performance impact. (Merge request performance guidelines) -
I have followed the style guides. -
This change is backwards compatible across updates, or this does not apply.
Availability and Testing
-
I have added/updated tests following the Testing Guide, or it's not needed. (Consider all test levels. See the Test Planning Process.) -
I have tested this MR in all supported browsers, or it's not needed. -
I have informed the Infrastructure department of a default or new setting change per definition of done, or it's not needed.
Security
Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.
- [-] Label as security and @ mention
@gitlab-com/gl-security/appsec
- [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
- [-] Security reports checked/validated by a reviewer from the AppSec team
Related to #337719 (closed)