当前位置:Gxlcms > 数据库问题 > MYSQL索引

MYSQL索引

时间:2021-07-01 10:21:17 帮助过:13人阅读

有关于B+Tree算法可以自行搜索下。

  • 查看表中已有索引
show index from 表名;
技术图片
mysql> 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 |          0 | id       |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
技术图片

我并没有给专门给student表添加过索引,但是由于student表的主键是id,主键默认添加索引,所以id字段也是索引。

  • 创建索引
create index 索引名称 on 表名(字段名称(长度))
# 如果指定字段是字符串,需要指定长度,建议长度与定义字段时的长度一致
# 字段类型如果不是字符串,可以不填写长度部分
  • 删除索引
drop index 索引名称 on 表名;
  • 验证索引是否能提升查找性能

1.创建一个表

mysql> create table t_news(name varchar(10));

2.用python写个程序往表里插入10万条数据

技术图片
import pymysql


def main():
    conn = pymysql.connect(host=localhost‘,port=3306,
                           user=root‘,password=xxx,
                           database=text‘,charset=utf8)
    cur = conn.cursor()
    sql = insert into t_news value (%s)for i in range(1,100001):
        cur.execute(sql,[新闻%d‘ % i])
    conn.commit()
    cur.close()
    conn.close()


if __name__ == __main__:
    main()
技术图片

结果(部分):

技术图片
| 新闻99994    |
| 新闻99995    |
| 新闻99996    |
| 新闻99997    |
| 新闻99998    |
| 新闻99999    |
| 新闻100000   |
+--------------+
100000 rows in set (0.04 sec)
技术图片

3.测试有无索引情况下查询的时间

技术图片
mysql> set profiling=1;  # 开启时间监测

mysql> select * from t_news where name=新闻480916;

mysql> create index name_index on t_news(name(10));

mysql> select * from t_news where name=新闻480916;

mysql> show profiles;  # 查看时间监测结果
+----------+------------+------------------------------------------------+
| Query_ID | Duration   | Query                                          |
+----------+------------+------------------------------------------------+
|        1 | 0.04325925 | select * from t_news where name=新闻480916‘   |
|        2 | 1.64268400 | create index name_index on t_news(name(10))    |
|        3 | 0.00065225 | select * from t_news where name=新闻480916‘   |
|        4 | 0.00004275 | show profiling                                 |
+----------+------------+------------------------------------------------+
# 对比第1和第3。可知结果快了很多倍!查询性能得到了优化!!!
技术图片
  • 说在后面

索引虽然可以明显提高某些字段的查询效率。但是不要滥用,建立太多的索引将会影响更新和插入的速度,因为它需要同样更新每个索引文件。对于一个经常需要更新和插入的表格,就没有必要为一个很少使用的where字句单独建立索引了,对于比较小的表,排序的开销不会很大,也没有必要建立另外的索引。索引也会占用磁盘空间(财大气粗的可以忽略不计)

 

 

cp :https://www.cnblogs.com/chichung/p/9599741.html

MYSQL索引

标签:除了   怎么   xxx   分段   使用   range   是什么   遇到   import   

人气教程排行