Skip to content

Rewrite GROUP BY queries with trivial expressions over the same column #4141

@puzpuzpuz

Description

@puzpuzpuz

Is your feature request related to a problem?

Example query:

-- 2.9s
SELECT ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3, COUNT(*) AS c FROM hits GROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3 ORDER BY c DESC LIMIT 10;

Here ClientIP is INT. This query can be rewritten in the following form:

-- 2.1s
SELECT ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3, COUNT
FROM (SELECT ClientIP, COUNT(*) AS c FROM hits GROUP BY ClientIP ORDER BY c DESC LIMIT 10);

Apart from subtraction, we could support adding, multiplication and division.

Describe the solution you'd like.

No response

Describe alternatives you've considered.

No response

Full Name:

Andrei Pechkurov

Affiliation:

QuestDB

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    PerformancePerformance improvementsSQLIssues or changes relating to SQL execution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions