1

私はその大きなデータベースを構築しています。私のテーブルの 1 つには 300K のレコードがあり、別のテーブルには 500 万のレコードがあります。現在、すべての外部キーと「ph.trigger_on」という名前の列にインデックスが付けられています。

私の質問は、テーブル/クエリを最適化してより高速な結果を得るにはどうすればよいですか? このコードでビューを作成しようとしましたが、このビューから、このビューに対してクエリを実行するときに必要なすべての情報を取得できます。

By the query はまだ遅く、EXPLAIN が示している結果を理解するのに苦労しています。これは私の現在のクエリです

EXPLAIN SELECT
ac.account_name AS accountName,
tm.name AS teamName,
cp.name AS campaignName,
cc.call_code_name AS callCode,
rc.result_code_name AS resultCode,
zn.name AS zoneName,
ind.name AS industry,
(su.first_name + su.middle_name + su.last_name) AS owner_name,
su.login_user AS ownerLoginUser,
(su1.first_name + su1.middle_name + su1.last_name) AS firstAttemptBy,
(su2.first_name + su2.middle_name + su2.last_name) AS lastAttemptBy,
(su3.first_name + su3.middle_name + su.last_name) AS modifiedBy,
ci.name AS clientName,
ph.trigger_on AS triggerOn,
ph.created_on AS createdOn,
ph.first_attempt_on AS firstAttemptOn,
ph.call_subject AS callSubject,
ph.status,
ph.last_attempt_on AS lastAttemptOn,
ph.total_attempts AS totalAttempts,
ph.call_direction AS callDirection,
ph.call_notes AS callNotes,
ph.call_duration AS callDuration,
ph.modified_on AS modifiedOn

FROM phone_calls AS ph
INNER JOIN accounts AS ac ON ph.account_id = ac.account_id
INNER JOIN clients AS ci ON ac.client_id = ci.client_id
INNER JOIN industries AS ind ON ac.industry_id = ind.industry_id
INNER JOIN call_codes AS cc ON ph.call_code_id = cc.call_code_id
INNER JOIN time_zones AS zn ON ph.time_zone_id = zn.time_zone_id
INNER JOIN users AS su ON ph.owner_id = su.user_id

LEFT  JOIN teams AS tm ON ph.team_id = tm.team_id
LEFT  JOIN result_codes AS rc ON ph.result_code_id = rc.result_code_id
LEFT  JOIN campaigns AS cp ON ph.campaign_id = cp.campaign_id
LEFT  JOIN users AS su1 ON ph.first_attempt_by = su1.user_id
LEFT  JOIN users AS su2 ON ph.last_attempt_by = su2.user_id
LEFT  JOIN users AS su3 ON ph.modified_by = su3.user_id
WHERE ph.trigger_on < now()
LIMIT 1000

これは私の現在の出力です。

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  ci  ALL PRIMARY             1   
1   SIMPLE  zn  ALL PRIMARY             1   Using join buffer (Block Nested Loop)
1   SIMPLE  su  ALL PRIMARY             1   Using join buffer (Block Nested Loop)
1   SIMPLE  ac  ref PRIMARY,client_id,industry_id   client_id   4   rdi_cms.ci.client_id    95917   
1   SIMPLE  ind eq_ref  PRIMARY PRIMARY 4   rdi_cms.ac.industry_id  1   
1   SIMPLE  ph  ref owner_id,call_code_id,account_id,time_zone_id,trigger_on    account_id  4   rdi_cms.ac.account_id   11  Using where
1   SIMPLE  tm  ALL PRIMARY             1   Using where; Using join buffer (Block Nested Loop)
1   SIMPLE  rc  eq_ref  PRIMARY PRIMARY 4   rdi_cms.ph.result_code_id   1   
1   SIMPLE  cc  eq_ref  PRIMARY PRIMARY 4   rdi_cms.ph.call_code_id 1   
1   SIMPLE  cp  ALL PRIMARY             1   Using where; Using join buffer (Block Nested Loop)
1   SIMPLE  su1 ALL PRIMARY             1   Using where; Using join buffer (Block Nested Loop)
1   SIMPLE  su2 ALL PRIMARY             1   Using where; Using join buffer (Block Nested Loop)
1   SIMPLE  su3 ALL PRIMARY             1   Using where; Using join buffer (Block Nested Loop)

テーブルまたはクエリを改善するにはどうすればよいですか。

4

1 に答える 1

2

次のように、クエリのSELECT部分​​のサブクエリに結合をプッシュすると、違いが生じる可能性があります。

SELECT
ac.account_name AS accountName,
tm.name AS teamName,
cp.name AS campaignName,
cc.call_code_name AS callCode,
rc.result_code_name AS resultCode,
(SELECT zn.name FROM time_zones AS zn WHERE ph.time_zone_id = zn.time_zone_id) AS zoneName,
(SELECT ind.name FROM industries AS ind WHERE ac.industry_id = ind.industry_id) AS industry,
(SELECT su.first_name + su.middle_name + su.last_name users AS su WHERE ph.owner_id = su.user_id) AS owner_name,
su.login_user AS ownerLoginUser,
(su1.first_name + su1.middle_name + su1.last_name) AS firstAttemptBy,
(su2.first_name + su2.middle_name + su2.last_name) AS lastAttemptBy,
(su3.first_name + su3.middle_name + su.last_name) AS modifiedBy,
ci.name AS clientName,
ph.trigger_on AS triggerOn,
ph.created_on AS createdOn,
ph.first_attempt_on AS firstAttemptOn,
ph.call_subject AS callSubject,
ph.status,
ph.last_attempt_on AS lastAttemptOn,
ph.total_attempts AS totalAttempts,
ph.call_direction AS callDirection,
ph.call_notes AS callNotes,
ph.call_duration AS callDuration,
ph.modified_on AS modifiedOn

FROM phone_calls AS ph
INNER JOIN accounts AS ac ON ph.account_id = ac.account_id
INNER JOIN clients AS ci ON ac.client_id = ci.client_id
INNER JOIN call_codes AS cc ON ph.call_code_id = cc.call_code_id
INNER JOIN time_zones AS zn ON ph.time_zone_id = zn.time_zone_id

LEFT  JOIN teams AS tm ON ph.team_id = tm.team_id
LEFT  JOIN result_codes AS rc ON ph.result_code_id = rc.result_code_id
LEFT  JOIN campaigns AS cp ON ph.campaign_id = cp.campaign_id
LEFT  JOIN users AS su1 ON ph.first_attempt_by = su1.user_id
LEFT  JOIN users AS su2 ON ph.last_attempt_by = su2.user_id
LEFT  JOIN users AS su3 ON ph.modified_by = su3.user_id
WHERE ph.trigger_on < now()
LIMIT 1000

ここで、3つの結合をSELECT部分​​にプッシュしました。

于 2013-03-18T21:02:22.307 に答える