带有PARTITION BY子句的ROW_NUMBER()在MariaDB上停止工作

q3aa0525  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(153)

我已经在我的生产服务器上使用一个带有这个SQL查询的应用程序超过3年了,没有对查询或所使用的MariaDB服务器的主要版本进行任何修改。它突然停止工作,我不知道为什么。
查询:

SELECT 
  c.* 
FROM 
  contestants c 
WHERE 
  (
    date BETWEEN '2022-08-01 00:00:00' 
    AND '2022-08-31 23:59:59'
  ) 
  AND (winner = 0) 
ORDER BY 
  ROW_NUMBER() OVER (
    PARTITION BY email 
    ORDER BY 
      RAND()
  ) 
LIMIT 
  10;

**预期行为:**从contestants表(该表包含提交的竞赛代码,每个参赛者在此表中可以有多行)中随机返回10个获胜者,每个email只能出现一次,只能从没有任何获胜代码的参赛者中选择(winner = 1)。同一查询会执行多次(多个奖项轮),此查询确保每个电子邮件每个月只能赢得一个奖项。这在过去3年多的时间里一直很有效。
**当前行为:**该查询似乎只是通过升序返回10个参赛者(ID 1、2、3 ...)。

如果我在不同的Web服务器上运行查询(本地或我的托管服务提供商之一),它运行得很好。我试过MariaDB 10.3、10.5和MySQL 8.0,它们都能正常工作。
我的生产服务器运行的是MariaDB 10.3.34,这似乎是2022年2月的安全更新。但是,我尝试在运行MariaDB 10.9.2的同一台服务器上创建另一个数据库,结果是相同的。
为什么这个查询突然停止工作,而在不同的环境中却工作得很好?
编辑:下面是一些可以尝试的示例数据:

CREATE TABLE `contestants` (
  `EntryID` int(11) DEFAULT NULL,
  `firstName` varchar(255) DEFAULT NULL,
  `lastName` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `psc` varchar(255) DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  `winner` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `contestants` (`EntryID`, `firstName`, `lastName`, `email`, `address`, `phone`, `code`, `city`, `psc`, `date`, `winner`) VALUES
(1, 'TEZvbGgxVW9oQkphNmdWaGpOdXJGZz09', 'OTRmVzZ3NjVkUUJIVzlZRUw4a0RzQT09', 'QTJlSHNwcWg2TEkrSFNpc05BcFBFcTh5YWFqcHZPekkrWCtzOWw1TWVtRT0=', 'T2J4cFl2eEwxS1R3YTJkcHpjbkx3Zz09', 'ZUI0U3o5djFXbHkrZk5KRGVXK25FUT09', 'WlJBSTl1VnFyRGk0UlBsSFRCT0pLQT09', 'Q094aGRURm1VODRsS0FrK2RYdGxNZz09', 'ZTJoaituNkJtZ0Yvc2EzamRqZmhJUT09', '2022-08-18 00:00:00', 0),
(2, 'aGZZK0dvbSs1UTIxN3VGcGxKdEZuZz09', 'VFE2VHRGaktnV0l1b21uemlUbytUUT09', 'dEIvbTI5OEhHZG5IdSsxMjVKUXpKZVlkYkVkcy9PQU00RnhsRUx0RnZIdz0=', 'KzgxQkVOb0xSNDkzRGFXcnhpM3Rtdz09', 'ZmJDMzE0V3UzaWxEcE9veXNtUFpZZz09', 'RlY3a01MSktZRlptYnhwdVJqamI1UT09', 'dXlFRmRSY25CMDBnb3REdVhFN3dPZz09', 'ZWozRXJ5VUk2bUppU2NuMmZoVm9CZz09', '2022-08-19 00:00:00', 0),
(3, 'OERRNUVCank2aHBRRVdkVjJPZmNsUT09', 'OC80TStVdlFwTi9sbGFGQVNHcEZTZz09', 'SW9nVVZ2SCtnQUg2TzRYMDBsNTBFeStReHdDZTZKUHNocStTU3FPMG1mZz0=', 'Y0ZNdXNQdGRIVEJKNzlLTDJPV3l2dz09', 'N1cyUHR0S3FPbUdUbGFmenh4ZHhFdz09', 'SHN5MjRFWTR2Zmx5R1QzVEsyMVRBUT09', 'ZjVVN3ZSYnZNdmY2cHlqR080c1JzUT09', 'OFpPcEM5R1NJTHh2eFh1N0tMQzNGdz09', '2022-08-19 00:00:00', 0),
(4, 'WFFKYTVQR3lRZFA3eD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 'YlNETTlqSmVPSkluSVJpZXc0eXRNQ2FKVWRkMUs3aXY3UlBtWTUvRVZJbz0=', 'UDIrdEJ1cDlXZDVyd2hQVFNMYis3Zz09', 'SXdVQ2xKd2tXNmE1QXUyaHkzS1pwdz09', 'dXlFRmRSY25CMDBnb3REdVhFN3dPZz09', 'ZWozRXJ5VUk2bUppU2NuMmZoVm9CZz09', '2022-08-25 00:00:00', 0),
(20, 'SDd6OE1YUFBBeHUwQWJuM1NVT1VsQT09', 'enhSdDZycC9VTGlvc2IwT0Z2SjRXQT09', 'UTJmWHBkeDhNVWQwa1h5L1R0Nisza1ZPakRxMHVTa2VnNHhCVWh2dDJMVT0=', 'b1ppUzhIMUJyVVlKVEVDYi9pWElnK1FLaVRjaTVibll4T1hsY0ZxWjFTdz0=', 'c045YlkyREtuZXA0K3UwOWRzQnBTdz09', 'YS9jKzJTcldUMlFoT0xmTjVJVzVmQT09', 'MmlXWUovanpYSUhOQVl3dkRPWE5IQT09', 'VWFvcXQ1VXEvaWRQd0pPUW1WRjdRUT09', '2022-08-25 00:00:00', 0);

ALTER TABLE `contestants`
  ADD KEY `idx_email` (`email`);
COMMIT;
eyh26e7m

eyh26e7m1#

查询query中没有任何内容指示电子邮件应该随机选择,您只为每封电子邮件随机选择一条记录。由于内部执行路径在正确的时间导致随机排序,这可能是运气使然,但无法保证查询会执行您所期望的操作。
考虑以下示例数据(忽略日期)
| 识别码|电邮地址|优胜者|
| - -|- -|- -|
| 一个|a@b.c|第0页|
| 2个|a@b.c|第0页|
| 三个|扣除扣除费用|第0页|
| 四个|扣除扣除费用|一个|
| 五个|x@y.z|第0页|
如果要将行号分配给新列并应用筛选器,则行号函数将获得两种可能的结果,具体取决于rand函数的结果:
| 识别码|电邮地址|优胜者|核糖核酸1|核糖核酸2|
| - -|- -|- -|- -|- -|
| 一个|a@b.c|第0页|一个|2个|
| 2个|a@b.c|第0页|2个|一个|
| 三个|扣除扣除费用|第0页|一个|一个|
| 五个|x@y.z|第0页|一个|一个|
这里的问题是,您没有进一步的排序,因此,虽然您可以确保第1行出现在第2行之前(反之亦然),其中的电子邮件是相同的,但没有任何排序逻辑可以指定任何其他行的顺序。您可能需要进一步排序结果,例如:

ORDER BY 
  ROW_NUMBER() OVER (
    PARTITION BY email 
    ORDER BY 
      RAND()
  ),
  RAND()

另一个问题,你有与上述是,如果你选择了前10名,无论你做什么,你会得到a@b.c两次,因为只有4个记录可供选择。
我也不认为你的查询反映了以下标准(强调我的):
仅从没有任何获胜代码的参赛者中进行选择(winner = 1)。
在我给出的示例数据中,我认为d@e.f就是一个例子--一行有中奖代码,但另一行还没有中奖,因此仍有资格在当前规则下的下一轮中奖中被选中。
如果以上是一个问题,那么我认为你想要以下:

SELECT  c.ID, c.Email
FROM    (   SELECT  c.ID, 
                    c.Email,
                    MAX(c.winner) OVER(PARTITION BY c.email) AS HasWinner,
                    ROW_NUMBER() OVER(PARTINTION BY c.email ORDER BY RAND()) AS RowNumber
            FROM    contestants AS c
            WHERE   c.date >= '2022-08-01'
            AND     c.date < '2022-09-01'
        ) AS c
WHERE   c.HasWinner = 0 -- No Winners
AND     c.RowNumber = 1 -- First Row per Email
ORDER BY RAND()
LIMIT 10;

如果这不是问题,您可以简单地将winner上的where子句添加回内部查询,并删除外部查询上的筛选器(以及它从内部查询中筛选的列)

ao218c7q

ao218c7q2#

请考虑

DROP TABLE IF EXISTS T;
CREATE TABLE T
(EMAIL VARCHAR(3),WINNER INT);
INSERT INTO T VALUES
('AAA',0),('BBB',0),('CCC',0),('AAA',0);

带限制4

SELECT 
  c.* ,ROW_NUMBER() OVER (PARTITION BY email ORDER BY RAND()) rn,
  RAND(ROW_NUMBER() OVER (PARTITION BY email ORDER BY RAND())) rnrand
FROM 
  T c 
WHERE 
  #(
  #  date BETWEEN '2022-08-01 00:00:00' 
  #  AND '2022-08-31 23:59:59'
  #) 
  #AND 
  winner = 0
ORDER BY ROW_NUMBER() OVER (PARTITION BY email ORDER BY RAND()) 
LIMIT 4;

+-------+--------+----+---------------------+
| EMAIL | WINNER | rn | rndand              |
+-------+--------+----+---------------------+
| BBB   |      0 |  1 | 0.40540353712197724 |
| CCC   |      0 |  1 | 0.40540353712197724 |
| AAA   |      0 |  1 | 0.40540353712197724 |
| AAA   |      0 |  2 |  0.6555866465490187 |
+-------+--------+----+---------------------+
4 rows in set (0.001 sec)

限制2

+-------+--------+----+---------------------+
| EMAIL | WINNER | rn | rnrand              |
+-------+--------+----+---------------------+
| BBB   |      0 |  1 | 0.40540353712197724 |
| CCC   |      0 |  1 | 0.40540353712197724 |
+-------+--------+----+---------------------+
2 rows in set (0.001 sec)

相关问题