打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
php – MySQL查询性能和Codeigniter

我有这个MySQL查询,我正在加载到我的家庭控制器,并在运行Codeigniter的$this-> output-> enable_profiler(TRUE);我的执行时间为5.3044

我的模型中的查询:

class Post extends CI_Model {    function stream($uid, $updated, $limit) {        $now = microtime(true);        $sql = "            SELECT                 *             FROM                 vPAS_Posts_Users_Temp             WHERE                 post_user_id = ?                 AND post_type !=4                 AND post_updated > ?                AND post_updated < ?             UNION             SELECT                 u.*             FROM                 vPAS_Posts_Users_Temp u             JOIN                 PAS_Follow f                 ON f.folw_followed_user_id = u.post_dynamic_pid                 WHERE u.post_updated > ?                AND post_updated < ?                 AND (( f.folw_follower_user_id = ? AND f.folw_deleted = 0 )                 OR ( u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by = ? AND u.post_user_id != ? AND u.post_type =4 ))             ORDER BY                 post_posted_date DESC             LIMIT ?        ";         $query = $this->db->query($sql, array($uid, $updated, $now, $updated, $now, $uid, $uid, $uid, $limit));        return $query->result();    }}

我可以在这做任何事情来改善执行时间,从而增加我的页面加载吗?

编辑

解释结果

MySQL Workbench Visual Explain

解决方法:

也许你不会相信它,但不要在你的SQL中检索SELECT *.只需写下你想要检索的字段,我认为它会加速很多.

我已经看到在执行查询时(从0.4秒到0.02秒)的速度增加超过20次,只需更改*为必填字段.

其他事项:如果表中的INSERT上有auto_increment id,请不要将post_posted_date用作ORDER字段.按DATETIME字段排序很慢,如果您可以使用INT id(希望您将作为索引),您将更快地获得相同的结果.

UPDATE

根据问题的要求,技术原因:

>不使用SELECT *:Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc.这适用于SQL,但适用于MySQL(不像之前的问题那样完整)mySQL Query – Selecting Fields
>对于按Datetime排序:SQL,SQL Server 2008: Ordering by datetime is too slow,以及与MySQL相关的:MySQL performance optimization: order by datetime field

奖励:学习如何设置索引:http://ronaldbradford.com/blog/tag/covering-index/

来源:https://www.icode9.com/content-2-264751.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
mysql根据A表更新B表的方法
MySql快速插入以及批量更新
mysql 批量更新与批量更新多条记录的不同值实现方法
[转]MySQL的replace into
Mysql 千万以上数据优化方法(一,SQL优化),月薪30K之路系列
MySql中4种批量更新的方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服