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
The "BEST" way to select the last row from a MySQL db.
Old 07-05-2010, 12:52 PM The "BEST" way to select the last row from a MySQL db.
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Hi Guys,

Im trying to lower the CPU time on some of my querys.

Currently if i want to select the age of the last person in my table ( table rows assigned auto incremented unique id's ) i would do this:

SELECT age FROM table ORDER BY id desc LIMIT 1;

Now,
While this works fine, I can see that it takes almost 3 seconds for it complete this query ( it says "sorting result" )

Is there a better way to do this?
All i find on google is the above result i came up with, However i fear this isnt the best way.

Ive got index's also, However it still takes a while. ( well, Not fast enough for me )

I was hoping there was some kind of
SELECT age WHERE row_is_last;

or something like that..

I fear the "ordering" part is eating up resources where its just not needed.
Thanks
G
__________________

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

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


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

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


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
 
Register now for full access!
Old 07-12-2010, 05:10 PM Re: The "BEST" way to select the last row from a MySQL db.
Novice Talker

Posts: 7
Location: USA
Trades: 0
Are you sure that your indexes are proper.

Also have you tried running

EXPLAIN SELECT age FROM table ORDER BY id desc LIMIT 1;

This will tell you what exactly mysql is doing to return the data and if all of the indexes are returned
__________________

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

Get various info about a website!
Who is hosting it, Google PR, Whois, Backlinks...
checkwebsite is offline
Reply With Quote
View Public Profile Visit checkwebsite's homepage!
 
Old 07-12-2010, 05:37 PM Re: The "BEST" way to select the last row from a MySQL db.
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Is your id field an integer ?
Is it the primary key ? If not, is there an index (composite or not) on that field.
And how many rows do you have in the table.
If you use mysql, which engine is used? Innodb, myisam ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-16-2010, 02:29 AM Re: The "BEST" way to select the last row from a MySQL db.
Experienced Talker

Posts: 41
Trades: 0
Hello friend, as you mention the code
SELECT age FROM table ORDER BY id desc LIMIT 1;
I think so, but you need more appropriate as compared that, still I'll my best but unable to provide an appropriate solution to you,Be patience
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
.
sandeep Kumar is offline
Reply With Quote
View Public Profile Visit sandeep Kumar's homepage!
 
Old 08-08-2010, 09:53 PM Re: The "BEST" way to select the last row from a MySQL db.
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
well, in another recent post, you mentioned having about "10000's" of database tables.

Perhaps that could be the reason it's taking so long to process your queries?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to The "BEST" way to select the last row from a MySQL db.
 

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.15484 seconds with 12 queries