(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)参考博客:
本文通过实例分析,发现登录页面加载耗时主要由特定SQL语句引起,进一步定位到数据库远程连接为根本原因。通过调整MySQL配置文件my.ini中的[mysql]属性,禁用DNS主机名解析或增加主机名缓存大小,有效提升了数据库连接速度。
483

被折叠的 条评论
为什么被折叠?



