Skip to content

accessibleRepositoryCondition makes an awful "where condition" in SQL #10457

Open
@bennyscetbun

Description

@bennyscetbun
  • Gitea version (or commit ref): 06cd3e0
  • Operating system: docker image
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant

Description

Hi we have a user with more than 200k repo and more than 600k action on them.
We cannot connect on the website anymore with this user
We found out that the issue was from a request that make the front end time out.
We ve been searching in the code. We ve found the request

SELECT "id", "user_id", "op_type", "act_user_id", "repo_id", "comment_id", "is_deleted", "ref_name", "is_private", "content", "created_unix" FROM "action"
WHERE
    repo_id IN (SELECT id FROM repository WHERE
        (
            "repository".is_private=false AND
            ("repository".owner_id NOT IN (SELECT id FROM "user" WHERE type=1) OR "repository".owner_id NOT IN (SELECT id FROM "user" WHERE visibility IN (2)))
        ) OR
        "repository".id IN (SELECT repo_id FROM "access" WHERE user_id=1 AND mode>0) OR
        "repository".id IN (SELECT id FROM "repository" WHERE owner_id=1) OR
        "repository".id IN (SELECT "team_repo".repo_id FROM team_repo INNER JOIN team_user ON "team_user".team_id = "team_repo".team_id WHERE "team_user".uid=1)
    ) AND
    user_id=1 AND
    is_deleted=false
ORDER BY "id" DESC LIMIT 20

The main issue here (at least in postgres) is the over use of OR and the use of IN with some sub queries.
To fix it we ve used some subqueries with UNION
For info we let the first query run for more than 20min without a response.
Our new query is less than 5 seconds.
We haven't try with all the different type of databases. Do you think there will be issues with other DB?
Do you see any other better/good solution?
Should we make a pull request?

Thanks
...

Screenshots

I am pretty sure you know what a 504 is :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue/confirmedIssue has been reviewed and confirmed to be present or accepted to be implementedperformance/speedperformance issues with slow downs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions