人们尝试在MySQL中模拟MSSQL RANK()或ROW_NUMBER()函数的方式有很多种,但到目前为止我尝试过的所有函数都很慢。
我有一个看起来像这样的表:
CREATE TABLE ratings
(`id` int, `category` varchar(1), `rating` int)
;
INSERT INTO ratings
(`id`, `category`, `rating`)
VALUES
(3, '*', 54),
(4, '*', 45),
(1, '*', 43),
(2, '*', 24),
(2, 'A', 68),
(3, 'A', 43),
(1, 'A', 12),
(3, 'B', 22),
(4, 'B', 22),
(4, 'C', 44)
;
除了它有220,000条记录。大约有90,000个唯一ID。
我想首先通过查看不是*
的类别来对id进行排名,其中较高等级的排名较低。
SELECT g1.id,
g1.category,
g1.rating,
Count(*) AS rank
FROM ratings AS g1
JOIN ratings AS g2 ON (g2.rating, g2.id) >= (g1.rating, g1.id)
AND g1.category = g2.category
WHERE g1.category != '*'
GROUP BY g1.id,
g1.category,
g1.rating
ORDER BY g1.category,
rank
输出:
id category rating rank
2 A 68 1
3 A 43 2
1 A 12 3
4 B 22 1
3 B 22 2
4 C 44 1
然后我想把最小的等级列为id,并将其与*类别中的等级进行平均。提供总查询:
SELECT X1.id,
(X1.rank + X2.minrank) / 2 AS OverallRank
FROM
(SELECT g1.id,
g1.category,
g1.rating,
Count(*) AS rank
FROM ratings AS g1
JOIN ratings AS g2 ON (g2.rating, g2.id) >= (g1.rating, g1.id)
AND g1.category = g2.category
WHERE g1.category = '*'
GROUP BY g1.id,
g1.category,
g1.rating
ORDER BY g1.category,
rank) X1
JOIN
(SELECT id,
Min(rank) AS MinRank
FROM
(SELECT g1.id,
g1.category,
g1.rating,
Count(*) AS rank
FROM ratings AS g1
JOIN ratings AS g2 ON (g2.rating, g2.id) >= (g1.rating, g1.id)
AND g1.category = g2.category
WHERE g1.category != '*'
GROUP BY g1.id,
g1.category,
g1.rating
ORDER BY g1.category,
rank) X
GROUP BY id) X2 ON X1.id = X2.id
ORDER BY overallrank
给我
id OverallRank
3 1.5000
4 1.5000
2 2.5000
1 3.0000
这个查询是正确的,我想要的输出,但它只挂在我的220,000条记录的真实表上。我该如何优化它?我的真实表格上有id,rating
和category
以及id,category
编辑:
SHOW CREATE TABLE ratings
的结果:
CREATE TABLE `rating` (
`id` int(11) NOT NULL,
`category` varchar(255) NOT NULL,
`rating` int(11) NOT NULL DEFAULT '1500',
`rd` int(11) NOT NULL DEFAULT '350',
`vol` float NOT NULL DEFAULT '0.06',
`wins` int(11) NOT NULL,
`losses` int(11) NOT NULL,
`streak` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`streak`,`rd`,`id`,`category`),
UNIQUE KEY `id_category` (`id`,`category`),
KEY `rating` (`rating`,`rd`),
KEY `streak_idx` (`streak`),
KEY `category_idx` (`category`),
KEY `id_rating_idx` (`id`,`rating`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PRIMARY KEY
是此表查询的最常见用例,这就是它成为群集密钥的原因。值得注意的是,该服务器是具有9GB / s FIO随机读取的固态硬盘10。所以我不怀疑没有聚集的指数会影响很多。
(select count(distinct category) from ratings)
的输出为50
为了这可能是数据的方式或对我的疏忽,我被包括在整个表的导出中。它只有200KB压缩:https://www.dropbox.com/s/p3iv23zi0uzbekv/ratings.zip?dl=0
第一个查询需要27秒才能运行
答案 0 :(得分:0)
您可以使用带有AUTO_INCREMENT列的临时表来生成排名(行号)。
例如 - 为'*'类别生成排名:
drop temporary table if exists tmp_main_cat_rank;
create temporary table tmp_main_cat_rank (
rank int unsigned auto_increment primary key,
id int NOT NULL
) engine=memory
select null as rank, id
from ratings r
where r.category = '*'
order by r.category, r.rating desc, r.id desc;
这可以在30毫秒的时间内运行。使用selfjoin的方法在我的机器上需要45秒。即使(category, rating, id)
上有新索引,它仍然需要14秒才能运行。
为每组(每个类别)生成排名有点复杂。我们仍然可以使用AUTO_INCREMENT列,但需要计算并减去每个类别的偏移量:
drop temporary table if exists tmp_pos;
create temporary table tmp_pos (
pos int unsigned auto_increment primary key,
category varchar(50) not null,
id int NOT NULL
) engine=memory
select null as pos, category, id
from ratings r
where r.category <> '*'
order by r.category, r.rating desc, r.id desc;
drop temporary table if exists tmp_cat_offset;
create temporary table tmp_cat_offset engine=memory
select category, min(pos) - 1 as `offset`
from tmp_pos
group by category;
select t.id, min(t.pos - o.offset) as min_rank
from tmp_pos t
join tmp_cat_offset o using(category)
group by t.id
这在约220毫秒内运行。使用新索引,selfjoin解决方案需要42秒或13秒。
现在您只需要将最后一个查询与第一个临时表结合起来,以获得最终结果:
select t1.id, (t1.min_rank + t2.rank) / 2 as OverallRank
from (
select t.id, min(t.pos - o.offset) as min_rank
from tmp_pos t
join tmp_cat_offset o using(category)
group by t.id
) t1
join tmp_main_cat_rank t2 using(id);
整体运行时间约为280毫秒而没有额外的索引,而且~240毫秒且索引位于(category, rating, id)
。
关于selfjoin方法的说明:这是一个优雅的解决方案,并且在小组规模下表现良好。平均组大小<= 2时速度很快。组大小为10时可以接受。但是您的平均组大小为447(count(*) / count(distinct category)
)。这意味着每行都有447个其他行(平均而言)。您可以通过删除group by子句来查看影响:
SELECT Count(*)
FROM ratings AS g1
JOIN ratings AS g2 ON (g2.rating, g2.id) >= (g1.rating, g1.id)
AND g1.category = g2.category
WHERE g1.category != '*'
结果超过10M行。
然而 - 使用(category, rating, id)
上的索引,您的查询在我的计算机上运行33秒。