简体   繁体   English

如何使我正在处理的网站更快地加载?

[英]How can I make the website I'm working on load faster?

I am working on a website that I "inherited" in my job and it currently uses PHP/HTML/CSS/JS and it takes AGES to load, but the thing is without the PHP it loads really fast.我正在开发一个我在工作中“继承”的网站,它目前使用 PHP/HTML/CSS/JS 并且需要 AGES 才能加载,但是没有 PHP 加载非常快。 I, however, am not very experienced with PHP so I don't know exactly where should I tweak it for it to work faster.但是,我对 PHP 不是很有经验,所以我不知道应该在哪里调整它以使其更快地工作。 First at the very top of the page I have this code to preload everything:首先在页面的顶部,我有这段代码来预加载所有内容:

<?

?>

Then further down a table gets made using this code (which I know I can get better just don't know how exactly since tables on the db seem to be unrelated and they specifically asked me not to touch the db.然后使用此代码进一步制作一个表(我知道我可以变得更好只是不知道如何准确,因为数据库上的表似乎不相关,他们特别要求我不要触摸数据库。

<?
        $count = 0;
        foreach ($domains as $row){
            $count++;
    ?>

a little of html goes here in between (mostly rows of a table) and then:一点 html 介于两者之间(主要是表格的行),然后:

<? 
     } ?>

So after about 2 days messing with the site I know my html/css/js is optimized but the php keeps making me wait around 5 - 10 seconds for everything to load (sometimes even longer) which of course is unacceptable, could anybody please help me out here?因此,大约 2 天后,我知道我的 html/css/js 已优化,但 php 让我等待大约 5 - 10 秒才能加载所有内容(有时甚至更长),这当然是不可接受的,任何人都可以帮忙我在这里?

EDIT编辑

I tried taking some of the connections outside from the foreach loop but I get an error if I do so =/我尝试从 foreach 循环之外获取一些连接,但如果这样做会出现错误 =/

Use a profiler like xhprof or xdebug +kcachegrind to find the places of your code that take a long time (or are executed too often) and optimize them.使用诸如xhprofxdebug +kcachegrind 之类的分析器来查找需要很长时间(或执行过于频繁)的代码位置并优化它们。

First you should start by stopping writing queries in the foreach loop.首先,您应该首先停止在foreach循环中编写查询。

Get rid of queries which use LIKE statements.摆脱使用LIKE语句的查询。

Then you should recheck the indexes for the given tables.然后您应该重新检查给定表的索引。 Rule of thumb would be that all rows which appear in WHERE and GROUP BY clauses should be indexed ( well.. there are exceptions, but you should be able to figure on case-to-case base ).经验法则是所有出现在WHEREGROUP BY子句中的行都应该被索引(嗯..有例外,但你应该能够根据具体情况进行计算)。

And you could get rid of the switch statement and replace it with array lookup.你可以去掉switch语句并用数组查找替换它。

.. well.. my 2 cents. ..好吧..我的2美分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM