Skip to content Skip to sidebar Skip to footer

Are Database Tables Sorted Before Or After Being Retrieved?

I am creating a high scores table stored on a database for my game, but I was wondering the best practices are for storing such tables. Should the table be resorted each time a new

Solution 1:

Rows in a relational database such as MySQL, Oracle, PostgreSQL, etc. are not maintained in any order. In the theory of relational databases result sets are returned in no specified order unless the query contains an ORDER BY clause. Any ordering is (must be) applied each time the data is retrieved.

Implementations may, in some cases, store the data in some order, but they are not required to do so. In fact, if you run the exact same query twice on the same data there is no guarantee that the data will be returned in the same sequence.

In other words, you cannot impose a storage order on your data, you impose order only on result sets at the time the query is executed.

Solution 2:

I recommend sorting the data in your MySQL query. As you said it is easier to only sort when needed, not when every record is added.

Solution 3:

Data in tables are unsorted. The actual physical order of rows in a relational table is undetermined. However, some databases will order rows on disks according to a clustered index.

If your tables contain a few thousand rows, two approaches are not much different about performance. However, if your tables are around more than 10,000 rows, you can use clustered index. ( for reference about clustered index, http://www.karafilis.net/sql-indexing-part2/).

Post a Comment for "Are Database Tables Sorted Before Or After Being Retrieved?"