PDA

View Full Version : large tables: is there a better way?


elvie
04-07-2004, 04:32 PM
I've got a product from created in PHP, with up-to 1000 values.

A typical insert query looks like

INSERT INTO `users_orders` VALUES ('3-070404-66093', 3, 'p161ahmar60m', '', '20665', '13845.55', 5, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 20, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

and with just 5 entries in this table it's a whooping 16kb in file size.

Is there a better – faster - smaller way to store the above information?

Thanks in advance...

Kiwi
04-07-2004, 10:34 PM
The size of each row stored is determined by the table definition -- when you create a new row, for most data-types, the database will create the space needed for each column.

Is there a better – faster - smaller way to store the above information?Speed is dependant on a bunch of different things -- but the size of the file is not really one of them. The number of rows, the number of indexes, the volume of data to be returned (or entered). These all effect speed -- but the table size on it's own is not really an issue.

As for size, the things you could do to reduce the file size are likely to slow down the select speed. The smallest non-compressed file structure for alpha-numeric data is a CSV (only the data is stored, a delimiter per row for each column and another for each row). However, it is also one of the slowest file-types to use for data work. Creating an index increases the file size and icnreases the select speed.

If you put up your table definition, there might be some things you can do to desrease the file size, but I doubt the gains are going to be huge.

raf
04-09-2004, 10:54 PM
For an SQL db, the overall size of a table is not that important.
This is maybe best illustrated by comparing a lookup in a CSV to a lookup in an SQL servers table.
CSV's are searched sequentialy. So it starts at the beginning and then runs through the file until the point where it finds the data you're looking for. Pretty much like a cassette --> you need to fastforward it to find the piece of music you need. If the mucsic you search is in sector 9056 then you could end up first going through 9055 sectors ...
In an SQL db, you can use indexes. Indexes are seperate, summerising 'side-tables' that contain a reference to the datapages where certain data is stored. For instance, references to the datapages where there are records with value X for variable Y. It then only needs to scan the datapage, instead of the complete table, which is much faster
It's like a CD --> if you need sector 9065, then it will first jump to cycle 52 and then look for sector 45 in that cycle (so you make two, much faster movements, instead of one slow movement)

So it's not the size, but the way that the data is stored and the accespaths that are determining the speed.

You should hower keep the table as small as possible, by
- limiting the columns to the smallest possible columntypes and lengths. If you for instance want to store ip's, then you don't need a memo feeld, or a varchar(255). A varchar(15) should normally do.
This is very important because when you read or write to a record, the RDBM will reserve memoryspace to read in/create the record, based on the tabledefinition. Taking up to much space, is wasting expensive resources for other processes and will have an effect on the overall perfomance;
- normalizing the db --> avoid dataredundancy by setting up a relational design where all data (specialy text) is only stored once, and then reused with primary key - foreign key relationships;
- don't have these teacher1, teacher2, teaher3, ... teacher10 columns in a table --> move such repeated column into a seperate table and have them as rows inthere.
- clean up our tables and archive unnesecarry records on a frequent basis. There is nothing as stupd as for instance a forum with 130 000 posts in it's operational table, if 129 000 of theme were not requested at all in the last year. An intelligent archiving policy will move older post into an archive table, and will only grab the records when they are requested or searched for