Window function is a feature introduced in MySQL Community Edition 8.0 to improve query and analysis capabilities. Window function is fully supported in PolarDB for MySQL 8.0 and can be executed in parallel.


  • A PolarDB for MySQL 8.0 Cluster Edition is used and the revision version is or later. For more information about how to check the version, see Query the engine version.
  • Only window functions that use the PARTITION BY clause can be executed in parallel.


  • Syntax:

    In PolarDB, you can use only the EXPLAIN FORMAT=TREE statement to check whether window functions are used.

  • Examples:
    In the following example, a table named employee_salaries is created and data is inserted into the table.
    CREATE TABLE `employee_salaries` (
      `dept` varchar(20) DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL,
      `salary` int DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    INSERT INTO `employee_salaries` VALUES
    The following example shows the parallel query execution plan for the employee_salaries table:
    explain format=tree select ROW_NUMBER() OVER(partition by dept order by salary desc) AS 'row_number' from employee_salaries\G
    *************************** 1. row ***************************
    -> Gather (slice: 1; workers: 4)  (cost=26.42 rows=12)
        -> Window aggregate  (cost=15.67 rows=3)
            -> Repartition (hash keys: employee_salaries.dept; slice: 2; workers: 4)  (cost=15.33 rows=3)
                -> Sort: employee_salaries.dept, employee_salaries.salary DESC  (cost=1.55 rows=13)
                    -> Parallel table scan on employee_salaries, with parallel partitions: 4

    In the preceding execution plan, four workers scan the employee_salaries table in parallel, and partition and distribute the data to workers of the next phase based on the employee_salaries.dept key specified in the PARTITION BY clause. This ensures that the window function can execute parallel calculations and deliver correct results. Finally, a leader gathers the window function results of the four workers to generate the final query results.