Reputation: 3517
I'm building a PHP app which will get a lot of traffic. I have always learned that I should limit the number of SQL queries (right now it's about 15 per visitor). I could limit this to about half, using PHP to filter and sort data, but would this be faster? Alternatively, I could use "JOIN" in the query to reduce the number of queries, but is this really faster than executing multiple queries? Thanks!
Upvotes: 5
Views: 2572
Reputation: 3206
If you have 15 queries per visitor, you most likely did something wrong, unless your application is pretty big.
Using PHP to sort and filter data instead of MySQL
Doing the sorting and filtering in PHP will not make your application faster, it will make it slower for sure, MySQL is optimized to be very fast given the right indexes, so you should certainly use it when you can.
Learn about database indexing and it will make your life easier and increase your application's performance. Here is a link :
http://www.mysqltutorial.org/mysql-create-drop-index.aspx
Joining versus multiple queries
You ask if using join's would be faster than executing multiple queries.
The answer is yes, it will always be faster to use join's given the right indexes. Which also comes back to the first topic, indexing.
However, keep in mind that joining is as efficient as you make it to be, it will be extremely efficient if done right, and extremely slow if done wrong, so you must know the basics to do it right.
You can look here for an introduction to join's :
http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/
Upvotes: 5