数据查询----建立索引

建立索引 create index 索引名称 on 表名(字段名称(长度));

查看我表中的索引 show index from 表名;

删除索引 drop index 索引名称 on 表名;

数据表的主键和外键MySQL都自动建立了索引

为了提升我们查询某个字段的效率,我们可以对这个字段采用特殊的数据结构,那就是索引。。。。
好了,接下来就开始建立索引
首先建立一张表test_index。。
create table test_index(title varchar(10));

然后往其中插入100000条数据

from pymysql import connect

def main():
# 创建Connection连接
conn = connect(host=‘localhost’,port=3306,database=‘jing_dong’,user=‘root’,password=‘mysql’,charset=‘utf8’)
# 获得Cursor对象
cursor = conn.cursor()
# 插入10万次数据
for i in range(100000):
cursor.execute(“insert into test_index values(‘ha-%d’)” % i)
# 提交数据
conn.commit()

if name == “main”:
main()

前面我们建立了一张test_index表格,然后往其中插入了100000条数据。

我们使用
set profiling=1; 开启计时功能
select * from test_index where title=‘ha-99999’; 查找最后一条数据。
show profiles;查看执行语句的执行时间 时间为0.02906650秒

create index title_index on test_index(title(10));然后我们建立title字段的索引,title_index为索引名字

select * from test_index where title=‘ha-99999’; 执行查询语句

show profiles; 再次查看执行时间,,建立索引后查询时间为0.00059275

所以建立索引会很节约时间的

MySQL索引采用B-tree 建立一个特殊的数据结构。

猜你喜欢

转载自blog.csdn.net/qq_40637313/article/details/88980664