http://qs1969.pair.com?node_id=195547


in reply to MySQL Table Creation using DBI

You need to read about database normalisation.

Essentially, what you have here is not a large complicated data structure containing many fields, but a very simple data structure containing very few, as so:

table stats ( jobid INT, try INT, item VARCHAR(255), value VARCHAR(255), # or maybe TEXT or equivalent PRIMARY KEY (jobid, try) );

Or similar. You would then be able to retrieve all the contents of the hash by:

select item, value from stats where jobid=273118 and try=1

It also allows you to get nice aggregate stats as so:

select owner, count(*) as n from stats where try=1 group by owner orde +r by n
Which would give you a nicely sorted list of all the different owners in all the jobs, and a count of how many jobs they own.

Even better, its trivial to code for to load and unload from the hash..

foreach (keys %hash) { $dbi->do('insert into stats (jobid,try,item,value) (?,?,?,?)',$job +id, $try, $_, $hash{$_}); }
etc.

Read a good book on SQL, or if you alreayd know SQL decently, on database normali|sz|ation and what it means for your data structures.

Create them well, and the database will end up doing all the work for you.