zoukankan      html  css  js  c++  java
  • MariaDB 查看running queries,转换存储引擎,导出为HTML,XML

    MariaDB 查看running queries,
    转换存储引擎,导出为HTML,XML

    unzip backup-MySQL-55-2016-03-26.zip
    CREATE DATABASE isfdb CHARACTER SET utf8;
    mysql -ujlive -p isfdb


    查看running queries

    1.模拟一个非常慢的查询 

    ALTER TABLE title_relationships DROP KEY titles;

    SELECT titles.title_id AS ID,

    titles.title_title AS Title,

    authors.author_legalname AS Name,

    (SELECT COUNT(DISTINCT title_relationships.review_id) FROM title_relationships

            WHERE title_relationships.title_id = titles.title_id) AS reviews

    FROM  titles,authors,canonical_author

    WHERE

            (SELECT COUNT(DISTINCT title_relationships.review_id) FROM title_relationships

                    WHERE title_relationships.title_id = titles.title_id)>=10

            AND canonical_author.author_id = authors.author_id

            AND canonical_author.title_id=titles.title_id

            AND titles.title_parent=0 ;


    说明:数据量太大时可以通过LIMIT ROWS EXAMINED语句定义查询长度以节省资源,如,在上面的查询语句后加上

    LIMIT ROWS EXAMINED 10000;

    还可以在查询长度的基础上限制输入长度

    LIMIT 100 ROWS EXAMINED 10000;

    2.在PROCESSLIST中查找查询时长超过60秒的ID,QUERY_ID

    (jlive)[isfdb]>SELECT INFO, TIME, ID, QUERY_ID  FROM INFORMATION_SCHEMA.PROCESSLIST  WHERE TIME > 60 LIMIT 2G

    *************************** 1. row ***************************

        INFO: SELECT titles.title_id AS ID,

    titles.title_title AS Title,

    authors.author_legalname AS Name,

    (SELECT COUNT(DISTINCT title_relationships.review_id) FROM title_relationships

            WHERE title_relationships.title_id = titles.title_id) AS reviews

    FROM  titles,authors,canonical_author

    WHERE

            (SELECT COUNT(DISTINCT title_relationships.review_id) FROM title_relationships

                    WHERE title_relationships.title_id = titles.title_id)>=10

            AND canonical_author.author_id = authors.author_id

            AND canonical_author.title_id=titles.title_id

            AND titles.title_parent=0

        TIME: 632

          ID: 30

    QUERY_ID: 1483

    *************************** 2. row ***************************

        INFO: NULL

        TIME: 860

          ID: 28

    QUERY_ID: 103

    3.查看对应查询ID的附加信息

    (jlive)[isfdb]>SHOW EXPLAIN FOR 30;

    +------+--------------------+---------------------+--------+-------------------+---------+---------+---------------------------------+--------+-------------+

    | id   | select_type        | table               | type   | possible_keys     | key     | key_len | ref                             | rows   | Extra       |

    +------+--------------------+---------------------+--------+-------------------+---------+---------+---------------------------------+--------+-------------+

      1 | PRIMARY            | authors             | ALL    | PRIMARY           | NULL    | NULL    | NULL                            | 136889 |             |

      1 | PRIMARY            | canonical_author    | ref    | titles,authors    | authors | 5       | isfdb.authors.author_id         |     10 | Using where |

      1 | PRIMARY            | titles              | eq_ref | PRIMARY,parent_id | PRIMARY | 4       | isfdb.canonical_author.title_id |      1 | Using where |

      3 | DEPENDENT SUBQUERY | title_relationships | ALL    | NULL              | NULL    | NULL    | NULL                            94824 | Using where |

      2 | DEPENDENT SUBQUERY | title_relationships | ALL    | NULL              | NULL    | NULL    | NULL                            94824 | Using where |

    +------+--------------------+---------------------+--------+-------------------+---------+---------+---------------------------------+--------+-------------+

    5 rows in set, 1 warning (0.00 sec)


    4.KILL掉该查询进程

    (jlive)[isfdb]>KILL QUERY ID 1483;

    Query OK, 0 rows affected (0.00 sec)


    5.还原KEY

    (jlive)[isfdb]>ALTER TABLE title_relationships ADD KEY titles (title_id);

    Query OK, 94824 rows affected (0.26 sec)

    Records: 94824  Duplicates: 0  Warnings: 0


    (jlive)[isfdb]>SHOW WARNINGS;

    Empty set (0.00 sec)


    转换存储引擎

    安装Cassandra存储引擎

    INSTALL SONAME 'ha_cassandra';

    或者

    INSTALL PLUGIN Cassandra SONAME 'ha_cassandra';

    SHOW PLUGINS;

    SHOW STORAGE ENGINES;

    卸载Cassandra

    UNINSTALL SONAME 'ha_cassandra';

    说明:mariadb-10.1的进抽包里默认没带cassandra的插件,这里先跳过

    迁移表从MyISAM到Aria

    (jlive)[isfdb]>SHOW TABLE STATUS LIKE 'authors'G

    *************************** 1. row ***************************

               Name: authors

             Engine: MyISAM

            Version: 10

         Row_format: Dynamic

               Rows: 136889

     Avg_row_length: 58

        Data_length: 7943084

    Max_data_length: 281474976710655

       Index_length: 4631552

          Data_free: 0

     Auto_increment: 227591

        Create_time: 2016-04-02 20:36:39

        Update_time: 2016-04-02 20:36:40

         Check_time: 2016-04-02 20:36:40

          Collation: latin1_swedish_ci

           Checksum: NULL

     Create_options: 

            Comment: 

    1 row in set (0.00 sec)


    (jlive)[isfdb]>ALTER TABLE authors ENGINE=Aria;

    Query OK, 136889 rows affected (0.98 sec)

    Records: 136889  Duplicates: 0  Warnings: 0


    (jlive)[isfdb]>SHOW TABLE STATUS LIKE 'authors'G

    *************************** 1. row ***************************

               Name: authors

             Engine: Aria

            Version: 10

         Row_format: Page

               Rows: 136889

     Avg_row_length: 66

        Data_length: 9043968

    Max_data_length: 17592186011648

       Index_length: 5128192

          Data_free: 0

     Auto_increment: 227591

        Create_time: 2016-04-04 19:50:13

        Update_time: 2016-04-04 19:50:14

         Check_time: 2016-04-04 19:50:14

          Collation: latin1_swedish_ci

           Checksum: NULL

     Create_options: 

            Comment: 

     

    1 row in set (0.00 sec)

    (jlive)[isfdb]>ALTER TABLE awards ENGINE=InnoDB;

    Query OK, 38496 rows affected (0.35 sec)

    Records: 38496  Duplicates: 0  Warnings: 0


    (jlive)[isfdb]>SHOW TABLE STATUS LIKE 'awards'G

    *************************** 1. row ***************************

               Name: awards

             Engine: InnoDB

            Version: 10

         Row_format: Compact

               Rows: 38265

     Avg_row_length: 123

        Data_length: 4734976

    Max_data_length: 0

       Index_length: 3178496

          Data_free: 4194304

     Auto_increment: 45358

        Create_time: 2016-04-04 19:52:54

        Update_time: NULL

         Check_time: NULL

          Collation: latin1_swedish_ci

           Checksum: NULL

     Create_options: 

            Comment: 

     

    1 row in set (0.00 sec)

    表存储引擎转换实际上是两个步骤

    1.用新的存储引擎创建一张和原表属性完全相同的新表,仅存储引擎不同,并把数据全部copy到新表

    2.删除原表,并装新表的名字改为原表的名字


    导出为HTML,XML

    1.导出为HTML

    echo "SELECT * FROM authors LIMIT 100;" >isfdb-001.sql

    mysql --html isfdb < isfdb-001.sql >isfdb-001.html

    或用tidy对html加工下,使输出的html更规范标准

    mysql --html isfdb < isfdb-001.sql | tidy -q -i -o isfdb-001.html


    2.导出为XML

    mysql --xml isfdb < isfdb-001.sql >isfdb-001.xml

  • 相关阅读:
    批量导出数据库表(oracle)
    Neo4j安装配置(mac)
    项目实施计划方案
    分布式数据库笔记
    SQL优化——ORACLE
    INNER JOIN & OUTER JOIN
    sqlMap.xml配置文件中迭代一个集合的方式
    sql下的xml配置文件中特殊使用的sql语句编写
    将查询列表内容保存到excel表格中,并保存到相应的盘中
    Spring框架中AOP特性
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814144.html
Copyright © 2011-2022 走看看