mysql对索引列的查询速度慢

时间:2013-07-31 02:10:06

标签: mysql

对索引列运行简单查询,但它需要超过500毫秒。这些查询经常运行,因此它会大大影响性能。

表中只有不到1M行,这是一个非常简单的表。使用MyISAM。我不明白为什么它会检查所有行,似乎它忽略了索引!我尝试在字段上添加第二个索引,一个普通索引而不是唯一索引,没有任何区别。谢谢你的期待。

# Time: 130730 22:00:07
# User@Host: engine[engine] @ engine [10.0.0.6]
# Query_time: 0.511209  Lock_time: 0.000050 Rows_sent: 1  Rows_examined: 932048
SET timestamp=1375236007;
SELECT * FROM `marketplacesales`.`sales_order` WHERE `marketplace_orderid` = 823123693003;

+---------------------+-----------------------+------+-----+---------+----------------+
| Field               | Type                  | Null | Key | Default | Extra          |
+---------------------+-----------------------+------+-----+---------+----------------+
| id                  | mediumint(8) unsigned | NO   | PRI | NULL    | auto_increment |
| marketplace_id      | smallint(5) unsigned  | NO   | MUL | NULL    |                |
| marketplace_orderid | varchar(255)          | NO   | UNI | NULL    |                |
| datetime            | datetime              | NO   |     | NULL    |                |
| added               | datetime              | NO   | MUL | NULL    |                |
| phone               | varchar(255)          | YES  |     | NULL    |                |
| email               | varchar(255)          | YES  |     | NULL    |                |
| company             | varchar(255)          | NO   |     | NULL    |                |
| name                | varchar(255)          | NO   |     | NULL    |                |
| address1            | varchar(255)          | NO   |     | NULL    |                |
| address2            | varchar(255)          | NO   |     | NULL    |                |
| city                | varchar(255)          | NO   |     | NULL    |                |
| state               | varchar(255)          | NO   |     | NULL    |                |
| zip                 | varchar(255)          | NO   |     | NULL    |                |
| country             | varchar(255)          | NO   |     | NULL    |                |
+---------------------+-----------------------+------+-----+---------+----------------+

EXPLAIN SELECT * FROM `marketplacesales`.`sales_order` WHERE `marketplace_orderid` = 823123693003;
+----+-------------+-------------+------+-------------------------------------------+------+---------+------+--------+-------------+
| id | select_type | table       | type | possible_keys                             | key  | key_len | ref  | rows   | Extra       |
+----+-------------+-------------+------+-------------------------------------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | sales_order | ALL  | marketplace_orderid,marketplace_orderid_2 | NULL | NULL    | NULL | 932053 | Using where |
+----+-------------+-------------+------+-------------------------------------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)

2 个答案:

答案 0 :(得分:5)

您需要使用说明来查看正在发生的事情。我的猜测是索引正在使用。

where子句是:

WHERE `marketplace_orderid` = 823123693003;

正如here所述,转换将作为浮点数进行。这需要在marketplace_orderid上进行转换。

修复表中的字段,使其为数字。或者,将值放在where子句中的引号中:

WHERE `marketplace_orderid` = '823123693003';

引号的问题是实际值可能有前导零,这会导致匹配失败。

答案 1 :(得分:0)

尝试使用:

SELECT * FROM `marketplacesales`.`sales_order` WHERE `marketplace_orderid LIKE '823123693003%'

这应该强制使用索引。

另外,如果可能的话,我会考虑将字段更改为varchar的bigint intead