最近做一个库存发货的业务,用户购买一个商品时(例如游戏点卡),需要随机的从库存表中选择一个返回给用户。
查了下资料,mysql大致有三种方式来实现随机查询,总结在这里
创建测试数据
创建一个库存表,包括产品id、劵码code
#创建表
DROP TABLE IF EXISTS `product_stock`
CREATE TABLE `product_stock` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`product_code` varchar(64) DEFAULT NULL,
`voucher_code` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `product_code` (`product_code`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
利用存储过程,依次创建1w条 kfc劵码库存、10w条McDonald劵码库存、100w条Dicos劵码库存
## 依次创建1w、10w、100w的测试数据
DROP PROCEDURE IF EXISTS batch_insert;
delimiter $$
create procedure batch_insert()
begin
DECLARE max int;
DECLARE rc int;
set max =10000;
set rc =0;
loopl: while rc<max do
INSERT INTO `test`.`product_stock`(`product_code`, `voucher_code`) VALUES ('Dicos',concat('hasdq23aad',rc));
set rc=rc+1;
end while loopl;
end$$
delimiter ;
call batch_insert();
查询数目
mysql> select product_code, count(*) from product_stock GROUP BY product_code;
+--------------+----------+
| product_code | count(*) |
+--------------+----------+
| Dicos | 1000000 |
| kfc | 10000 |
| McDonald | 100000 |
+--------------+----------+
3 rows in set (0.29 sec)
1. 最简单的方式:rand()
最简单的随机查询,就是利用mysql的rand()函数
## 1w row
mysql> SELECT * FROM product_stock where product_code = 'kfc' ORDER BY RAND() LIMIT 5;
5 rows in set (0.02 sec)
## 10w row
mysql> SELECT * FROM product_stock where product_code = 'McDonald' ORDER BY RAND() LIMIT 5;
5 rows in set (0.19 sec)
## 100w row
mysql> SELECT * FROM product_stock where product_code = 'Dicos' ORDER BY RAND() LIMIT 5;
5 rows in set (1.87 sec)
我们再分析下使用rand()
时mysql做了什么
mysql> EXPLAIN SELECT * FROM product_stock where product_code = 'kfc' ORDER BY RAND() LIMIT 5;
+----+-------------+---------------+------+---------------+--------------+---------+-------+-------+--------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+--------------+---------+-------+-------+--------------------------------------------------------+
| 1 | SIMPLE | product_stock | ref | product_code | product_code | 259 | const | 17758 | Using index condition; Using temporary; Using filesort |
+----+-------------+---------------+------+---------------+--------------+---------+-------+-------+--------------------------------------------------------+
分析下
Extra:Using index condition; Using temporary; Using filesort
Using index condition
:这条sql语句先通过product_code
索引查询出所有kfc
的行数
Using temporary
:对于每条记录,调用 rand() 函数生成一个随机小数,将随机小数和每行列信息(列信息太多时只会放索引)存进中间表
Using filesort
:对中间表数据,根据随机小数进行排序。排序结束后取前5行返回(filesort
排序原理请看:xxxx)再分析下
rows:17758
kfc有1w条数据,所以17758中有10000是根据product_code
索引查询出来的;
根据随机数排序完成后,需要查询前5条数据返回,所以剩下的7758中有5条记录是返回数据查询用的;
剩下的7753条数据,猜测就是排序时用到的。mysql中排序会用到快排,优先队列排序,数据量太大sort_buffer填不完时,还会进行归并排序
2. 最高效的方式:随机id
这个方法具体实现原理就是,在满足条件的行id中随机选择5条记录,然后再根据随机id查询出记录:
## 查出Dicos记录的最小id和最大id
select max(id),min(id) into @Max,@Min from product_stock where product_code = 'Dicos';
## 在[min,max]之间随机出5个id
set @X1= floor((@Max-@Min+1)*rand() + @Min);
set @X2= floor((@Max-@Min+1)*rand() + @Min);
set @X3= floor((@Max-@Min+1)*rand() + @Min);
set @X4= floor((@Max-@Min+1)*rand() + @Min);
set @X5= floor((@Max-@Min+1)*rand() + @Min);
## 根据随机id,查询出5条数据
select * from product_stock where id >= @X1 limit 1 UNION All
(select * from product_stock where id >= @X2 limit 1) UNION All
(select * from product_stock where id >= @X3 limit 1) UNION All
(select * from product_stock where id >= @X4 limit 1) UNION All
(select * from product_stock where id >= @X5 limit 1)
这种方法查询100w数据的“Dicos”,也只会使用0.08s
但是最大的弊端在于,这种方法要求数据的id连续不中断的;如果数据是随机分布,那用该办法可能会命中其余的数据;如果数据id连续但不均匀(如1,2,500,600……),则随机概率不准确
3. 最通用的方式:随机row
这种方式完善了上一种方式的缺陷,实现原理是,在满足条件的行中随机选择5行记录,然后使用limit查询返回:
## 查出Dicos记录的总数目
select count(*) into @C from product_stock where product_code = 'Dicos';
## 随机出5行
set @Y1 = floor(@C * rand());
set @Y2 = floor(@C * rand());
set @Y3 = floor(@C * rand());
set @Y4 = floor(@C * rand());
set @Y5 = floor(@C * rand());
## **注意,以下为伪代码**
## 获取行数最小和最大值
set @Min = MIN(@Y1,@Y2,@Y3,@Y4,@Y5)
set @Max = MAX(@Y1,@Y2,@Y3,@Y4,@Y5)
## 查询出最大行、最小行之间的id数据
select id from product_stock where product_code = 'Dicos' limit @Min, @Max-@Min+1
## 获取第0,@Y2-@Y1, @Y3-@Y1, @Y4-@Y1, @Y5-@Y1个数据的id,再根据id查询出指定数据即可
这种方式最耗时的是count(*)和limit语句,我们实践极端情况(最小,最大值横跨所有记录)的耗时为0.807s
select count(*) into @C from product_stock where product_code = 'Dicos';
select id from product_stock where product_code = 'Dicos' limit 0, 1000000;
我们再分析下 limit语句的执行
mysql> EXPLAIN select id from product_stock where product_code = 'Dicos' limit 0, 1000000;
+----+-------------+---------------+------+---------------+--------------+---------+-------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+--------------+---------+-------+--------+--------------------------+
| 1 | SIMPLE | product_stock | ref | product_code | product_code | 259 | const | 544082 | Using where; Using index |
+----+-------------+---------------+------+---------------+--------------+---------+-------+--------+--------------------------+
1 row in set (0.00 sec)
可以看出,虽然扫描了所有的Dicos,但是与rand()
方式相比,没有用到中间表,也没有用到file sort,所以速度上还是快很多
随机更新、删除
随机更新、删除的实现和随机查询差不多,这里提供rand()版:
## 随机更新
update product_stock set voucher_code = "aa" where product_code = 'Dicos' order by rand() limit 5;
## 随机删除
delete from product_stock where product_code = 'Dicos' order by rand() limit 5;