论mysql中有“索引”和没有“索引”的查询速度
来源:互联网 发布:可以美化图片的软件 编辑:程序博客网 时间:2024/05/21 10:03
索引是数据库的一个对象,索引在数据库中起到向导作用。其主要目的是为了提高对数据库中数据的查询速度,例如:我们在咋点钟查询一个汉字时,通常会先利用文字的偏方部首,汉语拼音来查询,在数据库中,索引就起到了偏旁部首和拼音的作用。但也会降低数据的插入速度。
索引一般会应用在有主键和唯一约束的情况中,在频繁使用对数据库进行查询时索引的应用至关重要。mysql中一般有两种索引方式,一种是hash索引,另一种是btree索引,其中hash索引较适合等值查询,btree适合范围查询。
如下是利用java通过jdbc的链接对数据库进行的有索引和无索引查询,并查看其速度:
1、数据库的连接
package forjdbc;import static org.junit.Assert.*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import org.junit.After;import org.junit.Before;import org.junit.Test;public class Testsuoyin { private static Connection conn;@Beforepublic void setUp() throws Exception {conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/for1031","root","root");}@Afterpublic void tearDown() throws Exception {conn.close();}
2、向数据库中的表格中插入1万条数据
@Testpublic void test() throws SQLException {Statement st=conn.createStatement();for(int i=0;i<100000;i++){st.execute("insert into stu (id,name) values ("+i+",'haha"+i+"')");//向表stu中插入数据}}
3、有索引查询
@Testpublic void test1() throws SQLException {Statement st=conn.createStatement();long l1=System.currentTimeMillis();//查询千的时间 st.executeQuery("select * from stu where id=50012");//id是索引,此处为有索引查询 long l2=System.currentTimeMillis();//查询后的时间 System.out.println(l2-l1);}
4、无索引查询
@Testpublic void test2() throws SQLException {Statement st=conn.createStatement();long l1=System.currentTimeMillis();//查询千的时间 st.executeQuery("select * from stu where name='haha50012'");//name不是索引,此处为无索引查询 long l2=System.currentTimeMillis();//查询后的时间 System.out.println(l2-l1);}}
查询结果:
通过结果不难看出有索引的查询速度比不用索引的查询速度快。
阅读全文
0 0
- 论mysql中有“索引”和没有“索引”的查询速度
- 比较有索引和无索引的查询速度(在mysql数据库中)
- 使用jdbc向数据库中插入多条记录,探究有索引和没有索引的查询速度的代码
- 1、Mysql:mysql简单的索引和in、or、union unionall语句查询速度
- mysql索引提升查询速度的实例测试
- MySQL索引对数据库查询速度的显著提升
- mysql简单的索引和in、or、union unionall语句查询速度
- 提高查询速度--数据库设计mysql索引
- 提高查询速度--数据库设计mysql索引
- MySQL > 建索引提高查询速度
- 通过建立索引优化MySQL查询速度
- 查询没有索引的表
- 【MySQL】连接查询中索引的重要性
- mysql连接查询中索引的重要性
- mysql中关于索引的一些查询
- mysql连接查询中索引的重要性
- mysql 索引的使用和查询语法
- Mysql的索引和查询优化
- html5图片上传【文件上传】
- return *this和return this
- 今日工作总结——调试Arduino——20171107
- Java并发之信号量
- vscode: Visual Studio Code 常用快捷键
- 论mysql中有“索引”和没有“索引”的查询速度
- boost编译
- c语言函数知识
- Another app is currently holding the yum lock; waiting for it to exit...
- 20171106
- HTML CSS学习笔记
- 关于Android seekTo的一点记录
- 使用共享内存和信号量实现数据通信
- mybaitls小细节之#与$