优化mysql随机查询

时间:2011-03-18 10:53:25

标签: php mysql query-optimization

我有两张桌子:

Table GAME: id(int), added_at(bigint)
Table META: id_game(int), meta(VARCHAR(64))

现在,每个游戏可以有0个或更多与之相关的Meta标签。我正在尝试检索9场比赛:

  • 1个随机游戏“特色”META
  • 1个“高级”META随机游戏
  • 随机游戏“双点”META
  • 最新的3场比赛(ORDER BY added_at DESC)
  • 3场随机游戏,不是上述6场比赛中的任何一场

截至目前,我有一个相当疯狂的系统,看起来或多或少是这样的:

$feat = getGameMetaRandom(1, 'featured');
$prem = getGameMetaRandom(1, 'premium');
$dubl = getGameMetaRandom(1, 'doublepoints');
$last = getGameLatest(3);
$rand = getGameRandom(3);

目前,每个随机函数都会进行两次查询(来自getGameMetaRandom($count, $meta);):

SELECT FLOOR(RAND() * (COUNT(*) - " . ($count - 1) .")) AS `offset` 
FROM table_meta WHERE meta = '{$meta}'

SELECT t1.* FROM table_meta t2
LEFT JOIN table_game t1 ON t1.id = t2.id_game
WHERE t2.meta = '{$meta}' LIMIT {$offset}, {$count}

(gameRandom非常相似)你可以看到这忽略了我对的限制,它不是上面的6个游戏中的任何一个,加上所有这些都需要9个查询,随机化并不是真正随机的。

所以我的三个目标和我可能的解决方案是:

  1. 如何制作3个随机游戏,不重复任何上述游戏。选择前六个游戏后,我可能会列出他们的ID并在最后一个查询中使用NOT IN(),但不会过度优化。
  2. 如何随机拍摄随机游戏,不选择随机游戏并从中获取 n 游戏?显然使用ORDER BY RAND(),但我听到它的速度有多慢,但我想,除非我的表有数百行,否则它没有什么区别?
  3. 如何减少查询次数?将前三个查询分组为一个,我留下5个查询,或者使用ORDER BY RAND()我可以忽略第一个“偏移检索”查询并使用类似SELECT t1.* FROM table_meta t2 LEFT JOIN table_game t1 ON t1.id = t2.id_game WHERE t2.meta = '{$meta}' ORDER BY RAND() LIMIT {$count}
  4. 的内容

    但是,仍然需要使用ORDER BY RAND()和我看到的一些测试让它看起来非常慢。有什么提示可以改善它吗?

1 个答案:

答案 0 :(得分:1)

游戏桌:

root@localhost [kris]> show create table games\G
*************************** 1. row ***************************
       Table: games
Create Table: CREATE TABLE `games` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `flags` enum('features','premium','doublepoints') NOT NULL,
  `added_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8184 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

示例游戏:

root@localhost [kris]> insert into games values ( NULL, floor(rand() * 4 ), now() - interval 1200 second);
Query OK, 1 row affected, 1 warning (0.00 sec)

Note (Code 1592): Statement may not be safe to log in statement format.

更多示例游戏:

root@localhost [kris]> insert into games select NULL, floor(rand() * 4), now() - interval 1200 second from games;
Query OK, 1 row affected, 1 warning (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

Note (Code 1592): Statement may not be safe to log in statement format.

重复上述声明,直到有足够的样本数据。可以忽略数据截断警告,它们是在enum()列中插入0的工件,从而产生无标记游戏,这就是我们想要的。

root@localhost [kris]> select count(*) from games;
+----------+
| count(*) |
+----------+
|     8192 |
+----------+
1 row in set (0.00 sec)

我们创建一个改组的游戏列表:

root@localhost [kris]> create table shuffle like games;
Query OK, 0 rows affected (0.09 sec)

root@localhost [kris]> alter table shuffle modify column id integer not null, drop primary key, add column shuffleid integer not null auto_increment, add primary key (shuffleid), add index(flags), add index(added_at), add index(id);
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

随机播放游戏:

root@localhost [kris]> insert into shuffle select id, flags, added_at, NULL from games order by rand();
Query OK, 8192 rows affected, 1 warning (0.34 sec)
Records: 8192  Duplicates: 0  Warnings: 0

Note (Code 1592): Statement may not be safe to log in statement format.

现在只需获取您需要的内容:

root@localhost [kris]> select min(id) as id from shuffle where flags = 'premium' 
    union all select min(id) from shuffle where flags = 'features' 
    union all select min(id) from games where flags = 'doublepoints' 
    union all ( select id from shuffle order by added_at limit 3 );
+------+
| id   |
+------+
| 8216 |
| 8214 |
| 8218 |
| 8213 |
| 8214 |
| 8216 |
+------+
6 rows in set (0.00 sec)

在第二个查询中选择3个不在上述集合中的随机行更有效:

root@localhost [kris]> select id from shuffle where id not in ( 8216, 8214, 8218, 8213, 8214, 8216) limit 3;
+------+
| id   |
+------+
| 8215 |
| 8219 |
| 8220 |
+------+
3 rows in set (0.00 sec)

然后从shuffle中删除9个值,以便随后使用该表将生成9个新值(或者如果您愿意,可以保留3个最新的值)。