MySQL Pagination - Should sorting be cached?
I can't find any information on this, so either MySQL handles this and is therefore a no开发者_JAVA百科n-issue or I'm using the wrong keywords:
If someone wants to sort MySQL results, should I have a cached version of the sort somewhere or should i make my queries with an ORDER BY clause? IS there a better way to do this so that users are not sorting thousands of rows every time they change the page?
You should create an index on the column that wish to order by.
See the documentation ORDER BY
Optimization for when MySQL can use an index to improve the performance of a query with ORDER BY
.
You should create an index "with a sort criteria" on the column that you want to sort.
This is the syntax to create mysql index:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[index_type]
ON tbl_name (index_col_name,...)
[index_type]
index_col_name:
col_name [(length)] [ASC | DESC]
So, if you usually retrieve some data sorted by some_column
ordered DESC
, you should:
CREATE INDEX my_index ON my_table (some_column DESC)
精彩评论