473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Keeping data sorted in a table

Hello,
To followup my last post, how do I keep a field in a table already
sorted. eg. if a field in it going to have values like "10", "3330" and
"1", I want the row with the "1' first in the table, and the "10"
second and so on. I want this to happen when I UPDATE, *not* when
SELECT-ing, as, according to the last post, if I 'm doing a SELECT num
where (num>1 AND num<2000) AND bar = "Toyota", by the time it gets past
the "10" row, it should know not to go any further down the list
looking for bar="Toyota".
Thanks.

Nov 6 '05 #1
2 2687
bi**********@hotmail.com wrote:
To followup my last post, how do I keep a field in a table already
sorted.
Databases don't have actual order for rows. It is only guaranteed that
the data will be sorted correctly when you run select with "order". If
you don't use "order", the data is _usually_ in the order you inserted
it in, but I wouldn't trust on that:

select * from tablename order by columnname;

So if you don't have any column you can sort the data in your
select-queries, you need to add one and for example insert numbers in
that column in the order you wish the rows to be.
it should know not to go any further down the list
looking for bar="Toyota"


MySQL database is propably smarter than you or I are. If you use indexes
properly, the database will automatically try to make queries as fast as
possible, so you don't need to worry about things like that. Sometimes
the fastest way could be to search for Toyota, instead of checking 2000
rows that have correct num-value. Assuming "bar" field has index also.

Or do you want your queries to run as you wish, no matter how slow it
will get?
Nov 6 '05 #2
>To followup my last post, how do I keep a field in a table already
sorted.
You DON'T. TABLES DO NOT HAVE AN ORDER. They can have indexes.
Those are sorted. You should create indexes on a table considering
the uniqueness constraints you want, and the queries you intend to
make. Indexes, if done well, speed up queries a lot but slow down
inserts and deletes (and updates, if the keys change), so don't
just make an index on everything or every combination of everything.
eg. if a field in it going to have values like "10", "3330" and
"1", I want the row with the "1' first in the table, and the "10"
second and so on.
Tough noogies. TABLES DO NOT HAVE AN ORDER. However, you might
want to create an index on that field.
I want this to happen when I UPDATE, *not* when
SELECT-ing, as, according to the last post, if I 'm doing a SELECT num
where (num>1 AND num<2000) AND bar = "Toyota", by the time it gets past
the "10" row, it should know not to go any further down the list
looking for bar="Toyota".


Note that inserting a record physically at the beginning of a 10GB
database can be VERY time-consuming. Inserting it somewhere and
putting it an an index (or several of them) is much faster.

If you have indexes on both num and bar, MySQL can decide (probably
better than you can) whether it is best to do an exact match on bar
= 'Toyota' (then look for records with num within the proper range)
or do a range match on num (then look for records with bar =
'Toyota').

Or, if you have an index on (bar,num), it can find the exact records
required from the index. If you have an index on (num,bar), though,
that's not better than an index on num alone. Think about trying
to look up people in a phone book with the first name 'George' and
a last name beginning with M-Z.

Gordon L. Burditt
Nov 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Terry | last post by:
I need some help refining an MS 2000 relational databse. I have created a simple relational database using two tables, 'Student Details', 'Exam Details' and two forms, 'Input/Edit Exam Details',...
2
by: kids | last post by:
Hi all, i have 2 files, 1 file that store 1000 records of name entries and it's key ID. With the sorting function all the records have been sorted and its correspond key id was store in a sorted...
3
by: Antanas | last post by:
Why is that even though I have added index with DESC sort order on primary key, records in Control Center are displayed by the sequence they were previously inserted, not by the value of primary...
12
by: Cindy Lee | last post by:
When I do a sorta on 1 table, then the other table goes back to the original order. What can I set so, it keeps the order of the other current gridview's order. I set all the gridview values...
1
by: jjjoic | last post by:
Hi, I use Access 2003 to generate the back-end data for a ColdFusion report at work. The report is sorted by a column and based on the sorting, rankings are assigned to each row(i.e. the biggest...
6
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my...
1
by: helpmeplease213 | last post by:
Hello, I have a form which has a option group, when a option in the group is selected it brings up a text box, you then enter a parameter into that text box and the data should be sorted out...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
0
by: coolsti | last post by:
To the more experienced C# programmers, how do I do this best? I have a 2 dimensional mapping of values in a database, which represent a somewhat round (but not exactly round) shape when drawn out...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.