我有一张有 4,000,000 条记录的表。创建表: (user_id int, partner_id int, PRIMARY_KEY ( user_id )) engine=InnoDB; 我想测试select
100 条记录的性能。然后,我测试了以下内容:
mysql> explain select user_id from MY_TABLE use index (PRIMARY) where user_id IN ( 1 );
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY | MY_TABLE | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------------+
1 row in set, 1 warning (0.00 sec)
还行吧。但是,这个查询是由 mysql 缓冲的。所以,这个测试在第一次测试之后没有。
然后,我想到了一个通过随机值选择的sql。我测试了以下内容:
mysql> explain select user_id from MY_TABLE use index (PRIMARY) where user_id IN ( select ceil( rand() ) );
+----+-------------+----------+-------+---------------+---------+---------+------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+------+---------+--------------------------+
| 1 | PRIMARY | MY_TABLE | index | NULL | PRIMARY | 4 | NULL | 3998727 | Using where; Using index |
+----+-------------+----------+-------+---------------+---------+---------+------+---------+--------------------------+
但是,很糟糕。
Explain
表明 possible_keys 为 NULL。因此,计划了全索引扫描,实际上,它比之前的速度太慢了。
然后,我想请你教我如何用索引查找写随机值。
谢谢