数据库
首页 > 数据库> > 如何优化此MySql查询-联接3个表?

如何优化此MySql查询-联接3个表?

作者:互联网

该查询非常慢.这非常简单,并且在JOIN和WHERE子句中的所有列上索引了所使用的3个表.如何优化查询或该查询的表?

这是慢查询.运行需要15到20秒.

 SELECT
    user.id,
    user.name,
    user.key,
    user.secret,
    account.id,
    account.name,
    account.admin,
    setting.attribute,
    setting.value
  FROM        user
  INNER JOIN  account ON account.id       = user.account_id
  INNER JOIN  setting ON setting.user_id  = user.id
    AND setting.deleted = 0
  WHERE user.deleted = 0

问题很可能是由于设置表上的联接引起的,因为下面的两个查询总共花费了大约5秒钟.虽然,5秒似乎还有些长?

  SELECT
    user.id,
    user.name,
    user.user_key,
    user.secret,
    account.id,
    account.name,
    account.admin
  FROM        user
  INNER JOIN  account ON account.user_id = user.id
  WHERE user.deleted = 0

  SELECT
    setting.user_id,
    setting.attribute,
    setting.value
  FROM setting
  WHERE setting.deleted = 0

慢查询的解释:

id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra

1, 'SIMPLE', 'user', 'ALL', 'PRIMARY,idx_id,idx_deleted', null, null, null, 600, 'Using where'
1, 'SIMPLE', 'account', 'eq_ref', 'PRIMARY', 'PRIMARY', '8', 'user.account_id', 1, null
1, 'SIMPLE', 'setting', 'ref', 'attribute_version_unique,idx_user_id,indx_deleted', 'attribute_version_unique', '8', 'user.id', 35, 'Using where'

模式:

CREATE TABLE user
(
    id BIGINT(20) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
    name VARCHAR(45) NOT NULL,
    user_key VARCHAR(45) NOT NULL,
    secret VARCHAR(16),
    account_id BIGINT(20) unsigned NOT NULL,
    name VARCHAR(40) NOT NULL,
    demo TINYINT(1) DEFAULT '0' NOT NULL,
    details VARCHAR(4000),
    date_created DATETIME NOT NULL,
    date_modified DATETIME NOT NULL,
    deleted TINYINT(1) DEFAULT '0' NOT NULL
);
CREATE INDEX idx_date_modified ON user (date_modified);
CREATE INDEX idx_deleted ON user (deleted);
CREATE INDEX idx_id ON pub_application (id);
CREATE UNIQUE INDEX idx_name_unique ON user (user_key);
CREATE TABLE account
(
    id BIGINT(20) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    display_name VARCHAR(100),
    admin TINYINT(1) DEFAULT '0' NOT NULL,
    visibility VARCHAR(15) DEFAULT 'public',
    cost DOUBLE,
    monthly_fee VARCHAR(300),
    date_created DATETIME NOT NULL,
    date_modified DATETIME NOT NULL,
    deleted TINYINT(1) DEFAULT '0'
);
CREATE INDEX idx_date_modified ON account (date_modified);
CREATE TABLE setting
(
    id BIGINT(20) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
    user_id BIGINT(20) unsigned NOT NULL,
    attribute VARCHAR(45) NOT NULL,
    value VARCHAR(4000),
    date_created DATETIME NOT NULL,
    date_modified DATETIME NOT NULL,
    deleted TINYINT(1) DEFAULT '0' NOT NULL
);
CREATE UNIQUE INDEX attribute_version_unique ON setting (user_id, attribute);
CREATE INDEX idx_user_id ON setting (user_id);
CREATE INDEX idx_date_modified ON setting (date_modified);
CREATE INDEX indx_deleted ON setting (deleted);

解决方法:

尊重您,您偶然发现了一个常见的反模式.索引“所有列”通常是无用的举动. MySQL(截至2016年底)在满足查询条件时,每个表最多可以利用一个索引.因此,额外的索引可能无助于查询,并且肯定会增加INSERT和UPDATE操作的开销.

可以通过一些专门设计的复合覆盖索引来改进此查询.

在您的用户表上尝试该索引.它是一个覆盖索引:旨在包含满足查询所需的所有列.它的组织顺序与您的WHERE子句相匹配.

CREATE INDEX idx_user_account_setting 
          ON user (deleted , account_id, id, name, key, secret);

该覆盖索引可能会对您的设置表有所帮助

CREATE INDEX idx_setting_user 
          ON setting (user_id, deleted , attribute, value);

如果第一列无济于事,请尝试使用此列,切换前两列的顺序.

CREATE INDEX idx_setting_user_alt
          ON setting (deleted, user_id, attribute, value);

最后,尝试使用此帐户.

CREATE INDEX idx_account_user 
          ON account (id, name, admin);

如果这些建议有帮助,请留下简短的评论,告诉他们有多少帮助.

读这个. http://use-the-index-luke.com/

标签:query-performance,mysql
来源: https://codeday.me/bug/20191112/2023655.html