为什么远程连接数据库速度会很慢?

(1)点击登陆后,跳转到首页(index.jsp)耗时9973ms,追根溯源,发现是sql语句中select * xxxx,就用了9000ms时间。

 <resultMap type="User" id="userResultMap">
        <id column="user_id" property="userId"/>
        <result column="loginname" property="loginname"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="rights" property="rights"/>
        <result column="status" property="status"/>
        <result column="role_id" property="roleId"/>
        <result column="parent_id" property="parentId"/>
    </resultMap> 
<select id="getUserByNameAndPwd" parameterType="User" resultMap="userResultMap">
        select * from tb_user 
            <where>
                <if test="loginname!=null and password!=null">
                    loginname = #{loginname} and password=#{password}
                </if>
            </where>
    </select>

但是

这里写图片描述

(2)分析问题:

Java程序中发现罪魁祸首就是,方法读取数据很耗时

这里写图片描述

sql很简单,不是sql的原因。

ssm框架我也怀疑过,但也不是它的原因。

最终我换了一个数据库连接,问题就解决了。

所以得出结论:数据库远程连接的问题

(3)解决问题:

在数据库配置文件my.ini(Windows下)中,修改[mysql]属性,看图

这里写图片描述

说明:How MySQL uses DNS
When a new thread connects to mysqld, mysqld will spawn a new thread to handle the request. This thread will first check if the hostname is in the hostname cache. If not the thread will call gethostbyaddr_r() and gethostbyname_r() to resolve the hostname.
If the operating system doesn’t support the above thread-safe calls, the thread will lock a mutex and call gethostbyaddr() and gethostbyname() instead. Note that in this case no other thread can resolve other hostnames that is not in the hostname cache until the first thread is ready.
You can disable DNS host lookup by starting mysqld with –skip-name-resolve. In this case you can however only use IP names in the MySQL privilege tables.
If you have a very slow DNS and many hosts, you can get more performance by either disabling DNS lookop with –skip-name-resolve or by increasing the HOST_CACHE_SIZE define (default: 128) and recompile mysqld.
You can disable the hostname cache with –skip-host-cache. You can clear the hostname cache with FLUSH HOSTS or mysqladmin flush-hosts.
If you don’t want to allow connections over TCP/IP, you can do this by starting mysqld with –skip-networking.
根据文档说明,如果你的mysql主机查询DNS很慢或是有很多客户端主机时会导致连接很慢,由于我们的开发机器是不能够连接外网的,所以DNS解析是不可能完成的,从而也就明白了为什么连接那么慢了。同时,请注意在增加该配置参数后,mysql的授权表中的host字段就不能够使用域名而只能够使用 ip地址了,因为这是禁止了域名解析的结果。
(4)参考博客:

1 连接mysql数据库速度很慢的原因1
2 连接mysql数据库速度很慢的原因2

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL数据库访问变慢可能是由多种原因导致的,这些原因可以分为性能瓶颈、配置不当、索引问题、网络延迟、硬件限制或并发问题等几个方面。以下是一些常见的原因和解决策略: 1. **查询优化问题**:SQL查询可能过于复杂或者没有有效的索引,导致全表扫描。检查是否存在不必要的JOIN操作,或者使用EXPLAIN分析查询执行计划。 2. **索引问题**:索引是加快查询速度的关键,如果关键字段没有合适的索引,查询将变得非常慢。确保对经常用于WHERE、JOIN和ORDER BY的列创建了适当的索引。 3. **内存限制**:如果内存不足,MySQL可能进行大量的磁盘I/O,降低性能。检查 Innodb_buffer_pool_size 配置是否足够,以及缓存命中率。 4. **硬件性能**:服务器的CPU、硬盘I/O或者内存速度过慢也可能影响数据库性能。考虑升级硬件资源。 5. **网络延迟**:如果数据存储在远程服务器,网络延迟可能导致通信时间过长。检查网络连接质量并优化网络配置。 6. **并发问题**:大量并发请求可能导致锁竞争,特别是InnoDB引擎。优化事务处理,减少死锁和锁定时间。 7. **数据库配置**:MySQL的系统参数设置不合理,比如max_connections限制过高或过低,也影响性能。适当调整参数。 8. **数据库碎片化**:随着数据的增长,表可能变得碎片化,影响查询效率。定期执行OPTIMIZE TABLE或ANALYZE TABLE来维护表的完整性。 9. **事务隔离级别**:高事务隔离级别可能导致更多的锁定,影响并发性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值