A teachable compression algorithm?

Status
Not open for further replies.
I'm working on a very large production system which needs to store many, many small segments of similar, text-like data (think alphanumeric with many similar substrings).

If we gzip each segment individually, we get a terrible 80% compression ratio. If we compress a few million rows together, we get a 8% compression ratio. I imagine the problem is gzip can't intrinsically know that each segment is similar, causing it to rebuild some virtually identical index or key table (or something) for each segment -- whereas for the combined compression, it only has to build this once.

Is there another algorithm we can use? Something that we can 'teach' about the general syntax/contents of the data we intend to compress? Given the scale of the system, it would have to be a very mature algorithm, which is disheartening because my googling could come up with nothing...

We use Java. And oracle on the backend.
 
A small segment of text isn't going to be very compressible, except for rare cases. Do yourself a favor and learn the LZ(X) compression algorithm and you'll understand pretty much every lossless algorithm. It's damn simple to learn, it's just the theory behind why it works takes some thought.

I'm just rather perplexed at why the hell you need to compress data in a database. It pretty much destroys all ability to treat the data as part of the database. It might as well be external if you can't process it.
 
You mistake me. I understand why a small segment is not very compressable.

My understanding also helps me understand why a concatenation of millions of such small segments are highly compressable. Which leads to the question -- if you can teach an algorithm the pattern of your data, can't these millions of small segments be highly compressable independently, as long as they follow the learned pattern? The LZ77 algorithms don't work in this teachable way -- but maybe another does?

Unfortunately I can't go into detail about the data, but our reason for compressing it are not misguided. The data is relatively freeform, and does not need to be indexed or searched; just stored.
 
Goetterdaemmerung":3u01tm96 said:
Which leads to the question -- if you can teach an algorithm the pattern of your data,

This statement is in conflict with the previous. If you knew the basics of compression, especially Huffman and LZ(X), you'd know they both build dictionaries of terms/symbols. One method "learns" the entire distribution of symbols, the other builds a dictionary of arbitrary length terms on the fly.

As I said, take the 10 minutes to understand the LZ(X) compression and you'll answer almost all of your questions.
 

norton_I

Ars Tribunus Angusticlavius
6,172
Subscriptor++
Huffman coding would be the easiest thing to implement this way, but it also won't exploit a lot of your compressibility. Just build up a frequency table from characteristic data and use that to encode each small string.

LZ77 based algorithms should also be adaptable. The idea would be that you run the compressor on some sample data to build up a dictionary, execute an output flush and save that state. Then feed each small string to the compressor, returning it to your saved state each time. Do the same thing for the decompression.

Both options have the intrinsic flaw that if the nature of the data changes over time your dictionary will become obsolete and the compression ratio will get worse and worse. The only way to fix that would be to decompress everything, generate a new dictionary, and recompress everything.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Both options have the intrinsic flaw that if the nature of the data changes over time your dictionary will become obsolete and the compression ratio will get worse and worse. The only way to fix that would be to decompress everything, generate a new dictionary, and recompress everything.

norton hits it on the head here.

You are asking to denormalise data *within a field*. If you want to do that split the field up first... RDBMS's tend to be designed on the basis that the designer of the schema will attempt to reduce redundancy, and that they are best placed to make the space/time tradeoffs involved.
 

JustinS

Ars Legatus Legionis
10,123
JoeJoe didn't really get an answer to his question ("It might as well be external if you can't process it.") so I'll phrase it a different way: is there any particular reason you aren't simply storing (groupings of) the data segments packed on a filesystem somewhere, in the obvious format of compressed tar files? Given that you don't need to index or search the data, the obvious approach is to use the database simply to index where a given segment is stored. This buys you a massive scalability advantage as well, since the amount of data you can afford to store (on any number of machines) using this approach is essentially arbitrary. The CPU time spent in compression/decompression could be decoupled from Oracle CPU time, which might represent a massive cost savings and performance increase.

If you look, I'm sure you'll find some operational benefits as well. (backup/restore leaps to mind) You would absolutely not be the first person to use Oracle in this way.

If having the data stay on Oracle 100% of the time is a priority for some reason, you can store each segment as its own little row when you bring it in and concatenate/compress/store a number of these rows periodically. No new compression algorithm required (depending on the nature of the data you might get the compression you want for free by using compressed tables with this approach) and it is easy to do with PL/SQL or java.

edit: you have played around with Oracle's compression features, haven't you? I should have started there. :)
 
The thing to remember here is that generally the compression coefficients are stored with each item. So if you share one coefficients table with a thousand items, you amortize the space overhead of the table. Which is probably why you are seeing so much better results when you compress in bulk. To avoid that, you would basically have to avoid storing the coefficients on every record.

One way to do that might be simply to choose a decent set of coefficients for your data (based on compressing thousands of items together), then always use that table (stored elsewhere) instead of packing a more exact set of coefficients with each item.

I guess that to 'learn' you could do a compression of the most recent N entries every now and again, and use that for smaller individual items. The assumption is that the data has some kind of temporal similarity. Compress the data (presumably not the lookup fields, else you won't be able to retrieve it) using the most recent coefficients. As above, do not store the coefficients table with the compressed data. Have an additional table that keeps a gradually increasing number of entries that are compression table coefficients. Store the coefficients index only with the compressed item data.

You will need to be able to rip apart the compression/decompression algorithm to do this, I would think, as you will need access to the compressed data separately than the coefficients.

If you're really worried about space, you could try a few different coefficient tables from the history, maybe selecting randomly or based on past success, and choose the best one to store. Obviously that would take a bit longer, so it all depends how much this matters to you.
 
throwing this out ;)

Magnetic Scrolls (British version of Infocom) employed a static huffman dictionary, so it was global, but each sentence of text in the game was compressed on its own, using the static dictionary, which gives you great compression on a micro basis.

The problem is, the dictionary is computed when all the text is known before hand so it is 100% optimal, and the text of the game does not change.

The best thing for you to do is have some form of on disk compression at the file system level, if you can.
 

cmannes

Ars Scholae Palatinae
836
Subscriptor++
This may be a stupid thought.

If you have a large number of small character sequences, of which there is a repeating pattern. (If I'm understanding this right.)

Couldn't you just split the sequence into pieces, identify the number of unique pieces, and do a lookup table (or tables) in the DB?

Really simple example.

Code:
Sequence 1: "Bob  Fred Ralph"
Sequence 2: "Fred Fred Ralph"
Sequence 3: "RalphRalphRalph"

Lookup Table
1 - "Bob  "
2 - "Fred "
3 - "Ralph"

Stored value 1 "123" 
Stored value 2 "223"
Stored value 3 "333"

You've now effectively compressed the data.

EDIT: Added the missing spaces. :)
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I actually learned Huffman compression from Huffman! But it was an 8:00am class, so it's kind of fuzzy in my memory.

Had you spotted an error I'm pretty sure that would have been priceless :)
I really would have love a Knuth cheque.

Incidentally if the values discussed here exhibit low entropy at the individual character level (most characters are used infrequently and just a few dominate) then going with a huffman dictionary will likely be the simplest solution (you then need an additional reference per row to the dictionary in case this distribution changes significantly and you aren't willing to go back and regenerate everything). The downside is that this would really require storage as bit sequences (so any particular byte is legal) rather than characters.
 

MilleniX

Ars Tribunus Angusticlavius
8,139
Subscriptor++
ShuggyCoUk":3m2sbwux said:
Pont":3m2sbwux said:
I actually learned Huffman compression from Huffman! But it was an 8:00am class, so it's kind of fuzzy in my memory.

Had you spotted an error I'm pretty sure that would have been priceless :)
I really would have love a Knuth cheque.
Knuth checks (he's American, spell it right! ;) ) are not that hard to come by. I was reading one of the fascicles for TAOCP Volume 4, found something that could be improved, and tossed it his way. I have a letter and check from him for 32 cents hanging over my desk. Not quite the same as an error, but it's still an autographed check.

Phil
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
but just happened across this blog post on a probabilistic lossless compression algo and thought it might be relevant.

http://www.deplump.com/contact.html

licensing information send email to licensing@deplump.com

Non free (in any sense right now) and alpha, wouldn't go near it with a barge pole for the OP's needs
 
Am I missing something, or isn't the entire purpose of having an archive container (zip, rar, etc) because the dictionary is shared by all of the components in the container. So moving the dictionary outside of some container, seems like it is just setting you up for failure when you a) a file that changes the dictionary and you don't update the original dictionary, and b) when you update this shared dictionary without recompressing all files that depend on it. With both of those really important points, then what is the point of extracting the dictionary and not using some container?
 
Status
Not open for further replies.