First in the Java program to insert data into the database, because to use the index, a small amount of data is not able to test the effect of the index, so to insert a large number of data for testing, this time we insert 500,000 records
The Java program is as follows: Create a table classfor, input 500,000 records into it
Package com.oracle.jdbc1.test;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;
public class Testupdate {public
static void Main (string[] args) {
try {
Connection conn= Drivermanager.getconnection ("jdbc:mysql://localhost:3306/for1703a", "root", "root");
Statement stat=conn.createstatement ();
StringBuilder sb=new StringBuilder ();
for (int i=0;i<500000;i++)
{
sb.append ("+ (i+1) +", ' Wowo "+ (i+1) +" ', "+i+") ");
if (i!=499999)
{
sb.append (",");
}
}
System.out.println (Sb.tostring ());
int num=stat.executeupdate ("Insert into classfor1 (id,name,number) values" +sb.tostring ());
SYSTEM.OUT.PRINTLN (num);
Conn.close ();
} catch (SQLException e) {
e.printstacktrace ();}}}
Test without indexing first:
The above is no index equivalent query, spents 0.144ms;
The above is no index range query, spents 0.303ms;
Next, the index query: Here's a question. You should be aware that when you select the index, you should pay attention to the choice of the engine, because some engines only support one index, so I chose two index compatible memory engine, the following figure:
We first test the Bterr index to query:
Btree Index of equivalent search: Spents 0.001ms;
Btree Index Search: spents 0.001ms;
Next, hash index query:
Hash index method of equivalent query: Spents 0.001ms;
Hash index method Range query: Spents 0.001ms;
According to the data show, in hundreds of thousands of records and even more data to query the desired results, the use of index query is very necessary, can save time and improve a certain degree of efficiency.
I have seen in a blog that the hash and btree indexes are different, and hash is more suitable for equivalent lookup, for example = OR. =, not suitable for range query, and btree more suitable for range query because it has no hash conflict. But this experiment did not test the difference between the two, I guess the amount of data is still not enough, but my computer too, I inserted 500,000 data program running for more than 10 minutes. The size of the data I am afraid of the computer will burn, I am just a beginner entry-level, but also invited the big guys to the wrong place a lot of criticism, under the gratitude.