MySQL Forums
Forum List  »  Performance

Re: Indexing: on WHERE columns or ORDER BY columns?
Posted by: Daniel Nichter
Date: June 01, 2005 12:29PM

Including col3 in the index will avoid the filesort. Here is an example:
-- Before with only an index on (col1, col2) ...
mysql> EXPLAIN SELECT * FROM foo WHERE col1 = 10 and col2 = 100 ORDER BY col3\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: foo
type: ref
possible_keys: col1_2
key: col1_2
key_len: 10
ref: const,const
rows: 1
Extra: Using where; Using filesort

-- And after with an index on (col1, col2, col3) ...
mysql> CREATE INDEX col1_2_3 ON foo (col1, col2, col3);
Query OK, 100000 rows affected (0.43 sec)
Records: 100000 Duplicates: 0 Warnings: 0

mysql> EXPLAIN SELECT * FROM foo WHERE col1 = 10 and col2 = 100 ORDER BY col3\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: foo
type: ref
possible_keys: col1_2,col1_2_3
key: col1_2_3
key_len: 10
ref: const,const
rows: 1
Extra: Using where

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Indexing: on WHERE columns or ORDER BY columns?
1559
June 01, 2005 12:29PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.