0

I'm a pretty new PHP developer yet have some good experience with T-SQL so I'm more than capable of building me some nice SQL queries. I'm building a PHP app and have ran into this issue on a number of occasions.

When I'm against a problem where I need to pull data from a MySQL database and process the output via PHP should I simply "*" out the whole result set and pick out what I need via PHP? IMO it makes more sense to put as much work in SQL as possible but I dunno.

In regards of performance and "best practice" what is generally best; rely on MySQL to do the big part of the work and let PHP pick up the output or let PHP do the majority of the work?

5 Answers 5

1

You should generally never apply the wild-card to grab all columns. It is far better practice to only retrieve the columns you are interested in using. This has implications on:

  • Performance: less overhead by only fetching and retrieving data that is required
  • Maintainability: it is much more obvious what your query is fetching. I.e., if you add additional columns to your structure you might be getting data you originally didn't expect.
2
  • What's best practice if you expect the schema to change in the future? When using * it will get all the new fields or NOT try to get the removed fields that were explicitly defined in a SQL query. Grab the fields first via An information schema query then dynamically feed the query the field names? Nov 10, 2011 at 22:35
  • My point was you want to be as explicit as possible. If you redefine your table structure, inevitably you should be altering your queries. By using wild cards, you are potentially allowing less noticeable bugs creep in. Nov 11, 2011 at 17:00
0

If you only don't need all of the columns from the table, specify the ones you want explicitly. This is better performance-wise, not to mention that it's more readable.

0

SQL will almost always produce a better performance over PHP, but it really depends on the query being run. The more complex it is, the more likely SQL will be a better answer. SQL was built around 'getting you the right data quickly' whereas PHP was built around being a general purpose scripting language.

2
  • Please elaborate on how "complex" a query would have to be to be slower than PHP. In my experience, PHP has usually bit a bit faster than SQL.. but maybe my queries need optimizing or I use too many joins?.. :S
    – Yes Barry
    Nov 10, 2011 at 21:08
  • @mmmshuddup MySQL will generally always be faster for grouping/sorting/filtering functionality. If this is not the case, you are probably doing something wrong. Nov 11, 2011 at 17:00
0

In general it's considered best practice to return specific columns. Performance is not a huge concern, unless you're dealing with tables that contain BLOBs (also consider if the table might have one in the future), but you might find it easier to deal with smaller return objects.

Of course, if you're in development mode, rather than maintenance mode, you might find SELECT * preferable if you're frequently adding or removing columns.

1
  • I suspect never to store BLOBs. Nov 10, 2011 at 22:36
0

In my experience the access time to gather a * query or a specific field query is dwarfed by the over all request time, at least if this is done over the web.

IE in my testing with websites hosted both in the US and overseas I generally see that the difference between select fld,fld,fld and select * is negligible unless you have large table rows and lots of rows.

I personally think this is somewhat of a judgement call based on the size of your system, tables and if your tables and columns are in any kind of a state of flux. If I were you, not knowing the size of your system I'd go with select * but clearly it is not the "best practice" but I think it really needs to be on a specific system-by-system basis.

I'm currently a member of a team on a HUGE C#, WCF, WPF, SQL project and everywhere they went with the "best practices" and what we have is a huge mess. In some cases so much effort was spent to follow the best practice that the result has been far more work to maintain it. We have interfaces by the bazillions as well as wrappers, adapters, unity etc. Right now to make one small change it touches 15 - 20 files. I realize this is slightly off topic but don't blindly follow the best practice just because its there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.