Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Should number of queries or size of queries take priority (optimizing)

7:36 pm on May 25, 2008 (gmt 0)

New User

10+ Year Member

joined:May 13, 2007
posts: 26
votes: 0

My table has one row for each page (that is, if an item has 3 pages, there will be 3 separate rows). I need to perform 2 tasks:

1) Display the content of the requested page; and
2) Check if the item has more than one page, if so, display links to allow users to change pages.

I have written two versions of the PHP code to do this.

The first way specifically queries for the row that is the correct item AND page number, then displays the content for the requested page. It then makes another request to determine how many pages there are for that given item, and if there are more than one page, it displays page change links.

The second way queries for ALL pages for that item. An array is created, and each row of the array represents the data for one page (this is accomplished using a while loop). Then, page change links are created based on the number of rows in that array.

Clearly, the first one needs two queries, but both relatively small. The second needs one potentially large query. I assume the second one requires more memory for PHP given that data for ALL the pages is stored in an array, while the first puts more load on MySQL since two separate queries are made. I also assume the second is more efficient for items with shorter and/or fewer pages, and the first is more efficient for items with longer and/or more pages.

Am I correct in my assumptions, and which method would you recommend for a production site? Sorry for the lengthy post, and thanks in advance for any answers.

8:15 pm on May 25, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
votes: 0

If I understand you correctly, that you're storing the content itself in the database, then I'd definitely do the two queries; there's no point in retrieving the content when you don't need it.
9:37 pm on May 25, 2008 (gmt 0)

New User

10+ Year Member

joined:May 13, 2007
posts: 26
votes: 0

I am storing the complete content in the database.

What you're saying makes sense. I am trying to keep the number of queries down, but the load may ultimately get too high with very large items.

I think I will time both versions of the code using various page counts and lengths and see which is faster under which conditions.

Thanks for the answer. Any others are also appreciated.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members