简体   繁体   English

innodb表中的随机行查询优化

[英]random row query optimization in innodb table

$offset = SELECT FLOOR(RAND() * COUNT(*)) FROM t_table
SELECT * FROM t_table WHERE LIMIT $offset,1

This works great in myisam but i would like to change this table to innodb (all other db tables are innodb) to take advantages of foreign-keys and avoid table level locking. 这在myisam中效果很好,但我想将此表更改为innodb (所有其他数据库表均为innodb)以利用外键并避免表级锁定。

The primaryId field of this table is a VARCHAR(10) 该表的primaryId字段是VARCHAR(10)

I can't "force" a numeric autoinc Id, because records are deleted/added all the time and a RANDOM(MIN(Id), MAX(Id)) prediction would probably miss rows lots of times. 我不能“强制”数字自动增量编号,因为记录始终会被删除/添加,并且RANDOM(MIN(Id),MAX(Id))预测可能会多次丢失行。

how can i optimize this query to innodb? 我如何优化此查询到innodb?

Thanks in advance! 提前致谢!
Arthur 亚瑟

这对您不起作用吗?

SELECT * FROM t_table ORDER BY RAND() LIMIT 1

"SELECT * FROM t_table ORDER BY RAND() LIMIT 1 " “ SELECT * FROM t_table ORDER BY RAND()限制1”

That will not work, because mySQL will check all the rows who match the condition (no condition here, so will take all the rows) they will copy the rows in a temporary table and then will select a random row 那是行不通的,因为mySQL将检查所有符合条件的行(此处没有条件,因此将获取所有行),它们将在临时表中复制行,然后选择一个随机行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM