2017-01-26 13 views
0

私は2つのmysqlサーバ(マスタとスレーブ)を持っています。私はSlaveでInnoDB圧縮を有効にしました。その後、mysqlはクエリで間違ったインデックスを選択することがあります。InnoDB圧縮後のMySQLのインデックスが間違っています

マスターに説明:

 

+----+-------------+-------+--------+---------------+---------+---------+-----------------------------+-----------+-------------+ 
| id | select_type | table | type | possible_keys | key  | key_len | ref       | rows  | Extra  | 
+----+-------------+-------+--------+---------------+---------+---------+-----------------------------+-----------+-------------+ 
| 1 | SIMPLE  | p  | range | PRIMARY  | PRIMARY | 8  | NULL      | 112017572 | Using where | 
| 1 | SIMPLE  | l  | eq_ref | PRIMARY  | PRIMARY | 8  |   p.loan_ID  |   1 | NULL  | 
| 1 | SIMPLE  | af | eq_ref | PRIMARY  | PRIMARY | 8  |   p.fromAccount_ID |   1 | Using where | 
| 1 | SIMPLE  | at | eq_ref | PRIMARY  | PRIMARY | 8  |   p.toAccount_ID |   1 | Using where | 
+----+-------------+-------+--------+---------------+---------+---------+-----------------------------+-----------+-------------+ 

をスレーブに説明:

 

+----+-------------+-------+--------+-------------------------------------------------------------------------------+--------------------+---------+-----------------------------+--------+----------------------------------------------+ 
| id | select_type | table | type | possible_keys                 | key    | key_len | ref       | rows | Extra          | 
+----+-------------+-------+--------+-------------------------------------------------------------------------------+--------------------+---------+-----------------------------+--------+----------------------------------------------+ 
| 1 | SIMPLE  | l  | index | PRIMARY                  | FK243910AAD869E6 | 9  | NULL      | 804876 | Using index; Using temporary; Using filesort | 
| 1 | SIMPLE  | p  | ref | PRIMARY,FK4BE7532292C5D482,FK4BE75322AE503A13,FK4BE75322382D11BC,POSTING_DATE | FK4BE75322382D11BC | 9  |   l.ID    | 101 | Using index condition; Using where   | 
| 1 | SIMPLE  | af | eq_ref | PRIMARY                  | PRIMARY   | 8  |   p.fromAccount_ID |  1 | Using where         | 
| 1 | SIMPLE  | at | eq_ref | PRIMARY                  | PRIMARY   | 8  |   p.toAccount_ID |  1 | Using where         | 
+----+-------------+-------+--------+-------------------------------------------------------------------------------+--------------------+---------+-----------------------------+--------+----------------------------------------------+ 
SELECT 
     p.ID AS 'payment_id', 
     p.loan_ID AS 'loan_id', 
     l.client_ID AS 'client_ID', 
     p.amount AS 'amount', 
     p.postingDate AS 'payment_date', 
      CASE 
       WHEN af.acc_type = 'POLCH' THEN 'wallet'  
       WHEN af.acc_type = 'PLTCH' THEN 'wallet'   
       WHEN af.acc_type = 'CNTT' THEN 'bank'   
       WHEN af.acc_type = 'CNT2' THEN 'bank'   
       WHEN af.acc_type = 'KONCH' THEN 'bank'   
       WHEN af.acc_type = 'KRDTM' THEN 'cash'   
       WHEN af.acc_type = 'LDRCH' THEN 'bank'   
       ELSE concat('UNKNOWN_',af.acc_type) 
       END AS 'payment_system_type', 
     af.description AS 'payment_system' 
     FROM Posting AS p 
     INNER JOIN Account AS af ON p.fromAccount_ID = af.ID 
     INNER JOIN Account AS at ON p.toAccount_ID = at.ID 
     INNER JOIN Loan AS l ON p.loan_id = l.ID 
     WHERE ( 
      af.acc_type = 'KONCH' 
      OR af.acc_type = 'PLTCH' 
      OR af.acc_type = 'POLCH' 
      OR af.acc_type = 'KRDTM' 
      OR af.acc_type = 'LDRCH' 
      OR af.acc_type = 'CNT2' 
      OR af.acc_type = 'CNTT') 
      AND at.acc_type = 'ABON' 
      AND p.postingDate < DATE(now()) 
      AND p.ID > 0 
ORDER BY p.ID LIMIT 10000; 

ローン - リットル

投稿 - P

マスター:

| Loan | CREATE TABLE `Loan` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
    `amount` decimal(19,4) DEFAULT NULL, 
    `amountToReturn` decimal(19,4) DEFAULT NULL, 
    `isGivenOut` bit(1) DEFAULT b'0', 
    `isPaid` bit(1) DEFAULT NULL, 
    `issueDate` datetime DEFAULT NULL, 
    `loanPeriod` int(11) DEFAULT NULL, 
    `productType` varchar(255) DEFAULT NULL, 
    `realPayDate` datetime DEFAULT NULL, 
    `client_ID` bigint(20) DEFAULT NULL, 
    `product_ID` bigint(20) DEFAULT NULL, 
    `givenOutDate` datetime DEFAULT NULL, 
    `isPaidByBank` bit(1) DEFAULT NULL, 
    `accountNumberNBKI` varchar(255) DEFAULT NULL, 
    `needManualProcessing` bit(1) DEFAULT NULL, 
    `isReverted` bit(1) DEFAULT b'0', 
    `showInNBCHReport` bit(1) DEFAULT b'1', 
    `stake` decimal(19,5) DEFAULT NULL, 
    `ignoreProlongation` bit(1) DEFAULT b'0', 
    `stakeAfter21` decimal(19,5) DEFAULT NULL, 
    `discount_id` bigint(20) DEFAULT NULL, 
    `showInEquifaxReport` bit(1) DEFAULT b'1', 
    `ignoreNbch` bit(1) DEFAULT b'0', 
    PRIMARY KEY (`ID`), 
    KEY `FK2439106EC0BA18` (`product_ID`), 
    KEY `ISPAID_INDEX` (`isPaid`) USING BTREE, 
    KEY `ISP_ISGOUT_INDEX` (`isPaid`,`isGivenOut`), 
    KEY `ISSUEDATE_INDEX` (`issueDate`), 
    KEY `FK243910735827C6` (`discount_id`), 
    KEY `idx_Loan_realPayDate` (`realPayDate`), 
    KEY `idx_Loan_givenOutDate` (`givenOutDate`), 
    KEY `FK243910AAD869E6` (`client_ID`), 
    CONSTRAINT `_FK243910735827C6` FOREIGN KEY (`discount_id`) REFERENCES `Discount` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2000623399 DEFAULT CHARSET=utf8 

Posting | CREATE TABLE `Posting` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
    `amount` decimal(19,4) DEFAULT NULL, 
    `postingDate` datetime DEFAULT NULL, 
    `fromAccount_ID` bigint(20) DEFAULT NULL, 
    `loan_ID` bigint(20) DEFAULT NULL, 
    `toAccount_ID` bigint(20) DEFAULT NULL, 
    `sourceType` varchar(255) DEFAULT NULL, 
    PRIMARY KEY (`ID`), 
    KEY `FK4BE7532292C5D482` (`fromAccount_ID`), 
    KEY `FK4BE75322AE503A13` (`toAccount_ID`), 
    KEY `FK4BE75322382D11BC` (`loan_ID`), 
    KEY `POSTING_DATE` (`postingDate`) USING BTREE 
) ENGINE=InnoDB AUTO_INCREMENT=230996702 DEFAULT CHARSET=utf8 

スレーブ:私は を追加する場合、クエリ

 
+----+-------------+-------+--------+------------------------------------------------------------+---------+---------+-----------------------------+-----------+-------------+ 
| id | select_type | table | type | possible_keys            | key  | key_len | ref       | rows  | Extra  | 
+----+-------------+-------+--------+------------------------------------------------------------+---------+---------+-----------------------------+-----------+-------------+ 
| 1 | SIMPLE  | p  | range | PRIMARY,FK4BE7532292C5D482,FK4BE75322AE503A13,POSTING_DATE | PRIMARY | 8  | NULL      | 107736559 | Using where | 
| 1 | SIMPLE  | af | eq_ref | PRIMARY             | PRIMARY | 8  | smsfinance.p.fromAccount_ID |   1 | Using where | 
| 1 | SIMPLE  | at | eq_ref | PRIMARY             | PRIMARY | 8  | smsfinance.p.toAccount_ID |   1 | Using where | 
+----+-------------+-------+--------+------------------------------------------------------------+---------+---------+-----------------------------+-----------+-------------+ 

からテーブルのローンを削除

| Loan | CREATE TABLE `Loan` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
    `amount` decimal(19,4) DEFAULT NULL, 
    `amountToReturn` decimal(19,4) DEFAULT NULL, 
    `isGivenOut` bit(1) DEFAULT b'0', 
    `isPaid` bit(1) DEFAULT NULL, 
    `issueDate` datetime DEFAULT NULL, 
    `loanPeriod` int(11) DEFAULT NULL, 
    `productType` varchar(255) DEFAULT NULL, 
    `realPayDate` datetime DEFAULT NULL, 
    `client_ID` bigint(20) DEFAULT NULL, 
    `product_ID` bigint(20) DEFAULT NULL, 
    `givenOutDate` datetime DEFAULT NULL, 
    `isPaidByBank` bit(1) DEFAULT NULL, 
    `accountNumberNBKI` varchar(255) DEFAULT NULL, 
    `needManualProcessing` bit(1) DEFAULT NULL, 
    `isReverted` bit(1) DEFAULT b'0', 
    `showInNBCHReport` bit(1) DEFAULT b'1', 
    `stake` decimal(19,5) DEFAULT NULL, 
    `ignoreProlongation` bit(1) DEFAULT b'0', 
    `stakeAfter21` decimal(19,5) DEFAULT NULL, 
    `discount_id` bigint(20) DEFAULT NULL, 
    `showInEquifaxReport` bit(1) DEFAULT b'1', 
    `ignoreNbch` bit(1) DEFAULT b'0', 
    PRIMARY KEY (`ID`), 
    KEY `FK2439106EC0BA18` (`product_ID`), 
    KEY `ISPAID_INDEX` (`isPaid`) USING BTREE, 
    KEY `ISP_ISGOUT_INDEX` (`isPaid`,`isGivenOut`), 
    KEY `ISSUEDATE_INDEX` (`issueDate`), 
    KEY `FK243910735827C6` (`discount_id`), 
    KEY `idx_Loan_realPayDate` (`realPayDate`), 
    KEY `idx_Loan_givenOutDate` (`givenOutDate`), 
    KEY `FK243910AAD869E6` (`client_ID`), 
    CONSTRAINT `_FK243910735827C6` FOREIGN KEY (`discount_id`) REFERENCES `Discount` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2000623399 DEFAULT CHARSET=utf8 
ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4 

Posting | CREATE TABLE `Posting` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
    `amount` decimal(19,4) DEFAULT NULL, 
    `postingDate` datetime DEFAULT NULL, 
    `fromAccount_ID` bigint(20) DEFAULT NULL, 
    `loan_ID` bigint(20) DEFAULT NULL, 
    `toAccount_ID` bigint(20) DEFAULT NULL, 
    `sourceType` varchar(255) DEFAULT NULL, 
    PRIMARY KEY (`ID`), 
    KEY `FK4BE7532292C5D482` (`fromAccount_ID`), 
    KEY `FK4BE75322AE503A13` (`toAccount_ID`), 
    KEY `FK4BE75322382D11BC` (`loan_ID`), 
    KEY `POSTING_DATE` (`postingDate`) USING BTREE 
) ENGINE=InnoDB AUTO_INCREMENT=230996702 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4 

場合はアカウント(acc_type)のインデックスACCを作成します。

プラン:

 
+----+-------------+-------+--------+-------------------------------------------------------------------------------+--------------------+---------+---------------------------+------+--------------------------------------------------------+ 
| id | select_type | table | type | possible_keys                 | key    | key_len | ref      | rows | Extra             | 
+----+-------------+-------+--------+-------------------------------------------------------------------------------+--------------------+---------+---------------------------+------+--------------------------------------------------------+ 
| 1 | SIMPLE  | af | range | PRIMARY,acc                 | acc    | 21  | NULL      | 4192 | Using index condition; Using temporary; Using filesort | 
| 1 | SIMPLE  | p  | ref | PRIMARY,FK4BE7532292C5D482,FK4BE75322AE503A13,FK4BE75322382D11BC,POSTING_DATE | FK4BE7532292C5D482 | 9  | smsfinance.af.ID   | 54 | Using index condition; Using where      | 
| 1 | SIMPLE  | l  | eq_ref | PRIMARY                  | PRIMARY   | 8  | smsfinance.p.loan_ID  | 1 | NULL             | 
| 1 | SIMPLE  | at | eq_ref | PRIMARY,acc                 | PRIMARY   | 8  | smsfinance.p.toAccount_ID | 1 | Using where           | 
+----+-------------+-------+--------+-------------------------------------------------------------------------------+--------------------+---------+---------------------------+------+--------------------------------------------------------+ 

クエリに長い時間を実行します。

+0

これが起こる理由はたくさんあります。 'l'と' p'に 'SHOW CREATE TABLE'と問題のクエリを加えてください。私は圧縮が適切かどうかは疑問です。 mysqlsはどのバージョンですか? –

+0

メインポストを編集します。 – unknown

答えて

0

圧縮が異なると統計情報が異なる可能性があり、異なる実行計画につながる可能性があります。

このクエリの表Loanは使用されません。問合せから削除すると、Explain計画が同じになることがあります。

各テーブルに200M行ありますか?もう一つのスピードアップはテーブルを縮小することです。

  • 変更BIGINTINT UNSIGNED〜(8バイトずつ)(4バイト、範囲0..4億)可能な限り。
  • _type列を標準化し、SMALLINT UNSIGNED(2バイト、範囲0..64K)または他の適切な整数型に置き換えます。有限の少数の「型」がある場合は、列をENUMに変更します。

AccountINDEX(acc_type)ですか? (あるいは、少なくともがacc_typeとを開始する。)

INNER JOIN Loan AS l ON p.loan_id = l.IDを外し、

(SELECT client_ID FROM Loans WHERE ID = p.loan_id) AS 'client_ID', 

l.client_ID AS 'client_ID',を置き換える私は、これは別のクエリプランは、おそらく良いものを強制すると思います。

+0

申し訳ありませんが、私は間違いをしました。今やテーブルの貸出がクエリに存在します。 – unknown

+0

いいえ、アカウントにacc_typeとclient_IDのインデックスがあります。 'ユニークキー' idx_unique_client_acc_type'(クライアントID、acc_type') – unknown

+0

acc_typeでインデックス_starting_が必要です。 –

関連する問題