user2015253
user2015253

Reputation: 1444

store data ordered by a row in mysql?

Can I somehow make that mysql inserts data in a way that the database keeps an predefined order?

Let's say I make a highscore list. I could always use ORDER BY when selecting the stuff, but all the ordering in 1,000,000+ datasets takes alot of performance when a user browses the highscore list.

My idea is now that I want to insert the data in a way, that the table is always ordered by score desc, so all the ORDER BY work doesn't have to happen when users browse the list.

Upvotes: 0

Views: 74

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239724

Tables have no inherent order that you should rely upon (they may, by coincidence, return rows in the same order that they were inserted, or by primary key sort order), so if you need a particular order, you should always use an ORDER BY clause.

However, the system may be able to perform the ordering more cheaply if it has an index available on the column on which you will have your ORDER BY clause - so add an index:

CREATE INDEX IX_Table_Scores ON `Table` (score desc);

Upvotes: 2

Hardik Patel
Hardik Patel

Reputation: 716

Also there is another solution. If you store these much records in order by than there is tedious. Another way is you can create index on high score column so mysql internally create one index so when you try to fetch these record in order by than its performance is better than direct fetching query.

Please create index of high score column.

Upvotes: 0

Related Questions