mysql:按组选择前n个记录的最有效方法

时间:2014-02-25 16:17:21

标签: mysql

有几个很好的条目,关于如何使用Mysql按组选择min / max或top n记录。我的方法无疑是行人(我明白了),但我想知道是否有更好的方式对我来说仍然是直观清晰的。一个特别难看的是需要对所需的计数进行硬编码(1000),因为人们可以set LIMIT_value := 1000;并将其用作查询中的参数。

所以我的问题是:从给定的片段中选择1000条记录是否有更好,更省力的方法?奖金问题(这可以通过sed处理并通过管道传输到mysql ...)我可以参数化1000条记录请求吗?

快速背景:我正在观察两个客户群,他们给出了两种不同的报价,但没有正确随机化。我根据不同的报价水平对后续活动进行分析。

我使用新近度(R,自上次购买后12米,或更多12米)和频率(F,1)随机匹配客户(一个来自第一个群组,一个来自第二个群组,来自第二个群组)时间买方,或2x +)分组,以控制在营销活动时未知的不同潜在客户群。对于那些在联系时被识别为新文件的客户,R和F值都是“NA”,并构成另一个分组级别。感谢。

/* _lp_stp: low price sample, _hp_stp: high price sample */

drop table if exists _lp_stp;
create table _lp_stp as
(select ID,price,rbin,fbin from _lp where rbin='NA'   and fbin='NA'  limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='NA'   and fbin='1x'  limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='NA'   and fbin='2x+' limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='12m'  and fbin='NA'  limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='12m'  and fbin='1x'  limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='12m'  and fbin='2x+' limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='12m+' and fbin='NA'  limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='12m+' and fbin='1x'  limit 1000)
union
(select ID,price,rbin,fbin from _lp where rbin='12m+' and fbin='2x+' limit 1000);


drop table if exists _hp_stp;
create table _hp_stp as
(select ID,price,rbin,fbin from _hp where rbin='NA'   and fbin='NA'  limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='NA'   and fbin='1x'  limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='NA'   and fbin='2x+' limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='12m'  and fbin='NA'  limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='12m'  and fbin='1x'  limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='12m'  and fbin='2x+' limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='12m+' and fbin='NA'  limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='12m+' and fbin='1x'  limit 1000)
union
(select ID,price,rbin,fbin from _hp where rbin='12m+' and fbin='2x+' limit 1000);

1 个答案:

答案 0 :(得分:0)

免责声明:查询只是为了看起来更简单,但性能可能很差,特别是如果您正在处理大表。从来没有我在生产数据库中想要这样的查询,

select ID,price,rbin,fbin 
from _lp
inner join (
select substring_index(substring_index(group_concat(id order by id asc),",", 1000),",",-1) as id, fbin, rbin
where fbin in (...) and rbin in (...)
group by fbin, rbin
where rbin='12m+' and fbin='2x+' ) as t 
on t.rbin = _lp.rbin and t.fbin = _lp.fbin and t.rbin <= _lp.id

对查询的一点解释:

  1. 子查询按升序获取第1000个ID的id。例如,如果你有5000条记录,如果没有给出分组,则子查询给出4000(我已经添加了一个额外的组以适合每个组)
  2. 外部查询获取然后获取值小于给定记录的所有记录