Thaiscorpion
Thaiscorpion

Reputation: 479

What is faster? retrieve data from the database or retrieve it from a text file?

I'm making a page with with lots of diferent elements and i want to show a tooltip when you hover over each one. There are thousands of elements so the information for each tooltip will need to be recorved with ajax.

I was wondering if its better to save all that tooltip information on mysql database and retrieve all necesery information with one ajax request, or is it better to save each tooltip to a seperate file and the ajax each file when needed.

Upvotes: 0

Views: 1506

Answers (4)

Serj Sagan
Serj Sagan

Reputation: 30267

I would say SQL database with caching is your best bet: See something like this http://www.troywolf.com/articles/php/class_db/

Upvotes: 0

ruslik
ruslik

Reputation: 14900

DB would be faster than separate files, because it will cache results. Either use DB, either use one single file.

Upvotes: 0

RHSeeger
RHSeeger

Reputation: 16282

The choice of whether to store the information in the database or in a flat text file can be made orthogonally to the choice of whether to retrieve all the values at once or one at a time.

That being said, you can consider the following two things:

  • Retrieving them all at once, if there's a lot of data, may slow down the response of the page
  • Retrieving them one at a time may slow down each item and result in many, many calls to the server

What might be a good resolution is to group the tooltips into bunches that are likely to be accessed around the same time (ie, a group for each section of the page). Then, you can retrieve the group that has the tooltip you need and it will grab the all the ones the user is likely to need shortly thereafter.

Another option is to retrieve the tooltips via a javascript file (rather than via ajax), and then load the file asynchronously once the page is loaded. The page will display and, while the user is first looking at it, the tooltips can be loading in the background. If I remember correctly, this will also let the browser cache the data (js file) so that the information won't need to be reloaded if the user comes back to the same page (ie, caching without you having to do any work to get it).

As to the decision of a db vs a text file, the text file will generally be faster. However, I would make the choice based more on how/when you're likely to change the tooltips. If you're comfortable needing to push a new version of the app whenever you change a tooltip, the flat file is a reasonable (and simple) choice. If you want to be able to change tooltips dynamically (which seems unlikely), the database is probably a better choice. Either way, the choice by which you choose when to load the tooltips and how many of them will probably overshadow the difference between the database and a flat file.

Upvotes: 1

Frank Rosario
Frank Rosario

Reputation: 2661

Performance wise, the text file would probably be faster; since you wouldn't incur the overhead of having a server side page hit the database and return the data to the client. The text file could just be read directly with little overhead.

Upvotes: 0

Related Questions