INDEX用法不同 - 得到了解决方案

时间:2013-12-14 17:16:41

标签: mysql sql optimization

我尝试运行一个SQL查询直接给出条件(它工作得更快)。

WHERE A = '1234'

但是传递变量需要花费更多时间。

SET @VAL = '1234'
WHERE A = @VAL

EXPLAIN参数还显示不同的关键字段。可能是什么原因?

快速(直接给予常量)

--------------------+---------+-----------------------------+------+----------------------------------------+
| id | select_type | table              | type   | possible_keys                                               | key                    | key_len | ref                         | rows | Extra                                  |
+----+-------------+--------------------+--------+-------------------------------------------------------------+------------------------+---------+-----------------------------+------+----------------------------------------+
|  1 | SIMPLE      | tracking           | ref    | unique-guest_user,FK4BBA1EB7AFA8C3E7,browser_id_hash_search | browser_id_hash_search | 257     | const                       |    3 | Using index condition; Using temporary |
|  1 | SIMPLE      | mn_connected_users | eq_ref | user_id                                                     | user_id                | 4       | 20131123_b.tracking.user_id |    1 | Using where                            |
+----+-------------+--------------------+--------+-------------------------------------------------------------+------------------------+---------+-----------------------------+------+----------------------------------------+
2 rows in set (0,01 sec)

慢一个(通过变量给出常数值)

+----+-------------+--------------------+--------+--------------------------------------+-------------------+---------+-----------------------------+--------+-------------------------------------------+
| id | select_type | table              | type   | possible_keys                        | key               | key_len | ref                         | rows   | Extra                                     |
+----+-------------+--------------------+--------+--------------------------------------+-------------------+---------+-----------------------------+--------+-------------------------------------------+
|  1 | SIMPLE      | tracking           | index  | unique-guest_user,FK4BBA1EB7AFA8C3E7 | unique-guest_user | 522     | NULL                        | 248724 | Using where; Using index; Using temporary |
|  1 | SIMPLE      | mn_connected_users | eq_ref | user_id                              | user_id           | 4       | 20131123_b.tracking.user_id |      1 | Using where                               |
+----+-------------+--------------------+--------+--------------------------------------+-------------------+---------+-----------------------------+--------+-------------------------------------------+
2 rows in set (0,00 sec)

解决方案:

我只声明为VARCHAR,但它不起作用。但我现在得到了解决方案。 CONVERT(使用latin1)用于解决问题。我在Where条件下给出了这个。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

解决方案:

我只声明为VARCHAR,但它不起作用。但我现在得到了解决方案。 CONVERT(使用latin1)用于解决问题。我在Where条件下给出了这个。感谢您的帮助 - user3080572