我在生产服务器上使用一个带有此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, 'WFFKYTVQR3lRZFA3eDdvR21KbGZaUT09', 'ZjVhTi95RW5kang1Q0JqU3dENkhjQT09', 'ZEhJZFlwNlJrSERneW5vMytkSU9qUmdtc1p2YlAxQWhjc2hxNzFIT0JBWT0=', 'UUtlVy9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1QXUyaHkzS1pwdz09', '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;
查询查询中没有任何内容表明应随机选择电子邮件,您只能为每个电子邮件随机选择一条记录。这可能是幸运的,因为内部执行路径会在正确的时间导致随机排序,但查询从来没有保证能达到预期效果。
考虑以下示例数据(忽略日期(
ID | 电子邮件 | 获胜者|
---|---|---|
1 | a@b.c | 0 |
2 | a@b.c | 0 |
3 | d@e.f | 0 |
4 | d@e.f | <1>|
5 | x@y.z | 0 |
考虑
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)