MySQL——索引的使用对查询、插入速度的影响
2017-11-05 23:36
856 查看
一.什么是索引?
1.索引:索引是对数据库中一列或者多列的值进行排序的一种数据结构。2.索引的作用:索引的作用是为了提高查询的速度。
3.几个特点:
①MySQL中,主键和唯一约束自带索引;
②在查询时,只有使用到有索引的列,才能提高查询速度;
③索引会降低插入速度,数据量越大,插入速度越慢。
4.索引的算法:Hash和Btree
Hash索引:适合等值查找,在范围查找时有可能发生Hash冲突;
Btree索引:适合范围查找,没有Hash冲突问题。
二.验证索引对查询、插入速度的影响
实验工具:Nacivat for MySQL
eclipse
实验步骤:
(一)实验环境建立:
1.使用 Nacivat for MySQL 创建表格 tb_Test;
其中,id为检测列,即用于插入索引的列。
2.利用eclipse连接MySQL操作表格 tb_Test ;
①eclipse代码:
package forJDBC; import static org.junit.Assert.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLEx 4000 ception; 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):
(二)验证过程:
1.当 id 列未添加索引时,运行 testSearch( ) 方法,查找“idxxx255”,耗费2.880s;
2.应用Navicat for MySQL对id列添加索引,再次查找“idxxx255”,耗时1.036s;
※发现:查找相同内容,有索引时可以大大缩短查询时间。
3.运行 testDelete( ) 删除表内记录,并删去索引;
4.更改代码中i值为10000,再次运行 testInsert( ),向没有索引的表中插入10000组数据,耗时1.371s;
5.再次运行 testDelete( ) 删除所有数据后,对 id 列添加索引后,再次插入500组同样的数据,耗时1.817s;
※发现:插入相同数据,无索引比有索引时速度快一些。
三.结论
经过验证,索引的使用对查询和插入速度有以下影响:查询时,使用带有索引的列可以明显提高查询速度;
插入时,如果插入的列有索引,插入速度会减慢。
四.分析讨论
经过本次实验,在以后创建表格时,尽量保证将主要的数据都插入之后,再添加索引,避免在添加索引之后进行数据插入,以保证效率最高。注意:删除时,要把代码前面的插入和查询代码注释掉,防止删除失败。
相关文章推荐
- 使用jdbc向数据库中插入多条记录,探究有索引和没有索引的查询速度的代码
- mysql使用索引为什么查询速度变快很多?
- mysql使用索引为什么查询速度变快很多?
- mysql在数据库表中,使用索引可以大大提高查询速度
- 在SQLite中使用索引优化查询速度
- mysql慢查询Slow Query Log和未使用索引(Not Using Indexes)查询配置和使用
- mysql中索引使用不当速度比没加索引还慢的测试
- 在SQLite中使用索引优化查询速度(转)
- MySQL查询优化讲座之使用索引
- 在MySQL查询中忽略或强制使用某索引
- MYSQL查询优化:使用索引
- mysql的索引使用不当速度比没加索引还慢
- 合理使用MySQL索引建立高质量查询语句
- 合理使用MySQL索引建立高质量查询语句
- mysql中索引使用不当速度比没加索引还慢的测试
- 在SQLite中使用索引优化查询速度
- MySQL查询优化技术系列讲座之使用索引(二)
- [MySQL] 用索引加速你的查询速度
- 在SQLite中使用索引优化查询速度
- MySQL查询优化技术之使用索引