如何使MySQL与整型字段和浮点字段的键相交?

如何使MySQL与整型字段和浮点字段的键相交?,mysql,query-optimization,Mysql,Query Optimization,我有以下MySQL表(表大小-大约10K条记录): 如您所见,我有两个整数字段(r_id和m_id)和一个浮点字段(price)。 对于每个字段,我都有一个索引 现在,当我在第一个整数和第二个整数上运行带条件的查询时,一切正常: mysql> explain select * from tmp_index_test where m_id=1 and r_id=2; +----+-------------+----------------+-------------+------------

我有以下MySQL表(表大小-大约10K条记录):

如您所见,我有两个整数字段(r_id和m_id)和一个浮点字段(price)。 对于每个字段,我都有一个索引

现在,当我在第一个整数和第二个整数上运行带条件的查询时,一切正常:

mysql> explain select * from tmp_index_test where m_id=1 and r_id=2;
+----+-------------+----------------+-------------+---------------+-------------+---------+------+------+-------------------------------------------+
| id | select_type | table          | type        | possible_keys | key         | key_len | ref  | rows | Extra                                     |
+----+-------------+----------------+-------------+---------------+-------------+---------+------+------+-------------------------------------------+
|  1 | SIMPLE      | tmp_index_test | index_merge | m_key,r_key   | r_key,m_key | 5,5     | NULL |    1 | Using intersect(r_key,m_key); Using where |
+----+-------------+----------------+-------------+---------------+-------------+---------+------+------+-------------------------------------------+
看起来MySQL的性能非常好,因为在额外的字段中有使用intersect(r\u key,m\u key)。 我不是MySQL专家,但据我所知,MySQL首先在索引上进行交集,然后才从表本身收集交集的结果

但是,当我运行非常相似的查询时,我没有对两个整数设置条件,而是对一个整数和一个浮点设置了相似的条件,MySQL拒绝在索引上与结果相交:

mysql> explain select * from tmp_index_test where m_id=3 and price=100;
+----+-------------+----------------+------+-----------------+-----------+---------+-------+------+-------------+
| id | select_type | table          | type | possible_keys   | key       | key_len | ref   | rows | Extra       |
+----+-------------+----------------+------+-----------------+-----------+---------+-------+------+-------------+
|  1 | SIMPLE      | tmp_index_test | ref  | m_key,price_key | price_key | 5       | const |    1 | Using where |
+----+-------------+----------------+------+-----------------+-----------+---------+-------+------+-------------+
如您所见,MySQL决定只使用价格指数

我的第一个问题是为什么,以及如何解决它

除此之外,我还需要运行带有更多符号(>)的查询,而不是价格上的等号(=)。目前解释显示,对于此类查询,MySQL只使用整数键

mysql> explain select * from tmp_index_test where m_id=3 and price > 100;
+----+-------------+----------------+------+-----------------+-------+---------+-------+------+-------------+
| id | select_type | table          | type | possible_keys   | key   | key_len | ref   | rows | Extra       |
+----+-------------+----------------+------+-----------------+-------+---------+-------+------+-------------+
|  1 | SIMPLE      | tmp_index_test | ref  | m_key,price_key | m_key | 5       | const |    2 | Using where |
+----+-------------+----------------+------+-----------------+-------+---------+-------+------+-------------+
我需要先让MySQL在索引上进行交集。有人知道怎么做吗

提前多谢

来自:

如果联接仅使用键的最左侧前缀,或者如果 该键不是主键或唯一索引(换句话说,如果 join无法基于键值选择单个行)。如果钥匙 这是一种很好的连接类型


price
不是唯一的或主要的,因此选择了ref。我不相信你可以强制进行交叉。

但是m\u id和I\u id unique/primary都没有,但是对于它们,使用了index\u merge。有人知道为什么吗?
mysql> explain select * from tmp_index_test where m_id=3 and price > 100;
+----+-------------+----------------+------+-----------------+-------+---------+-------+------+-------------+
| id | select_type | table          | type | possible_keys   | key   | key_len | ref   | rows | Extra       |
+----+-------------+----------------+------+-----------------+-------+---------+-------+------+-------------+
|  1 | SIMPLE      | tmp_index_test | ref  | m_key,price_key | m_key | 5       | const |    2 | Using where |
+----+-------------+----------------+------+-----------------+-------+---------+-------+------+-------------+