Re: Indexing: on WHERE columns or ORDER BY columns?
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
Subject
Views
Written By
Posted
2419
May 31, 2005 04:58AM
1585
May 31, 2005 07:23PM
Re: Indexing: on WHERE columns or ORDER BY columns?
1559
June 01, 2005 12:29PM
1443
June 02, 2005 06:44PM
1552
June 01, 2005 11:41AM
1526
June 03, 2005 05:21PM
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.