'2018/01/09'에 해당되는 글 2건

  1. 2018/01/09 visualp mariadb 파티션 테이블 , 운영 mysql Maintenance of Partitions
  2. 2018/01/09 visualp mysql, mariadb query 세로로 , 쿼리 세로로

METHOD
CHECK TABLE, OPTIMIZE TABLE, ANALYZE TABLE, and REPAIR TABLE

HOWTO

ALTER TABLE tb_cycle_data CHECK   PARTITION tb_cycle_data2013;

ALTER TABLE {TABLE-NAME} {METHOD}   PARTITION {PARTITION-TABLE-NAME};


22.3.4 Maintenance of Partitions

A number of table and partition maintenance tasks can be carried out using SQL statements intended for such purposes on partitioned tables in MySQL 5.7.

Table maintenance of partitioned tables can be accomplished using the statements CHECK TABLE, OPTIMIZE TABLE, ANALYZE TABLE, and REPAIR TABLE, which are supported for partitioned tables.

You can use a number of extensions to ALTER TABLE for performing operations of this type on one or more partitions directly, as described in the following list:

  • Rebuilding partitions.  Rebuilds the partition; this has the same effect as dropping all records stored in the partition, then reinserting them. This can be useful for purposes of defragmentation.

    Example:

    ALTER TABLE t1 REBUILD PARTITION p0, p1;
  • Optimizing partitions.  If you have deleted a large number of rows from a partition or if you have made many changes to a partitioned table with variable-length rows (that is, having VARCHAR, BLOB, or TEXT columns), you can use ALTER TABLE ... OPTIMIZE PARTITION to reclaim any unused space and to defragment the partition data file.

    Example:

    ALTER TABLE t1 OPTIMIZE PARTITION p0, p1;

    Using OPTIMIZE PARTITION on a given partition is equivalent to running CHECK PARTITION, ANALYZE PARTITION, and REPAIR PARTITION on that partition.

    Some MySQL storage engines, including InnoDB, do not support per-partition optimization; in these cases, ALTER TABLE ... OPTIMIZE PARTITION analyzes and rebuilds the entire table, and causes an appropriate warning to be issued. (Bug #11751825, Bug #42822) Use ALTER TABLE ... REBUILD PARTITION and ALTER TABLE ... ANALYZE PARTITION instead, to avoid this issue.

  • Analyzing partitions.  This reads and stores the key distributions for partitions.

    Example:

    ALTER TABLE t1 ANALYZE PARTITION p3;
  • Repairing partitions.  This repairs corrupted partitions.

    Example:

    Press CTRL+C to copy
     
    ALTER TABLE t1 REPAIR PARTITION p0,p1;

    Normally, REPAIR PARTITION fails when the partition contains duplicate key errors. In MySQL 5.7.2 and later, you can use ALTER IGNORE TABLE with this option, in which case all rows that cannot be moved due to the presence of duplicate keys are removed from the partition (Bug #16900947).

  • Checking partitions.  You can check partitions for errors in much the same way that you can use CHECK TABLE with nonpartitioned tables.

    Example:

    Press CTRL+C to copy
     
    ALTER TABLE trb3 CHECK PARTITION p1;

    This command will tell you if the data or indexes in partition p1 of table t1 are corrupted. If this is the case, use ALTER TABLE ... REPAIR PARTITION to repair the partition.

    Normally, CHECK PARTITION fails when the partition contains duplicate key errors. In MySQL 5.7.2 and later, you can use ALTER IGNORE TABLE with this option, in which case the statement returns the contents of each row in the partition where a duplicate key violation is found. Only the values for the columns in the partitioning expression for the table are reported. (Bug #16900947)

Each of the statements in the list just shown also supports the keyword ALL in place of the list of partition names. Using ALL causes the statement to act on all partitions in the table.

The use of mysqlcheck and myisamchk is not supported with partitioned tables.

In MySQL 5.7, you can also truncate partitions using ALTER TABLE ... TRUNCATE PARTITION. This statement can be used to delete all rows from one or more partitions in much the same way that TRUNCATE TABLE deletes all rows from a table.

ALTER TABLE ... TRUNCATE PARTITION ALL truncates all partitions in the table.

Prior to MySQL 5.7.2, ANALYZE, CHECK, OPTIMIZE, REBUILD, REPAIR, and TRUNCATE operations were not permitted on subpartitions (Bug #14028340, Bug #65184).

[원문]
https://dev.mysql.com/doc/refman/5.7/en/partitioning-maintenance.html

2018/01/09 18:23 2018/01/09 18:23
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://blog.visualp.com/rss/response/731

댓글+트랙백 ATOM :: http://blog.visualp.com/atom/response/731

종 데이터를 횡 데이트로 만들기 (세로 - 가로)

횡으로 구성되어있는 데이터를 종으로 만드는 방법에 대해 설명합니다. 먼저 전, 후의 데이터를 비교해보세요.

처리 전 데이터

1
2
3
4
5
6
7
8
9
10
+-----+------+-------+-------+
| idx | name | class | score |
+-----+------+-------+-------+
|   7 | choi | kor   |    90 |
|   8 | choi | eng   |    80 |
|   9 | choi | math  |    70 |
|  10 | kim  | kor   |    60 |
|  11 | kim  | eng   |    85 |
|  12 | kim  | math  |   100 |
+-----+------+-------+-------+

처리 후 데이터

1
2
3
4
5
6
+------+------+------+------+
| name | kor  | eng  | math |
+------+------+------+------+
| choi |   90 |   80 |   70 |
| kim  |   60 |   85 |  100 |
+------+------+------+------+

처리 후 데이터를 보면 name 기준으로 kor, eng, math항목이 횡으로 나열된 것을 볼 수 있습니다.

종 데이터 샘플 생성

종 데이터로 사용할 사용할 가상의 샘플 데이터를 생성해 보겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE TABLE `score` (
  `idx` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `class` varchar(32) NOT NULL,
  `score` int(11) NOT NULL,
  PRIMARY KEY (`idx`)
);
 
INSERT INTO score (name, class, score) VALUES ('choi', 'kor', 90);
INSERT INTO score (name, class, score) VALUES ('choi', 'eng', 80);
INSERT INTO score (name, class, score) VALUES ('choi', 'math', 70);
 
INSERT INTO score (name, class, score) VALUES ('kim', 'kor', 60);
INSERT INTO score (name, class, score) VALUES ('kim', 'eng', 85);
INSERT INTO score (name, class, score) VALUES ('kim', 'math', 100);

종 데이터 생성이 완료 되었습니다.

1
2
3
4
5
6
7
8
9
10
11
mysql> SELECT * FROM score;
+-----+------+-------+-------+
| idx | name | class | score |
+-----+------+-------+-------+
|   7 | choi | kor   |    90 |
|   8 | choi | eng   |    80 |
|   9 | choi | math  |    70 |
|  10 | kim  | kor   |    60 |
|  11 | kim  | eng   |    85 |
|  12 | kim  | math  |   100 |
+-----+------+-------+-------+

alias를 이용한 컬럼 생성

종 데이터를 kor, eng, math로 구성된 횡 데이터로 만들기 위해 alias를 이용 K, E, M 컬럼을 생성하고 kor데이터는 K에 eng데이터는 E에 math데이터는 M에 넣습니다.

값이 저장되지 않은 컬럼은 NULL이 저장됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT
      name,
      CASE WHEN class = 'kor' THEN score END AS K,
      CASE WHEN class = 'eng' THEN score END AS E,
      CASE WHEN class = 'math' THEN score END AS M
FROM score;
 
+------+------+------+------+
| name | K    | E    | M    |
+------+------+------+------+
| choi |   90 | NULL | NULL |
| choi | NULL |   80 | NULL |
| choi | NULL | NULL |   70 |
| kim  |   60 | NULL | NULL |
| kim  | NULL |   85 | NULL |
| kim  | NULL | NULL |  100 |
+------+------+------+------+

아직까지는 종 데이터의 형식이지만 이제 횡데이터로 만들기 위한 준비는 되었습니다.

횡 데이터 생성

위에서 만들었던 쿼리를 이용해서 임의의 테이블을 만듭니다.
임의의 테이블을 group by하고 alias를 이용해서 생성한 K, E, M 컬럼을 이용 횡데이터로 만듭니다.
K, E, M컬럼은 SUM 함수를 이용해서 더해주고 alias를 이용 kor, eng, math로 컬럼명을 지정합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT name, SUM(K) as kor, SUM(E) as eng, SUM(M) as math FROM
(
  SELECT
        name,
        CASE WHEN class = 'kor' THEN score END AS K,
        CASE WHEN class = 'eng' THEN score END AS E,
        CASE WHEN class = 'math' THEN score END AS M
  FROM score
)AS T GROUP BY name;
 
+------+------+------+------+
| name | kor  | eng  | math |
+------+------+------+------+
| choi |   90 |   80 |   70 |
| kim  |   60 |   85 |  100 |
+------+------+------+------+


[원문]http://blog.devez.net/307

참고 URL

http://stackoverflow.com/questions/1241178/mysql-rows-to-columns

2018/01/09 10:57 2018/01/09 10:57
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://blog.visualp.com/rss/response/730

댓글+트랙백 ATOM :: http://blog.visualp.com/atom/response/730