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

建立索引 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 建立一个特殊的数据结构。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值