Results 1 to 4 of 4

Thread: [mysql 5] optimisation for small indexes

  1. #1
    Join Date
    Jan 2008
    Posts
    3

    Question [mysql 5] optimisation for small indexes

    Hi,

    I warn you this is a very very strange problem.
    A problem of slowness with one of my indexes. Let me explain.

    I have a table users with about 100,000 entries.


    There is a field state, that is a int(4) and that has 3 different values.
    There is an index on this field.


    The following request runs in about 1 second (!)
    Code:
    SELECT id
    FROM users
    WHERE state > '0'
    ORDER BY id DESC 
    LIMIT 1

    I have another field birthyear that is a int(11) with no index on it.

    The following request (almost the same with a nex condition) runs in less than 0.001 second (!)
    Code:
    SELECT id
    FROM users
    WHERE birthyear > '1980'
    ORDER BY id DESC 
    LIMIT 1

    I told you, this is strange ...

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Did you report it to MySql vendor?

  3. #3
    Join Date
    Jan 2008
    Posts
    3
    Hi, didn't know how to do. How can I report to mysql vendor ?

    I have run an EXPLAIN on the 2 queries in case that can help to understand :

    slow one :

    id 1
    select_type SIMPLE
    table users
    type range
    possible_keys state
    key state
    key_len 4
    ref NULL
    rows 79245
    Extra Using where; Using filesort

    quick one

    id 1
    select_type SIMPLE
    table users
    type index
    possible_keys NULL
    key PRIMARY
    key_len 4
    ref NULL
    rows 84197
    Extra Using where

  4. #4
    Join Date
    Jan 2008
    Posts
    3
    Well I think I have solved the problem after reading this doc :

    http://dev.mysql.com/doc/refman/5.0/...imization.html


    Now the querie runs wihtin 0.003 second!
    Only by adding FORCE INDEX ( PRIMARY )


    Code:
    SELECT id
    FROM users
    FORCE INDEX ( 
    PRIMARY ) 
    WHERE state < '0'
    ORDER BY id DESC 
    LIMIT 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •