zoukankan      html  css  js  c++  java
  • MySQL--06(索引)

    索引

    数据量很大时,查询慢的问题;

    数据量巨大时,索引无效

    索引定义:是一个排好序的,便于快速查找的,数据结构。

    主键是一种特殊的索引,

    index

    pid : parent id ->pid

    parent : 父母,双亲

    可以在任何字段上创建索引,但不是每个字段都适合做索引;

    查看索引

    show index from 表名;

    创建索引的命令

    create index idx_索引名  on 表名(字段名(索引长度))

    索引类型 

    主键、唯一索引、普通索引、联合(复合索引)、全文索引

    添加唯一索引命令

    alter table 表名 add unique index idx_索引名(要索引的字段(长度))

    如果添加了 唯一索引 添加重复的数据,则会报类似错误:

    ERROR 1062 (23000):** Duplicate** entry '湖南省永州市' for key 'idx_address'

    删除索引

    drop index 索引名称 on 表名;
    MariaDB [books]> drop index idx_address on student;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    MariaDB [books]> show index from student;
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | student |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
    | student |          1 | idx_name |            1 | name        | A         |           6 |       10 | NULL   |      | BTREE      |         |               |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    2 rows in set (0.00 sec)
    View Code

    Key_name:索引名称

    MariaDB [books]> show index from student;
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | student |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
    | student |          1 | idx_name |            1 | name        | A         |           6 |       10 | NULL   |      | BTREE      |         |               |
    | student |          1 | idx_fuhe |            1 | name        | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
    | student |          1 | idx_fuhe |            2 | address     | A         |           6 |     NULL | NULL   | YES  | BTREE      |         |               |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    4 rows in set (0.00 sec)
    View Code

    创建全文索引

    MariaDB [books]> alter table student add  FULLTEXT idx_full(address);
    Query OK, 0 rows affected, 1 warning (0.18 sec)
    Records: 0  Duplicates: 0  Warnings: 1
    View Code
    MariaDB [books]> create table idx(
        ->     id int primary key auto_increment,
        ->     name varchar(20),
        ->     email varchar(60),
        ->     unique idx_email (email(20))
        -> )engine=innodb default charset=utf8;
    Query OK, 0 rows affected (0.02 sec)
    
    MariaDB [books]> show index from idx;
    +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | idx   |          0 | PRIMARY   |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
    | idx   |          0 | idx_email |            1 | email       | A         |           0 |       20 | NULL   | YES  | BTREE      |         |               |
    +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    2 rows in set (0.00 sec)
    View Code

    事务

    事务:一系列的mysql操作,它的特性具有acid特性。

    acid:原子性,一致性,隔离性,持久性。

    start transaction;/begin;

    commit;

    提交事务的案例

    1. 开启 窗口1

    2. 开启窗口2

    3. 两个窗口都使用同一数据库

    4. 窗口一 start transaction;或者begin开启事务

    5. 窗口一 执行插入或修改的操作,

    6. 窗口一查看,这时可以看到操作的结果

    7. 窗口二查看,这时看不到操作的结果

    8. 窗口一commit

    9. 窗口一查看,可以看到操作结果,窗口二查看,可以看到操作结果,完成事务操作。

  • 相关阅读:
    JQuery扩展方法
    RabbitMQ消息机制广播分发
    RabbitMQ消息机制单人分发
    对函数的参数求和
    ajax jsonp
    绑定函数bind()
    this 指向
    DOM兼容
    命名空间 namespace
    开始看编写高质量的代码
  • 原文地址:https://www.cnblogs.com/xinzaiyuan/p/13501881.html
Copyright © 2011-2022 走看看