vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   efficient (https://vborg.vbsupport.ru/showthread.php?t=64994)

sabret00the 05-11-2004 01:45 PM

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]
                     "
); 


NTLDR 05-11-2004 01:54 PM

You really need to get those two queries within the while loop removed.

sabret00the 05-11-2004 05:44 PM

Any suggestions on an alternative?

NTLDR 05-11-2004 05:55 PM

Whats the $projects query? Its possible it can be integrated with that.

sabret00the 05-11-2004 06:03 PM

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.

sabret00the 05-12-2004 10:03 AM

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?

Xenon 05-12-2004 10:58 AM

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

sabret00the 05-12-2004 11:14 AM

dunno just read in another thread that left joins would cripple any highly active server.

Xenon 05-12-2004 11:22 AM

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 :)

sabret00the 05-12-2004 11:26 AM

oh ok, thanks for explaining that and i'm just gonna try out the code now :)


All times are GMT. The time now is 06:40 AM.

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.01177 seconds
  • Memory Usage 1,752KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete