我有两张桌子:
Table GAME: id(int), added_at(bigint)
Table META: id_game(int), meta(VARCHAR(64))
现在,每个游戏可以有0个或更多与之相关的Meta标签。我正在尝试检索9场比赛:
截至目前,我有一个相当疯狂的系统,看起来或多或少是这样的:
$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个查询,随机化并不是真正随机的。
所以我的三个目标和我可能的解决方案是:
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}
但是,仍然需要使用ORDER BY RAND()和我看到的一些测试让它看起来非常慢。有什么提示可以改善它吗?
答案 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个最新的值)。