【发布时间】:2014-03-10 03:01:26
【问题描述】:
一般来说是 MySQL 和 SQL 的新手 - 所以请保持温和 :-)
我有一个行数非常多的表。表格是:
create table iostat (
pkey int not null auto_increment,
serverid int not null,
datestr char(15) default 'NULL',
esttime int not null default 0,
rs float not null default 0.0,
ws float not null default 0.0,
krs float not null default 0.0,
kws float not null default 0.0,
wait float not null default 0.0,
actv float not null default 0.0,
wsvct float not null default 0.0,
asvct float not null default 0.0,
pctw int not null default 0,
pctb int not null default 0,
device varchar(50),
avgread float not null default 0.0,
avgwrit float not null default 0.0,
primary key (pkey),
index i_serverid (serverid),
index i_esttime (esttime),
index i_datestr (datestr),
index i_rs (rs),
index i_ws (ws),
index i_krs (krs),
index i_kws (kws),
index i_wait (wait),
index i_actv (actv),
index i_wsvct (wsvct),
index i_asvct (asvct),
index i_pctb (pctb),
index i_device (device),
index i_servdate (serverid, datestr),
index i_servest (serverid, esttime)
)
engine = MyISAM
data directory = '${IOSTATdatadir}'
index directory = '${IOSTATindexdir}'
;
目前该表有 834,317,203 行。
是的 - 我需要所有数据。数据的最高级别组织是按收集日期 (datestr)。它是一个 CHAR 而不是日期,以保留我用于各种加载、提取和分析脚本的特定日期格式。
每天增加大约 16,000,000 行。
我想加快的操作之一是(限制通常为 50,但范围从 10 到 250):
create table TMP_TopLUNsKRead
select
krs, device, datestr, esttime
from
iostat
where
${WHERECLAUSE}
order by
krs desc limit ${Limit};
其中:
serverid = 29 and esttime between X and Y and device like '%t%'
其中 X 和 Y 是从 4 分钟到 24 小时的时间戳。
我宁愿不更改数据库引擎。这让我可以将数据和索引放在单独的驱动器上,这给了我显着的整体性能。它也是总共 16 亿行,重新加载将花费大量时间。
【问题讨论】:
-
如果将 EXPLAIN 添加到此查询的开头,您会得到什么结果?
-
在您的查询中对性能影响最大的元素是
${WHERECLAUSE},向我们展示该变量的设置会很有用。 -
我知道我会忘记这一点:serverid = 29 和 X 和 Y 之间的 esttime 以及像 '%t%' 这样的设备——X 和 Y 是从 4 分钟到 24 小时不等的时间戳.
-
|编号 |选择类型 |表|类型 |可能的键 |关键 | key_len |参考 |行 |额外 | +----+-------------+--------+--------+------------- ------------------+-------+------------+- -----+--------+-------------+ | 1 |简单 | iostat |索引 | i_serverid,i_esttime,i_servdate,i_serve | i_krs | 4 |空 | 69421 |使用位置 | +----+-------------+--------+--------+------------- ------------------+-------+------------+- -----+--------+-------------+
标签: mysql query-performance large-data-volumes