开发者

Retrieving Records : Array VS DB

开发者 https://www.devze.com 2023-03-18 06:15 出处:网络
Concern about my page loading speed, I know there are a lot of factors that affect page loading time.

Concern about my page loading speed, I know there are a lot of factors that affect page loading time.

Does retr开发者_StackOverflowieving records (Categories) in a array instead of DB is faster?

Thanks


It is faster to keep it all in PHP till you have an absurd amount of records and you use up RAM.

BUT, both of these things are super fast. Selecting a handful of records on a single table that has an index should take less than a msec. Are you sure that you know the source of your web page slowness?

I would be a little bit cautious of having your Data in your code. It will make your system less maintainable. How will users change categories?

THis gets back to deciding if you want your site static versus dynamic.


Yes of course retrieving data from an array is much faster than retrieving data from a Database, but usually arrays and databases have totally different use cases, because data in an array is static (you type the value in code or in a separate file and you can't modify them) while data in a database is dynamic


Yes, it's probably faster to have an array of your categories directly in your PHP script, especially if you need all the categories on every page load. This makes it possible for APC to cache the array (if you have APC running), and also lessen the traffic to/from the database.

But is this where your bottleneck is? It seems to me as the categories should have been cached in the query cache and therefore be easily retrieved. If this is not your biggest bottleneck, chances are you won't see any decrease in loading times. Make sure to profile your application to find the large bottlenecks or you will waste your time on getting only small performance gains.


If you store categories in a database, you have to connect to the database, prepare a SQL statement, send it to the server, fetch the result set, and (probably) store the results in an array. (But you'll probably already have a connection to the database anyway, and hardware and software is designed to do this kind of work quickly.)

  • Storing and retrieving categories from a database trades speed for maintenance. Your data is always up to date; it might take a little longer to get it.

You can also store categories as constants or as literals in an array assignment. It would be smart to generate the constants or the array literals from data stored in the database, but you might not have to do that for every page load. If "categories" doesn't change much, you might be able to get away with generating the code once or twice a day, plus whenever someone adds a category. It depends on your application.

  • Storing and retrieving categories from an array trades maintenance for speed. Your data loads a little faster; it might be incomplete.

The unsatisfying answer is that you're not going to be able to tell how different storage and page generation strategies affect page loading speed until you test them. And even testing isn't that easy, because the effect of changing server and database parameters can be, umm, surprising.

(You can also generate static pages from the database using php. I suggest you test some static pages to give you an idea of "best case" performance.)

0

精彩评论

暂无评论...
验证码 换一张
取 消