vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   "ORDER BY" without large table scans and filesort (https://vborg.vbsupport.ru/showthread.php?t=97015)

Andreas 09-27-2005 06:39 PM

"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 ... :(

Marco van Herwaarden 09-28-2005 07:46 PM

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

Try playing with a HAVING max(id).

Andreas 09-28-2005 08:15 PM

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?

Marco van Herwaarden 09-28-2005 08:47 PM

Well this seems like there is no index on threadid. Try creating another unique ascending index on threadid.

Andreas 09-28-2005 08:52 PM

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]

AN-net 09-28-2005 10:15 PM

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

Andreas 09-28-2005 10:19 PM

Yeah, that gives me the ID - but I need the whole record :)

AN-net 09-28-2005 10:26 PM

maybe
[sql]
SELECT MAX(threadid) AS threadid, thread.x FROM thread;
[/sql]

Andreas 09-28-2005 10:32 PM

Does not work, as MAX() is a grouping function

AN-net 09-28-2005 10:37 PM

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

Or maybe
[sql]
SELECT * FROM thread FORCE INDEX (indexname) ORDER BY threadid DESC LIMIT 1;
[/sql]


All times are GMT. The time now is 04:45 AM.

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.01188 seconds
  • Memory Usage 1,726KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete