sql >> データベース >  >> RDS >> Mysql

繰り返されるキーのMysqlgroup_concatと1つのクエリでの複数の列の繰り返しのカウント(クエリ最適化)

    2つのクエリをJOINと組み合わせてみませんか?

    SQL:

     SELECT
        tbl1.key1, tbl1.key2, tbl1.info1, tbl1.info2, tbl1.serial_count, tbl1.serial_ids,
        tbl2.product_data_count, tbl2.product_data_ids
     FROM 
     (
    select * , count(*) as serial_count,GROUP_CONCAT(key1,' ',key2) as serial_ids from 
     (
     SELECT distinct 
     if(b.serial  < 10,a.key1,null) AS `key1`,
     if(b.serial  < 10,a.key2,null) AS `key2`,
     if(b.serial  < 10,a.info1,null) AS `info1`, 
             if(b.serial  < 10,a.info2,null) AS `info2`
     FROM main_info a inner join product1 b on  a.key1 = b.key1 AND a.key2= b.key2
     ) as sub group by info1,info2
     ) tbl1
     LEFT OUTER JOIN 
     (
    select * , count(*) as product_data_count,GROUP_CONCAT(key1,' ',key2) as product_data_ids from 
     (
     SELECT distinct 
     if(b.product_data IS NOT NULL,a.key1,null) AS `key1`,
     if(b.product_data IS NOT NULL,a.key2,null) AS `key2`,
     if(b.product_data IS NOT NULL,a.info1,null) AS `info1`, 
             if(b.product_data IS NOT NULL,a.info2,null) AS `info2`
     FROM main_info a inner join product1 b on  a.key1 = b.key1 AND a.key2= b.key2
     ) as sub group by info1,info2
     ) tbl2
     ON tbl1.info1 = tbl2.info1 AND tbl1.info2 = tbl2.info2
     ORDER BY 3,4
     ;
    

    出力:

    mysql>  SELECT
        -> tbl1.key1, tbl1.key2, tbl1.info1, tbl1.info2, tbl1.serial_count, tbl1.serial_ids,
        -> tbl2.product_data_count, tbl2.product_data_ids
        ->  FROM
        ->  (
        -> select * , count(*) as serial_count,GROUP_CONCAT(key1,' ',key2) as serial_ids from
        ->  (
        ->  SELECT distinct
        ->  if(b.serial  < 10,a.key1,null) AS `key1`,
        ->  if(b.serial  < 10,a.key2,null) AS `key2`,
        ->  if(b.serial  < 10,a.info1,null) AS `info1`,
        ->          if(b.serial  < 10,a.info2,null) AS `info2`
        ->  FROM main_info a inner join product1 b on  a.key1 = b.key1 AND a.key2= b.key2
        ->  ) as sub group by info1,info2
        ->  ) tbl1
        ->  LEFT OUTER JOIN
        ->  (
        -> select * , count(*) as product_data_count,GROUP_CONCAT(key1,' ',key2) as product_data_ids from
        ->  (
        ->  SELECT distinct
        ->  if(b.product_data IS NOT NULL,a.key1,null) AS `key1`,
        ->  if(b.product_data IS NOT NULL,a.key2,null) AS `key2`,
        ->  if(b.product_data IS NOT NULL,a.info1,null) AS `info1`,
        ->          if(b.product_data IS NOT NULL,a.info2,null) AS `info2`
        ->  FROM main_info a inner join product1 b on  a.key1 = b.key1 AND a.key2= b.key2
        ->  ) as sub group by info1,info2
        ->  ) tbl2
        ->  ON tbl1.info1 = tbl2.info1 AND tbl1.info2 = tbl2.info2
        ->  ORDER BY 3,4
        ->  ;
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | key1 | key2 | info1 | info2 | serial_count | serial_ids  | product_data_count | product_data_ids |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    | NULL | NULL |  NULL |  NULL |            1 | NULL        |               NULL | NULL             |
    |    1 |    2 |    14 |    92 |            1 | 1 2         |                  1 | 1 2              |
    |    1 |    3 |    15 |    82 |            3 | 1 3,1 4,1 5 |                  3 | 1 3,1 4,1 5      |
    |    1 |    1 |    15 |    90 |            1 | 1 1         |                  1 | 1 1              |
    |    2 |    1 |    17 |    90 |            1 | 2 1         |                  3 | 2 2,2 3,2 1      |
    +------+------+-------+-------+--------------+-------------+--------------------+------------------+
    5 rows in set (0.01 sec)
    
    mysql>  select version();
    +-----------------+
    | version()       |
    +-----------------+
    | 10.1.10-MariaDB |
    +-----------------+
    1 row in set (0.00 sec)
    


    1. ON条件なしでmysqlJOINを使用するにはどうすればよいですか?

    2. SQLite MAX

    3. 挿入無視をエミュレートし、postgresqlを使用して重複キー更新(SQLマージ)を行う方法は?

    4. データベースを最適化するためのMySQLパフォーマンスチューニングのヒント