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 09-27-2005, 06:39 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default "ORDER BY" without large table scans and filesort

OK, this seems to be impossible but I thought I could ask anyway, maybe I overlooked smth. and somebody has the 1.000.000 $ answer

Let's say I got a table with several tenthousand records; the primary index as an auto-increment integer.
Now I want the last record.
I could do
[sql]SELECT * FROM table ORDER BY id DESC LIMIT 1[/sql]

But this causes large table scans ...
Reply With Quote
  #2  
Old 09-28-2005, 07:46 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Primary index should be an unique index, and not cause full table scans.

Try playing with a HAVING max(id).
Reply With Quote
  #3  
Old 09-28-2005, 08:15 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmm ...

[sql]EXPLAIN SELECT * FROM thread ORDER BY threadid DESC LIMIT 1[/sql]

Code:
 
id  	select_type	table	type	possible_keys	key	key_len		ref	rows  	 Extra
1 	SIMPLE 		thread	ALL	NULL		NULL 	NULL		NULL	32
I only got 32 Threads on my testboard, so this is a full tablescan.
Any ideas?
Reply With Quote
  #4  
Old 09-28-2005, 08:47 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well this seems like there is no index on threadid. Try creating another unique ascending index on threadid.
Reply With Quote
  #5  
Old 09-28-2005, 08:52 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Erm ... threadid is the PRIMARY index of table thread
Although it IMHO doesn't make sense to add another index to the column, i did so - no change.

Schema
[sql]
CREATE TABLE thread (
threadid int(10) unsigned NOT NULL auto_increment,
title varchar(250) NOT NULL default '',
firstpostid int(10) unsigned NOT NULL default '0',
lastpost int(10) unsigned NOT NULL default '0',
forumid smallint(5) unsigned NOT NULL default '0',
pollid int(10) unsigned NOT NULL default '0',
`open` smallint(6) NOT NULL default '0',
replycount int(10) unsigned NOT NULL default '0',
postusername varchar(100) NOT NULL default '',
postuserid int(10) unsigned NOT NULL default '0',
lastposter varchar(50) NOT NULL default '',
dateline int(10) unsigned NOT NULL default '0',
views int(10) unsigned NOT NULL default '0',
iconid smallint(5) unsigned NOT NULL default '0',
notes varchar(250) NOT NULL default '',
visible smallint(6) NOT NULL default '0',
sticky smallint(6) NOT NULL default '0',
votenum smallint(5) unsigned NOT NULL default '0',
votetotal smallint(5) unsigned NOT NULL default '0',
attach smallint(5) unsigned NOT NULL default '0',
similar varchar(55) NOT NULL default '',
PRIMARY KEY (threadid),
UNIQUE KEY threadid (threadid),
KEY postuserid (postuserid),
KEY pollid (pollid),
KEY forumid (forumid,visible,sticky,lastpost),
KEY lastpost (lastpost,forumid)
)
[/sql]
Reply With Quote
  #6  
Old 09-28-2005, 10:15 PM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

[sql]
SELECT MAX(threadid) AS threadid FROM thread;
[/sql]
Reply With Quote
  #7  
Old 09-28-2005, 10:19 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah, that gives me the ID - but I need the whole record
Reply With Quote
  #8  
Old 09-28-2005, 10:26 PM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

maybe
[sql]
SELECT MAX(threadid) AS threadid, thread.x FROM thread;
[/sql]
Reply With Quote
  #9  
Old 09-28-2005, 10:32 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Does not work, as MAX() is a grouping function
Reply With Quote
  #10  
Old 09-28-2005, 10:37 PM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

[sql]
SELECT MAX(threadid), title FROM thread;
[/sql]

Or maybe
[sql]
SELECT * FROM thread FORCE INDEX (indexname) ORDER BY threadid DESC LIMIT 1;
[/sql]
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 02:43 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.07303 seconds
  • Memory Usage 2,251KB
  • 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
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete