mysql无规律主键_在 MySQL 中,从 10 万条主键不连续的数据里随机取 3000 条,如何做到高效?...

mysql> select count(*) from s;

+----------+

| count(*) |

+----------+

| 2708736 |

+----------+

1 row in set (6.52 sec)

mysql> select column_name from s order by rand() limit 3;

+-----------------+

| column_name |

+-----------------+

| pay_desc |

| ISOLATION_LEVEL |

| rows_examined |

+-----------------+

3 rows in set (10.76 sec)

为了显示方便,在270万条记录的表里随机取3行,用时11秒。选3000条,也是同样时间。此表s没有主键,没有索引,大约十几个字段,column_name是其中一个字符串型字段。

如果加where条件取其中的十几万条记录,也同样很快。

mysql> select count(*) from s where column_name like "a%";

+----------+

| count(*) |

+----------+

| 142080 |

+----------+

1 row in set (1.61 sec)

mysql> select column_name from s where column_name like "a%" order by rand() limit 3;

+-------------+

| column_name |

+-------------+

| a |

| address |

| apname |

+-------------+

3 rows in set (1.86 sec)

数据库版本5.7,在配置普通的阿里云服务器和自己的DELL XPS笔记本上都测过,结果一致。

难道是因为题主的数据库版本太旧,对order by rand() limit 的优化不够?

建议大家自己都实测验证下。我是用

create table s as select * from information_schema.columns

创建表。然后用

insert into s select * from s;

执行八、九遍,估计就200多万条了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值