I have table in database named ads, this table contains data about each ad.
I want to get that data from table to display ad.
Now, I have two choices:
- Either get all data from table and store it in array, and then , I will treat with this array to display each ad in its position
by using loops. - Or access to table directly and get each ad data to display it, note this way will consume more queries to database.
Which one is the best way, and not make the script more slow ?
In most Cases #1 is better.
Because, if you can select the data (smallest, needed set) in one query,
then you have less roundtrips to the database server.
Accessing Array or Objectproperties (from Memory) are usually faster than DB Queries.
You could also consider to prepare your Data and don’t mix
fetchingwithviewoutput.The second Option “select on demand” could make sense if you need to “lazy load”,
maybe because you can or want to recognize client properties, like viewport.