一个项目系统的调优
最近公司在用一个类似于BLOG的一个系统来管理项目, PHP+MYSQL架构,同事反应这个系统打开项目的时候非常慢,由于这个系统的服务器在美国,为了查找问题,首先从ftp站点把源文件下载下来,然后用phpmyadmin把mysql数据库的文件备份下来,这样就可以在本地虚拟机搭建起了一个测试环境,
ubuntu10.4+apache2+mysql+php
开始做一些简单配置系统就开始运行,可能跟源系统的具体配置不一样,php运行起来还是有点错误,结果导致apache2的错误日志文件急速增长,最后通过在php语句中添加错误屏蔽语句才解决这个,这样系统总算能运行起来了,
接下来开始优化一些mysql的参数变量,做了些修改以后还是觉得没有明显的改善。
设置慢查询,查询结果如下:
mysqldumpslow mysql-slow.log,发现下面这条语句耗时最多,而且需要重复运行很多遍
sbvgroup@ubuntu104:/var/log/mysql$ mysqldumpslow mysql-slow.log
Reading mysql slow query log from mysql-slow.log
Count: 29 Time=3.68s (106s) Lock=0.00s (0s) Rows=646.8 (18757), root[root]@localhost
SELECT
prj_id, dep_id
FROM
sys_projeto,
sys_grupo,
sys_grupo_projeto,
sys_usuario_grupo,
sys_usuario,
sys_departamento,
sys_departamento_projeto,
sys_usuario_projeto_departamento
WHERE
prj_remove <> 'S'
AND grp_remove <> 'S'
AND grpr_remove <> 'S'
AND usu_id = '100048' AND usu_remove <> 'S'
AND usug_remove <> 'S'
AND dep_remove <> 'S'
AND depp_remove <> 'S'
AND usupd_remove <> 'S'
AND usu_id = usug_idusuario
AND usug_idgrupo = grp_id
AND grp_id = grpr_idgrupo
AND grpr_idprojeto = prj_id
AND usu_id = usupd_idusuario
AND prj_id = usupd_idprojeto
AND prj_id = depp_idprojeto
AND dep_id = usupd_iddepartamento
AND dep_id = depp_idderpatamento
AND usupd_iddepartamento <> '100048'
UNION
SELECT
prj_id, dep_id
FROM sys_projeto,
sys_usuario,
sys_departamento,
sys_departamento_projeto
WHERE prj_remove <> 'S'
AND usu_remove <> 'S'
AND dep_remove <> 'S'
AND depp_remove <> 'S'
AND dep_id = depp_idderpatamento
AND depp_idprojeto = prj_id
AND usu_id = '100048'
AND prj_idgerente = usu_id
然后针对语句条用的表字段做了些索引,结果得到改善,本人对sql语句不是很精通,可能是这条语句本身的执行效率就不高,再加上没有适当索引,所以导致系统运行很慢。