3

在索引列上运行一个简单的查询,但它需要超过 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)
4

2 回答 2

6

您需要使用 explain 来查看发生了什么。我的猜测是索引没有被使用。

where条款是:

WHERE `marketplace_orderid` = 823123693003;

正如这里所解释的,转换将作为浮点数进行。这需要在marketplace_orderid.

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

WHERE `marketplace_orderid` = '823123693003';

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

于 2013-07-31T02:16:49.643 回答
0

尝试使用:

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

那应该强制使用索引。

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

于 2013-07-31T02:14:38.633 回答