Mysql database index creation and performance testing bitsCN.com
# --------- Mysql learning (IV) index creation --------###
# I suddenly got started today, so I need to add some indexing knowledge.
# Create an index. here, the mytab table with less data is used as an example:
# Original data:
Mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
Mysql> select * from mytab;
+ ---- + -------- + ----- + -------- +
| Id | name | age | salary |
+ ---- + -------- + ----- + -------- +
| 1 |? Joan | 23 | 1000 |
| 2 | Qiu Shui shrimp | 24 | 500 |
| 3 | attackers | 22 | 100 |
+ ---- + -------- + ----- + -------- +
3 rows in set (0.00 sec)
# Alter table table_name add index index_name (column) =
# Create index index_name on table_name (column );
# Alter index creation example
Mysql> alter table mytab add index mytab_name (name );
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0
# Create an index example:
Mysql> create index mytab_id on mytab (id );
Query OK, 3 rows affected (0.16 sec)
Records: 3 Duplicates: 0 Warnings: 0
# Viewing indexes
Mysql> show index from mytab;
+ ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------
------- + ---------- + -------- + ------ + ------------ + --------- +
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi
Nality | Sub_part | Packed | Null | Index_type | Comment |
+ ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------
------- + ---------- + -------- + ------ + ------------ + --------- +
| Mytab | 0 | PRIMARY | 1 | id | A |
3 | NULL | BTREE |
| Mytab | 1 | mytab_id | 1 | id | A |
3 | NULL | BTREE |
+ ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------
------- + ---------- + -------- + ------ + ------------ + --------- +
2 rows in set (0.00 sec)
# Create a unique index
Mysql> alter table mytab add unique (name );
Query OK, 3 rows affected (0.20 sec)
Records: 3 Duplicates: 0 Warnings: 0
# Create a joint index:
Mysql> create index mytab_id_name on mytab (id, name );
Query OK, 3 rows affected (0.20 sec)
Records: 3 Duplicates: 0 Warnings: 0
Mysql> show index from mytab;
+ ------- + ------------ + --------------- + -------------- + ------------- + ----------- +-
------------ + ---------- + -------- + ------ + ------------ + --------- +
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |
Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+ ------- + ------------ + --------------- + -------------- + ------------- + ----------- +-
------------ + ---------- + -------- + ------ + ------------ + --------- +
| Mytab | 0 | PRIMARY | 1 | id | A |
3 | NULL | BTREE |
| Mytab | 0 | name | 1 | name | A |
3 | NULL | BTREE |
| Mytab | 1 | mytab_name | 1 | name | A |
3 | NULL | BTREE |
| Mytab | 1 | mytab_id_name | 1 | id | A |
3 | NULL | BTREE |
| Mytab | 1 | mytab_id_name | 2 | name | A |
3 | NULL | BTREE |
+ ------- + ------------ + --------------- + -------------- + ------------- + ----------- +-
------------ + ---------- + -------- + ------ + ------------ + --------- +
5 rows in set (0.00 sec)
# Next we will try to delete the index and delete it with drop
# Drop index index_name on table_name =
# Alter table table_name drop index index_name;
# Drop example:
Mysql> drop index mytab_id on mytab;
Query OK, 3 rows affected (0.17 sec)
Records: 3 Duplicates: 0 Warnings: 0
# Alter example:
Mysql> alter table mytab drop index mytab_id_name;
Query OK, 3 rows affected (0.17 sec)
Records: 3 Duplicates: 0 Warnings: 0
# It is found that the value of an index cannot be determined due to the small amount of data.
#
# Here I plan to add 3000 rows of data to it. Java code is required:
#
| 3001 | yiha_2997 | 22 | 5997 |
| 3002 | yiha_2998 | 22 | 5998 |
| 3003 | yiha_2999 | 22 | 5999 |
+ ------ + ----------- + ----- + -------- +
3003 rows in set (0.01 sec)
####################### Java code segment ############### ###############
Public static void main (String [] args ){
Connection conn = DBConnection. getConnection ();
Try {
Conn. setAutoCommit (false );
PreparedStatement state = conn. prepareStatement
("Insert into mytab (name, age, salary) values (?,?,?) ");
For (int I = 0; I <3000; I ++ ){
State. setString (1, "yiha _" + I );
State. setInt (2, 22 );
State. setInt (3, 3000 + I );
State. addBatch ();
}
State.exe cuteBatch ();
Conn. commit ();
State. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
###################### Database connection ############### #######
Private static String url = "jdbc: mysql: //" +
"Localhost: 3306/mydb? UseUnicode = true & characterEncoding = UTF-8 ";
Private static String driver = "com. mysql. jdbc. Driver ";
Private static String name = "root ";
Private static String pwd = "root ";
Public static Connection getConnection (){
Connection conn;
Try {
Class. forName (driver). newInstance ();
Conn = DriverManager. getConnection (url, name, pwd );
Return conn;
######################################## ###########################
# There are currently 3003 data records in the database. let's take a look at the data retrieval time.
# Search:
Id NAME age salary
| 2894 | yiha_2890 | 22 | 5890 |
# Id and name are indexed, but age and salary are not indexed
Mysql> select * from mytab where id = 2894;
+ ------ + ----------- + ----- + -------- +
| Id | name | age | salary |
+ ------ + ----------- + ----- + -------- +
| 2894 | yiha_2890 | 22 | 5890 |
+ ------ + ----------- + ----- + -------- +
1 row in set (0.00 sec)
Mysql & gt; select * from mytab where salary = 5890;
+ ------ + ----------- + ----- + -------- +
| Id | name | age | salary |
+ ------ + ----------- + ----- + -------- +
| 2894 | yiha_2890 | 22 | 5890 |
+ ------ + ----------- + ----- + -------- +
1 row in set (0.00 sec)
# It can be seen that there is no difference. Maybe the data is still too small, and now the data is increased to 30000;
Mysql> select * from mytab where id = 30000; # id is the index
+ ------- + ------------ + ----- + -------- +
| Id | name | age | salary |
+ ------- + ------------ + ----- + -------- +
| 30000 | yiha_29996 | 23 | 32996 |
+ ------- + ------------ + ----- + -------- +
1 row in set (0.00 sec)
Mysql> select * from mytab where salary = 32996; # salary is not indexed
+ ------- + ------------ + ----- + -------- +
| Id | name | age | salary |
+ ------- + ------------ + ----- + -------- +
| 30000 | yiha_29996 | 23 | 32996 |
+ ------- + ------------ + ----- + -------- +
1 row in set (0.02 sec)
# Because name is also an index, try to use name to search for data:
Mysql> select * from mytab where name = 'yiha _ 000000'; # name indicates the index.
+ ------- + ------------ + ----- + -------- +
| Id | name | age | salary |
+ ------- + ------------ + ----- + -------- +
| 30000 | yiha_29996 | 23 | 32996 |
+ ------- + ------------ + ----- + -------- +
1 row in set (0.00 sec)
# Although the index function can be seen in multiple data experiments, it is not very obvious. The time consumed by each of the above groups is
# The maximum number of times a person looks.
# I personally feel that the index test is boring. many articles only write the index for exact search.
# It seems that there are very few related things to use. The data in the database can be expanded at will. I personally feel like this first.
BitsCN.com