she*_*don 11 mysql myisam mysql-5.5
我有一个大约 100 个表的数据库来存储各种信息。
最重要的表是我们的订单表,用于存储客户订单,截至目前已超过 100000 条记录,并且还在不断增长。
该表是我们数据库中查询最多的表,用于获取实时订单仪表板、统计数据、分析等所需的各个部分信息。
我定期监视数据库,并在数据库上启用慢速查询以跟踪问题。
我每天都使用像 mysqltuner 这样的脚本来输出查询。
我还使用 mysqlsla 收集有关我们数据库中前 10 个最慢查询的信息。
sample stat
Count : 11.48k (30.66%)
Time : 19.623758 s total, 1.709 ms avg, 239 µs to 2.475017 s max (18.64%)
95% of Time : 5.246833 s total, 481 µs avg, 239 µs to 1.095 ms max
Lock Time (s) : 14.460071 s total, 1.259 ms avg, 53 µs to 2.462555 s max (41.38%)
95% of Lock : 806.43 ms total, 74 µs avg, 53 µs to 137 µs max
Rows sent : 1 avg, 0 to 9 max (0.99%)
Rows examined : 6 avg, 1 to 28 max (0.15%)
Run Code Online (Sandbox Code Playgroud)
大多数最慢的查询都涉及到上面提到的订单表。我使用 MyISAM 作为我的存储引擎,所以可能的问题可能是:
我如何改进这些统计数据,我为这些表建立了索引,并不断调整它们以改进读取查询。
表模式
`orderid` int(11) NOT NULL AUTO_INCREMENT,
`cityid` tinyint(3) unsigned NOT NULL DEFAULT '1',
`model_type` tinyint(1) unsigned DEFAULT '1',
`userid` int(11) DEFAULT NULL,
`usertype` char(1) DEFAULT NULL,
`time` time DEFAULT NULL,
`ordercode` char(8) DEFAULT NULL,
`restid` smallint(3) unsigned NOT NULL,
`areaid` smallint(3) unsigned DEFAULT NULL,
`restname` varchar(50) DEFAULT NULL,
`date` date NOT NULL,
`del_time` time NOT NULL,
`status` tinyint(3) unsigned NOT NULL,
`amount` float NOT NULL,
`deliverycharge` smallint(4) unsigned DEFAULT '0',
`tax` float NOT NULL,
`total` float NOT NULL,
`extras` varchar(255) DEFAULT NULL,
`requests` varchar(255) DEFAULT NULL,
`discount` float DEFAULT NULL,
`rdiscount` float DEFAULT NULL,
`reason` varchar(255) DEFAULT NULL,
`rest_order` tinyint(1) unsigned DEFAULT NULL,
`admin_user` varchar(25) DEFAULT NULL,
`mode` char(1) NOT NULL,
`priority_order` tinyint(1) unsigned DEFAULT '0',
`payment_mode` tinyint(1) unsigned DEFAULT '0',
`km` tinyint(3) unsigned DEFAULT NULL,
`order_type` tinyint(1) NOT NULL DEFAULT '1',
`coupon_discount` smallint(3) DEFAULT '0',
`pickup_time` time NOT NULL,
PRIMARY KEY (`orderid`),
KEY `cityid` (`cityid`),
KEY `date_3` (`date`,`status`,`mode`),
KEY `orderid` (`orderid`),
KEY `time` (`time`),
KEY `userid` (`userid`,`usertype`),
KEY `restid` (`restid`,`date`,`status`)
Run Code Online (Sandbox Code Playgroud)
慢日志查询
SELECT `a`.`orderid`, `a`.`date`, `a`.`status`, `a`.`restname`, `a`.`admin_user`, `a`.`model_type`, `b`.`name` as cityname
FROM `tk_order_queue` AS a
INNER JOIN `tk_cities` AS b ON `a`.`cityid` = `b`.`id`
WHERE `a`.`date` = '2012-06-30'
AND `a`.`status` = 0
AND `a`.`mode` = 1
ORDER BY `a`.`orderid` desc;
Run Code Online (Sandbox Code Playgroud)
您必须比较所有查询的 WHERE 子句和 GROUP BY 和 ORDER BY 语句,以确保您当前的索引可以在其 EXPLAIN 计划中支持它们。
昨天,我回答了这个问题:InnoDB vs MyISAM with many index
在那个问题中,我建议对 MyISAM 表做一些你也可以做的事情
ALTER TABLE orders ROW_FORMAT=Fixed;
Run Code Online (Sandbox Code Playgroud)
这会将所有 VARCHAR 视为 CHAR。每行的长度将完全相同。这将增加 80%-100% 的磁盘空间。您的表格将膨胀到行布局乘以行数的最大大小。你的桌子可以扩大一倍或三倍。
好处在哪里?然后,您的 MyISAM 表的读取/写入速度将提高 20% - 30%,而无需更改任何其他内容。
我从MySQL Database Design and Tuning 的第 72,73 页了解到这一点。
我以前写过关于这个的文章:
归档时间: |
|
查看次数: |
10002 次 |
最近记录: |