数据库测试

发布于 2021-03-21  575 次阅读


为了完成数据库中的索引测试,我模拟大数据往数据库中插入大量数据,最后观察在使用索引和不使用索引两种情况下的性能;

准备工作:

连接数据库

生成随机字符串

插入近3w条数据;

代码保存:

package com.yang.Insert;

import java.sql.*;
import java.util.Random;

class Okk {
    public static void main(String[] args) {
        Connection conn = null;
        // 3执行sql
        // 获得执行sql的对象
        Statement statement;
        statement = null;
        // 执行sql
        ResultSet rs = null;
        try {
            // 1 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2 建立连接

            conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai","root","123456789");;

            statement = conn.createStatement();
            System.out.println("成功");
            // 编写sql语句
            for(int i = 0 ; i < 1000000; i++) {
                String s = "insert into indexdemo value(rand()*100000000,substring(MD5(RAND()),1,30),substring(MD5(RAND()),1,3))";
                statement.execute(s);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
        }

    }

}

我测试到六万条,发现性能提升基本无明显变化,看来需要大数据的时候再使用索引较好!

 

 

 

 

mysql 索引 性能提升_亲身体验MySQL的索引对搜索性能的提升

桃子胖 2021-02-18 06:03:46 收藏
文章标签: mysql 索引 性能提升
版权
1,创建一个user表,包含两列name,phone 2,用python(你喜欢的任何语言)插入100W条记录(lz的笔记本比较老,大概用了1分钟吧): #!/usr/bin/env python# -*- coding:utf-8 -*-import MySQLdbconn = MySQLdb.connect(host='localhost',user='root',db='milli

1,创建一个user表,包含两列name,phone

2,用python(你喜欢的任何语言)插入100W条记录(lz的笔记本比较老,大概用了1分钟吧):

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import MySQLdb

conn = MySQLdb.connect(host='localhost',user='root',db='millionMessage')

cur = conn.cursor()

for i in range(1,1000000):

uname = "user" + str(i)

uphone = "188000" + str(i)

sql = "insert into user(name,phone) values('%s','%s')" % (uname,uphone)

cur.execute(sql)

conn.commit()

cur.close()

conn.close()3,在没建立索引的情况下搜索:

mysql> select * from user where name='user55555';

+-------+-----------+-------------+

| uid | name | phone |

+-------+-----------+-------------+

| 55567 | user55555 | 18800055555 |

+-------+-----------+-------------+

1 row in set (0.53 sec)

mysql> select phone from user where name='user55555';

+-------------+

| phone |

+-------------+

| 18800055555 |

+-------------+

1 row in set (0.46 sec)

4,对name属性建立索引:

mysql> alter table user add index index_username(name);

Query OK, 0 rows affected (22.27 sec)

Records: 0 Duplicates: 0 Warnings: 0

5,查询:

mysql> select * from user where name='user55555';

+-------+-----------+-------------+

| uid | name | phone |

+-------+-----------+-------------+

| 55567 | user55555 | 18800055555 |

+-------+-----------+-------------+

1 row in set (0.00 sec)

mysql> select * from user where name='user999999';

+---------+------------+--------------+

| uid | name | phone |

+---------+------------+--------------+

| 1000011 | user999999 | 188000999999 |

+---------+------------+--------------+

1 row in set (0.00 sec)

结果秒出。可见在海量数据的数据库上,索引对搜索性能的提升是非常大的。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!
————————————————
版权声明:本文为CSDN博主「桃子胖」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_26911099/article/details/114336700


她喜欢所以就做咯