MySql - 我怎样才能加快这个查询

Ran*_*Ran 5 mysql performance index query query-performance

我有以下表格:

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `last_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `account_data` text COLLATE utf8_unicode_ci,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `twitter_username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `crypted_password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password_salt` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `persistence_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `single_access_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `perishable_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `login_count` int(11) NOT NULL DEFAULT '0',
  `failed_login_count` int(11) NOT NULL DEFAULT '0',
  `last_request_at` datetime DEFAULT NULL,
  `current_login_at` datetime DEFAULT NULL,
  `last_login_at` datetime DEFAULT NULL,
  `current_login_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `last_login_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `is_admin` tinyint(1) DEFAULT '0',
  `referrer_id` int(11) DEFAULT NULL,
  `partner` tinyint(1) DEFAULT '0',
  `subscription_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT 'free',
  `workflow_state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `persona_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `persona_index` (`persona_id`)
) ENGINE=InnoDB 
Run Code Online (Sandbox Code Playgroud)

和表:

CREATE TABLE `user_actions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `action_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `module` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `data` text COLLATE utf8_unicode_ci,
  `timestamp` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id_index` (`user_id`),
  KEY `action_type_index` (`action_type`),
  KEY `user_action_type_index` (`user_id`,`action_type`),
  KEY `timestamp_index` (`timestamp`),
  KEY `user_id_timestamp_index` (`user_id`,`timestamp`)
) ENGINE=InnoDB 
Run Code Online (Sandbox Code Playgroud)

问题在于以下查询:

    SELECT user_actions.*, users.twitter_username, users.email FROM `user_actions` 
INNER JOIN users ON (user_actions.user_id=users.id) ORDER BY timestamp DESC LIMIT 0, 30
Run Code Online (Sandbox Code Playgroud)

这是解释:

user_actions    
The table was retrieved with this index: user_id_timestamp_index
You can speed up this query by querying only fields that are within the index. Or you can create an index that includes every field in your query, including the primary key.
Approximately 76 rows of this table were scanned.
users   
This table was retrieved with a full table scan, which is often quite bad for performance, unless you only retrieve a few rows.
The table was retrieved with this index:
No index was used in this part of the query.
A temporary table was created to access this part of the query, which can cause poor performance. This typically happens if the query contains GROUP BY and ORDER BY clauses that list columns differently.
MySQL had to do an extra pass to retrieve the rows in sorted order, which is a cause of poor performance but sometimes unavoidable.
You can speed up this query by querying only fields that are within the index. Or you can create an index that includes every field in your query, including the primary key.
Approximately 3445 rows of this table were scanned.
Run Code Online (Sandbox Code Playgroud)

这个查询需要很长时间来执行,任何想法如何改进?

Rol*_*DBA 5

这是您的原始查询:

SELECT
    user_actions.*,
    users.twitter_username,
    users.email
FROM
    `user_actions`  
    INNER JOIN users
    ON (user_actions.user_id=users.id)
    ORDER BY timestamp
    DESC LIMIT 0, 30
;
Run Code Online (Sandbox Code Playgroud)

我注意到的第一件事是您正在连接两个完整的表。由于您只需要twitter_usernameemail来自users表,因此您应该只users使用三列进行连接:id,twitter_usernameemail

第二件事是LIMIT条款。加入后正在执行。您应该在加入之前执行它。在您的情况下,您正在请求 30 个最近的用户操作。如果您可以保证仅从 检索 30 行,则联接的user_actions运行速度应该会快得多。

如果您从 @DTest 阅读答案,他的前两个要点已经告诉您查询出了什么问题,因为 mysql 在从每个表收集数据时将采取的操作。关键是要了解处理查询时临时表的外观以及数据驻留的位置(内存或磁盘)。

您需要做的是重构查询以欺骗 MySQL 查询优化器。强制查询生成较小的临时表。在大多数情况下,my.cnf 中的配置更改应该会产生巨大的差异。在其他情况下,例如这种情况,重构查询可能就足够了。

这是我建议对您的查询进行的更改,它应该可以更快地工作:

SELECT
    ua.*,
    u.twitter_username,
    u.email
FROM
    (SELECT * FROM `user_actions`
    ORDER BY timestamp DESC LIMIT 30) ua
    LEFT JOIN
    (SELECT id,twitter_username,email FROM `users`) u
    ON (ua.user_id=u.id)
;
Run Code Online (Sandbox Code Playgroud)

以下是重构查询的原因:

原因#1

如果您查看内联表ua,我使用LIMIT. 无论user_actions桌子有多大,这都会发生。它已经被订购了,因为它ORDER BY timestamp DESC发生在LIMIT.

原因#2

如果你看内联表u,它有id, twitter_username, email。的id需要来实现加入。

原因#3

我使用LEFT JOIN而不是INNER JOIN出于两(2)个原因:

  1. 保留查询的顺序基于 ua
  2. 如果表中ua不再存在user_id ,则显示所有用户操作users

做这些事情会迫使临时表变小。尽管如此,您仍然需要实现@DTest 的回答中的第 3 项要点,以抢占临时表在磁盘上的位置。