![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? |
#3
| |||
| |||
|
|
On 2011-02-24 13:57, The Natural Philosopher wrote: I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? If you don't specify an order by clause the dbms is free to return the rows in whatever order it find most appropiate at the moment. |
#4
| |||
| |||
|
|
On Thu, 24 Feb 2011 14:10:41 +0100, Lennart Jonsson wrote: On 2011-02-24 13:57, The Natural Philosopher wrote: I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? If you don't specify an order by clause the dbms is free to return the rows in whatever order it find most appropiate at the moment. Which might be in oder written, which might be tempered by reuse of rows, which might be altered by whatever results are lying around in a cache. In short, there is no "natural order". well obviously there IS, but it is not something that is reliably the |
#5
| |||
| |||
|
|
I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? |
#6
| |||
| |||
|
|
Peter H. Coffin wrote: On Thu, 24 Feb 2011 14:10:41 +0100, Lennart Jonsson wrote: On 2011-02-24 13:57, The Natural Philosopher wrote: I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? If you don't specify an order by clause the dbms is free to return the rows in whatever order it find most appropiate at the moment. Which might be in oder written, which might be tempered by reuse of rows, which might be altered by whatever results are lying around in a cache. In short, there is no "natural order". well obviously there IS, but it is not something that is reliably the same .. Either by accident or by design.. Thanks for the response..I hadn't thought about caching at all. |
#7
| |||
| |||
|
|
El 24/02/2011 13:57, The Natural Philosopher escribió/wrote: I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? The default order in SQL explicitly undefined so DBMS are free to do what best suits them. The obvious approach is to send data in the same order it's retrieved from disc/memory/cache (any non-requested sorting would just increase query time for no reason). Such order probably depends on both the physical location of data and intermediate operations required to comply with the query terms (imagine a GROUP BY clause or aggregate functions). I'm sure that even a mere server restart will alter the order of some queries. So the no-order order is not really random but it's pretty arbitrary. Yup. you say 'explicitly' undefined..where is that made explicit in mysql? |
#8
| |||
| |||
|
|
Álvaro G. Vicario wrote: El 24/02/2011 13:57, The Natural Philosopher escribió/wrote: I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? The default order in SQL explicitly undefined so DBMS are free to do what best suits them. The obvious approach is to send data in the same order it's retrieved from disc/memory/cache (any non-requested sorting would just increase query time for no reason). Such order probably depends on both the physical location of data and intermediate operations required to comply with the query terms (imagine a GROUP BY clause or aggregate functions). I'm sure that even a mere server restart will alter the order of some queries. So the no-order order is not really random but it's pretty arbitrary. Yup. you say 'explicitly' undefined..where is that made explicit in mysql? |

#9
| |||
| |||
|
|
On 24-02-11 18:14, The Natural Philosopher wrote: Álvaro G. Vicario wrote: El 24/02/2011 13:57, The Natural Philosopher escribió/wrote: I was just prototyping a select on a single table, when I noticed that the order of what came out, was rather weird. There is an autoincrement on the primary key, and I expected that in the absence of an 'order by' clause the order would be ascending in terms of insertion, hence ID, but it appeared that one late entry was at the top of the list. Possibly an older deleteed entry had been 'reused'? with a high number key? This is a curiousity question only: obviously if I cared about the order there would be an order by statement in there. I just wondered whether the behaviour was defined, or really 'implementation dependent' and might vary according to the storage engine and indeed OS platform used? The default order in SQL explicitly undefined so DBMS are free to do what best suits them. The obvious approach is to send data in the same order it's retrieved from disc/memory/cache (any non-requested sorting would just increase query time for no reason). Such order probably depends on both the physical location of data and intermediate operations required to comply with the query terms (imagine a GROUP BY clause or aggregate functions). I'm sure that even a mere server restart will alter the order of some queries. So the no-order order is not really random but it's pretty arbitrary. Yup. you say 'explicitly' undefined..where is that made explicit in mysql? Its in the definition of SQL, i'm pretty sure the link is somewhere on the wikipedia-link ![]() http://en.wikipedia.org/wiki/SQL The ORDER BY clause identifies which columns are used to sort the resulting data, and in which direction they should be sorted (options are ascending or descending). Without an ORDER BY clause, the order of rows returned by an SQL query is undefined. Excellent Luuk, I knew if anyone had the definitive answer, it would be you. |
#10
| |||
| |||
|
|
Peter H. Coffin wrote: Which might be in oder written, which might be tempered by reuse of rows, which might be altered by whatever results are lying around in a cache. In short, there is no "natural order". well obviously there IS, but it is not something that is reliably the same .. |
![]() |
| Thread Tools | |
| Display Modes | |
| |