Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the recommended way to save a M by N matrix in sql table

Tags:

sql

If I have a M by N matrix filled with data, what is the best way to save it to a sql table for fast query? I can save a 3 column table (X, Y,Z) total MN rows, or simply M rows with N columns. N is in the order of hundreds to thousands, M can be much larger. I guess the second way is faster for query, since the query can be build on known column names. But would it recommended to have as many columns in a table? thanks,

like image 820
user 4231 Avatar asked Mar 11 '11 15:03

user 4231


1 Answers

Your second option (N columns) will make it harder to select values from column unknown in compile time (you'll have to dynamicaly generate selects). So I'll go with table (X,Y,value) and make (x,y) a primary key.

like image 104
ajuc Avatar answered Sep 29 '22 08:09

ajuc