1

問題:

  1. クエリは 10 分後に中止されます
  2. クエリはより高速にする必要があります

次のクエリを作成しました。これはいくつかのバージョンの中で最速でした。残念ながら、より多くのデータを使用すると、これでも 600 秒後に「エラー コード: 2013. クエリ中に MySQL サーバーへの接続が失われました」というエラーで中止されます。

    CREATE OR REPLACE VIEW 1 AS
    SELECT `Timeperiod`    AS `Timeperiod`    ,
           "at"            AS `Domain`        ,
           `Content Groups`AS `Content Groups`,
    ...
    FROM   a

UNION ALL

SELECT `Timeperiod`    AS `Timeperiod`    ,
       "com"           AS `Domain`        ,
       `Content Groups`AS `Content Groups`,
       ...
FROM   b

UNION ALL

SELECT `Timeperiod`    AS `Timeperiod`    ,
       "com"                 AS `Domain`,
       `Content Groups`AS `Content Groups`,
       ...
FROM   c

UNION ALL

SELECT `Timeperiod`    AS `Timeperiod`    ,
       "fr"                 AS `Domain`,
       `Content Groups`AS `Content Groups`,
       ...
FROM   d

UNION ALL

SELECT `Timeperiod`    AS `Timeperiod`    ,
       "it"                 AS `Domain`,
       `Content Groups`AS `Content Groups`,
       ...
FROM   e;

CREATE OR REPLACE VIEW 2 AS
SELECT `Timeperiod`        AS `Timeperiod`       ,
       `Content Group`     AS `Content Group`    ,
       "at"                AS `Domain`,
       ...
FROM   f

UNION ALL

SELECT `Timeperiod`        AS `Timeperiod`       ,
       `Content Group`     AS `Content Group`    ,
       "com"               AS `Domain`,
       ...
FROM g

UNION ALL

SELECT `Timeperiod`        AS `Timeperiod`       ,
       `Content Group`     AS `Content Group`    ,
       "com"               AS `Domain`,
       ...
FROM h

UNION ALL

SELECT `Timeperiod`        AS `Timeperiod`       ,
       `Content Group`     AS `Content Group`    ,
       "fr"                AS `Domain`,
       ...
FROM   i

UNION ALL

SELECT `Timeperiod`        AS `Timeperiod`       ,
       `Content Group`     AS `Content Group`    ,
       "it"                AS `Domain`,
       ...    
FROM   j;

CREATE OR REPLACE VIEW 3 AS
SELECT CG.`Domain`                                        AS `Domain`             ,
       TP.`TimeperiodAlias`                               AS `Timeperiod`         ,
       CG.`Content Groups`                                AS `Content Group`      ,
       M.`InternalName`                                   AS `Internal Model Name`,
       ...
FROM   1 CG                      ,
       Timperiods TP             ,
       Models M
WHERE  CG.`Content Groups` LIKE CONCAT(M.`ContentGroupName`, '%')
AND    CG.`Timeperiod`        = TP.`Timeperiod`;

CREATE OR REPLACE VIEW 4 AS
SELECT CGD.`Domain`                                              AS `Domain`        ,
       TP.`TimeperiodAlias`                                      AS `Timeperiod`    ,
       CGD.`Content Group`                                       AS `Content Group`,
       ...
FROM   2 CGD,
       Timeperiods TP                 ,
       Models M 
WHERE  CGD.`Content Group` LIKE CONCAT(M.`ContentGroupName`, '%')
AND    CGD.`Timeperiod`       = TP.`Timeperiod`;

DROP TABLE IF EXISTS 5;

CREATE TABLE IF NOT EXISTS 5
             (
                          `Domain`     VARCHAR(3) NOT NULL ,
                          `Timeperiod` VARCHAR(30) NOT NULL,
                          `Content Group` varchar(70),
                          `Internal Model Name` VARCHAR(50),
                          ...
                           PRIMARY KEY (`Domain`,`Timeperiod`, `Content Group`)
             )    
AS        

SELECT CG.`Domain`              AS `Domain`             ,
       CG.`Timeperiod`          AS `Timeperiod`         ,
       CG.`Content Group`       AS `Content Group`      ,
       CG.`Internal Model Name` AS `Internal Model Name`,
       ...
FROM   3 CG,
       4 CGD
WHERE  CG.`Content Group` = CGD.`Content Group`
AND    CG.`Timeperiod`    = CGD.`Timeperiod`
AND    CG.`Domain`        = CGD.`Domain`;

ステップの行数は次のとおりです。

1: 64763 2: 51932

期間: 36

モデル: 15

3: 2706

4:2172

これは説明です:

'1', 'PRIMARY', 'M', 'ALL', NULL, NULL, NULL, NULL, '15', ''
'1', 'PRIMARY', 'M', 'index', NULL, 'CGIndex', '242', NULL, '15', 'Using index; Using join buffer'
'1', 'PRIMARY', '<derived3>', 'ALL', NULL, NULL, NULL, NULL, '9528', 'Using where; Using join buffer'
'1', 'PRIMARY', 'TP', 'eq_ref', 'PRIMARY', 'PRIMARY', '65', 'CG.Timeperiod', '1', ''
'1', 'PRIMARY', '<derived9>', 'ALL', NULL, NULL, NULL, NULL, '21226', 'Using where; Using join buffer'
'1', 'PRIMARY', 'TP', 'eq_ref', 'PRIMARY', 'PRIMARY', '65', 'CGD.Timeperiod', '1', 'Using where'
'9', 'DERIVED', 'ContentGroupDuration_jMKL35_ALL', 'ALL', NULL, NULL, NULL, NULL, '17794', ''
'10', 'UNION', 'ContentGroupDurationVisitDuration_k4cZ5M_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'11', 'UNION', 'ContentGroupDurationVisitDuration_k4cZ5M_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'12', 'UNION', 'ContentGroupDuration_jMKL35_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'13', 'UNION', 'ContentGroupDuration_jMKL35_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
NULL, 'UNION RESULT', '<union9,10,11,12,13>', 'ALL', NULL, NULL, NULL, NULL, NULL, ''
'3', 'DERIVED', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'4', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'5', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'6', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '10476', ''
'7', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
NULL, 'UNION RESULT', '<union3,4,5,6,7>', 'ALL', NULL, NULL, NULL, NULL, NULL, ''

クエリを固定する方法や接続の中止を回避する方法を知っている人はいますか?

解決:

問題 1: コマンド ラインから "set wait_timeout=2147483" を実行する (SQL 内ではない)

問題 2: 中間結果を一時テーブルに格納し、インデックスを追加します。次に、大規模な結合を実行します。

一番

キリスト教徒

4

1 に答える 1

0

私は2つの方法があると思います:-非対話型接続のタイムアウトを変更する(Mysqlの場合はwait_timeout)-または何らかの方法でテーブル構造を最適化する

私は過去に大規模な商用データベースに取り組んできましたが、結合のパフォーマンスは、フェッチされる行の量よりも、テーブルのインデックスの方法に関係しています。適切なテーブルに適切なキーがあることを確認し、可能であればそれらを拡張してみてください。

とにかく、wait_timeoutを変更します。長くて複雑なクエリを許可する場合、接続がすぐに停止することはありません。

タイムアウトを変更するには、rootとしてmysqlにログインします:mysql -u root -p、パスワードを入力し、次のように入力します:set global wait_timeout = 2147483

これは、Windowsの23日に対応する最大値です。Linuxディストリビューションでははるかに高くなる可能性がありますが、とにかくそれほど長くは必要ありません。

乾杯、

于 2011-11-04T15:36:52.810 に答える