索引的效果测试

来源:互联网 发布:武汉百纳信息知乎 编辑:程序博客网 时间:2024/05/21 13:58

首先用navicat Lite 连接MYSQL数据库在数据库中建立一个表,表名tb_student

并将StudentID建立索引,索引方式为btree

建立一个表名为,tb_student1,列相同,无索引

接着用JDBC连接数据库,并像其中循环插入数据,一次40000条

代码如下

import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class TestStatement {   public static void main(String[] args) throws SQLException {Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xiao", "root", "root");Statement stat=conn.createStatement();StringBuilder sb=new StringBuilder();for(int i=0;i<40000;i++) {sb.append("('lilei"+i+"',"+i+",18)");if(i!=39999)sb.append(",");}//System.out.println(sb.toString());stat.executeUpdate("insert into tb_student(StudentName,StudentID,age) values"+sb.toString());}}
接着运行6次,然后,再像tb_student1中循环插入6次数据。


接着进行有无索引的查询速度对比,查询语句如下图



测试结果,无索引等值查找在0.150ms到0.190ms之间,而有索引的等值查找为0.000ms和0.001ms。

索引的效果在40000*6=240000条记录的情况下,节省了0.15ms的时间。。-_-!




而非等值有无索引查找的测试结果,对你没看错,经过多次测试,结果就是没什么区别




结论就是,索引。在数据量不大的时候。。真的没什么用,至于多大有用,我测试不出来,欢迎大佬告诉。

原创粉丝点击