1

40,000,000行のテーブルがあり、時間がかかりすぎるため、クエリを最適化しようとしています。
まず、これは私のテーブルです:
CREATE TABLE resume
yearMonthchar(6)DEFAULT NULL、
typechar(1)DEFAULT NULL、
agen_ochar(5)DEFAULT NULL、
tarchar(2)DEFAULT NULL、
cve_entchar(1)DEFAULT NULL、
cve_munchar(3)DEFAULT NULL、
cve_regint(1)DEFAULT NULL、
id_opechar(1)DEFAULT NULL、
ope_tipchar(2)DEFAULT NULL、
ope_cvechar(3)DEFAULT NULL、
cob_uint(9)DEFAULT NULL、
tot_impbigint(15)DEFAULT NULL、
)ENGINE = MyISAM DEFAULT CHARSET = latin1;


これは私のクエリです:
SELECT m.name_ope AS cve、
SUBSTRING(r.yearMonth、5,2)AS period、
COUNT(DISTINCT(CONCAT(r.agen_ope、r.cve_ope)))AS num、
1 PRIMARY r ref indice indice 2const14774607使用場所; filesortの使用













そこで、yearMonthで別のインデックスope_cveを追加しましたが、まだ「usingfilesort」があります。どうすればこれを最適化できますか?
ありがとう

4

1 に答える 1

1

yearMonth にインデックスがある場合は、テーブル構造を変更せずに、これを試すことができます。

SELECT m.name_ope AS cve,
SUBSTRING(r.yearMonth,5,2) AS period,
COUNT(DISTINCT(CONCAT(r.agen_ope,r.cve_ope))) AS num,
SUM(CASE WHEN r.type='A' THEN r.cob_u ELSE 0 END) AS tot_u,
FROM resume r, media m
WHERE CONCAT(r.id_ope,SUBSTRING(r.ope_cve,3,1))=m.ope_cve AND
r.type IN ('C','D','E') AND
r.yearMonth LIKE '2012%' AND
r.id_ope='X' AND
SUBSTRING(r.ope_cve,1,2) IN (SELECT cve_med FROM catNac WHERE numero='0')
GROUP BY r.yearMonth,SUBSTRING(r.ope_cve,3,1)

変更:

  • Using を使用r.yearMonth LIKE '2012%'すると、where 句のその部分にインデックスを使用できるようになります。
  • 2012 年以外はすでに毎年除外しているため、GROUP BY r.yearMonth単独でグループ化できます。
  • を含めない限りORDER BY、MySQL は でソートされるため、句は必要ありません。GROUP BYORDER BY NULL
于 2012-10-17T20:01:56.130 に答える