• ADADADADAD

    Percona 5.5如何定位未使用的索引[ mysql数据库 ]

    mysql数据库 时间:2024-11-26 22:15:31

    作者:文/会员上传

    简介:

    打开userstat参数,然后让MySQL运行一段时间。之后可以在INFORMATION_SCHEMA.INDEX_STATISTICS表中查询到索引的使用频率。mysql> show global variables like '%users%&#

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    打开userstat参数,然后让MySQL运行一段时间。
    之后可以在INFORMATION_SCHEMA.INDEX_STATISTICS表中查询到索引的使用频率。

      mysql> show global variables like '%users%';

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

      | Variable_name | Value |

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

      | userstat | OFF |

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

      1 row in set (0.00 sec)

      mysql> select * from information_schema.index_statistics;

      Empty set (0.00 sec)

      mysql> set global userstat=1;

      Query OK, 0 rows affected (0.00 sec)

      mysql> show global variables like '%users%';

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

      | Variable_name | Value |

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

      | userstat | ON |

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

      1 row in set (0.00 sec)

      mysql> select * from information_schema.index_statistics;

      Empty set (0.00 sec)

      mysql> explain select * from emp where hiredate > '1982-01-01' and deptno = 20;

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

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

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

      | 1 | SIMPLE | emp | range | idx_date_sal_job,idx_date | idx_date_sal_job | 4 | NULL | 3 | Using where |

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

      1 row in set (0.00 sec)

      mysql> select * from information_schema.index_statistics;

      Empty set (0.00 sec)

      mysql> select * from emp where hiredate > '1982-01-01' and deptno = 20;

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

      | empno | ename | job | mgr | hiredate | sal | comm | deptno |

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

      | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000 | NULL | 20 |

      | 7876 | ADAMS | CLERK | 7788 | 1983-01-12 00:00:00 | 1100 | NULL | 20 |

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

      2 rows in set (0.00 sec)

      mysql> select * from information_schema.index_statistics;

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

      | TABLE_SCHEMA | TABLE_NAME | INDEX_NAME | ROWS_READ |

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

      | test | emp | idx_date_sal_job | 3 |

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

      1 row in set (0.04 sec)

      mysql> select * from emp;

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

      | empno | ename | job | mgr | hiredate | sal | comm | deptno |

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

      | 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | NULL | 20 |

      | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 |

      | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 |

      | 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | NULL | 20 |

      | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 |

      | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | NULL | 30 |

      | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | NULL | 10 |

      | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000 | NULL | 20 |

      | 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000 | NULL | 10 |

      | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 |

      | 7876 | ADAMS | CLERK | 7788 | 1983-01-12 00:00:00 | 1100 | NULL | 20 |

      | 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | NULL | 30 |

      | 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | NULL | 20 |

      | 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | NULL | 10 |

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

      14 rows in set (0.00 sec)

      mysql> select * from information_schema.index_statistics;

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

      | TABLE_SCHEMA | TABLE_NAME | INDEX_NAME | ROWS_READ |

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

      | test | emp | PRIMARY | 14 |

      | test | emp | idx_date_sal_job | 3 |

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

      2 rows in set (0.00 sec)

      mysql> select * from emp where hiredate > '1982-01-01' and deptno = 20;

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

      | empno | ename | job | mgr | hiredate | sal | comm | deptno |

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

      | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000 | NULL | 20 |

      | 7876 | ADAMS | CLERK | 7788 | 1983-01-12 00:00:00 | 1100 | NULL | 20 |

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

      2 rows in set (0.00 sec)

      mysql> select * from emp;

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

      | empno | ename | job | mgr | hiredate | sal | comm | deptno |

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

      | 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | NULL | 20 |

      | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 |

      | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 |

      | 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | NULL | 20 |

      | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 |

      | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | NULL | 30 |

      | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | NULL | 10 |

      | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000 | NULL | 20 |

      | 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000 | NULL | 10 |

      | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 |

      | 7876 | ADAMS | CLERK | 7788 | 1983-01-12 00:00:00 | 1100 | NULL | 20 |

      | 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | NULL | 30 |

      | 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | NULL | 20 |

      | 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | NULL | 10 |

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

      14 rows in set (0.00 sec)

      mysql> select * from information_schema.index_statistics;

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

      | TABLE_SCHEMA | TABLE_NAME | INDEX_NAME | ROWS_READ |

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

      | test | emp | PRIMARY | 28 |

      | test | emp | idx_date_sal_job | 6 |

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

      2 rows in set (0.00 sec)

    Percona 5.5如何定位未使用的索引.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: percona