I have a mysql database with a table with about 40k entries. Executing the following statement takes about 10 seconds (Database is already selected):
SELECT * FROM MyTable WHERE Column < 3
Why does this take so long and how do I improve the performance?
Are other Databases faster? (e.g. MongoDB, CouchDB, … ) I’d prefer to use a MySQL Database though.
EDIT:
The following query…
EXPLAIN SELECT * FROM MyTable WHERE Column < 3;
results in the following:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE Occurances index NULL SearchString 102 NULL 40242 Using where; Using index
The following query shows the distribution of values in the Column
SELECT COUNT(*), Column FROM MyTable GROUP BY Column;
The result is the following:
COUNT(*) Column
43 0
5 1
106 2
71 3
42 4
283 5
2337 6
9491 7
22073 8
1191 9
1064 10
1105 11
919 12
393 13
288 14
288 15
200 16
123 17
71 18
71 19
36 20
10 21
13 22
8 23
4 24
3 25
4 29
I’d venture to guess that you have no index on the
Columncolumn. Try creating one:Try comparing the output of
EXPLAINbefore and after you create the index:You should see that with the index, an index scan (or better) is performed.
An index will only help you if a small enough set of rows match your criteria. If most of the table matches the expression
Column < 3then an index won’t help and the planner will fall back on a table scan, since that will turn out to be faster than using the index.If you want a more detailed answer, then you’ll have to provide more information. The output of these two queries would be helpful:
As well as a list of indexes on
MyTable.