本帖最后由 马天艺 于 2023-7-20 10:18 编辑 mysql8的select count(*) 反而慢了,之前字符集是utf8mb3 更换为utf8mb4 也是一样的 Table: scanrecord Create Table: CREATE TABLE `scanrecord` ( `SCANRECORD_ID` varchar(20) CHARACTER SET utf8mb3 NOT NULL, `SN` varchar(150) CHARACTER SET utf8mb3 NOT NULL, `PRODUCTTYPE` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `MODEL` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `CREATETIME` datetime NOT NULL, `LASTUPDATE` datetime NOT NULL, `QUANTITY` int DEFAULT '1', `CUSTOMER_ID` varchar(20) CHARACTER SET utf8mb3 NOT NULL, `OUTERMODEL` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `STATUS` int DEFAULT '1', `MSG` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `destination_country` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `integral_open` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `integral_role_id` varchar(20) CHARACTER SET utf8mb3 DEFAULT '' COMMENT '绉垎瑙勫垯琛↖D', `retry_flag` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `retry_date` datetime DEFAULT NULL COMMENT 'Retry鏃堕棿', `user_id` varchar(20) CHARACTER SET utf8mb3 DEFAULT '' COMMENT '鐢ㄦ埛ID', `code` int DEFAULT '1' COMMENT '鎵爜缁撴灉鐘舵€?1:鎴愬姛', `sale_region_name` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `sn_data` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `transactionas_last_date` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `transactionas_date` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `integral_rule_id` bigint DEFAULT NULL COMMENT '绉垎瑙勫垯琛↖D', `integral_rule_scancode_id` bigint DEFAULT NULL COMMENT '鎵爜绉垎瑙勫垯琛↖D', `integral` int DEFAULT NULL COMMENT '绉垎鏁?, `dup_scan` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `bus_country_code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `promotion_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `present_integral` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `activity_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `language_field` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, PRIMARY KEY (`SCANRECORD_ID`), KEY `CUSTOMER_ID` (`CUSTOMER_ID`), KEY `scanrecord_ibfk_2` (`user_id`), KEY `integral_role_id` (`integral_role_id`), KEY `CREATETIME` (`CREATETIME`) USING BTREE, KEY `idx_SN` (`SN`,`STATUS`), KEY `idx_scanrecord_sn` (`SN`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec) #mysql8.0 上的执行 root@mysqldb 22:19: [gugauat]> select count(*) from scanrecord; +----------+ | count(*) | +----------+ | 27093343 | +----------+ 1 row in set (33.46 sec) root@mysqldb 22:21: [gugauat]> root@mysqldb 22:21: [gugauat]> select count(*) from scanrecord; +----------+ | count(*) | +----------+ | 27093343 | +----------+ 1 row in set (33.49 sec) root@mysqldb 22:22: [gugauat]> explain format=json select count(*) from scanrecord; { "query_block": { "select_id": 1, "cost_info": { "query_cost": "3293518.66" }, "table": { "table_name": "scanrecord", "access_type": "index", "key": "CREATETIME", "used_key_parts": [ "CREATETIME" ], "key_length": "5", "rows_examined_per_scan": 23081395, "rows_produced_per_join": 23081395, "filtered": "100.00", "using_index": true, "cost_info": { "read_cost": "985379.16", "eval_cost": "2308139.50", "prefix_cost": "3293518.66", "data_read_per_join": "560G" } } } } | 1 row in set, 1 warning (0.00 sec) root@mysqldb 22:22: [gugauat]> select @@version -> ; +-----------+ | @@version | +-----------+ | 8.0.34 | +-----------+ 1 row in set (0.00 sec) root@mysqldb 22:23: [gugauat]> #####mysql5.7上的执行 mysql> select count(*) from scanrecord; +----------+ | count(*) | +----------+ | 27270612 | +----------+ 1 row in set (10.41 sec) mysql> select count(*) from scanrecord; +----------+ | count(*) | +----------+ | 27270626 | +----------+ 1 row in set (3.75 sec) mysql> explain format=json select count(*) from scanrecord; { "query_block": { "select_id": 1, "cost_info": { "query_cost": "4314379.40" }, "table": { "table_name": "scanrecord", "access_type": "index", "key": "CREATETIME", "used_key_parts": [ "CREATETIME" ], "key_length": "5", "rows_examined_per_scan": 17408057, "rows_produced_per_join": 17408055, "filtered": "100.00", "using_index": true, "cost_info": { "read_cost": "832768.00", "eval_cost": "3481611.19", "prefix_cost": "4314379.40", "data_read_per_join": "320G" } } } } | 1 row in set, 1 warning (0.00 sec) mysql> select @@version; +------------+ | @@version | +------------+ | 5.7.25-log | +------------+ 1 row in set (0.00 sec) |
yejr
2023-7-20 13:37:53
| ||
马天艺
2023-7-26 15:20:25
| ||
yejr
2023-7-26 22:08:01
| ||
马天艺
2023-7-27 10:08:27
| ||
yejr
2023-8-4 14:24:31
| ||
fander
2023-8-4 17:56:07
| |
yejr
2023-8-6 15:59:28
| ||
合作电话:010-64087828
社区邮箱:greatsql@greatdb.com