views:

145

answers:

4

I've read that (all things equal) PHP is typically faster than MySQL at arirthmetic and string manipulation operations. This being the case, where does one draw the line between what one asks the database to do versus what is done by the web server(s)? We use stored procedures exclusively as our data-access layer. My unwritten rule has always been to leave output formatting (including string manipulation and arithmetic) to the web server. So our queries return:

  • unformatted dates
  • null values
  • no calculated values (i.e. return values for columns "foo" and "bar" and let the web server calculate foo*bar if it needs to display value foobar)
  • no substring-reduced fields (except when shortened field is so significantly shorter that we want to do it at database level to reduce result set size)
  • two separate columns to let front-end case the output as required

What I'm interested in is feedback about whether this is generally an appropriate approach or whether others know of compelling performance/maintainability considerations that justify pushing these activities to the database.

Note: I'm intentionally tagging this question to be dbms-agnostic, as I believe this is an architectural consideration that comes into play regardless of one's specific dbms.

+2  A: 

Usually, data formatting is better done on client side, especially culture-specific formatting.

Dynamic pivoting (i. e. variable columns) is also an example of what is better done on client side

When it comes to string manipulation and dynamic arrays, PHP is far more powerful than any RDBMS I'm aware of.

However, data formatting can use additional data which is also kept in the database. Like, the coloring info for each row can be stored in additional table.

You should then correspond the color to each row on database side, but wrap it into the tags on PHP side.

The rule of thumb is: retrieve everything you need for formatting in as few database round-trips as possible, then do the formatting itself on the client side.

Quassnoi
+2  A: 

I would draw the line on how certain layers could rotate out in place for other implementations. It's very likely that you will never use a different RDBMS or have a mobile version of your site, but you never know.

The more orthogonal a data point is, the closer it should be to being released from the database in that form. If on every theoretical version of your site your values A and B are rendered A * B, that should be returned by your database as A * B and never calculated client side.

Let's say you have something that's format heavy like a date. Sometimes you have short dates, long dates, English dates... One pure form should be returned from the database and then that should be formatted in PHP.

So the orthogonality point works in reverse as well. The more dynamic a data point is in its representation/display, the more it should be handled client side. If a string A is always taken as a substring of the first six characters, then have that be returned from the database as pre-substring'ed. If the length of the substring depends on some factor, like six for mobile and ten for your web app, then return the larger string from the database and format it at run time using PHP.

Mark Canlas
@mark - accepted as answer over quassnoi despite his having some excellent points because you've given me a new favorite vocab word :)
codemonkey
+1  A: 

I believe in returning the data pretty much as-is from the database and letting it be formatted on the front-end instead. I don't stick to it religously, but in general I think it's better as it provides greater flexibility - e.g. 1 sproc can service n different requirements for data, each of which can format the data as each individually needs. Otherwise, you end up either with multiple queries returning the same data with slightly different formatting from the DB (from a SQL Server point of view, thus reducing execution plan caching benefits - therefore negative impact on performance).

AdaTheDev
+1  A: 

Leave output formatting to the web server

onupdatecascade