MySQL索引对查询的影响_MySQL——索引的使用对查询、插入速度的影响

一.什么是索引?

1.索引:索引是对数据库中一列或者多列的值进行排序的一种数据结构。java

2.索引的做用:索引的做用是为了提升查询的速度。mysql

3.几个特色:web

①MySQL中,主键和惟一约束自带索引;

②在查询时,只有使用到有索引的列,才能提升查询速度;

③索引会下降插入速度,数据量越大,插入速度越慢。

4.索引的算法:Hash和Btree算法

Hash索引:适合等值查找,在范围查找时有可能发生Hash冲突;

Btree索引:适合范围查找,没有Hash冲突问题。

二.验证索引对查询、插入速度的影响

实验工具:sql

Nacivat for MySQL

eclipse

实验步骤:

(一)实验环境创建:

1.使用 Nacivat for MySQL 建立表格 tb_Test;

1992ae9e0f78da281bd1fd5ad30d1b37.png

其中,id为检测列,即用于插入索引的列。数据库

2.利用eclipse链接MySQL操做表格 tb_Test ;数据结构

①eclipse代码:app

package forJDBC;

import static org.junit.Assert.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

public class Boker {

private Connection conn;

@Before

public void setUp() throws Exception {

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/for1703", "root", "root");

}

@After

public void tearDown() throws Exception {

conn.close();

}

@Test

public void testInsert() throws SQLException {

this.insertS();

}

private void insertS() {

try {

Statement stat=conn.createStatement();

StringBuilder sb=new StringBuilder();

for(int i=0;i<500;i++) {

sb.append("('idxxx"+i+"',"+(i+1)+")");

if(i!=499) {

sb.append(",");

}

}

String sql="insert into tb_Test (id,idNumber) values"+sb.toString();

int num=stat.executeUpdate(sql);

System.out.println(num);

} catch (SQLException e) {

e.printStackTrace();

}

}

@Test

public void testSearch() throws SQLException {

this.searchS();

}

private void searchS() throws SQLException {

Statement stat=conn.createStatement();

String sql="select * from tb_Test where id='idxxx255'";

ResultSet rs=stat.executeQuery(sql);

while(rs.next()) {

System.out.println(rs.getString("id")+"\t"+rs.getInt("idNumber")+"\t");

}

}

@Test

public void testDelete(){

this.deleteS();

}

private void deleteS() {

try {

Statement stat =conn.createStatement();

String sql="delete from tb_Test";

System.out.println(stat.executeUpdate(sql));

} catch (SQLException e) {

e.printStackTrace();

}

}

}

②运行 testInsert( )向表中插入500条数据,其中id=“idxxx”+(idNumber-1):eclipse

6e25cc4fc41629c6c1a4e73902eb3bea.png

(二)验证过程:svg

1.当 id 列未添加索引时,运行 testSearch( ) 方法,查找“idxxx255”,耗费2.880s;

4b034712cf43d14bc14361ee182d775a.png

2.应用Navicat for MySQL对id列添加索引,再次查找“idxxx255”,耗时1.036s;

c7c9735fd1a288f202a4635c5a2423fc.png

a10c6e945527124f6de620ea5f7bac6a.png

※发现:查找相同内容,有索引时能够大大缩短查询时间。

3.运行 testDelete( ) 删除表内记录,并删去索引;

4.更改代码中i值为10000,再次运行 testInsert( ),向没有索引的表中插入10000组数据,耗时1.371s;

a78b458ed70fe5b05783e965791cf122.png

5.再次运行 testDelete( ) 删除全部数据后,对 id 列添加索引后,再次插入500组一样的数据,耗时1.817s;

e647251a0b002eb2f5e48ddc8b4c0b51.png

※发现:插入相同数据,无索引比有索引时速度快一些。

三.结论

通过验证,索引的使用对查询和插入速度有如下影响:

查询时,使用带有索引的列能够明显提升查询速度;

插入时,若是插入的列有索引,插入速度会减慢。

四.分析讨论

通过本次实验,在之后建立表格时,尽可能保证将主要的数据都插入以后,再添加索引,避免在添加索引以后进行数据插入,以保证效率最高。

注意:删除时,要把代码前面的插入和查询代码注释掉,防止删除失败。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值