shinve — 2012-01-27T15:37:11-05:00 — #1
MySQL offers some ways to perform things that you might put in your application logic. For example, say you want to find the oldest people in a database with an age column.
SELECT name, age FROM persons WHERE age = (SELECT MAX(age) FROM persons)
This would select only the rows that have the highest integer in the age column (so it would select one or more people).
But you could also just do:
"SELECT name, age FROM persons"
and then use the results of this query to do some php to find the oldest people.
In general, which is better for speed and performance? I generally try to minimize using my database usage for doing logic, but I don't know if this is a good plan or where to draw the line. It would be helpful to have a good rule of thumb to judge whether I should use the database to do a calculation or organization over php and vice versa.
guelphdad — 2012-01-27T15:38:28-05:00 — #2
Do the work in your database, use your front end application (PHP or whatever) to display the results in a stylized format for web or otherwise. The work belongs in the database.
shinve — 2012-01-27T16:51:23-05:00 — #3
Although this was a simple example, sometimes a query might take a long time, relatively speaking, and running that many times simultaneously sounds like it might be bad for a database. When should I opt for doing things in php rather than the database?
r937 — 2012-01-27T18:24:27-05:00 — #4
it depends on the situation
shinve — 2012-01-27T19:49:30-05:00 — #5
Just for understandings sake, could you give me an example of something I could do in mysql that I should do in PHP? (I really don't have any guidelines to go by. :()
r937 — 2012-01-27T20:33:29-05:00 — #6
a printout that is supposed to look like this --
counting as well as displaying details --
SELECT COUNT(*) FROM daTable;
SELECT * FROM daTable;
doug_g — 2012-01-27T20:40:08-05:00 — #7
Another consideration is whether or not you expect your application will need to support different backend database systems. If you put a lot of logic in the dbms using db procedures, etc, it can be difficult or impossible to port to a different dbms.
r937 — 2012-01-27T20:51:10-05:00 — #8
that sounds like the type of fear, uncertainty and doubt that i used to hear in the days of the database wars between the likes of oracle, ibm, and microsoft
doug_g — 2012-01-28T01:24:54-05:00 — #9
No FUD, just a comment based on experience from working on a multi-db web application that supported 3 different backend dbms. If you are working with such an app you have to decide on just what functionality offered by a dbms you choose to take advantage of, and what functionality you end up implementing within your application code.
r937 — 2012-01-28T03:53:40-05:00 — #10
you are right, and i was just teasing you
but converting from one database's stored procs to another's isn't hard, and you should put whatever naturally belongs in a stored proc into the stored proc and not in application code just because you might want to change databases some day...
shinve — 2012-01-28T12:49:46-05:00 — #11
Thanks. I didn't understand your second example though. How can you do a SELECT * FROM tablem or do a count of rows in PHP only? That seems like you actually have to rely on the database to get the information from these selects.
r937 — 2012-01-28T13:57:41-05:00 — #12
if you're going to retrieve all the rows, there's no point in also asking the database to count them -- just use the php function mysql_num_rows
shinve — 2012-01-28T14:24:54-05:00 — #13
I got it. So if a php-specific function exists to get some (aggregate or meta) information from the db, use that.