Mysql -----------------+--+
应用Wilson Hauck提供的修复程序后更新了解释。查询速度降至~0.005秒强>Mysql -----------------+--+,mysql,Mysql,应用Wilson Hauck提供的修复程序后更新了解释。查询速度降至~0.005秒 | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |----|-------------|----------------|-------|---------------|---
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|-------------|----------------|-------|---------------|---------------|---------|------------------|------|-----------------------------|
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 966 | Using where; Using filesort |
| 1 | PRIMARY | t1 | ref | activity_date | activity_date | 5 | t2.activity_date | 1 | |
| 2 | DERIVED | wp_ap_activity | range | activity_q_id | activity_q_id | 8 | NULL | 966 | Using index condition |
| id |选择|类型|类型|可能的|键|键|列|参考|行|额外|
|----|-------------|----------------|-------|---------------|---------------|---------|------------------|------|-----------------------------|
|1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | NULL | 966 |使用where;使用文件排序|
|1 |主要| t1 |参考|活动|日期|活动|日期| 5 | t2.活动|日期| 1 ||
|2 |派生| wp | U ap |活动|范围|活动| q | id |活动| q | id | 8 |空| 966 |使用索引条件|
你的解释
根本不可能完全相同,请看行尾。第二个查询的注释中有BNL,这意味着块嵌套循环处理(总是很慢)。必须避免
您需要在每个表的activity\u date
上建立索引
从MySQL命令提示符运行,显示wp\u ap\u活动的索引每个服务器上的代码>都将重建索引,使其成为当前索引
将查询更改为选择SQL\u NO\u CACHE………
进行测试,以避免使用查询缓存结果,并从每个查询的第二次和第三次执行中再次获取要比较的计时
请告诉我们您的结果。您是否做了解释?@user3783243否-这是一个WordPress应用程序,但问题与WP或PHP无关,我已更新了标记。请从两台服务器发布表的CREATE语句。使用SHOW CREATE TABLE wp\u ap\u activity
@Jay Blanchard我在上面添加了解释结果。这两个表的查询速度完全相同。@Pail Spiegel在上面添加了SHOW CREATE TABLE
结果。在activity_date上添加索引将查询速度从~6秒提高到~0.005秒,令人难以置信。非常感谢@Albion索引是您高速访问的门票。免责声明:我是我的个人资料、网络个人资料中提到的网站的内容作者,在那里我们有免费的实用程序脚本下载,以帮助进行性能调整。请访问我们。
CREATE TABLE `wp_ap_activity` (
`activity_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`activity_action` varchar(45) NOT NULL,
`activity_q_id` bigint(20) unsigned NOT NULL,
`activity_a_id` bigint(20) unsigned DEFAULT NULL,
`activity_c_id` bigint(20) unsigned DEFAULT NULL,
`activity_user_id` bigint(20) unsigned NOT NULL,
`activity_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`activity_id`),
KEY `activity_q_id` (`activity_q_id`),
KEY `activity_a_id` (`activity_a_id`),
KEY `activity_user_id` (`activity_user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=113657 DEFAULT CHARSET=utf8mb4
+----+-------------+----------------+-------+---------------+---------------+---------+------+--------+-------------------------------------------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+----------------+-------+---------------+---------------+---------+------+--------+-------------------------------------------------+--+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 970 | Using temporary; Using filesort | |
+----+-------------+----------------+-------+---------------+---------------+---------+------+--------+-------------------------------------------------+--+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 109514 | Using where; Using join buffer (flat, BNL join) | |
+----+-------------+----------------+-------+---------------+---------------+---------+------+--------+-------------------------------------------------+--+
| 2 | DERIVED | wp_ap_activity | range | activity_q_id | activity_q_id | 8 | NULL | 970 | Using index condition | |
+----+-------------+----------------+-------+---------------+---------------+---------+------+--------+-------------------------------------------------+--+
+----+-------------+----------------+------------+-------+---------------+---------------+---------+---------------------------+-------+----------+----------------------------------------------+--+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | |
+----+-------------+----------------+------------+-------+---------------+---------------+---------+---------------------------+-------+----------+----------------------------------------------+--+
| 1 | PRIMARY | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 20270 | 100.00 | Using where; Using temporary; Using filesort | |
+----+-------------+----------------+------------+-------+---------------+---------------+---------+---------------------------+-------+----------+----------------------------------------------+--+
| 1 | PRIMARY | <derived2> | NULL | ref | <auto_key0> | <auto_key0> | 5 | helpdesk.t1.activity_date | 10 | 100.00 | Using index | |
+----+-------------+----------------+------------+-------+---------------+---------------+---------+---------------------------+-------+----------+----------------------------------------------+--+
| 2 | DERIVED | wp_ap_activity | NULL | range | activity_q_id | activity_q_id | 8 | NULL | 970 | 100.00 | Using index condition | |
+----+-------------+----------------+------------+-------+---------------+---------------+---------+---------------------------+-------+----------+----------------------------------------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|-------------|----------------|-------|---------------|---------------|---------|------------------|------|-----------------------------|
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 966 | Using where; Using filesort |
| 1 | PRIMARY | t1 | ref | activity_date | activity_date | 5 | t2.activity_date | 1 | |
| 2 | DERIVED | wp_ap_activity | range | activity_q_id | activity_q_id | 8 | NULL | 966 | Using index condition |