【发布时间】:2017-05-02 10:52:15
【问题描述】:
我在 AWS 中有一个生产数据库,但是 localhost 中的同一个数据库要快得多(25 秒 VS 7 秒),做一些研究我发现同一个 SQL 中有一个差异:
EXPLAIN extended
SELECT *
FROM pipeline p
JOIN invoice i ON p.invoice_id = i.id
WHERE i.whenCreated BETWEEN "2017-01-01 00:00:00" AND "2017-01-31 00:00:00"
在 AWS =====> key_len = 8 和 Extra = 使用 where。
在 localhost ==> key_len = 5 和 Extra = 使用索引条件。
我之前在两个站点都跑过:
OPTIMIZE TABLE invoice;
可能是配置问题,但我迷路了。
更多信息:
AWS 中的 MySQL 版本:5.5.46
本地主机中的mysql版本:5.6.24
在 AWS 中解释:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: i
type: range
possible_keys: PRIMARY,IDX_5FD82ED84DD79520
key: IDX_5FD82ED84DD79520
key_len: 8
ref: NULL
rows: 847
filtered: 100.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: p
type: ref
possible_keys: UNIQ_848ABB8F2989F1FD
key: UNIQ_848ABB8F2989F1FD
key_len: 5
ref: ontrocrm.i.id
rows: 1
filtered: 100.00
Extra: Using where
在本地解释:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: i
type: range
possible_keys: PRIMARY,IDX_5FD82ED84DD79520
key: IDX_5FD82ED84DD79520
key_len: 5
ref: NULL
rows: 847
filtered: 100.00
Extra: Using index condition
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: p
type: ref
possible_keys: UNIQ_848ABB8F2989F1FD
key: UNIQ_848ABB8F2989F1FD
key_len: 5
ref: ontrocrm.i.id
rows: 1
filtered: 100.00
Extra: NULL
两者的创建表是一样的,一个是另一个的备份:
CREATE TABLE `invoice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`number` int(11) NOT NULL,
`whenCreated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_number` (`number`),
KEY `IDX_5FD82ED84DD79520` (`whenCreated`)
)
【问题讨论】:
标签: mysql amazon-web-services indexing explain