[英]Return rows with the highest value on one column corresponding to a given value in another
There's a MySQL table named raw_contacts with the following structure: 有一个名为raw_contacts的MySQL表,其结构如下:
ID (primary auto-increment, int)
PHONE (composite unique with NAME, varchar)
NAME (composite unique with PHONE, varchar)
FREQUENCY (int)
Composite unique key is named PHONENUM
I am trying to write a query to return the row with the highest corresponding value in the FREQUENCY column for any given value for PHONE. 我正在尝试编写查询以返回FREQUENCY列中具有PHONE的任何给定值的对应值最高的行。 I did find a related post but the accepted answer doesn't work for my case since I have a composite key of two columns to run the check against. 我确实找到了相关的帖子,但是接受的答案对我的情况不起作用,因为我有两列的复合键来进行检查。
Just to illustrate, consider the following sample: 仅作说明,请考虑以下示例:
1 1234 John 6
2 1234 Dave 2
3 2199 Bill 9
4 1878 Dani 3
5 1234 Cory 7
6 1234 Gore 5
7 3319 Cory 1
Run against 1234 on the above sample, the query should return the 5th row since Cory has the highest count in the FREQUENCY column for that number. 在上面的示例中针对1234运行,查询应返回第5行,因为Cory在FREQUENCY列中具有该编号的最高计数。
Here's what I've come up with and it works great: 这是我想出的,效果很好:
select RC.ID, RC.PHONE, RC.FREQUENCY, RC.NAME
from `raw_contacts` RC
inner join(
select PHONE, max(FREQUENCY) FREQUENCY
from `raw_contacts`
where PHONE="11111"
group by PHONE
) RC2 on RC.PHONE = RC2.PHONE and RC.FREQUENCY = RC2.FREQUENCY
limit 1
Is there any more resource-friendly query for this task since it needs to run at a very high frequency on a table with millions of records? 由于此任务需要在具有数百万条记录的表上以很高的频率运行,因此是否还有其他资源友好的查询? I'm looking to optimize it to the bone! 我正在寻找最优化的方法!
PS In case of more than one rows qualifying, I need only one of them, which one doesn't matter. PS如果排位赛超过一排,我只需要其中一排,那没关系。
join is a costly operation in DB and I think you must avoid using it as much as possible!!! join在DB中是一项昂贵的操作,我认为您必须尽可能避免使用它!!! I suggest to use the following query and compare its result with your one. 我建议使用以下查询并将其结果与您的查询进行比较。
select * from `raw_contacts` RC1 where PHONE="11111"
and FREQUENCY>=all (select FREQUENCY from `raw_contacts` RC2 where RC2.PHONE=RC1.PHONE) LIMIT 1
also you can consider different types of indexing ( a good toturial is here ) on your table to speedup special and more frequent queries 您还可以考虑在表上使用不同类型的索引( 此处是一个很好的索引),以加快特殊查询和更频繁查询的速度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.