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-28-2005 10:40 PM

1) Does not work becasue MAX() is a grouping function
2) Does work, but gives the same output as in Post #3, eg. Table Scan :(

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

interesting as mysql.com said using force index makes it so it uses indexes only and requires no table scan>_>

maybe substitute FORCE INDEX with USE INDEX ;)

Marco van Herwaarden 09-29-2005 03:14 AM

[sql]select * from post where postid = (select max(postid) from post)[/sql]But i think this will only work for MySQL 4.1 and up.

Otherwise create 2 queries: 1 to get max postid, and 1 to retrieve the row.

Ohh my example is for post table, but should be the same for thread.

Also please notice that you should check with a fully loaded table, since the optimizer might decide that if there are only a few rows, it is more efficient to use a full table scan then to use the index.

Paul M 09-29-2005 11:07 AM

Quote:

Originally Posted by KirbyDE
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?

If I were you I would check/rebuild your indexes, you should get this ;

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

table        type        possible_keys        key          key_len        ref        rows        Extra 
thread        index        NULL                PRIMARY        4        NULL        82


Princeton 09-29-2005 12:07 PM

Quote:

Originally Posted by KirbyDE
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 ... :(

since there is no condition (WHERE and/or GROUP BY) - this query will be a full index scan

Marco van Herwaarden 09-29-2005 03:27 PM

Quote:

Originally Posted by princeton
since there is no condition (WHERE and/or GROUP BY) - this query will be a full index scan

But still could use the index for ordering.


All times are GMT. The time now is 07:42 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.01006 seconds
  • Memory Usage 1,727KB
  • 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
  • (2)bbcode_code_printable
  • (3)bbcode_quote_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
  • (6)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