Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 12-28-2005, 11:11 AM
StarBuG's Avatar
StarBuG StarBuG is offline
 
Join Date: Dec 2001
Location: Germany
Posts: 1,033
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Slow Query Problem for calculating a Ranking - MySQL 5.0.15

Hi

I've got a SQL query to calculate a ranking based on points a user has.
This query performs good when there're just a few rows in the table.
But when i try the query with more rows it gets realy slow. I am using mysql 5.0.15

here are some "benchmarks":
10 rows = 0,018 sec
1000 rows = 1,520 sec
2000 rows = 6,337 sec
5000 rows = 22,680 sec

How can i optimize the query ?

-- the table with the users and the points
DROP TABLE IF EXISTS points;

CREATE TABLE points (
user_id bigint(20) unsigned NOT NULL,
points int(10) unsigned default '0',
PRIMARY KEY (user_id),
KEY points (points)
) ENGINE=InnoDB;

-- index for the points column (seems useless ...)
create index i_points on points ( points );

-- 10 rows of demo data
INSERT INTO points (user_id, points) VALUES
(1,111), (2,111), (3,132), (4,13), (5,2511),
(6,51), (7,261), (8,121), (9,91), (10,981);

-- and heres the query that need optimization
SELECT U1.user_id, U1.points,
COUNT(U2.points) + 1 AS rank
FROM points AS U1
LEFT OUTER JOIN
points AS U2
ON ( U2.points > U1.points)
WHERE U1.user_id > 0
GROUP BY U1.user_id
ORDER BY rank

Another approach would be:

SET @rank:=0;
SELECT user_id,points, IF(@last_points!=points,@rank:=@rank + 1,@rank) as rank, @last_points:=points FROM points ORDER BY points DESC;

But is there any chance to put these to queries into *one* view ?

If anyone has an Idea for how to improve the performance or has a much better approach I would be realy greatfull.

Thank you in advance

StarBuG
Reply With Quote
  #2  
Old 12-28-2005, 11:32 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

1. Why use InnoDB?

2. How are you processing the results? If a PHP script is using them, why not calculate the rank there, would save you an expensive join.

3. Limit the query to the number of rows you want to use.
Reply With Quote
  #3  
Old 12-28-2005, 11:39 AM
StarBuG's Avatar
StarBuG StarBuG is offline
 
Join Date: Dec 2001
Location: Germany
Posts: 1,033
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

1. because all my tables use innodb (transactions, foreign keys)
2. the query is actualy a view. i need to join against it. that´s why i can´t do it in php or whatever.
3. that doesn't help, because of 2.
Reply With Quote
  #4  
Old 12-28-2005, 12:06 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The only reason for that join is to calculate the rank if i am correct.
Reply With Quote
  #5  
Old 12-28-2005, 12:09 PM
StarBuG's Avatar
StarBuG StarBuG is offline
 
Join Date: Dec 2001
Location: Germany
Posts: 1,033
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

that is true
Reply With Quote
  #6  
Old 12-28-2005, 01:23 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Then better handle rank-counting while reading the rows in your php script.
Reply With Quote
  #7  
Old 12-28-2005, 01:34 PM
StarBuG's Avatar
StarBuG StarBuG is offline
 
Join Date: Dec 2001
Location: Germany
Posts: 1,033
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

i think you don´t understand the problem but thank you for trying
Reply With Quote
  #8  
Old 12-28-2005, 01:53 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Then make me understand

PS Changing the ORDER BY to ORDER BY points DESC could also improve performance since it can use the index and don't need to do a sort using a temp table.
Reply With Quote
  #9  
Old 12-29-2005, 05:57 PM
StarBuG's Avatar
StarBuG StarBuG is offline
 
Join Date: Dec 2001
Location: Germany
Posts: 1,033
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thank you, but this doesn?t change the time the query needs
Reply With Quote
Reply

Thread Tools
Display Modes

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 11:38 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.06244 seconds
  • Memory Usage 2,227KB
  • 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
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)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_postinfo_query
  • fetch_postinfo
  • 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