473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

mysql-question

Hi!
My company make several flash-based games, and I use php to communicate with
mysql to provide highscore-lists.

My problem is this:
When I save a player's score in the mysql-table, I want to find which place
the player got with his score (today). To get this I have tried two
different solutions, which both works, but are very ineffective: (The
Time-field is a DateTime type, and I have Score and Time as Indexes)

1) SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE().
- or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().

.... $place = mysql_num_rows($result)

Both give the right result, but the problem is that when there are many
players playing at the same time, and the table consists of several million
records, the query is just too heavy for the mysql-server, and it breakes
down.

So my question is: Are there any better ways of getting a player's place?

Ole Johan, Norway
Aug 4 '06 #1
6 1480

ojorus wrote:
Hi!
My company make several flash-based games, and I use php to communicate with
mysql to provide highscore-lists.

My problem is this:
When I save a player's score in the mysql-table, I want to find which place
the player got with his score (today). To get this I have tried two
different solutions, which both works, but are very ineffective: (The
Time-field is a DateTime type, and I have Score and Time as Indexes)

1) SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE().
- or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().

... $place = mysql_num_rows($result)

Both give the right result, but the problem is that when there are many
players playing at the same time, and the table consists of several million
records, the query is just too heavy for the mysql-server, and it breakes
down.

So my question is: Are there any better ways of getting a player's place?

Ole Johan, Norway
If I understand you correctly, you can use:

SELECT score FROM table ORDER BY score DESC LIMIT 1

I don't understand why you are using Time>CURDATE(), because it isn't
needed in this situation.

Thanks,
Michael Boutros

Aug 4 '06 #2
Rik
Michael Boutros wrote:
ojorus wrote:
>Hi!
My company make several flash-based games, and I use php to
communicate with mysql to provide highscore-lists.

My problem is this:
When I save a player's score in the mysql-table, I want to find
which place the player got with his score (today). To get this I
have tried two different solutions, which both works, but are very
ineffective: (The Time-field is a DateTime type, and I have Score
and Time as Indexes)

1) SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE().
- or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().

... $place = mysql_num_rows($result)

Both give the right result, but the problem is that when there are
many players playing at the same time, and the table consists of
several million records, the query is just too heavy for the
mysql-server, and it breakes down.

So my question is: Are there any better ways of getting a player's
place?

Ole Johan, Norway

If I understand you correctly, you can use:

SELECT score FROM table ORDER BY score DESC LIMIT 1

I don't understand why you are using Time>CURDATE(), because it isn't
needed in this situation.

That's not what the op wants.
To rephrase what I gather from his post:
He wants the placing of the score compared to other scores on that
particular day.

"SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE()"
Would be my choice, but seems to slow. Indexes are already created according
to the OP, so that's no way to go either. Maybe save only the day of the
score in the YYYYMMDD format, and use Time=CURDATE()+0.

About using COUNT(*):
"COUNT(*) is optimized to return very quickly if the SELECT retrieves from
one table, no other columns are retrieved, and there is no WHERE clause."

There is a WHERE clause here, hence the optimization is lost. So, maybe use
COUNT(score). I don't know wether this will be faster, but it's worth a
shot.

Grtz,
--
Rik Wasmus
Aug 4 '06 #3


"Michael Boutros" <mi*************@gmail.comskrev i melding
news:11**********************@i42g2000cwa.googlegr oups.com...
>
ojorus wrote:
>Hi!
My company make several flash-based games, and I use php to communicate
with
mysql to provide highscore-lists.

My problem is this:
When I save a player's score in the mysql-table, I want to find which
place
the player got with his score (today). To get this I have tried two
different solutions, which both works, but are very ineffective: (The
Time-field is a DateTime type, and I have Score and Time as Indexes)

1) SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE().
- or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().

... $place = mysql_num_rows($result)

Both give the right result, but the problem is that when there are many
players playing at the same time, and the table consists of several
million
records, the query is just too heavy for the mysql-server, and it breakes
down.

So my question is: Are there any better ways of getting a player's place?

Ole Johan, Norway

If I understand you correctly, you can use:

SELECT score FROM table ORDER BY score DESC LIMIT 1

I don't understand why you are using Time>CURDATE(), because it isn't
needed in this situation.

Thanks,
Michael Boutros
Hi! Thanks for your replay.
The Score-table contain scores for many days. Since I want to get the
player's place for TODAY, I'll have to use Time>CURDATE(). (Which means
Time>'2006-08-04 00:00:00').
The query you propose will by the way only return the best score ever, which
I'm not interessted in. It is the player's PLACE for today I want, based on
his score.

Ole Johan
Aug 4 '06 #4
Rik
Rik wrote:
Michael Boutros wrote:
>ojorus wrote:
>>Hi!
My company make several flash-based games, and I use php to
communicate with mysql to provide highscore-lists.

My problem is this:
When I save a player's score in the mysql-table, I want to find
which place the player got with his score (today). To get this I
have tried two different solutions, which both works, but are very
ineffective: (The Time-field is a DateTime type, and I have Score
and Time as Indexes)

1) SELECT COUNT(*) FROM table WHERE Score>=$score AND
Time>CURDATE(). - or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().

... $place = mysql_num_rows($result)

Both give the right result, but the problem is that when there are
many players playing at the same time, and the table consists of
several million records, the query is just too heavy for the
mysql-server, and it breakes down.

So my question is: Are there any better ways of getting a player's
place?

Ole Johan, Norway

If I understand you correctly, you can use:

SELECT score FROM table ORDER BY score DESC LIMIT 1

I don't understand why you are using Time>CURDATE(), because it isn't
needed in this situation.


That's not what the op wants.
To rephrase what I gather from his post:
He wants the placing of the score compared to other scores on that
particular day.

"SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE()"
Would be my choice, but seems to slow. Indexes are already created
according to the OP, so that's no way to go either. Maybe save only
the day of the score in the YYYYMMDD format, and use Time=CURDATE()+0.

About using COUNT(*):
"COUNT(*) is optimized to return very quickly if the SELECT retrieves
from one table, no other columns are retrieved, and there is no WHERE
clause."

There is a WHERE clause here, hence the optimization is lost. So,
maybe use COUNT(score). I don't know wether this will be faster, but
it's worth a shot.

Adding:
Is score an integer? That's how the database will work best. If it isn't an
integer, consider making it one.

BTW: if this isn't a solution, try (alt.)comp.databases.mysql. Those guys
will know a lot more about mysql optimization.

Grtz,
--
Rik Wasmus
Aug 4 '06 #5

"Rik" <lu************@hotmail.comskrev i melding
news:18*************************@news2.tudelft.nl. ..
Rik wrote:
>Michael Boutros wrote:
>>ojorus wrote:
Hi!
My company make several flash-based games, and I use php to
communicate with mysql to provide highscore-lists.

My problem is this:
When I save a player's score in the mysql-table, I want to find
which place the player got with his score (today). To get this I
have tried two different solutions, which both works, but are very
ineffective: (The Time-field is a DateTime type, and I have Score
and Time as Indexes)

1) SELECT COUNT(*) FROM table WHERE Score>=$score AND
Time>CURDATE(). - or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().

... $place = mysql_num_rows($result)

Both give the right result, but the problem is that when there are
many players playing at the same time, and the table consists of
several million records, the query is just too heavy for the
mysql-server, and it breakes down.

So my question is: Are there any better ways of getting a player's
place?

Ole Johan, Norway

If I understand you correctly, you can use:

SELECT score FROM table ORDER BY score DESC LIMIT 1

I don't understand why you are using Time>CURDATE(), because it isn't
needed in this situation.


That's not what the op wants.
To rephrase what I gather from his post:
He wants the placing of the score compared to other scores on that
particular day.

"SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE()"
Would be my choice, but seems to slow. Indexes are already created
according to the OP, so that's no way to go either. Maybe save only
the day of the score in the YYYYMMDD format, and use Time=CURDATE()+0.

About using COUNT(*):
"COUNT(*) is optimized to return very quickly if the SELECT retrieves
from one table, no other columns are retrieved, and there is no WHERE
clause."

There is a WHERE clause here, hence the optimization is lost. So,
maybe use COUNT(score). I don't know wether this will be faster, but
it's worth a shot.


Adding:
Is score an integer? That's how the database will work best. If it isn't
an
integer, consider making it one.

BTW: if this isn't a solution, try (alt.)comp.databases.mysql. Those guys
will know a lot more about mysql optimization.

Grtz,
--
Rik Wasmus

No, unfortunately the score is defined as decimal (20,2). So that's probably
one of the slow-down factors.

One solution to improve performance could be to have a table just containing
today's games, which will be cleared every midnight. Then I can skip the
CURDATE-thing, and the search will probably be a lot faster.

Thanks for the mysql-group-info.

Ole Johan

Aug 4 '06 #6
Rik
ojorus wrote:
"Rik" <lu************@hotmail.comskrev i melding
news:18*************************@news2.tudelft.nl. ..
>Rik wrote:
>>Michael Boutros wrote:
ojorus wrote:
Hi!
My company make several flash-based games, and I use php to
communicate with mysql to provide highscore-lists.
>
My problem is this:
When I save a player's score in the mysql-table, I want to find
which place the player got with his score (today). To get this I
have tried two different solutions, which both works, but are very
ineffective: (The Time-field is a DateTime type, and I have Score
and Time as Indexes)
>
1) SELECT COUNT(*) FROM table WHERE Score>=$score AND
Time>CURDATE(). - or -
2) SELECT Score FROM table WHERE Score>=$score AND Time>CURDATE().
>
... $place = mysql_num_rows($result)
>
Both give the right result, but the problem is that when there are
many players playing at the same time, and the table consists of
several million records, the query is just too heavy for the
mysql-server, and it breakes down.
>
So my question is: Are there any better ways of getting a player's
place?
>
Ole Johan, Norway

If I understand you correctly, you can use:

SELECT score FROM table ORDER BY score DESC LIMIT 1

I don't understand why you are using Time>CURDATE(), because it
isn't needed in this situation.
That's not what the op wants.
To rephrase what I gather from his post:
He wants the placing of the score compared to other scores on that
particular day.

"SELECT COUNT(*) FROM table WHERE Score>=$score AND Time>CURDATE()"
Would be my choice, but seems to slow. Indexes are already created
according to the OP, so that's no way to go either. Maybe save only
the day of the score in the YYYYMMDD format, and use
Time=CURDATE()+0.

About using COUNT(*):
"COUNT(*) is optimized to return very quickly if the SELECT
retrieves from one table, no other columns are retrieved, and there
is no WHERE clause."

There is a WHERE clause here, hence the optimization is lost. So,
maybe use COUNT(score). I don't know wether this will be faster, but
it's worth a shot.


Adding:
Is score an integer? That's how the database will work best. If it
isn't an
integer, consider making it one.

BTW: if this isn't a solution, try (alt.)comp.databases.mysql. Those
guys will know a lot more about mysql optimization.

Grtz,
--
Rik Wasmus

No, unfortunately the score is defined as decimal (20,2). So that's
probably one of the slow-down factors.
Is it just 1 decimal? In that case I'd suggest just multiplying it with 10
and store it like an integer.
The same goes for Time, if it's OK to have just a date, just store it like
an integer with date in YYYYMMDD format.
One solution to improve performance could be to have a table just
containing today's games, which will be cleared every midnight. Then
I can skip the CURDATE-thing, and the search will probably be a lot
faster.
It surely will, with a simple cronjob, but if you want to get info from past
days (which you want to save I presume, as you're keeping them in the
database), you'll have the same problem, only less often.
Thanks for the mysql-group-info.
NP

Grtz,
--
Rik Wasmus
Aug 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: francescomoi | last post by:
Hi. I'm trying to build 'MySQL-python-1.2.0' on my Linux FC2: ---------------------------------- # export PATH=$PATH:/usr/local/mysql/bin/ # export mysqlclient=mysqlclient_r # python setup.py...
4
by: mikey | last post by:
Hi all, I'm having great problems trying to install the latest MySQl RPM package onto my Red Hat Linux OS. There is already MySQL v 3.0 pre-installed with the RH Linux distribution disk but I...
0
by: Yun Guan | last post by:
Hello mysql gurus, I am trying to run perl on mysql database on Red Hat box. I want to install DBI and DBD:mysql using CPAN: perl -MCPAN -e shell cpan>install DBI The above succeeded, but...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: Saqib Ali | last post by:
I installed mySQL and have it running.... but I think I made a mistake somewhere along the line...... I believe I did follow the instructions that were provided with the distribution at:...
1
by: Alex Hunsley | last post by:
I am trying to install the DBD::mysql perl module. However, it claims I need mysql.h: cpan> install DBD::mysql CPAN: Storable loaded ok Going to read /home/alex/.cpan/Metadata Database was...
0
by: ./Rob & | last post by:
Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it...
2
by: trihanhcie | last post by:
I m currently working on a Unix server with a fedora 3 as an os My current version of mysql is 3.23.58. I'd like to upgrade the version to 5.0.18. After downloading from MYSQL.COM the package on...
1
by: manish deshpande | last post by:
Hi, When i'm installing MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm by the following command: rpm -i MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm the following error is being shown: ...
3
by: menzies | last post by:
Hi, I"m new to this forum, but I have been trying all day to install DBD::mysql onto my Intel MacBook. I've read lots of forums pages and none have gotten me to a successful 'make test' or a...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.