2022-08-29

Recursive query hangs then get "Error Code: 1030. Got error 1 - 'Operation not permitted' from storage engine" error

I'm trying to build a recursive query to enable me to find all future sports match records for the two players of a given match. In addition to this I need the query to return any match for any player that plays in any descendant match. To illustrate using some example data:

match_id match_date p1_id p2_id
1 01/01/2022 1 2
2 02/01/2022 1 3
3 03/01/2022 3 4
4 04/01/2022 5 6

I only really need match_id so if the start match is match_id = 1 then I'm looking for the query to return 1. The query should also return 2 because p1_id = 1 played in the start match. The query should also return 3 because p2_id = 3 played in match_id = 2.

I've written the following query:

WITH RECURSIVE match_ids AS (
  SELECT
    rt1.match_id,
    rt1.p1_id,
    rt1.p2_id,
    rt1.match_date
  FROM recursive_test_so AS rt1
  WHERE rt1.match_id = 1
  UNION ALL
  SELECT
    rt2.match_id,
    rt2.p1_id,
    rt2.p2_id,
    rt2.match_date
  FROM recursive_test_so AS rt2
    JOIN match_ids ON 
      rt2.match_date > match_ids.match_date
  WHERE (
    rt2.p1_id IN (match_ids.p1_id, match_ids.p2_id)
    OR rt2.p2_id IN (match_ids.p1_id, match_ids.p2_id)
  )
)
SELECT DISTINCT match_id
FROM match_ids;

This works fine on the sample data.

However, when I scale the data up to 10k rows then the query runs for about 5 mins with no output and then I get the following error:

Error Code: 1030. Got error 1 - 'Operation not permitted' from storage engine

What might I be doing wrong?

SQL to replicate the sample data table:

CREATE TABLE `recursive_test_so` (
  `match_id` int NOT NULL,
  `match_date` date NOT NULL,
  `p1_id` int NOT NULL,
  `p2_id` int NOT NULL,
  PRIMARY KEY (`match_id`),
  KEY `match_date` (`match_date`),
  KEY `p1_id` (`p1_id`),
  KEY `p2_id` (`p2_id`),
  KEY `comp_all` (`match_date`,`p1_id`,`p2_id`),
  KEY `comp_player_ids` (`p1_id`,`p2_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;

INSERT INTO `recursive_test_so`
VALUES
  (1,'2022-01-01',1,2),
  (2,'2022-01-02',1,3),
  (3,'2022-01-03',3,4),
  (4,'2022-01-04',5,6);

Not sure how I could post the 10k rows data?



No comments:

Post a Comment