Jump to content

Big query or multiple queries?


DavidT

Recommended Posts

Hi there,

I'm creating for my website a system that allows logged user to send a "mail", which is actually a mysql database entry. Through a simple form, user can insert subject and message, which will be stored and can be viewed by the website crew.

 

The entry has an additional entry, the user id, from which we can get the user mail, name etc.

 

Now I'm preparing the page where the crew can see the messages, which should create a table with the classic fields, such as:

time&date - subject - user's username - user's mail

 

I do a query to retrieve the message informations, and a loop to create the table. The problem is the following: since the user's nickname and mail are stored in a different table of the database, I have two choices:

1) for each message I create a new mysql query which gets the nickname and male associated to the stored id

2) I store all the id's of the showed messages, then I create a single query to get all the data together (something like SELECT * FROM `usr_messages` WHERE `id` = x1 OR `id` = x2 etc...) and then handle it through the script

 

Which one is more performing?

 

Thanks in advance

Link to comment
Share on other sites

Great, thank you very much!

Sorry for the very beginner question. I used mysql/php fo a while, but since I mostly need only the same, usual tasks, I've never actually heard of this joining system (I used LEFT JOIN by the way).

 

Thanks again!

Link to comment
Share on other sites

I am now thinking one more thing. I use LIMIT 0,15 to limit the number of messages shown in the first page, however, I want that the script also gives me a number of the total messages.

I cannot just use php to count the given elements, since this is limited to 15 because of the query string: is there a smarter way that doing two different queries, adding first "count" and then without it using LIMIT?

Link to comment
Share on other sites

You can use SQL_CALC_FOUND_ROWS modifier and FOUND_ROWS() function

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

 

Although some benchmarks suggest that it's better to just run another query with COUNT() (providing certain conditions are met)

http://www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/

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.