【发布时间】:2017-01-01 23:02:56
【问题描述】:
我是数据库查询优化的新手。 这是创建表查询:
CREATE TABLE mo (
id int UNSIGNED NOT NULL auto_increment,
msisdn varchar(20) NOT NULL,
operatorid int UNSIGNED NOT NULL,
shortcodeid int UNSIGNED NOT NULL,
text varchar(100) NOT NULL,
auth_token varchar(60) NOT NULL,
created_at DATETIME,
PRIMARY KEY(id)
);
我的查询是这样的:
SELECT count(id) as mo_count from mo where created_at > DATE_SUB(NOW(), INTERVAL 15 MINUTE)
当我测试它时,结果是
Time taken for tests: 3.50 seconds
[0] => Array
(
[id] => 1
[select_type] => SIMPLE
[table] => mo
[type] => ALL
[possible_keys] =>
[key] =>
[key_len] =>
[ref] =>
[rows] => 10000255
[Extra] => Using where
)
请教我如何优化这个查询。非常感谢。
【问题讨论】:
-
如果此表继续增长,并且您经常查询计数,您可能会对新设计感兴趣。例如,您可以将最后的记录保存在一个非常小的“始终新鲜”表中。同时,将有一个包含所有记录的“历史”表。您还可以按特定时间段(天?)对表进行摆弄和分区,这样可以限制每个计数查询的 IO。