ab11
ab11

Reputation: 20100

How to optimize this MySql query - joins 3 tables?

This query is very slow. It is pretty simple and the 3 tables used are indexed on all columns in JOIN and WHERE clauses. How can I optimize my query, or my tables for this query?

This is the slow query. It takes 15-20 seconds to run.

 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

It is likely issue is caused by join on the setting table, because the below two queries take about 5 seconds total. Although, 5 seconds still seems a little long?

  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

The explain for the slow query:

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'

The schema:

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);

Upvotes: 3

Views: 169

Answers (1)

O. Jones
O. Jones

Reputation: 108839

With respect, you've stumbled across a common antipattern. Indexing "all columns" ordinarily is a useless move. MySQL (as of late 2016) can exploit at most one index per table when satisfying a query. So the extra indexes are likely to help no queries, and definitely add overhead on INSERT and UPDATE operations.

This query might be improved by some purpose-designed compound covering indexes.

Try this index on your user table. It's a covering index: intended to contain all the columns necessary to satisfy the query. It's organized in an order that matches your WHERE clause.

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

This covering index might help on your setting table

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

Also try this one, switching the order of the first two columns, if the first one doesn't help.

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

Finally try this one on account.

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

Please, if these suggestions help leave a brief comment telling how much they helped.

Read this. http://use-the-index-luke.com/

Upvotes: 3

Related Questions