Jump to content

Reduce Number of mysql_query()


perky416

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.