简体   繁体   中英

Which would be faster in retriving data from a mysql database?

Which would be faster in retrieving data from a mysql database? Data split across two columns, or the same size data from a single column?

Edit:

Just to give an idea, I have a table for a question and five answers it will receive. These answers are quite big about 1500 words each. When it comes to data retrieval speed, should I use a column for each answer, or should I just make one column with the 5 answers separated by a delimiter then use a script to separate them?

Do not combine the answers into a single column, delimited or not. If you do you are not only breaking a rule-of-thumb in db design ( first normal form ) you're also setting yourself up for a programming world-of-hurt down the road.

You should also consider that you're partaking in premature optimization here. Also don't do that. In my experience, and I think others will concur, you should trust your RDBMS to store and retrieve the data efficiently for you. The only thing you need to do is set up your Indexes properly. If this does turn into a performance issue down the road, you can denormalize at that point.

In response to your edit, neither of the options you present is the "correct" design for this problem. You should make 5 rows in an answer table, each with the ID of the question and a single answer in one column.

Only if this turns out to have performance problems should you consider other designs. It's unlikely to have problems unless you are collecting millions of questions.

If they are in the same table, it won't make a difference.

Once a row is accessed, looking up data from columns takes a trivial amount of time.

You should make your decision based on your data and how you will be accessing it.

Don't rely on guesses and assumptions. Benchmark it yourself, on your hardware, with your coding, and your data.

I would say that the way you are going to access and use the information is more relevant than the way it's actually stored.

I would probably say that if you are going to refer to each answer independently such as multiple choice answers, storing the answers in a 3 column table with the following structure:

CREATE TABLE answers (
  question_id INTEGER PRIMARY KEY,
  answer INTEGER,
  answer_text TEXT
);

where answer would be something like 1, 2, 3, 4,.. for each of the answers in a given question, would be more suitable and sane.

Some DBs don't index text type columns. So if you break up the columns from a text column into a varchar, and you had each column indexed, and you're using a DB that doesn't index text columns, you'd get better performance breaking them up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM