[Tutor] Sorting Data in Databases

Lie Ryan lie.1296 at gmail.com
Mon Nov 23 05:17:03 CET 2009


Ken G. wrote:
> The way I use to do it in another programming language (Liberty Basic in 
> Windows) was:
> 
> 1. Count number of data entry in database.
> 2. Dimension or create an array to equal the maximum number of entry.
> 3. Copy data entry from database into array.
> 4. Use the SORT command on array, ascending or descending sequence.
> 5. Copy sorted data from array into database.

In any programming language, that was the wrong algorithm, except for 
very low-volume purpose. You should ask the DBMS system to sort the 
database whenever possible instead of doing the sorting yourself.

>  From what I know of Python so far, is it possible to sort the database 
> as it or do I have to copy data into either a list, tuple or dictionary 
> and sort there and then, after sorting copy sorted entry back into the 
> database.

Question time:
1. What DBMS are you using?
2. What python interface to the DBMS you are using?
3. Why do you think you need to sort the database? There is no advantage 
(that I know of) of having a sorted database. A decent DBMS should 
determine how best to store the data (it's their problem, not yours). If 
your DBMS can't do this, you should consider using another DBMS.
4. Can't you simply sort the queried data? An SQL-based DBMS should 
support ORDER BY command to sort the SELECT-ed rows. That should be 
faster than sorting the whole database. You can sort the returned tuple 
yourself, but it would be very difficult to beat the DBMS's ORDER BY.

> An outline of what needed to be done will be fine so I can study and 
> learn the proper way of doing this in Python.  FWIW, I just finished 
> Alan's tutorial on list, tuple and dictionary.  Whew!!!  Quite 
> comprehensive.



More information about the Tutor mailing list