Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
"SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
Old 08-31-2007, 02:44 PM "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
Experienced Talker

Posts: 38
Trades: 0
MySQL v3.x
PHP v4.x

I have a scoring table with almost 15,000 rows. Ranking a particular person is required.

I currently have this script to calculate the rank:

PHP Code:
$myScore 233292;
$result mysql_query("SELECT COUNT(*) as count FROM tbl WHERE score>$myScore");
$row mysql_fetch_array($resultMYSQL_ASSOC);
echo 
"You rank $row[count].";   // You rank 123422. 

which... according to my web host, is causing excessive server load.

I'm wondering if there's any other way to optimize it?

thanks!
__________________
-Thomas Yeung

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
tomazws is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-31-2007, 02:49 PM Re: "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
If you're using mysql I don't know, but in SQL Server you can query the information schema find what you need to know.

You could try building an index on the score column if you don't have one already. That'll change you from a table scan to an index scan. But the database is still going to parse through all the rows just to count them.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 08-31-2007, 04:21 PM Re: "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
Your counting every row in the table. Like Learning Newbie said you can try creating an index on the score column.

Do you have a numbered column such as id? If so you can do a query for the last entry in the table.
select id from table order by id desc limit 1;
That will give you the last id number in the table.
joder is offline
Reply With Quote
View Public Profile
 
Old 08-31-2007, 07:43 PM Re: "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
I agree, create an index for the column field score and try something like this:

$result = mysql_query("SELECT COUNT(score) AS count FROM tbl WHERE score > $myScore");
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is online now
Reply With Quote
View Public Profile
 
Old 09-02-2007, 08:29 AM Re: "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
Experienced Talker

Posts: 38
Trades: 0
I think that's where the problem is. I totally forgot to index the columns that I needed to rank. heheh...
__________________
-Thomas Yeung

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
tomazws is offline
Reply With Quote
View Public Profile
 
Old 09-02-2007, 10:47 PM Re: "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Try a before and after with the index. ( You can drop it to go back to the before state. )
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to "SELECT COUNT(*) FROM tbl WHERE score>$score" Causing Excessive Load?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45587 seconds with 12 queries