views:

94

answers:

6

What are the most efficient SQL queries that would take an existing MySQL table and re-sort it by one of the columns? Not a selection, but the whole table should be sorted by one column. Somehow using a temporary table, I guess, but what's the best way?

+2  A: 

The best way is to keep your hands off the table structure and learn database basics.

Make an index on this column.

Col. Shrapnel
It goes without saying the table is indexed; in fact I am sorting on the int(10) column that has the primary index.
Vacilando
+1  A: 

the table is just the data its not inherently sorted. a query obtaining the data from that table may be sorted, but the table itself is not.

Having indexes on the columns you want your queries to sort on will help give the best query time.

Sam Holder
+3  A: 

Tables aren't sorted*. Do not assume they contain data in any default order. This is what ORDER BY is for in your queries, even if there is no specific WHERE clause filter.

Joe
Tables can be sorted in SQL Server and PostgreSQL, just CLUSTER them on a index and you have a sorted table.
Frank Heikens
Well, if I do "SELECT column FROM table", I retrieve the results in the order they are written in the table (I assume always in the same order?!) But in my application I need them to be sorted. But when I do "SELECT column FROM table ORDER BY column" it is a very heavy operation. Because the table is static (regularly imported data from elsewhere), I would like to sort the table by column once and then my selections would be fast and sorted without using "ORDER BY". That's the idea anyway.
Vacilando
There is no way to guarantee anything about the "order they are written to the table". If your table uses InnoDB, there's some limited clustered index support (which defines the order the data is stored); for MyISAM this is not possible.
Joe
+1  A: 

The easiest thing is to add a key to that column.

ALTER TABLE table ADD KEY (column);

Then you can use EXPLAIN SELECT to show that you're using the key for the query.

If the table is constantly having rows added and removed, you can also run OPTIMIZE TABLE table for good measure.

Michael Vuoncino
+1  A: 

Even though you really shouldn't need to do it, you can use ALTER TABLE ... ORDER BY ... to reorder your table. But think twice before doing it. Then think a few more times. Then don't do it.

Ignacio Vazquez-Abrams
A: 

My 0.02c; maybe he just wants to reorder his table in a more readable way. Imagine a table like:

 TABLE FOO with ID Char(3) Primary Key,DESCRIPTION Varchar

with 4 rows

ID  | DESCRIPTION
001 | Test1
004 | Test4
003 | Test3
002 | Test2

It could be that rows 003 and 002 were inserted later (not known when table was created), so OP would like to reorder records in a more readable way (i know he could select with order clause).

In this case i would backup the table (SELECT * INTO backup_FOO FROM FOO) , remove every constraints, truncate it and reload it ordered by OP preferred column.

systempuntoout
whilst this might be true, why not just view the table with a query with an order? What does reordering the table give you over that?
Sam Holder
I'm just speculating here :): you don't have to specify "order by" everytime you make a select and, speculating again,a partially ordered table could be misleading for the reader who does not know that some missing IDs are at the bottom of the table.
systempuntoout
"Why not just view the table with a query with an order?" As systempuntoout guessed, it is precisely because if the table were ordered by ID then subsequent selects would not need to use "ORDER BY" and they would be quicker. (Again, the table in the example does not change dynamically.)
Vacilando
@Vacilando So, why don't you just backup and reload the table as i said?
systempuntoout
@Vacilando I was the only one who undestood your problem,i've upvoted your question, i got 0 point and you marked another answer Valid.Sad life :))).
systempuntoout
I marked Sam Holder's answer above correct because he seems to say there is no way to have an inherently sorted table; that records must be sorted during selection. Correct me, anybody, if I did not understand Sam's answer correctly.
Vacilando