有两个数据库,AWS RDS和Digitalocean $ 5 VPS.我把同一个.sql文件导入到两个数据库中,它们之间的性能相差近66000%。
select count(special_cargo_id) from special_cargos;
AWS RDS耗时=35.681秒。
DO 5$ VPS on Mysql 经过时间=0.086秒
详细内容。
表长
1.85GB行数~26万
MySQL第8版
表引擎InnoDB
RDS输出
CREATE TABLE `special_cargos` (
`special_cargo_id` int(11) NOT NULL AUTO_INCREMENT,
`barcode` varchar(20) DEFAULT '',
`salesCode` varchar(15) NOT NULL,
`price` varchar(15) NOT NULL,
`para` varchar(3) DEFAULT NULL,
`postaceki` varchar(25) DEFAULT '',
`iban` varchar(30) DEFAULT NULL,
`send_type` tinyint(1) unsigned NOT NULL,
`customer_id` int(11) NOT NULL,
`address_id` int(11) NOT NULL,
`height` varchar(5) DEFAULT '',
`weight` varchar(5) DEFAULT '',
`width` varchar(5) NOT NULL DEFAULT '',
`desi` varchar(5) DEFAULT '',
`length` varchar(255) DEFAULT '',
`customer_firstname` varchar(32) NOT NULL,
`customer_lastname` varchar(32) NOT NULL,
`customer_company` varchar(40) NOT NULL,
`customer_address` varchar(255) NOT NULL,
`customer_city` int(11) NOT NULL DEFAULT '0',
`customer_city_name` varchar(55) DEFAULT '',
`customer_zone_id` int(11) NOT NULL DEFAULT '0',
`customer_zone_name` varchar(55) DEFAULT '',
`customer_postcode` varchar(10) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`company` varchar(40) DEFAULT '',
`address` varchar(255) NOT NULL,
`country_id` int(5) DEFAULT NULL,
`country_name` varchar(64) DEFAULT NULL,
`city_id` int(11) NOT NULL,
`city_name` varchar(55) NOT NULL,
`district_id` int(11) NOT NULL,
`district_name` varchar(64) NOT NULL,
`email` varchar(96) NOT NULL,
`phone` varchar(55) NOT NULL,
`postcode` varchar(10) DEFAULT '',
`product` text,
`sendText` text,
`returnText` text,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`cargo_firm` tinyint(1) NOT NULL DEFAULT '0',
`ups_barcode_link` text,
`status` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`special_cargo_id`),
KEY `customer_id` (`customer_id`),
KEY `special_cargo_id` (`special_cargo_id`),
KEY `barcode` (`barcode`),
KEY `salesCode` (`salesCode`),
KEY `price` (`price`),
KEY `postaceki` (`postaceki`),
KEY `customer_firstname` (`customer_firstname`),
KEY `customer_lastname` (`customer_lastname`),
KEY `firstname` (`firstname`),
KEY `lastname` (`lastname`),
KEY `customer_company` (`customer_company`),
KEY `customer_address` (`customer_address`),
KEY `customer_city_name` (`customer_city_name`),
KEY `customer_zone_name` (`customer_zone_name`),
KEY `company` (`company`),
KEY `address` (`address`),
KEY `city_name` (`city_name`),
KEY `district_name` (`district_name`),
KEY `email` (`email`),
KEY `phone` (`phone`),
KEY `postcode` (`postcode`),
KEY `index_special_cargos` (`special_cargo_id`)
) ENGINE=InnoDB AUTO_INCREMENT=497539 DEFAULT CHARSET=utf8
+-----------------------+----------+
|Variable_name |Value |
+-----------------------+----------+
|innodb_buffer_pool_size|1073741824|
+-----------------------+----------+
+--+-----------+--------------+----------+-----+-------------+-----------+-------+----+------+--------+-----------+
|id|select_type|table |partitions|type |possible_keys|key |key_len|ref |rows |filtered|Extra |
+--+-----------+--------------+----------+-----+-------------+-----------+-------+----+------+--------+-----------+
|1 |SIMPLE |special_cargos|NULL |index|NULL |customer_id|4 |NULL|146973|100 |Using index|
+--+-----------+--------------+----------+-----+-------------+-----------+-------+----+------+--------+-----------+
SIMPLE VPS ON MYSQL OUTPUT(简单的VPS)
CREATE TABLE `special_cargos` (
`special_cargo_id` int(11) NOT NULL AUTO_INCREMENT,
`barcode` varchar(20) NOT NULL,
`salesCode` varchar(15) NOT NULL,
`price` varchar(15) NOT NULL,
`para` varchar(3) DEFAULT NULL,
`postaceki` varchar(25) NOT NULL,
`iban` varchar(30) DEFAULT NULL,
`send_type` tinyint(1) unsigned NOT NULL,
`customer_id` int(11) NOT NULL,
`address_id` int(11) NOT NULL,
`height` varchar(5) NOT NULL,
`weight` varchar(5) NOT NULL,
`width` varchar(5) NOT NULL,
`desi` varchar(5) NOT NULL,
`length` varchar(255) NOT NULL,
`customer_firstname` varchar(32) NOT NULL,
`customer_lastname` varchar(32) NOT NULL,
`customer_company` varchar(40) NOT NULL,
`customer_address` varchar(255) NOT NULL,
`customer_city` int(11) NOT NULL,
`customer_city_name` varchar(55) NOT NULL,
`customer_zone_id` int(11) NOT NULL,
`customer_zone_name` varchar(55) NOT NULL,
`customer_postcode` varchar(10) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`company` varchar(40) NOT NULL,
`address` varchar(255) NOT NULL,
`country_id` int(5) DEFAULT NULL,
`country_name` varchar(64) DEFAULT NULL,
`city_id` int(11) NOT NULL,
`city_name` varchar(55) NOT NULL,
`district_id` int(11) NOT NULL,
`district_name` varchar(64) NOT NULL,
`email` varchar(96) NOT NULL,
`phone` varchar(55) NOT NULL,
`postcode` varchar(10) NOT NULL,
`product` text,
`sendText` text NOT NULL,
`returnText` text NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`deleted_at` datetime DEFAULT NULL,
`cargo_firm` int(1) NOT NULL DEFAULT '0',
`ups_barcode_link` text,
`status` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`special_cargo_id`),
KEY `customer_id` (`customer_id`),
KEY `special_cargo_id` (`special_cargo_id`),
KEY `barcode` (`barcode`),
KEY `salesCode` (`salesCode`),
KEY `price` (`price`),
KEY `postaceki` (`postaceki`),
KEY `customer_firstname` (`customer_firstname`),
KEY `customer_lastname` (`customer_lastname`),
KEY `firstname` (`firstname`),
KEY `lastname` (`lastname`),
KEY `customer_company` (`customer_company`),
KEY `customer_address` (`customer_address`),
KEY `customer_city_name` (`customer_city_name`),
KEY `customer_zone_name` (`customer_zone_name`),
KEY `company` (`company`),
KEY `address` (`address`),
KEY `city_name` (`city_name`),
KEY `district_name` (`district_name`),
KEY `email` (`email`),
KEY `phone` (`phone`),
KEY `postcode` (`postcode`)
) ENGINE=InnoDB AUTO_INCREMENT=497037 DEFAULT CHARSET=utf8
+-----------------------+---------+
|Variable_name |Value |
+-----------------------+---------+
|innodb_buffer_pool_size|134217728|
+-----------------------+---------+
+--+-----------+--------------+----------+-----+-------------+-----------+-------+----+------+--------+-----------+
|id|select_type|table |partitions|type |possible_keys|key |key_len|ref |rows |filtered|Extra |
+--+-----------+--------------+----------+-----+-------------+-----------+-------+----+------+--------+-----------+
|1 |SIMPLE |special_cargos|NULL |index|NULL |customer_id|4 |NULL|133967|100 |Using index|
+--+-----------+--------------+----------+-----+-------------+-----------+-------+----+------+--------+-----------+
有两个方面的问题在这里.方面一,更重要的IMO是查询 - 它的坏。它做了全索引扫描(是的,primary也是一个索引)。它的性能与更好的和想要的B+tree的nlog(n)线性缩放。
方面二。为什么执行时间会有差异。这可能与缓存有关(在DO的情况下,查询是从缓冲池服务的,而在RDS中是从磁盘(=EBS)服务的)。这可能与MySQL优化器有关(DO和RDS可以选择不同的索引来服务查询primary与较小的二级索引)。但由于查询本身的问题,这些都不太重要。
你能不能提供RDS和你的非RDS上的输出。
SHOW CREATE TABLE special_cargos;
SHOW GLOBAL VARIABLES LIKE 'innodb_buffer_pool_size';
EXPLAIN select count(special_cargo_id) from special_cargos;
在RDS和非RDS实例上的输出?
假设 special_cargo_id
是主键,而且它适合放在RAM中,而且innodb_buffer_pool_size的配置也很合理(在一个有2GB RAM的服务器上,1GB是一个合理的数量,尽管RDS应该预先配置到一个合理的值),它应该在两个节点上运行得差不多。
我想到了两种可能的解释。
1) 在RDS上,你并不具备以下条件 special_cargo_id
定义为PK。
2)RDS由于某种原因,选择不使用该指数(比较的 EXPLAIN
输出将确认是否发生这种情况)。)
编辑。
其实 - 你是 肯定的 你的5美元VPS使用的是InnoDB?如果你在0.08s内就得到了回复,那听起来就像它实际上连索引都没有扫描。很有可能你在VPS上使用的是MyISAM,它在每次写入后都会更新表头的行数,特别是为了使得 SELECT COUNT(*) FROM table_name;
查询瞬间返回--就像你观察到的那样。
编辑2:
哇 - 所以VPS只配置了128MB的innodb_buffer_pool_size,它仍然比配置了1GB innodb_buffer_pool_size的RDS快,而且两者的执行计划是一样的?
OK,试试这个。
select count(1) from special_cargos FORCE INDEX (PRIMARY);
当你只有2GB内存的时候,1GB的缓冲区_池是很危险的。 可能是那个系统在疯狂地交换。
降低到200M,看看运行速度如何。
看看他们是否有监控交换使用情况的方法。