0

我有一个带有两列的 MySQL 数据库。“钥匙”和“二手”。Key 是一个字符串,Used 是一个整数。有没有一种非常快速的方法来搜索特定的键,然后在一个包含 6000000 行数据的巨大 MySQL 数据库中返回使用。

4

2 回答 2

2

key您可以通过在字段上创建索引来加快速度:

CREATE INDEX mytable_key_idx ON mytable (`key`);

实际上,您可以通过在两个(key, used)字段上创建覆盖索引来加快阅读速度:

CREATE INDEX mytable_key_used_idx ON mytable (`key`, `used`);

在这种情况下,当读取时,MySQL 可以used从索引本身中检索值,而无需读取表(仅索引扫描)。但是,如果您有很多写入活动,覆盖索引可能会运行得更慢,因为现在它必须同时更新索引和实际表。

于 2013-07-13T04:44:07.373 回答
0

The normative SQL for that would be:

SELECT t.key, t.used FROM mytable t WHERE t.key = 'particularvalue' ; 

The output from

EXPLAIN 
SELECT t.key, t.used FROM mytable t WHERE t.key = 'particularvalue' ; 

Would give details about the access plan, what indexes are being considered, etc.

The output from a

SHOW CREATE TABLE mytable ; 

would give information about the table, the engine being used and the available indexes, as well as the datatypes.

Slow performance on a query like this is usually indicative of a suboptimal access plan, either because suitable indexes are not available, or not being used. Sometimes, a characterset mismatch between the column datatype and the literal datatype in the predicate can make an index "unusable" by a particular query.

于 2013-07-13T04:55:06.550 回答