Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-11-2004, 01:45 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default efficient

is this code efficient?

PHP Code:
                while ($project_info $DB_site->fetch_array($projects))
                 {
                     
$project_info['text'] = nl2br(stripslashes($project_info['text']));
                     
extract($project_info);
                     
$time_posted vbdate('n-j-y, g:i:s a'$timestamp);
                     
// selects the number of times each confeession was rated
                     
$number_of_rates $DB_site->query_first("SELECT COUNT(*) AS votes
                     FROM project_rate
                     WHERE projectid = 
$projectid
                     "
);
                     
$num_rates number_format($number_of_rates['votes']);
                     
// selects the username of the person who made the confeession
                     
$who_done_it $DB_site->query_first("SELECT username
                     FROM user
                     WHERE userid = 
$project_info[userid]
                     "
); 
Reply With Quote
  #2  
Old 05-11-2004, 01:54 PM
NTLDR's Avatar
NTLDR NTLDR is offline
Coder
 
Join Date: Apr 2002
Location: Bristol, UK
Posts: 3,644
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You really need to get those two queries within the while loop removed.
Reply With Quote
  #3  
Old 05-11-2004, 05:44 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Any suggestions on an alternative?
Reply With Quote
  #4  
Old 05-11-2004, 05:55 PM
NTLDR's Avatar
NTLDR NTLDR is offline
Coder
 
Join Date: Apr 2002
Location: Bristol, UK
Posts: 3,644
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Whats the $projects query? Its possible it can be integrated with that.
Reply With Quote
  #5  
Old 05-11-2004, 06:03 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

projects is pulling the text out one by one
where as as i have the second table is the project_rate table where i store each rating the projct text gets so later as i display it, i'm hoping to display the number of ratings each project i had on the page

project
Code:
projectid   	  text   	  timestamp   	  userid
project_rate
Code:
rateid 	 projectid 	 userid 	 timestamp 	 rate

kinda like where on the index of TS3 you got the number of comments displayed along with each of your news items.
Reply With Quote
  #6  
Old 05-12-2004, 10:03 AM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

heres the exact query
PHP Code:
            $projects $DB_site->query("
                SELECT projectid, text, timestamp
                FROM projects
                
$clause
                ORDER BY projectid DESC
                
$limit
            "
); 
i think i may be able to do a left join (the user table to the projects table) to irradicate the last query, but i heard that left joins are bad?
Reply With Quote
  #7  
Old 05-12-2004, 10:58 AM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

why should left joins be bad?

but here, it's suggest a inner join

PHP Code:
$projects $DB_site->query(
        SELECT COUNT(*) AS votes, project_rate.projectid, 
            projects.text, projects.timestamp, user.username
        FROM project_rate
        INNER JOIN projects ON (project_rate.projectid = projects.projectid)
        INNER JOIN user ON (user.userid = projects.userid)
        
$clause
        GROUP BY project_rate.projectid
        ORDER BY project_rate.projectid DESC 
        
$limit 
"
); 
while (
$project_info $DB_site->fetch_array($projects))
{
    
$project_info['text'] = nl2br(stripslashes($project_info['text']));
    
$project_info['num_rates'] = number_format($project_info['votes']);
    
extract($project_info);
    
$time_posted vbdate('n-j-y, g:i:s a'$timestamp); 
that should work i think
Reply With Quote
  #8  
Old 05-12-2004, 11:14 AM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

dunno just read in another thread that left joins would cripple any highly active server.
Reply With Quote
  #9  
Old 05-12-2004, 11:22 AM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

nah, it depends on which tables you join.

for example thread and post table shouldn't be joined, as these are bother very big ones.
on the other hand the session table and the user table could always be joined, as the session table is normally a very small one.

also it's always better to join instead of looping a query a lot
Reply With Quote
  #10  
Old 05-12-2004, 11:26 AM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

oh ok, thanks for explaining that and i'm just gonna try out the code now
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:14 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04174 seconds
  • Memory Usage 2,273KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete