-I am creating a website where people can post and upload different things and each user has their own profile picture so things can get a little difficult. If a user posts something, the table stores their name but not the picture so I have to use a query within the loop that I use to get all the posts so this means that if there are 10 user posts than one page loads 11 queries (including the one to get the posts) plus any others that are already on the page for other purposes. I was just a little worried about this and I was also wondering what are the consequences of having too many queries. I would assume sites like Facebook a lot of queries on a single page but I can't be sure. Thanks for all your help!Do you know how to do joins? It sounds like you don't.
This should be a single query. You want to do a join on "the table" with the table that contains the users picture. Hopefully you have a primary key for each user? userid or something?
So, without knowing your table structure, this is just a wild guess, but you want something like this:
SELECT posts.text,users.photo
FROM posts
JOIN users ON posts.userid = users.userid
WHERE posts.id = $some_post_id;There is no such thing as too many mySQL queries!
A forum, for example, will have about 6 query types, with over 9000 of them running at the same time. Don't even get me started with facebook, consider every single feed, a query, so no, 11 isn't that much. The consequences of having too much (example, ur searching for a 10mb string 10000 times every second) would be a mass of network bill increase and general lag.
没有评论:
发表评论