homepage Welcome to WebmasterWorld Guest from 54.196.197.153
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




msg:3658744
 7:36 pm on May 25, 2008 (gmt 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.

 

cameraman




msg:3658768
 8:15 pm on May 25, 2008 (gmt 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.

yumigator




msg:3658796
 9:37 pm on May 25, 2008 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved