Jump to content
Larry Ullman's Book Forums

Recommended Posts

With Ajax, if I need to get info out of a database, I need to have server side code. So using Ajax, I would point at a php file (for example) with code with a db query in it to run against the database.  This doesn't save a trip to the database though it would not require a reload of the page.

Does it make sense then to:

Create a pre-populated xml or json file from a database that is fairly small and/or not updated that often? JS could then just pull info out of the xml avoiding the trip to the database?

This would eliminate the need to go to the database.

 

Sorry if I'm mis-understanding something here.  Marie 

Share this post


Link to post
Share on other sites

Hello, and welcome to the forums, Marie.

 

Your understanding seems correct, but if you get data from a JSON/XML file instead of the DB, you're not really saving any time/efficiency, as you're still getting data from a file, regardless of whether the format is XML, JSON or the DB.

That make sense?

 

Certainly, if there is some data you need on page load, then it makes sense to immediately get that data via PHP/MySQL and render it on the screen, without using JS/Ajax, but for anything after the initial page load, using Ajax could be beneficial.

 

Does that answer your question?

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks for your quick response. I'm new to database. I keep reading that we need to write efficient code that it's "costly" to go to the database. So that's where my question came from.

 

So essentially having to parse the xml/json or querying the database would be just about the same?

Share this post


Link to post
Share on other sites

Whether it's SQL being used to get DB data, or a file handler/parser used to get XML/JSON data, the basic steps are the same (and most of them are done for you automatically behind the scenes).

 

Regardless of the data format though, it's always a balancing act of getting enough data up-front without getting too much, and thus slowing things down unnecessarily.

Nowadays, I generally get enough data up-front to handle the page load as well as a little extra to handle common interactions; anything after that is Ajax.

 

Of course, it's always a judgment call, and I can't tell you exactly how much to do in every scenario, but usually a bit of experimentation will get you there.

Good luck!

Share this post


Link to post
Share on other sites

Presumably the XML/JSON data would come from the database, no? Database queries are expensive, but very necessary. And there are definitely situations where you don't want to rely upon them, and busy sites use other techniques. For example, they might cache database queries or pre-fetch data. So there are good techniques in this area and it may be worth pursuing, but not from the get-go.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...