开发者

If I frequently ORDER BY o.creationDate - should I mark creationDate with @Index?

开发者 https://www.devze.com 2023-01-21 18:43 出处:网络
I know that indexes on database are a good way to increase query performance, but how much performance I would gain by creating an index for a creationDate property of a given object that I frequently

I know that indexes on database are a good way to increase query performance, but how much performance I would gain by creating an index for a creationDate property of a given object that I frequently order by creationDate in my开发者_开发技巧 queries? Would this frequently "order by o.creationDate" justify creating an index for the creationDate property?


Yes, if the result set returned from your select is large. Remember that the ordering is (logically) performed on the result set after filtering, but a good optimizer might use an index during filtering for ordering as well. If you are returning a results set with five rows, ordering is trivial. If it contains a million rows, a (B-Tree based) index would definitely help. A hash-index would be of no help, however.

You can get more details with regard to MySQL and indexes for ORDER BY columns from the following article: Database Index Tips


It depends on your read/write ratio as to if it's justified, but will it help? From your usage description yes, an index on creationDate will allow it to run the query without an extra sort operation at the end on it. You can find a full write-up on ORDER BY optimization here.

I'd say if this is a web application or any other high read:write ratio application then yes, it's worth it. Just remember that indexes do have a cost, and inserts or updates on that column will be more expensive when it's added, but when your reads far outweigh the writes, it's usually worth it.

0

精彩评论

暂无评论...
验证码 换一张
取 消