0

有一个表包含这样的列:

id、person_id、pet_id、描述

关于这张表有一些说法:

  1. id 是主键,自增
  2. 每对 (person_id, pet_id) 都是唯一的
  3. id、person_id、pet_id 是整数且不为 NULL
  4. “漏洞”是可能的(最大 pet_id 并没有告诉我们总共有(最大)个列具有这样的 person_id)
  5. 表中很可能有更多不同的 person_id,即每个人的平均 pet_id 数。

问题是:如何快速选择当前person_id的随机N个pet_id?

表格示例:

1.  1   1     cat
2.  1   2     dog
3.  2   40    horse
4.  2   35    dog
5.  3   46    duck
6.  2   39    duck
7.  1   3     duck
..................
100000  403  12  monkey

示例:我想为第二个人选择两个 RANDOM 行。一种可能的随机选择是第 3 行。和第 6 行。选择应该是真正“随机的”(应该以相同的概率出现)。

如何使用 mysql 查询 SELECT 来做到这一点?

PS 当然,我已经阅读了有关从表中选择几个随机行的信息,这是一些棘手解决方案的基本问题。但是,就我而言,有两行,而不是一行。

我正在考虑一种比

select id from tablename where person_id = 2 order by random()  limit 2;
4

2 回答 2

1

您的稻草人查询已尽您所能。如果你没有一个人有很多宠物(并且你有一个索引person_id),它应该运行得很快。如果你有这样的人,那你就倒霉了。忘记随机选择,即使确定一个人有多少只宠物也需要时间 O(# pets)。

One possible other idea which probably won't work for you: If you don't care about the independence of your selections (i.e., you might get the same random response every time), then you can add a column which you populate with a random number when the row is inserted. Add an index on person_id,random_column and select the first N rows ordered by that pair. Slightly better is to add multiple random columns and select one to order by at random. Unfortunately, this doesn't scale well and I don't think you'd be happy with the results.

于 2013-01-06T00:07:45.767 回答
0

试试这个,这里我们减少了在数据库中执行 random() 函数的时间。

 $max_sql = "SELECT max(id) AS max_id  FROM " . $table;
  $max_row = mysql_fetch_array(mysql_query($max_sql));
  $random_number = mt_rand(1, $max_row['max_id']);

  $random_sql = "SELECT * FROM " . $table . "
                 WHERE " . $column . " >= " . $random_number . " 
                 ORDER BY " . $column . " ASC
                 LIMIT 1"; 
于 2013-01-05T22:32:25.550 回答