0

I would need some help on how to optimize the query.

select * from transaction where id < 7500001 order by id desc limit 16

when i do an explain plan on this - the type is "range" and rows is "7500000" According to the some online reference's this is explained as, it took the query 7,500,000 rows to scan and get the data.

Is there any way i can optimize so it uses less rows to scan and get the data. Also, id is the primary key column.

5
  • 1
    This should be a give, but just in case, I need to ask: is there an index on id? Commented May 7, 2010 at 2:51
  • 1
    @Max: it's PK, as he specified in the end of the question. Commented May 7, 2010 at 2:53
  • @zerkms: I could swear that wasn't there when I read it, but there's no revision history. Weird. Commented May 7, 2010 at 3:01
  • @Max: it was definitely there, because it was there before I start answered. Commented May 7, 2010 at 3:21
  • yeah, as zerkms mentioned in his answer- why to retell what explain have said? Why not to bring the explain output itself? Commented May 7, 2010 at 4:09

2 Answers 2

1

online reference's this is explained as, it took the query 7,500,000 rows to scan and get the data

not actually. it's the approximate (optimizer cannot say the correct number in many different cases) number of rows that potentially will be scanned. but you specified LIMIT - so only first 16 rows will be affected while query executed.

ps: i hope the used key in EXPLAIN is id?

Sign up to request clarification or add additional context in comments.

2 Comments

The used key in EXPLAIN should be PRIMARY
yep. actually it changes nothing. whether key (contains id as it leftmost part) is used - this query would be fast.
0

I performed an explain with your query on a 8 million rows table

id   select_type  table       type  possible_keys  key      key_len  ref    rows     Extra
1   SIMPLE       transaction range PRIMARY        PRIMARY  8        NULL   4079100  Using where

The actual execution was fast, Execution Time : 00:00:00:044.

1 Comment

Its not about time, but about the no. of rows it read to get the data. that's the only concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.