很多小的sql语句会大大减慢我的网站速度

很多小的sql语句会大大减慢我的网站速度

问题描述:

I have a zen-cart with 25-30 categories. On the front page of the site each category will get it's own little area in which the 4 best selling products in that category will be displayed.

Being a bit of a noob the best way I can think of doing this is to have a single sql statement for each category which picks out the best sellers each time the page loads. I've read somewhere that you're best to limit the amount of sql statements so as not to affect site speed.

Would doing it this way be very much slower?

Is there a more sensible way of doing this?

我有一个包含25-30个类别的zen-cart。 在网站的首页上,每个类别都会得到它自己的小区域,其中将显示该类别中的4个最畅销的产品。 p>

作为一个菜鸟是最好的方式我 可以想到这样做是为每个类别都有一个单独的sql语句,每次加载页面时都会选出最畅销的。 我在某处读过你最好限制sql语句的数量,以免影响网站速度。 p>

这样做会慢得多吗? p>

有更合理的方法吗? p> div>

That highly depends on the server backing your website. But 25 to 30 queries shouldn't have a major impact on the page performance.

A better way would be to design a database query that fetches all the items with one query. But to help you doing so we'd need the schema of your database.

Also in case you have to do many similar queries there is the options to use "prepared queries" that usually speed up the performance a good bit.

After all generally speaking, if you are new to working with database, its often better to design a few more small queries that are easily structures and can be handled by you, then one large query that has the potential to contain errors and maybe slows the database down more then then multiple small queries.