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 07-27-2004, 09:01 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default vBcron batching?

Well i'm just looking into it. I need to run about 10k queries every night around midnight. The queries are ran in threes. Two selects and one update.

Obviously it would not be sensible to run that many queries in one go. So would it be possible to do it in batches using vBcron?
Reply With Quote
  #2  
Old 07-27-2004, 09:16 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Jeez! Are you sure the there isn't smth. wrong with your database design?
Reply With Quote
  #3  
Old 07-27-2004, 09:32 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hehe probably Basically i'm upgrading my battle system to work vB3. I have my table structure like so:

Code:
CREATE TABLE `battle_records` ( 
   `battleid` smallint(5) unsigned NOT NULL auto_increment, 
   `userid1` smallint(5) unsigned NOT NULL default '0', 
   `userid2` smallint(5) unsigned NOT NULL default '0', 
   `votesforuserid1` smallint(5) unsigned NOT NULL default '0', 
   `votesforuserid2` smallint(5) unsigned NOT NULL default '0', 
   `threadid` text NOT NULL, 
   `battletype` varchar(50) NOT NULL default '', 
   `timeadded` int(14) default NULL, 
    `winner` smallint(5) unsigned NOT NULL default '0',
   PRIMARY KEY  (`battleid`) 
 ) TYPE=MyISAM AUTO_INCREMENT=1888 ;
Now I want to keep a log of all my users wins/losses. I'd rather do this every night and restrict it to one table than having to manage several tables when I go to edit/delete a battle. So the situation i'm faced with is updating every user on my boards win/loss tally in the user table. That way I can display the information in the postbit/profile without any extra queries
Reply With Quote
  #4  
Old 07-27-2004, 11:57 PM
Modin Modin is offline
 
Join Date: Jun 2004
Posts: 162
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

if you have shell access I'd recommend doing something this big via unix cron.

if you don't have shell access then batching is possible but would be tough. You'd have to write somewhere (either in the db or a file) saying how far a previous batch got so that you're not redoing queries. This opens up race conditions where you'd have to obtain a semaphore before starting... (A semaphore is a lock, if one process already has the lock then the next one who wants the lock will wait until the one holding the lock is finished.)

Something similar to this at the top of the batch file

PHP Code:
<?php
$mutex 
sem_get(12345);

//grab the lock and only do actions if lock was successful
if(sem_acquire($mutex))
{
    
$file fopen("mark_file.txt""w+");
    
//read and write some marking data to a file
    
fclose($file);
    
    
//release the lock
    
sem_release($mutex);

    
//start queries based on acquired mark
}
?>
Reply With Quote
  #5  
Old 07-28-2004, 08:30 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Interesting, I've never worked with a real-cron system on UNIX so I'll talk to my server admin when he gets back off holiday and see what we can do
Reply With Quote
  #6  
Old 07-28-2004, 09:50 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmm, I am not sure about your table structure and what you actuaklly want to do, but assuming that winner is the userid which won the battle and that your user table has fiels battles, wins and losses for the # of battles, wins and losses of each user, the following queries might work:

[sql]
CREATE TEMPORARY TABLE battle_results_temp (userid INT( 10 ) NOT NULL , battles INT( 10 ) NOT NULL , wins INT( 10 ) NOT NULL);
CREATE TEMPORARY TABLE battle_results_temp2 (userid INT( 10 ) NOT NULL , battles INT( 10 ) NOT NULL , wins INT( 10 ) NOT NULL, losses INT (10) NOT NULL);
INSERT INTO battle_results_temp SELECT userid1 AS userid, COUNT(userid1) AS battles, SUM(IF(userid1=winner, 1, 0)) AS wins FROM battle_records GROUP BY userid1;
INSERT INTO battle_results_temp SELECT userid2 AS userid, COUNT(userid2) AS battles, SUM(IF(userid2=winner, 1, 0)) AS wins FROM battle_records GROUP BY userid2;
INSERT INTO battle_results_temp2 SELECT userid, SUM(battles) AS battles, SUM(wins) AS wins, SUM(battles) - SUM(wins) AS losses FROM battle_results_temp GROUP BY userid;
UPDATE battle_results_temp2, user SET user.battles=battle_results_temp2.battles, user.wins=battle_results_temp2.wins, user.losses=battle_results_temp2.losses WHERE battle_results_temp2.userid=user.userid;
[/sql]
Reply With Quote
  #7  
Old 07-28-2004, 01:01 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

another way would be to use the CASE function to assign different values to different users, you can save a lot of queries that way
Reply With Quote
  #8  
Old 07-28-2004, 03:01 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

My table is exactly as it is above. Right now I have this query which will give me the wins/losses for a user (thanks to a guy at sitepoint):

Code:
select sum(
         case when $id = userid1 
               and votesforuserid1 > votesforuserid2
                or $id = userid2 
               and votesforuserid1 < votesforuserid2
              then 1 else 0 end
          ) as wins  
     , sum(
         case when $id = userid1 
               and votesforuserid1 > votesforuserid2
                or $id = userid2 
               and votesforuserid1 < votesforuserid2
              then 0 else 1 end
          ) as losses  
  from battle_records 
 where $id in ( userid1 , userid2 )
Where $id is the userid in question.

The thing that i'm stuck with now is the easiest way to run that for all my users every night. Obviously running that 10,000 times is not very efficient.
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 07:19 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.04021 seconds
  • Memory Usage 2,235KB
  • Queries Executed 11 (?)
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
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete