1.memcached
memcached 是一个高效的分布式的内存对象缓存系统 ,他可以支持把各种php的数据(array,对象,基本数据类型)放入到它管理的内存中.注:需通过脚本定时清除缓存,防止缓存过大影响网站性能
示例代码:
conn.php
memcache_getid.php
connect('127.0.0.1', 11211) or die ("连接失败");
//$memcache->flush(); 清除缓存
if($info=$memcache->get($id))
{
echo $info;
exit;
}
else
{
$result=mysql_query("select * from user where id=$id");
if($result)
{
$arr=mysql_fetch_array($result);
echo "need mysql query";
$memcache->add($id,$arr['id'],MEMCACHE_COMPRESSED,60*60*24);
}
}
?>
2.页面静态化技术
a.真静态化
1.创建模板文件template.html
2.通过模板文件,创建静态页面的 php文件 xx.php
3. 用户访问生成的静态页面 xx.html
立即学习“PHP免费学习笔记(深入)”;
newsAction.php
tmp->html 逐行读取template.tpl文件,然后逐行替换
while(!feof($fp_tmp)){
//读取一行.
$row=fgets($fp_tmp);
//替换(小函数)
$new_row=replace($row,$title,$content);
//把替换后的一行写入到html文件
fwrite($fp_html_file,$new_row);
}
//关闭文件流
fclose($fp_tmp);
fclose($fp_html_file);
echo "添加到数据库并成功创建html文件返回列表";
}
mysql_close($conn);
}
?>
show_news.php
获取文件的最后修改时间
//filemtime($html_filename)+30>time() 表示静态文件,
// if(file_exists($html_filename)&& filemtime($html_filename)+30>time()){
//
// //直接访问html页面(把html页面的内容 echo 浏览器)
// echo file_get_contents($html_filename);
// exit;
// }
//
// $conn=mysql_connect("localhost","root","root");
//
// if(!$conn){
// die("连接失败");
// }
//
// mysql_select_db("spdb1",$conn);
//
//
// $sql="select * from news where id=$id";
// $res=mysql_query($sql);
// //开启ob缓存
// ob_start();
// if($row=mysql_fetch_assoc($res)){
//
// header("content-type:text/html;charset=utf-8");
// echo "| 新闻详细内容 |
| {$row['title']} |
| {$row['content']} |
b.伪静态化
环境配置:#LoadModule rewrite_module modules/mod_rewrite.so 在httpd.conf去掉改项#,并项目目录下配置.htaccess文件
.htaccess#写你的rewrite规则 RewriteEngine On #news-id(\d+)\.html$ 是规则 news.php?id=$1 是转发的页面 #正则 子表达式 捕获 反向引用 # "news-id33.html" # 可以配置多个规则,匹配的顺序是从上到下 RewriteRule news-id(\d+)\.html$ news.php?id=$1 RewriteRule news-id(\d+)\.html$ error.php
①真静态访问效率高,利于seo.可以减少对数据库的操作。但是会占用大量的磁盘.
②伪静态一、可以方便的实现对搜索引擎的优化,二、占空间比较小。三、通过生成不同view-id2.hmtl 可以实现内容的变化.四有效的防止了注入攻击
注:但是两者在启用页面缓存时(ob_start)需要注意一个问题,不要需要经常修改的html文件放入页面缓存,否则会造成页面无法刷新得到最新结果,页面缓存一般存放经常被查询的html且不会被更新
c.mysql优化技巧
配置慢查询日志:
在my.ini最下面配置
log-slow-queries = e:/wamp/logs/mysql_slow_query.log long_query_time=2
通过 show status/variables like '%query%'' 查看是否配置成功(即slow_query_log=ON)
分析慢查询日志
通过select sleep(4);测试
通过explain 慢sql语句或mysqldumpslow 慢查询日志
查询sql语句状态
set profilling=on; show profiles; show profile for query id;
1. 使用order by null 禁用排序(默认为filesort)
比如 select * from dept group by ename order by null
2. 在精度要求高的应用中,建议使用定点数(decimal)来存储数值,以保证结果的准确性
3.表的水平划分/垂直分割











