Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] MySQL select default order

Started by Tree Penguin Aug 25, 2006 at 7:20 AM 2 replies 5.4k views
Original Post
Tree Penguin
Tree Penguin
Hi, i didn't really get a definitive answer on this anywhere so here it goes: When you select rows in MySQL without adding an ORDER to the query, do you always get the rows in the order in which they are stored into the database, and does that order stay the same as long as the database keeps running, even if rows are added? And what if rows are deleted, is there any chance the order of rows in the database is altered, like MySQL putting the last row in there to fill the gap, or are new rows inserted into these gaps first instead of putting them at the end? Thanks.
Mathachew
Mathachew
I believe the default order of the rows selected will be based on the index set for that table. For the last part, no, it will simply display the same order minus the deleted row, and any additional rows are simply added to the bottom. If I'm not mistaken, it's based on the primary index.
Graphain
Graphain
MySQL Databases are unordered which means, unfortunately for you if you are dealing with existent implemented data, you have to put some form of identifcation of data entry order by either a time-date field or an index if you want to deal with the database from a first-in first-out perspective. I'm not 100% sure on the default sorting setting for a select command but I have a hunch it would be ordered ascending/descending for the first column selected.
markr
markr
If you don't supply an ORDER BY, the results will come out in no particular order. This cannot be guaranteed to be the same from moment to moment (although it usually is).

The exact order that this is is highly dependent on what you're doing. If you're doing a GROUP BY, for instance, it often orders it by some index it's picked up in the group by.

It's typically whatever index it happens to have used to do some filtering / joining / grouping last or something.

If you don't put in a WHERE clause, join, or group by, then I imagine it's the table order. But even that isn't guaranteed.

But it really CANNOT be relied on to be consistent.

Mark

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.