Frage

Newbie here. More of a design question. So I have to choose one of the following storing approachs for better performance (I tend to check the type of the record multiple times) while dealing with a large dataset in Mysql database

1) two columns - one for the type of the record (varchar(11)), and the value (int (11))

   eg: type="one", value= 12

2) merging the columns and prepend the type before the value

   eg: value="one_12"

Which approach seems more feasible? Thanks for your help.

War es hilfreich?

Lösung 2

approach 1 is better as you need to query with type of record multiple times. performance problem with approach 2 because every time you need to do string operation for extracting the record type from the data and then match

Andere Tipps

Obviously, the first variant. The second one kills the idea of data structuring and relational database. Start with reading about database normalization.

I would recommend approach number one. Presumably you'll store type in a fact table and join that to your raw data. If so, it will require far less storage on behalf of the larger table to store a small integer value and (assuming you create an index on (value, type)) joining to fact table will be very efficient and fast.

In other words, I'd recommend something like the following

Data Table: id, data, more data, type_value

Type table: type_value, type

Index the type table using a compound (aka complex aka multicolumn) index on (type, type_value). Then your join can leverage a covering index, eg,

SELECT a.*
FROM data AS A
INNER JOIN type AS b
ON a.type_value = b.type_value 
WHERE b.type = ?

Note that the order matters in compound indices in MySQL. So if the goal is indeed to evaluate WHERE b.type = "One" (for example) you'll want to index (type, type_value), not the other way around. That'll let you filter your type table and apply the id value from an index without resorting to an index merge.

Approach 1 is better than 2nd. But here is my suggestion if you have the list of Types defined.

Create a new table for types (int type_id, varchar type) and index the type varchar column. Use the foreign key reference in your actual table. This would be ideal way to handle when your table grows large.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top