perky416 Posted May 14, 2011 Share Posted May 14, 2011 Hi guys, Yesterday i was told that the more mysql_query()'s i have the more my website will slow down to a halt if it get a large number of visitors. My website has LOADS of mysql_query()'s. Most of my queries are select queries with he odd update and insert query. I tried reading up on how to combine multiple select queries on a page into 1, however someone on another forum said you cannot combine multiple queries using mysql_query(). If this is not possible please could somebody tell me how i would go about reducing the number of selects? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/ Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 It depends really on what your queries look like. If you notice, at the bottom of this page it says Page created in 0.024 seconds with 16 queries. and believe me, those queries are long. Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215305 Share on other sites More sharing options...
floridaflatlander Posted May 14, 2011 Share Posted May 14, 2011 What do you call large? I also don't know what you mean by combine queries but basiclly you can write and/or place your queries so you can use the data again if needed. I'm sure the guys in the know on here will want to see some table structure(s) and queries to give you details. Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215307 Share on other sites More sharing options...
perky416 Posted May 14, 2011 Author Share Posted May 14, 2011 Oh yeah i never noticed that before. That totally contradicts what I read somewhere as someone said its best to have 1 query per page, maximum of 4 if its a complex page. These are a few of the select queries on one of my pages. I know i should replace * with the actual file names i need. I found out that i should do this after i wrote the page. I only have 5 select queries on this page however on one page i have about 30. $query = mysql_query("SELECT * FROM users WHERE id='$id'"); $payment_query = mysql_query("SELECT * FROM payment_info WHERE username='$username'"); if (mysql_num_rows(mysql_query("SELECT email FROM users WHERE email='$email'")) > 0) On the page that has 30 queries, most of them are in if statements, so only about 5 will be used at any 1 time. Im not too sure if this will make a difference or not. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215311 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 Select everything you can from one table. If you repeat a select for one value of the same table then you're not doing it right. In other words... your code is redundant. You could have easily checked the email value after you selected * from users in the beginning. Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215312 Share on other sites More sharing options...
perky416 Posted May 14, 2011 Author Share Posted May 14, 2011 Good advice! Looking at it like that with them next to each other I see what you mean. Would using a different query for each different table should be ok? I only have about 12 tables, and only about 3 of them are used on any 1 page. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215315 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 So long as the query has the information you want, then yea.. use one query per table. Though if you start limiting your queries with WHERE and LIMIT clauses you're least likely to have that information. I'm not saying don't use WHERE and LIMIT, I'm saying.. don't waste valuable data and then recreate it. Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215316 Share on other sites More sharing options...
perky416 Posted May 14, 2011 Author Share Posted May 14, 2011 Ok thanks mate, Ill try and make better use of my queries. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/236389-reduce-number-of-mysql_query/#findComment-1215317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.