Should I use PHP OR MySQL ??

by 5 replies
6
Hello,

I need to discuss one thing.

Suppose,

I have a table in mysql database:

table1:
  • col_1

If I need to retrieve records in asc order from col_1, I can do this in two manners. One is I can simply use this 'ORDER BY col_1 ASC' in the end of the query. The 2nd way is I can use the php functions to sort my records in ascending order.

Should I use mysql or php to sort my records in asc order. If PHP then why ?? If mysql then why ??

What are its effects on our script related to time execution of our script ??

Thanks in advance.
#programming #mysql #php
  • You have to retrieve the records anyway using mySQL so why increase more work by then sorting it using PHP? Get the data you want in the order you want it using the mySQL query. That's why they have them.
  • I know it can be so comfortable to retrieve records via mysql but what I am trying to ask, if we are working with a large software having a huge database, then don't you think so that it can put a bit more load on our mysql server ??
    • [1] reply
    • If you designed the table/database correctly, you'll be sorting on a primary key or an indexed field. The MySQL database is highly optimized to sort using these keys, so the overhead is going to be a LOT lower if you let the database do the heavy work. The stress you're putting on the MySQL server is going to be minimal compared to doing it via PHP. Also remember that if you're sorting with PHP, the server is going to have to process all of the rows and transmit them to you (presumably over a network if you're worried about putting an extra burden on the MySQL server), and the time it takes to do this will be exponentially longer than just letting MySQL sort and then send over the results to you.

      For basic sorts/queries on data that changes a lot, just let the database handle it. Yeah you may have more load on the database server, but that's its job. If it becomes an issue, you may either have to do some caching yourself or come up with a better solution (or database design).
      • [ 1 ] Thanks
      • [1] reply
  • Thanks Christopher And Mickster for clearing my concept..

Next Topics on Trending Feed