Does anyone use if statements a lot in mysql? What I typically do, is a select.. then in php do statements based on that data, and issue more queries if necessary (which in some cases, can result in 10's of potential queries).. however, I suppose this could all be done on the sql side as well.. any feedback about pros and cons would be great.
Thanks!
mysql IF statements
Moderator: General Moderators
One of the most resource intensive things you can do in PHP are queries. 10 queries are a lot slower than doing 1 query and then doing a bunch of processing on that 1 query.
Probably the worst thing you can do is do a query, then loop on the results of that query and do another query for every record in the result set. I see this done so many times in code I fix. People do this because they don't see a slow down when they are testing 10 records, but when the database grows it becomes a huge bottle neck. A simple join is usually the fix for this.
So I guess the short answer is doing one optimized query is a lot better than doing a bunch of queries to accomplish the same thing.
Probably the worst thing you can do is do a query, then loop on the results of that query and do another query for every record in the result set. I see this done so many times in code I fix. People do this because they don't see a slow down when they are testing 10 records, but when the database grows it becomes a huge bottle neck. A simple join is usually the fix for this.
So I guess the short answer is doing one optimized query is a lot better than doing a bunch of queries to accomplish the same thing.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA